private void ParseInput() { if (_parser == null) { Markup = "Grammar is not valid"; Output = ""; return; } // Load input CodeDocument doc = null; try { doc = CodeDocument.Load(_parser, _input); } catch (ParserException e) { Output = e.Message; Markup = "Input is not valid"; return; } // Convert to markup Markup = doc.ToMarkup(); ProcessOutput(doc); }
/// <summary>Create the parser to read grammar definition</summary> /// <exclude/> private static void CreateMetaParser() { if (_instance != null) { return; } int step = 1; TextBuffer buffer = new FlatBuffer(MetaParser.SoftMetaGrammarAndSettings); CodeDocument metaGrammarDoc = null; try { // Hard coded parser Parser hardcodeParser = GetHardCodeParser(buffer.Status); if (buffer.Status.Error == null) { step = 2; // Use hard coded grammar to read meta grammar. metaGrammarDoc = hardcodeParser.ParseString(buffer); string gg = metaGrammarDoc.ToMarkup(); step = 3; } if (buffer.Status.Error == null) { _instance = new Parser() { Level = 2 }; ParserBuilder.BuildRules(_instance, metaGrammarDoc, buffer.Status); } } catch (Exception e) { // if an exception occurs under parsing MetaGrammar it is an HardGrammar error var e2 = new ParserException((step <= 2 ? HardGrammar_ : MetaGrammar_) + " " + e.Message); if (e is ParserException) { e2.AllErrors = ((ParserException)e).AllErrors; } throw e2; } if (buffer.Status.Error != null) { // if an proces-error occurs under parsing MetaGrammar it is an MetaGrammar error var e2 = new ParserException((step == 1 ? HardGrammar_ : MetaGrammar_) + " " + buffer.Status.Error.Message); e2.AllErrors = buffer.Status.AllErrors; throw e2; } }
public void ITC24MetaSyntax() { // Build a grammar to test meta grammar string actual1, actual2, expect, msg; expect = MetaParser.SoftMetaGrammarAndSettings; var parser = new Parser(MetaParser.SoftMetaGrammarAndSettings); actual1 = parser.GetGrammar(); // Test Meta grammar after after compilation in a Parser. actual2 = MetaParser.Instance.GetGrammar(); // Test Meta grammar in internal Parser. msg = Util.CompareTextLines(actual1, expect); Assert.AreEqual(string.Empty, msg, "Meta grammar diff error"); msg = Util.CompareTextLines(actual2, expect); Assert.AreEqual(string.Empty, msg, "Meta grammar internal diff error"); // Compare generated CodeDocument with 'selected nodes' string actualTags1, actualTags2, expectTags; CodeDocument docExpect = TestMetaGrammarDoc(); //var metaParser = _metaparser; TextBuffer buffer = new FlatBuffer(MetaParser.SoftMetaGrammarAndSettings); CodeDocument docActual1 = parser.ParseString(buffer); buffer = new FlatBuffer(MetaParser.SoftMetaGrammarAndSettings); CodeDocument docActual2 = MetaParser.Instance.ParseString(buffer); expectTags = docExpect.ToMarkup(); actualTags1 = docActual1.ToMarkup(); actualTags2 = docActual2.ToMarkup(); msg = CodeDocument.CompareCode(docActual1, docExpect); Assert.AreEqual(string.Empty, msg, "Meta grammar document diff error"); msg = CodeDocument.CompareCode(docActual2, docExpect); Assert.AreEqual(string.Empty, msg, "Meta grammar internal document diff error"); Assert.AreEqual(docExpect.Name, docActual1.Name, "Document name diff error"); Assert.AreEqual(expectTags, actualTags1, "Meta grammar document.toMarkup diff error"); Assert.AreEqual(expectTags, actualTags2, "Meta grammar document.toMarkup diff error"); }
private void BuildParser() { _parser = null; try { _parser = new Parser(_grammar); GrammarOutput = "Grammar is ok" + "\r\n\r\n" + _parser.GetGrammar(); ParseInput(); } catch (ParserException e) { GrammarOutput = e.Message; } try { CodeDocument doc = CodeDocument.Load(new Parser(string.Empty), _grammar); GrammarMarkup = doc.ToMarkup(); } catch (ParserException e) { GrammarMarkup = e.Message; } }
public static string ParserLoad(string grammar, string code, string markup, params string[] errors) { // Try create parser //Parser parser = ParserGrammar(grammar, errors); Parser parser = null; try { parser = new Parser(grammar); } catch (ParserException e) { Assert.IsTrue(errors.Length > 0, "Grammar error: " + e.Message); Assert.IsTrue(e.AllErrors.Count >= errors.Length, "Grammar error: missing error 1"); e.AllErrors.Sort(ParserError.Compare); for (int i = 0; i < e.AllErrors.Count; i++) { if (i < errors.Length) { Assert.AreEqual(errors[i], e.AllErrors[i].Message, "Grammar error: Build error " + i); } } } if (errors.Length == 0) { Assert.IsNotNull(parser, "Grammar error: parser is null"); } else if (string.IsNullOrEmpty(code) && markup != "grammar ok") { Assert.IsNull(parser, "Grammar error expected"); } if (parser == null || (string.IsNullOrEmpty(code) && string.IsNullOrEmpty(markup))) { return(string.Empty); } // Try read the code string s = parser.GetGrammar(); var buf = new FlatBuffer(code); CodeDocument doc = parser.ParseString(buf); if (buf.Status.Error == null) { Assert.IsTrue(errors.Count() == 0, " Expecting error"); Assert.IsNotNull(doc, " doc er null"); string actual = doc.ToMarkup(); if (!string.IsNullOrEmpty(markup)) { string err = CompareTextLines(actual, markup); Assert.AreEqual(string.Empty, err, " AST"); } return(actual); } else { Assert.IsTrue(errors.Count() > 0, " Parsing error: " + buf.Status.Error.Message); Assert.IsTrue(buf.Status.AllErrors.Count >= errors.Length, " Load missing error 1"); buf.Status.AllErrors.Sort(ParserError.Compare); for (int i = 0; i < buf.Status.AllErrors.Count; i++) { if (i < errors.Length) { Assert.AreEqual(errors[i], buf.Status.AllErrors[i].Message, " Load error " + i); } else { return(buf.Status.AllErrors[i].Message); } } } return(string.Empty); }
private void ReadLevels() { if (_parserLevel == null) { _parserLevel = new Parser("levels = {int};"); } string tag = Benchmark?.Tag; Benchmark = null; Result = string.Empty; PreparedData = string.Empty; _generatedInputFull = string.Empty; try { _docLevel = CodeDocument.Load(_parserLevel, _levels); } catch (Exception e) { PreparedData = "Can't read levels: " + e.Message; return; } if (_docLevel.Codes().Count() == 0) { PreparedData = "No levels found"; return; } List <int> levels = new List <int>(); foreach (var item in _docLevel.Codes()) { levels.Add(int.Parse(item.Value)); } if (levels.Any(i => i < 1)) { PreparedData = "Levels must be 1 or greater"; return; } if (levels.Count() > 5) { PreparedData = "Max 5 levels"; return; } string code = string.Empty; for (int i = 0; i < levels.Count(); i++) { code += string.Empty + letters[i] + levels[i]; } Benchmark = new BenchmarkResult(code, tag, levels.Aggregate(1, (mul, j) => mul *= j)); string grammar = BuildGrammar(Benchmark.GetLevelListFromCode()); _parserTest = new Parser(grammar); if (Benchmark.Combi <= 500) { PrepareInput(false); GeneratedInput = _generatedInputFull; CodeDocument doc = CodeDocument.Load(_parserTest, _generatedInputFull); GeneratedInputAsMarkup = doc.ToMarkup(); } else { GeneratedInput = "Number of combinations > 500"; GeneratedInputAsMarkup = "Number of combinations > 500"; PreparedData += "Input must be prepared:\r\n"; PreparedData += "Test grammar:\r\n" + _parserTest.GetGrammar(); } }