Ejemplo n.º 1
0
 public void parseJson()
 {
     JsonParser json = new Json.JsonParser();
     json.ToString();
     //json(parseJson());
 }
Ejemplo n.º 2
0
        public static Grammar Read(Identifier grammarKey, string source)
        {
            var json = new Json.JsonParser(source)
            {
                SkipNulls = true
            };
            var dict = json.Parse() as Dictionary <string, object>;
            var tmp  = default(string);

            if (dict != null)
            {
                var sectionMap = new Dictionary <string, Tuple <string, string, GrammarSection> >();
                var grammar    = new Grammar
                {
                    StylerKey           = (Identifier)dict.String("stylerKey"),
                    BracketSymbols      = dict.String("brackets"),
                    NonWordSymbols      = dict.String("delimeters"),
                    CommentMask         = dict.String("commentMask"),
                    NumberLiteral       = (tmp = dict.String("numbers")) != null ? new NumberLiteral(tmp) :null,
                    IndentComponentKey  = (Identifier)dict.String("indentProvider"),
                    FoldingComponentKey = (Identifier)dict.String("foldingProvider"),
                    AutocompleteSymbols = dict.String("autocompleteSymbols")
                };

                var rt = ProcessSection(grammarKey, dict);
                sectionMap.Add("root", rt);
                grammar.Sections.Add(rt.Item3);
                var sections = dict.Object("sections") as List <object> ?? new List <object>();

                foreach (var o in sections)
                {
                    var nd  = o as Dictionary <string, object>;
                    var tup = ProcessSection(grammarKey, nd);
                    tup.Item3.Id = grammar.Sections.Count;
                    grammar.Sections.Add(tup.Item3);

                    var key = tup.Item1 ?? "root";

                    if (sectionMap.ContainsKey(key))
                    {
                        throw new SlotException($"Duplicate section key '{tup.Item1}' in grammar '{grammarKey}'.");
                    }

                    sectionMap.Add(key, tup);
                }

                foreach (var tup in sectionMap.Values)
                {
                    if (tup.Item1 == null)
                    {
                        continue;
                    }

                    var parentKey = tup.Item2 ?? "root";
                    Tuple <string, string, GrammarSection> parent = null;

                    if (!sectionMap.TryGetValue(parentKey, out parent))
                    {
                        throw new SlotException($"A parent section with key '{parentKey}' not found in grammar '{grammarKey}'.");
                    }

                    tup.Item3.ParentId = parent.Item3.Id;
                    parent.Item3.Sections.Add(tup.Item3);
                }

                return(grammar);
            }

            return(null);
        }