コード例 #1
0
ファイル: Program.cs プロジェクト: racoltacalin/LLLPG-Samples
 public static object Parse(Stream stream, string fileName = "", bool allowComments = false, IMessageSink errSink = null)
 {
     return(JsonParser.Parse(new StreamCharSource(stream), fileName, 0, true, allowComments, errSink));
 }
コード例 #2
0
ファイル: GrammarReader.cs プロジェクト: vorov2/slot
        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);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: racoltacalin/LLLPG-Samples
 /// <summary>Parses a json string into an object.</summary>
 /// <param name="json">JSON data</param>
 /// <returns>a Dictionary{string, object}, List{object}, string or number.</returns>
 public static object Parse(string json, bool allowComments = false, IMessageSink sink = null)
 {
     return(JsonParser.Parse((UString)json, "", 0, true, allowComments, sink));
 }