public static void ParseWithEtoParse (string grammar_text, string text_to_compile) { var style = (Eto.Parse.Grammars.EbnfStyle)( (uint)Eto.Parse.Grammars.EbnfStyle.Iso14977 & ~( (uint) Eto.Parse.Grammars.EbnfStyle.WhitespaceSeparator ) | (uint) Eto.Parse.Grammars.EbnfStyle.EscapeTerminalStrings); Eto.Parse.Grammars.EbnfGrammar grammar; Eto.Parse.Grammar parser; try { grammar = new Eto.Parse.Grammars.EbnfGrammar(style); parser = grammar.Build(grammar_text, "grammar"); } catch (Exception ex) { Trace.WriteLine (ex.ToString ()); /* System.ArgumentException: the topParser specified is not found in this ebnf at Eto.Parse.Grammars.EbnfGrammar.Build (System.String bnf, System.String startParserName) [0x00048] in <filename unknown>:0 at Globals.Main (System.String[] args) [0x0002b] in /var/calculate/remote/distfiles/egit-src/SqlParser-on-EtoParse.git/test1/Program.cs:20 */ throw; } var match = parser.Match(text_to_compile); if (match.Success == false) { Console.Out.WriteLine ("No luck!"); } else { // DumpAllMatches (match, nameOfTheStartingRule); } }
public Parser(string textGrammar, string root_rule, EbnfStyle style) { var ebnfGrammar = new Eto.Parse.Grammars.EbnfGrammar(style); myGrammar = ebnfGrammar.Build(textGrammar, root_rule); myGrammar.CheckAbsentRules(); }
private static string GetCode(CommandLineOptions opts) { switch (Path.GetExtension(opts.GrammarFile)) { case ".bnf": var gr = new BnfGrammar(); return gr.ToCode(File.ReadAllText(opts.GrammarFile), opts.StartParser, opts.GrammarName); case ".ebnf": var egr = new EbnfGrammar(EbnfStyle.W3c | EbnfStyle.SquareBracketAsOptional | EbnfStyle.WhitespaceSeparator); return egr.ToCode(File.ReadAllText(opts.GrammarFile), opts.StartParser, opts.GrammarName); case ".gold": var ggr = new GoldGrammar(); return ggr.ToCode(File.ReadAllText(opts.GrammarFile), opts.GrammarName); } throw new Exception("Unknown Grammar. Try .ebnf .bnf .gold"); }
public Parser(string textGrammar, string root_rule, EbnfStyle style) { var ebnfGrammar = new Eto.Parse.Grammars.EbnfGrammar( style ); myGrammar = ebnfGrammar.Build(textGrammar, root_rule); myGrammar.CheckAbsentRules (); }
public void TestSqlParser() { const string sqlStatement = @"select * from MyTable where MyColumn = hello"; var ebnfGrammar = new EbnfGrammar(EbnfStyle.W3c | EbnfStyle.SquareBracketAsOptional | EbnfStyle.WhitespaceSeparator); var xmlGrammar = ebnfGrammar.Build(sqlW3cEbnf, "command"); var match = xmlGrammar.Match(sqlStatement); Assert.IsTrue(match.Success, match.ErrorMessage); }
public void TestXmlParser() { const string xmlDocument = @"<?xml version='1.0' encoding='utf-8' ?> <body class=""blah""> <div>some text</div> <!-- some comment --> </body>"; var ebnfGrammar = new EbnfGrammar(EbnfStyle.W3c & ~EbnfStyle.UseCommentRuleWithSeparator); var xmlGrammar = ebnfGrammar.Build(xmlW3cEbnf, "document"); var match = xmlGrammar.Match(xmlDocument); Assert.IsTrue(match.Success, match.ErrorMessage); }
public void TestXmlParser() { const string xmlDocument = @"<?xml version='1.0' encoding='utf-8' ?> <body class=""blah""> <div>some text</div> <!-- some comment --> </body>"; var ebnfGrammar = new EbnfGrammar(EbnfStyle.W3c & ~EbnfStyle.UseCommentRuleWithSeparator); var xmlGrammar = ebnfGrammar.Build(xmlW3cEbnf, "document"); // define the terminals in the grammar xmlGrammar.SetTerminals("Letter", "BaseChar", "Ideographic", "CombiningChar", "Digit", "Extender", "PubidChar", "Char", "S", "EnumeratedType", "NameChar", "Eq"); var match = xmlGrammar.Match(xmlDocument); Assert.IsTrue(match.Success, match.ErrorMessage); }
public void Simple() { var grammarString = @" ws := {? Terminals.WhiteSpace ?}; letter or digit := ? Terminals.LetterOrDigit ?; simple value := letter or digit, {letter or digit}; bracket value = simple value, {simple value}; optional bracket = '(', bracket value, ')' | simple value; first = optional bracket; second = optional bracket; grammar = ws, first, second, ws; "; var input = " hello ( parsing world ) "; // our grammar var grammar = new EbnfGrammar(EbnfStyle.Iso14977).Build(grammarString, "grammar"); var match = grammar.Match(input); Assert.IsTrue(match.Success, match.ErrorMessage); Assert.AreEqual("hello", match["first"]["simple value", true].Text); Assert.AreEqual("parsing world", match["second"]["bracket value", true].Text); }
public void EbnfToCode() { var ebnfGrammar = new EbnfGrammar(EbnfStyle.Iso14977 | EbnfStyle.UseCommentRuleWithSeparator); var code = ebnfGrammar.ToCode(ebnf, "syntax", "MyEbnfGrammar"); var myEbnf = Helper.Create<Grammar>(code, "MyEbnfGrammar"); SetEbnfRules(myEbnf); var match = myEbnf.Match(ebnf); Assert.IsTrue(match.Success, match.ErrorMessage); }
public void TestEbnf() { var ebnfGrammar = new EbnfGrammar(EbnfStyle.Iso14977 | EbnfStyle.UseCommentRuleWithSeparator); var myEbnf = ebnfGrammar.Build(ebnf, "syntax"); SetEbnfRules(myEbnf); var match = myEbnf.Match(ebnf); Assert.IsTrue(match.Success, match.ErrorMessage); }