Example #1
0
        public JsonParser()
        {
            Parser <object> valueParser = null;

            var stringParser =
                RegexParser.Regex("\".*?\"");
            var numberParser =
                RegexParser.Regex(@"\d+").Select(v => double.Parse(v));
            var keyValueParser =
                from key in stringParser
                from _ in RegexParser.String(":")
                from value in valueParser
                select Tuple.Create(key, value);

            var arrayParser =
                from _ in RegexParser.String("[")
                from xs in valueParser.Repsep(RegexParser.String(","))
                from _2 in RegexParser.String("]")
                select xs;
            var dictParser =
                from _ in RegexParser.String("{")
                from xs in keyValueParser.Repsep(RegexParser.String(","))
                from _2 in RegexParser.String("}")
                select xs;

            valueParser = dictParser.asObject()
                          | arrayParser
                          | numberParser.asObject()
                          | stringParser
                          | Parser.Fail <object>("Parse value failed");

            mParser = RegexParser.Phrase(valueParser);
        }