public NTermName() { this.GrammarComments = @"Test Seq grammar"; //Terminals var term1 = new DsvLiteral("<", TypeCode.String); var MyNonTerm = new NonTerminal("MyNonTerm"); //Rules MyNonTerm.Rule = term1; this.Root = MyNonTerm; this.LanguageFlags |= LanguageFlags.NewLineBeforeEOF; }//constructor
public GSeq() { this.GrammarComments = @"Test Seq grammar"; //Terminals var term1 = new DsvLiteral("MULT", TypeCode.String); var term2 = new DsvLiteral("PLUS", TypeCode.String); var s = new NonTerminal("S"); //Rules s.Rule = term1 + term2; this.Root = s; this.LanguageFlags |= LanguageFlags.NewLineBeforeEOF; }//constructor
public WikiText_Template_Grammar() { // Terminals //var text = new WikiTextTerminal("text"); var pipeChar = ToTerm("|"); var identifier = new IdentifierTerminal("identifier"); //,"",""); var variableName = new DsvLiteral("variableName", TypeCode.String, "="); var variableValue = new DsvLiteral("variableValue", TypeCode.String, "\n"); // Non-terminals var wikiTemplate = new NonTerminal("wikiElement"); var templateData = new NonTerminal("templateData"); var includeOnly = new WikiBlockTerminal("includeOnly", WikiBlockType.EscapedText, "<includeonly>", "</includeonly>", "includeOnly"); var noInclude = new WikiBlockTerminal("includeOnly", WikiBlockType.EscapedText, "<noinclude>", "</noinclude>", "includeOnly"); //var wikiText = new NonTerminal("wikiText"); var templateDetails = new NonTerminal("templateDetails"); var row = new NonTerminal("row"); var emptyRow = new NonTerminal("emptyRow"); var dataRow = new NonTerminal("dataRow"); var rows = new NonTerminal("rows"); //var drive = new NonTerminal("drive"); // BNF rules templateDetails.Rule = identifier + ":" + includeOnly + noInclude; dataRow.Rule = pipeChar + variableName + variableValue; row.Rule = emptyRow | dataRow; emptyRow.Rule = ToTerm("|-") + NewLine; rows.Rule = MakePlusRule(rows, row); templateData.Rule = templateDetails + NewLine + rows; wikiTemplate.Rule = ToTerm("{{") + templateData + "}}"; // + ToTerm("}}"); //wikiText.Rule = "{{ " + MakeStarRule(wikiText, wikiElement) + "}}" ; //wikiText.Rule = MakeStarRule(wikiText, wikiElement) ; // config //this.Root = wikiText; this.Root = wikiTemplate; //this.WhitespaceChars = string.Empty; MarkTransient(row); MarkPunctuation(pipeChar); //NewLine.SetFlag(TermFlags.IsPunctuation, true); this.LanguageFlags = LanguageFlags.NewLineBeforeEOF; //this.LanguageFlags |= LanguageFlags.DisableScannerParserLink | LanguageFlags.NewLineBeforeEOF | LanguageFlags.CanRunSample // | LanguageFlags.CreateAst ; }
public void TestDataLiterals() { Parser parser; Token token; Terminal term; // FixedLengthLiteral --------------------------------------------------------- term = new FixedLengthLiteral("fixedLengthInteger", 2, TypeCode.Int32); parser = TestHelper.CreateParser(term, null); token = parser.ParseInput("1200"); Assert.True(token.Value != null, "Failed to parse fixed-length integer."); Assert.True((int)token.Value == 12, "Failed to parse fixed-length integer - result value does not match."); term = new FixedLengthLiteral("fixedLengthString", 2, TypeCode.String); parser = TestHelper.CreateParser(term); token = parser.ParseInput("abcd", useTerminator: false); Assert.True(token != null && token.Value != null, "Failed to parse fixed-length string."); Assert.True((string)token.Value == "ab", "Failed to parse fixed-length string - result value does not match"); // DsvLiteral ---------------------------------------------------------------- term = new DsvLiteral("DsvInteger", TypeCode.Int32, ","); parser = TestHelper.CreateParser(term); token = parser.ParseInput("12,"); Assert.True(token != null && token.Value != null, "Failed to parse CSV integer."); Assert.True((int)token.Value == 12, "Failed to parse CSV integer - result value does not match."); term = new DsvLiteral("DsvInteger", TypeCode.String, ","); parser = TestHelper.CreateParser(term); token = parser.ParseInput("ab,"); Assert.True(token != null && token.Value != null, "Failed to parse CSV string."); Assert.True((string)token.Value == "ab", "Failed to parse CSV string - result value does not match."); // QuotedValueLiteral ---------------------------------------------------------------- term = new QuotedValueLiteral("QVDate", "#", TypeCode.DateTime); parser = TestHelper.CreateParser(term); token = parser.ParseInput("#11/15/2009#"); Assert.True(token != null && token.Value != null, "Failed to parse quoted date."); Assert.True((DateTime)token.Value == new DateTime(2009, 11, 15), "Failed to parse quoted date - result value does not match."); } //method
public void TestDataLiterals() { Terminal term; // FixedLengthLiteral --------------------------------------------------------- term = new FixedLengthLiteral("fixedLengthInteger", 2, TypeCode.Int32); SetTerminal(term); TryMatch("1200"); Assert.IsTrue(_token != null && _token.Value != null, "Failed to parse fixed-length integer."); Assert.IsTrue((int)_token.Value == 12, "Failed to parse fixed-length integer - result value does not match."); term = new FixedLengthLiteral("fixedLengthString", 2, TypeCode.String); SetTerminal(term); TryMatch("abcd"); Assert.IsTrue(_token != null && _token.Value != null, "Failed to parse fixed-length string."); Assert.IsTrue((string)_token.Value == "ab", "Failed to parse fixed-length string - result value does not match"); // DsvLiteral ---------------------------------------------------------------- term = new DsvLiteral("DsvInteger", TypeCode.Int32, ","); SetTerminal(term); TryMatch("12,"); Assert.IsTrue(_token != null && _token.Value != null, "Failed to parse CSV integer."); Assert.IsTrue((int)_token.Value == 12, "Failed to parse CSV integer - result value does not match."); term = new DsvLiteral("DsvInteger", TypeCode.String, ","); SetTerminal(term); TryMatch("ab,"); Assert.IsTrue(_token != null && _token.Value != null, "Failed to parse CSV string."); Assert.IsTrue((string)_token.Value == "ab", "Failed to parse CSV string - result value does not match."); // QuotedValueLiteral ---------------------------------------------------------------- term = new QuotedValueLiteral("QVDate", "#", TypeCode.DateTime); SetTerminal(term); TryMatch("#11/15/2009#"); Assert.IsTrue(_token != null && _token.Value != null, "Failed to parse quoted date."); Assert.IsTrue((DateTime)_token.Value == new DateTime(2009, 11, 15), "Failed to parse quoted date - result value does not match."); } //method
public SampleCsvGrammar() { this.GrammarComments = @"A sample grammar for reading comma-separated file containing cars information Demonstrates use of DsvLiteral. Use sample data file DataFiles\CarModels.csv."; //Terminals var year = new DsvLiteral("Year", TypeCode.Int16); // comma is default separator var maker = new DsvLiteral("Maker", TypeCode.String); var model = new DsvLiteral("Model", TypeCode.String); var comment = new DsvLiteral("Comment", TypeCode.String); var price = new DsvLiteral("Price", TypeCode.Double, null); //null means no terminator, look for NewLine var line = new NonTerminal("Line"); var lines = new NonTerminal("Lines"); var data = new NonTerminal("data"); //Rules line.Rule = year + maker + model + comment + price + NewLine; //we don't specify comma between fields, because our terminals consume separator automatically lines.Rule = MakeStarRule(lines, line); data.Rule = lines + NewLineStar; //to allow empty lines after this.Root = data; this.LanguageFlags |= LanguageFlags.NewLineBeforeEOF; } //constructor