Example #1
0
 public void CacheResult(Rule rule, int pos, Node node)
 {
     if (!cache.ContainsKey(pos)) 
         cache.Add(pos, new Dictionary<Rule, Node>());
     var tmp = cache[pos];
     if (!tmp.ContainsKey(rule))
         tmp.Add(rule, node);
 }
Example #2
0
 public static void Test(string input, Rule r)
 {
     var nodes = r.Parse(input);
     if (nodes == null)
         throw new Exception("Parser failed");
     foreach (var n in nodes)
     {
         if (n.Label == "Object")
         {
              var d = JsonObject.Eval(n);
             var s = d.ToString();
             Console.WriteLine(s);
         }
         else
         {
             Console.WriteLine(n.ToXml.ToString());
         }
     }
 }
Example #3
0
 public static void Test(string s, Rule r)
 {
     try
     {
         Console.WriteLine("Using rule {0} to parse string {1}", r.Name, s);
         var nodes = r.Parse(s);
         if (nodes == null || nodes.Count != 1)
             Console.WriteLine("Parsing failed!");
         else if (nodes[0].Text != s)
             Console.WriteLine("Parsing partially succeeded");
         else
             Console.WriteLine("Parsing suceeded");
         Console.WriteLine(nodes[0].Text);
     }
     catch (Exception e)
     {
         Console.WriteLine("Parsing failed with exception" + e.Message);
     }
 }
Example #4
0
 public static Rule OneOrMore(Rule r)
 {
     return new PlusRule(r);
 }
Example #5
0
 public static Rule ZeroOrMore(Rule r)
 {
     return new ZeroOrMoreRule(r);
 }
Example #6
0
 public static Rule Not(Rule r)
 {
     return new NotRule(r);
 }
Example #7
0
 public static Rule At(Rule rule)
 {
     return new AtRule(rule);
 }
Example #8
0
 /// <summary>
 /// This rule should be used only with a named rule, since the name
 /// of the rule is used as the label.
 /// </summary>
 /// <param name="rule"></param>
 /// <returns></returns>
 public static Rule Node(Rule rule)
 {
     return new NodeRule(rule);
 }
Example #9
0
 public ZeroOrMoreRule(Rule r)
     : base(r)
 { }
Example #10
0
 public static void TestParse(string s, Rule r)
 {
     GrammarTest.Test(s, r);
 }
Example #11
0
 public static Rule Parenthesize(Rule r) { return CharToken('(') + r + WS + CharToken(')'); }
Example #12
0
 public static Rule CommaDelimited(Rule r) { return Opt(r + (ZeroOrMore(CharToken(',') + r) + Opt(CharToken(',')))); }
Example #13
0
 public NotRule(Rule r)
     : base(r)
 { }
Example #14
0
 public AtRule(Rule r)
     : base(r)
 { }
Example #15
0
 public OptRule(Rule r)
     : base(r)
 { }
Example #16
0
 public PlusRule(Rule r)
     : base(r)
 { }
Example #17
0
 public static Rule Opt(Rule r)
 {
     return new OptRule(r);
 }
Example #18
0
 public static Rule Group(Rule r) { return OpenBrace + r + CloseBrace; }
 public dynamic Eval(string s, Rule r)
 {
     var nodes = r.Parse(s);
     var root = JavaScriptTransformer.Transform(nodes[0]);
     return Eval(root);
 }
Example #20
0
 public static Rule AdvanceWhileNot(Rule r)
 {
     if (r == null) throw new ArgumentNullException();
     return ZeroOrMore(Seq(Not(r), AnyChar));
 }
Example #21
0
 public NodeRule(Rule r)
     : base(r)
 { }