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); }
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()); } } }
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); } }
public static Rule OneOrMore(Rule r) { return new PlusRule(r); }
public static Rule ZeroOrMore(Rule r) { return new ZeroOrMoreRule(r); }
public static Rule Not(Rule r) { return new NotRule(r); }
public static Rule At(Rule rule) { return new AtRule(rule); }
/// <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); }
public ZeroOrMoreRule(Rule r) : base(r) { }
public static void TestParse(string s, Rule r) { GrammarTest.Test(s, r); }
public static Rule Parenthesize(Rule r) { return CharToken('(') + r + WS + CharToken(')'); }
public static Rule CommaDelimited(Rule r) { return Opt(r + (ZeroOrMore(CharToken(',') + r) + Opt(CharToken(',')))); }
public NotRule(Rule r) : base(r) { }
public AtRule(Rule r) : base(r) { }
public OptRule(Rule r) : base(r) { }
public PlusRule(Rule r) : base(r) { }
public static Rule Opt(Rule r) { return new OptRule(r); }
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); }
public static Rule AdvanceWhileNot(Rule r) { if (r == null) throw new ArgumentNullException(); return ZeroOrMore(Seq(Not(r), AnyChar)); }
public NodeRule(Rule r) : base(r) { }