예제 #1
0
        internal static SyntaxMatch ReadMatch(YamlMappingNode mapping, Dictionary <string, string> variables)
        {
            string        match    = null;
            List <string> scope    = new List <string> ();
            var           captures = new List <Tuple <int, string> > ();

            ContextReference push = null, set = null, withPrototype = null;
            bool             pop = false;

            foreach (var entry in mapping.Children)
            {
                switch (((YamlScalarNode)entry.Key).Value)
                {
                case "match":
                    match = CompileRegex(ReplaceVariables(((YamlScalarNode)entry.Value).Value, variables));
                    break;

                case "scope":
                    ParseScopes(scope, ((YamlScalarNode)entry.Value).Value);
                    break;

                case "captures":
                    foreach (var captureEntry in ((YamlMappingNode)entry.Value).Children)
                    {
                        captures.Add(
                            Tuple.Create(
                                int.Parse(((YamlScalarNode)captureEntry.Key).Value),
                                ((YamlScalarNode)captureEntry.Value).Value
                                )
                            );
                    }
                    break;

                case "push":
                    push = ReadContextReference(entry.Value, variables);
                    break;

                case "with_prototype":
                    withPrototype = ReadContextReference(entry.Value, variables);
                    break;

                case "pop":
                    // according to the spec the only accepted value
                    pop = true;
                    break;

                case "set":
                    set = ReadContextReference(entry.Value, variables);
                    break;

                case "syntax":
                    // ?
                    break;
                }
            }
            return(new SyntaxMatch(match, scope, new Captures(captures), push, pop, set, withPrototype));
        }
 internal SyntaxMatch(string match, IReadOnlyList <string> scope, Captures captures, ContextReference push, bool pop, ContextReference set, ContextReference withPrototype)
 {
     Match         = match;
     Scope         = scope;
     Captures      = captures ?? Captures.Empty;
     Push          = push;
     Pop           = pop;
     Set           = set;
     WithPrototype = withPrototype;
 }
 public SyntaxContextWithPrototype(SyntaxContext ctx, ContextReference withPrototype) :  base(ctx.Name)
 {
     this.ctx        = ctx;
     this.definition = ctx.definition;
     matches         = new List <SyntaxMatch> (ctx.Matches);
     foreach (var c in withPrototype.GetContexts(ctx))
     {
         if (c.Matches == null)
         {
             c.PrepareMatches();
         }
         this.matches.AddRange(c.Matches);
     }
 }
예제 #4
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);
                }

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

                    var list = new List <object> ();
                    if (whileLoop != null)
                    {
                        list.Add(new SyntaxMatch("^(?!" + Sublime3Format.CompileRegex(whileLoop) + ")", 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/while 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));
        }