Пример #1
0
 public override IEnumerable <SyntaxContext> GetContexts(SyntaxContext context)
 {
     yield return(Context);
 }
Пример #2
0
 internal AnonymousMatchContextReference(SyntaxContext context)
 {
     Context = context;
 }
Пример #3
0
            public Task <HighlightedLine> GetColoredSegments(ITextSource text, int offset, int length)
            {
                if (ContextStack.IsEmpty)
                {
                    return(Task.FromResult(new HighlightedLine(new [] { new ColoredSegment(0, length, ScopeStack.Empty) })));
                }
                SyntaxContext        currentContext = null;
                List <SyntaxContext> lastContexts   = new List <SyntaxContext> ();
                Match       match            = null;
                SyntaxMatch curMatch         = null;
                var         segments         = new List <ColoredSegment> ();
                int         startOffset      = offset;
                int         curSegmentOffset = offset;
                int         endOffset        = offset + length;
                int         lastMatch        = -1;

restart:
                if (lastMatch == offset)
                {
                    if (lastContexts.Contains(currentContext))
                    {
                        offset++;
                        length--;
                    }
                    else
                    {
                        lastContexts.Add(currentContext);
                    }
                }
                else
                {
                    lastContexts.Clear();
                    lastContexts.Add(currentContext);
                }
                if (length <= 0)
                {
                    goto end;
                }
                lastMatch      = offset;
                currentContext = ContextStack.Peek();
                match          = null;
                curMatch       = null;
                foreach (var m in currentContext.Matches)
                {
                    if (m.GotTimeout)
                    {
                        continue;
                    }
                    var r = m.GetRegex();
                    if (r == null)
                    {
                        continue;
                    }
                    try {
                        var possibleMatch = r.Match(text, offset, length, matchTimeout);
                        if (possibleMatch.Success)
                        {
                            if (match == null || possibleMatch.Index < match.Index)
                            {
                                match    = possibleMatch;
                                curMatch = m;
                                // Console.WriteLine (match.Index + " possible match : " + m + "/" + possibleMatch.Index + "-" + possibleMatch.Length);
                            }
                            else
                            {
                                // Console.WriteLine (match.Index + " skip match : " + m + "/" + possibleMatch.Index + "-" + possibleMatch.Length);
                            }
                        }
                        else
                        {
                            // Console.WriteLine ("fail match : " + m);
                        }
                    } catch (RegexMatchTimeoutException) {
                        LoggingService.LogWarning("Warning: Regex " + m.Match + " timed out on line:" + text.GetTextAt(offset, length));
                        m.GotTimeout = true;
                        continue;
                    }
                }

                if (match != null)
                {
                    // Console.WriteLine (match.Index + " taken match : " + curMatch + "/" + match.Index + "-" + match.Length);
                    var matchEndOffset = match.Index + match.Length;
                    if (curSegmentOffset < match.Index && match.Length > 0)
                    {
                        segments.Add(new ColoredSegment(curSegmentOffset - startOffset, match.Index - curSegmentOffset, ScopeStack));
                        curSegmentOffset = match.Index;
                    }
                    if (curMatch.Pop)
                    {
                        PopMetaContentScopeStack(currentContext, curMatch);
                    }

                    PushScopeStack(curMatch.Scope);

                    if (curMatch.Captures.Groups.Count > 0)
                    {
                        foreach (var capture in curMatch.Captures.Groups)
                        {
                            var grp = match.Groups [capture.Item1];
                            if (grp == null || grp.Length == 0)
                            {
                                continue;
                            }
                            if (curSegmentOffset < grp.Index)
                            {
                                ReplaceSegment(segments, new ColoredSegment(curSegmentOffset - startOffset, grp.Index - curSegmentOffset, ScopeStack));
                            }
                            ReplaceSegment(segments, new ColoredSegment(grp.Index - startOffset, grp.Length, ScopeStack.Push(capture.Item2)));
                            curSegmentOffset = Math.Max(curSegmentOffset, grp.Index + grp.Length);
                        }
                    }

                    if (curMatch.Captures.NamedGroups.Count > 0)
                    {
                        foreach (var capture in curMatch.Captures.NamedGroups)
                        {
                            var grp = match.Groups [capture.Item1];
                            if (grp == null || grp.Length == 0)
                            {
                                continue;
                            }
                            if (curSegmentOffset < grp.Index)
                            {
                                ReplaceSegment(segments, new ColoredSegment(curSegmentOffset - startOffset, grp.Index - curSegmentOffset, ScopeStack));
                            }
                            ReplaceSegment(segments, new ColoredSegment(grp.Index - startOffset, grp.Length, ScopeStack.Push(capture.Item2)));
                            curSegmentOffset = grp.Index + grp.Length;
                        }
                    }

                    if (curMatch.Scope.Count > 0 && curSegmentOffset < matchEndOffset && match.Length > 0)
                    {
                        segments.Add(new ColoredSegment(curSegmentOffset - startOffset, matchEndOffset - curSegmentOffset, ScopeStack));
                        curSegmentOffset = matchEndOffset;
                    }

                    if (curMatch.Pop)
                    {
                        if (matchEndOffset - curSegmentOffset > 0)
                        {
                            segments.Add(new ColoredSegment(curSegmentOffset - startOffset, matchEndOffset - curSegmentOffset, ScopeStack));
                        }
                        //if (curMatch.Scope != null)
                        //	scopeStack = scopeStack.Pop ();
                        PopStack(currentContext, curMatch);
                        curSegmentOffset = matchEndOffset;
                    }
                    else if (curMatch.Set != null)
                    {
                        // if (matchEndOffset - curSegmentOffset > 0)
                        //	segments.Add (new ColoredSegment (curSegmentOffset, matchEndOffset - curSegmentOffset, ScopeStack));
                        //if (curMatch.Scope != null)
                        //	scopeStack = scopeStack.Pop ();
                        PopMetaContentScopeStack(currentContext, curMatch);
                        PopStack(currentContext, curMatch);
                        //curSegmentOffset = matchEndOffset;
                        var nextContexts = curMatch.Set.GetContexts(currentContext);
                        PushStack(curMatch, nextContexts);
                        goto skip;
                    }
                    else if (curMatch.Push != null)
                    {
                        var nextContexts = curMatch.Push.GetContexts(currentContext);
                        PushStack(curMatch, nextContexts);
                    }
                    else
                    {
                        if (curMatch.Scope.Count > 0)
                        {
                            for (int i = 0; i < curMatch.Scope.Count; i++)
                            {
                                ScopeStack = ScopeStack.Pop();
                            }
                        }
                    }

                    if (curSegmentOffset < matchEndOffset && match.Length > 0)
                    {
                        segments.Add(new ColoredSegment(curSegmentOffset - startOffset, matchEndOffset - curSegmentOffset, ScopeStack));
                        curSegmentOffset = matchEndOffset;
                    }
skip:
                    length -= curSegmentOffset - offset;
                    offset  = curSegmentOffset;
                    goto restart;
                }

end:
                if (endOffset - curSegmentOffset > 0)
                {
                    segments.Add(new ColoredSegment(curSegmentOffset - startOffset, endOffset - curSegmentOffset, ScopeStack));
                }
                return(Task.FromResult(new HighlightedLine(segments)));
            }
Пример #4
0
 public abstract IEnumerable <SyntaxContext> GetContexts(SyntaxContext context);
Пример #5
0
        static SyntaxMatch ReadMatch(PDictionary dict)
        {
            List <string> matchScope = new List <string> ();

            Sublime3Format.ParseScopes(matchScope, (dict ["name"] as PString)?.Value);

            Captures captures    = null;
            var      captureDict = dict ["captures"] as PDictionary;

            if (captureDict != null)
            {
                captures = ReadCaptureDictionary(captureDict);
            }

            ContextReference pushContext = null;

            var begin = (dict ["begin"] as PString)?.Value;

            if (begin != null)
            {
                Captures beginCaptures = null;
                captureDict = dict ["beginCaptures"] as PDictionary;

                if (captureDict != null)
                {
                    beginCaptures = ReadCaptureDictionary(captureDict);
                }

                var           end         = (dict ["end"] as PString)?.Value;
                Captures      endCaptures = null;
                List <string> endScope    = new List <string> ();
                if (end != null)
                {
                    captureDict = dict ["endCaptures"] as PDictionary;
                    if (captureDict != null)
                    {
                        endCaptures = ReadCaptureDictionary(captureDict);
                    }

                    var list = new List <object> ();
                    if (end != null)
                    {
                        list.Add(new SyntaxMatch(Sublime3Format.CompileRegex(end), endScope, endCaptures ?? captures, null, true, null, null));
                    }
                    var patternsArray = dict ["patterns"] as PArray;
                    if (patternsArray != null)
                    {
                        ReadPatterns(patternsArray, list);
                    }

                    List <string> metaContent  = null;
                    var           contentScope = (dict ["contentName"] as PString)?.Value;
                    if (contentScope != null)
                    {
                        metaContent = new List <string> {
                            contentScope
                        };
                    }

                    var ctx = new SyntaxContext("__generated begin/end capture context", list, metaScope: metaContent);

                    pushContext = new AnonymousMatchContextReference(ctx);
                }

                return(new SyntaxMatch(Sublime3Format.CompileRegex(begin), matchScope, beginCaptures ?? captures, pushContext, false, null, null));
            }

            var match = (dict ["match"] as PString)?.Value;

            if (match == null)
            {
                return(null);
            }
            return(new SyntaxMatch(Sublime3Format.CompileRegex(match), matchScope, captures, pushContext, false, null, null));
        }