public static void Test(string s, Grammar.Rule r) { try { Print(string.Format("Using rule {0} to parse string {1}", r.ToString(), s)); var nodes = RubyParser.ParseByRule(s, r); if (nodes == null || nodes.Count != 1) { Print("Parsing failed!", ConsoleColor.Red); } else if (nodes[0].ToString() != s) { Print("Parsing partially succeeded", ConsoleColor.Yellow); } else { Print("Parsing suceeded", ConsoleColor.Green); } if (nodes != null && nodes.Count > 0) { Console.WriteLine(nodes[0] + "\n"); } else { Console.WriteLine(); } } catch (Exception e) { Print("Parsing failed with exception:", ConsoleColor.Red); Print(e.Message + "\n" + e.StackTrace + "\n", ConsoleColor.Gray); } }
public static List <RubyAstNode> ParseByRule(string s, Grammar.Rule rule) { Parser parser = new Parser(s); try { bool bResult = parser.Parse(rule); if (!bResult) { throw new Exception("failed to parse input"); } } catch (Exception e) { Console.WriteLine("Parsing error occured with message: " + e.Message); Console.WriteLine(parser.ParserPosition); throw e; } RubyScript tmp = new RubyScript(parser.GetAst()); return(tmp.mStatements); }
public static void TestParse(string s, Grammar.Rule r) { GrammarTest.Test(s, r); }