Esempio n. 1
0
        public void Compile_WhenErrorExpressionIsRepeated_YieldsNoErrors()
        {
            var grammar = new PegParser().Parse("a = (#error{ \"\" })*");

            var result = PegCompiler.Compile(grammar);

            Assert.That(result.Errors.Where(e => !e.IsWarning).Select(e => e.ErrorText), Is.Empty);
        }
Esempio n. 2
0
        public void Compile_WhenAZeroLengthProductionIsRepeatedWithADelimiter_YieldsNoErrors()
        {
            var grammar = new PegParser().Parse("start = ''<0,,'OK'>");

            var result = PegCompiler.Compile(grammar);

            Assert.That(result.Errors.Where(e => !e.IsWarning).Select(e => e.ErrorText), Is.Empty);
        }
Esempio n. 3
0
        public void Compile_ForAllFoundTutorials_Succeeds(Tutorial tutorial)
        {
            var grammar = new PegParser().Parse(tutorial.GrammarText);

            var result = PegCompiler.Compile(grammar);

            Assert.That(result.Errors, Is.Empty);
        }
Esempio n. 4
0
        public void Compile_WhenARuleContainsAStateExpressionAsPartOfASequenceThatEndsWithACodeExpression_IncludesTheContentOfTheCodeExpression()
        {
            var grammar = new PegParser().Parse("foo = #{OK;} a:'OK' {TEST};");

            var result = PegCompiler.Compile(grammar);

            Assert.That(result.Code, Contains.Substring("TEST"));
        }
Esempio n. 5
0
        public void Compile_WhenARuleContainsAStateExpressionAsPartOfASequence_IncludesTheContentOfThatStateExpression()
        {
            var grammar = new PegParser().Parse("foo = #STATE{TEST;} 'OK';");

            var result = PegCompiler.Compile(grammar);

            Assert.That(result.Code, Contains.Substring("TEST"));
        }
Esempio n. 6
0
        public void Parse_WithClassExpression_YieldsClassExpressionWithCorrectCaseSensitivity(string subject, bool? ignoreCase)
        {
            var parser = new PegParser();

            var grammar = parser.Parse(subject);
            var charClass = (ClassExpression)grammar.Rules.Single().Expression;

            Assert.That(charClass.IgnoreCase, Is.EqualTo(ignoreCase));
        }
Esempio n. 7
0
        public void Parse_WhenTheGrammarIsEntirelyEmpty_ReturnsAnEmptySequence(string subject)
        {
            var parser = new PegParser();

            var grammar = parser.Parse(subject);

            var result = (SequenceExpression)grammar.Rules.Single().Expression;
            Assert.That(result.Sequence, Is.Empty);
        }
Esempio n. 8
0
        public void Parse_WithNoRules_YieldsEmptyGrammar()
        {
            var subject = string.Empty;
            var parser = new PegParser();

            var grammar = parser.Parse(subject);

            Assert.That(grammar.Rules, Is.Empty);
        }
Esempio n. 9
0
        public void Parse_WithLiteralExpression_YieldsLiteralExpressionWithCorrectCaseSensitivity(string subject, bool ignoreCase)
        {
            var parser = new PegParser();

            var grammar = parser.Parse(subject);
            var literal = (LiteralExpression)grammar.Rules.Single().Expression;

            Assert.That(literal.IgnoreCase, Is.EqualTo(ignoreCase));
        }
Esempio n. 10
0
        public void Compile_WhenTheGrammarContainsAnAndCodeExpression_ExecutesExpression()
        {
            var grammar = new PegParser().Parse("a = &{true} 'OK'");

            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<string>(compiled);

            Assert.That(parser.Parse("OK"), Is.EqualTo("OK"));
        }
Esempio n. 11
0
        public void Compile_WhenEmptyParensAreIncluded_CompilesCorrectly()
        {
            var grammar = new PegParser().Parse("a = () b; b = 'OK';");

            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<string>(compiled);

            Assert.That(parser.Parse("OK"), Is.EqualTo("OK"));
        }
Esempio n. 12
0
        public void Compile_WhenAZeroLengthProductionIsRepeated_YieldsError(string grammarText)
        {
            var grammar = new PegParser().Parse(grammarText);

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.Where(e => !e.IsWarning).Single();
            Assert.That(error.ErrorNumber, Is.EqualTo("PEG0021"));
        }
Esempio n. 13
0
        public void Compile_Performance_PegGrammar()
        {
            var pegGrammar = new PegParser().Parse(File.ReadAllText("PegParser.peg"));

            Evaluate(() =>
            {
                PegCompiler.Compile(pegGrammar);
            });
        }
Esempio n. 14
0
        public void Compile_WhenGivenAGrammarWithAnInvalidAccessibilitySetting_YieldsError(string settingName, string value)
        {
            var grammar = new PegParser().Parse("@" + settingName + " {" + value + "}; a = 'OK';");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.Single();
            Assert.That(error.ErrorNumber, Is.EqualTo("PEG0012"));
        }
Esempio n. 15
0
        public void Parse_WithLiteralExpression_YieldsLiteralExpressionWithCorrectString()
        {
            var subject = "a = 'testString'";
            var parser = new PegParser();

            var grammar = parser.Parse(subject);
            var literal = (LiteralExpression)grammar.Rules.Single().Expression;

            Assert.That(literal.Value, Is.EqualTo("testString"));
        }
Esempio n. 16
0
        public void Compile_WhenACSharpExpressionContainsWarnings_YieldsWarning()
        {
            var grammar = new PegParser().Parse("a = {{\n#warning OK\nreturn \"OK\";\n}}");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.First();
            Assert.That(error.ErrorNumber, Is.EqualTo("CS1030"));
            Assert.That(error.IsWarning, Is.True);
        }
Esempio n. 17
0
        public void Compile_WhenPublicRuleNameIsLowercase_YieldsWarning()
        {
            var grammar = new PegParser().Parse("a -public = 'OK'");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.First();
            Assert.That(error.ErrorNumber, Is.EqualTo("PEG0025"));
            Assert.That(error.IsWarning, Is.True);
        }
Esempio n. 18
0
        public void Compile_ImpossibleQuantifier_YieldsWarning(int min, int max)
        {
            var grammar = new PegParser().Parse("a = 'OK'<" + min + "," + max + ">;");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.First();
            Assert.That(error.ErrorNumber, Is.EqualTo("PEG0015"));
            Assert.That(error.IsWarning, Is.True);
        }
Esempio n. 19
0
        public void Compile_WhenACSharpExpressionContainsError_YieldsError()
        {
            var grammar = new PegParser().Parse("a = {{ return \"OK\" }}");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.First();
            Assert.That(error.ErrorNumber, Is.EqualTo("CS1002"));
            Assert.That(error.IsWarning, Is.False);
        }
Esempio n. 20
0
        public void Parse_ForAllFoundTutorials_Succeeds(Tutorial tutorial)
        {
            var grammar = new PegParser().Parse(tutorial.GrammarText);
            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<object>(compiled.Code);

            var result = parser.Parse(tutorial.TestText);

            Assert.That(result, Is.Not.Null);
        }
Esempio n. 21
0
        public void RegressionTest()
        {
            var grammar = new PegParser().Parse(File.ReadAllText(@"Tracing\tracing-test.peg"));
            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<string>(compiled);

            parser.Tracer = NullTracer.Instance;
            parser.Parse(File.ReadAllText(@"Tracing\tracing-test.txt"));

            Assert.Pass();
        }
Esempio n. 22
0
        public void Parse_For05Calculator_ReturnsCorrectValueForMathematicalExpressions(string expression, double value)
        {
            var tutorial = Tutorial.FindAll().Single(t => t.Name == "05 - Calculator");
            var grammar = new PegParser().Parse(tutorial.GrammarText);
            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<object>(compiled);

            var result = parser.Parse(expression);

            Assert.That(result, Is.EqualTo(value).Within(0.1).Percent);
        }
Esempio n. 23
0
        public void EndToEndTest()
        {
            var grammar = new PegParser().Parse(File.ReadAllText(@"Tracing\tracing-test.peg"));
            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<string>(compiled);

            parser.Tracer = DiagnosticsTracer.Instance;
            var output = TraceUtility.Trace(() =>
            {
                parser.Parse(File.ReadAllText(@"Tracing\tracing-test.txt"));
            });

            var stateKey = Regex.Match(output, @"state key (\d+)").Groups[1].Value;
            Assert.That(output, Is.EqualTo(StringUtilities.JoinLines(
                $"Begin 'start' at (1,1) with state key {stateKey}",
                $"    Begin 'basicRule' at (1,1) with state key {stateKey}",
                $"    End 'basicRule' with success at (1,3) with state key {stateKey}",
                $"    Begin 'leftRecursiveRule' at (1,3) with state key {stateKey}",
                $"        Cache miss.",
                $"        Seeding left-recursion with an unsuccessful match.",
                $"        Begin 'leftRecursiveRule' at (1,3) with state key {stateKey}",
                $"            Cache hit.",
                $"        End 'leftRecursiveRule' with failure at (1,3) with state key {stateKey}",
                $"        Caching result and retrying.",
                $"        Begin 'leftRecursiveRule' at (1,3) with state key {stateKey}",
                $"            Cache hit.",
                $"        End 'leftRecursiveRule' with success at (1,5) with state key {stateKey}",
                $"        Caching result and retrying.",
                $"        Begin 'leftRecursiveRule' at (1,3) with state key {stateKey}",
                $"            Cache hit.",
                $"        End 'leftRecursiveRule' with success at (1,8) with state key {stateKey}",
                $"        No forward progress made, current cache entry will be kept.",
                $"    End 'leftRecursiveRule' with success at (1,8) with state key {stateKey}",
                $"    Begin 'outerRule' at (1,8) with state key {stateKey}",
                $"        Begin 'innerRule' at (1,8) with state key {stateKey}",
                $"        End 'innerRule' with success at (1,10) with state key {stateKey}",
                $"    End 'outerRule' with success at (1,10) with state key {stateKey}",
                $"    Begin 'memoizedRule' at (1,10) with state key {stateKey}",
                $"        Cache miss.",
                $"        Caching result.",
                $"    End 'memoizedRule' with success at (1,12) with state key {stateKey}",
                $"    Begin 'memoizedRule' at (1,10) with state key {stateKey}",
                $"        Cache hit.",
                $"    End 'memoizedRule' with success at (1,12) with state key {stateKey}",
                $"    Begin 'memoizedRule' at (1,12) with state key {stateKey}",
                $"        Cache miss.",
                $"        Caching result.",
                $"    End 'memoizedRule' with failure at (1,12) with state key {stateKey}",
                $"    Begin 'memoizedRule' at (1,12) with state key {stateKey}",
                $"        Cache hit.",
                $"    End 'memoizedRule' with failure at (1,12) with state key {stateKey}",
                $"End 'start' with success at (1,12) with state key {stateKey}")));
        }
Esempio n. 24
0
        public void Compile_WhenTheGrammarHasLexicalRuleConsistingOfANameExpression_ProducesAParserThatReturnsTheLexicalElements()
        {
            var grammar = new PegParser().Parse("a -lexical = b; b = 'OK';");
            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<string>(compiled.Code);

            IList<LexicalElement> lexicalElements;
            var result = parser.Parse("OK", null, out lexicalElements);

            var actual = lexicalElements.Select(e => e.Name + "@" + e.StartCursor.Location + ":" + e.EndCursor.Location).ToArray();
            Assert.That(actual, Is.EqualTo(new[] { "[email protected]:2" }));
        }
Esempio n. 25
0
        public void Parse_WhenACSharpExpressionDoesntConsumeAllOfTheSourceText_YieldsError()
        {
            var parser = new PegParser();

            try
            {
                parser.Parse("a = {{ return \"OK\"; } extra }");
            }
            catch (FormatException ex)
            {
                Assert.That(ex.Message, Is.StringStarting("PEG0011:"));
                var cursor = (Cursor)ex.Data["cursor"];
                Assert.That(cursor.Location, Is.EqualTo(22));
            }
        }
Esempio n. 26
0
        public void Parse_WithClassExpression_YieldsClassExpressionWithCorrectCharacterRanges()
        {
            var subject = "a = [-a-z0-9]";
            var parser = new PegParser();

            var grammar = parser.Parse(subject);
            var charClass = (ClassExpression)grammar.Rules.Single().Expression;

            var expected = new[]
            {
                new CharacterRange('-', '-'),
                new CharacterRange('a', 'z'),
                new CharacterRange('0', '9'),
            };
            Assert.That(charClass.Ranges, Is.EquivalentTo(expected));
        }
Esempio n. 27
0
        /// <summary>
        /// Parse and compile a PEG grammar from a string.
        /// </summary>
        /// <param name="subject">The PEG grammar to parse and compile.</param>
        /// <param name="fileName">The filename to use in errors.</param>
        /// <returns>A <see cref="CompileResult"/> containing the result of the compilation.</returns>
        public static CompileResult CompileString(string subject, string fileName = null)
        {
            Grammar grammar;
            try
            {
                grammar = new PegParser().Parse(subject ?? string.Empty, fileName);
            }
            catch (FormatException ex)
            {
                var cursor = ex.Data["cursor"] as Cursor;
                if (cursor != null && Regex.IsMatch(ex.Message, @"^PEG\d+:"))
                {
                    var parts = ex.Message.Split(new[] { ':' }, 2);
                    var result = new CompileResult(null);
                    result.Errors.Add(new CompilerError(cursor.FileName ?? string.Empty, cursor.Line, cursor.Column, parts[0], parts[1]));
                    return result;
                }

                throw;
            }

            return PegCompiler.Compile(grammar);
        }
Esempio n. 28
0
        public void GeneratedParser_Performance_Regression(string testName)
        {
            var parserSource = File.ReadAllText([email protected]"TestCases\{testName}.peg");
            var subject = File.ReadAllText([email protected]"TestCases\{testName}.txt");
            var parsed = new PegParser().Parse(parserSource);
            var compiled = PegCompiler.Compile(parsed);
            Assert.That(compiled.Errors.Where(e => !e.IsWarning), Is.Empty);
            var pegParser = CodeCompiler.Compile<dynamic>(compiled);

            Evaluate(() =>
            {
                pegParser.Parse(subject);
            });
        }
Esempio n. 29
0
        public void Compile_WithUnusedRules_YieldsWarning()
        {
            var grammar = new PegParser().Parse("a = b; b = 'OK'; c = d; d = 'OK' c;");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.First();
            Assert.That(error.ErrorNumber, Is.EqualTo("PEG0017"));
            Assert.That(error.IsWarning, Is.True);
        }
Esempio n. 30
0
        public void Compile_WithUnrecognizedSetting_YieldsWarning()
        {
            var grammar = new PegParser().Parse("@barnacle OK; a = 'OK';");

            var result = PegCompiler.Compile(grammar);

            var error = result.Errors.First();
            Assert.That(error.ErrorNumber, Is.EqualTo("PEG0006"));
            Assert.That(error.IsWarning, Is.True);
        }