Exemple #1
0
        public void UnlexedRuleTest()
        {
            LexResultGroup lexedSource = _testLexer.Lex("   samIAm       ");

            LexResultValidator checker = lexed => lexed.MatchType == LexMatchType.None;
            LexResultParser    parser  = lexed => {
                return(DetailedParseResult.CreateError(
                           new ASTNode(ParseMatchType.None, lexed),
                           $"Unlexed token `{lexed.Text}' detected at character {lexed.Start}."
                           ));
            };

            IParseRule unlexedTokenRule = new ParseRule(checker, parser);

            DetailedParseResult result = unlexedTokenRule.Parse(lexedSource[1]) as DetailedParseResult;

            Assert.AreEqual("Unlexed token `samIAm' detected at character 3.", result.Message);
        }
Exemple #2
0
        public void ParserRuleTest()
        {
            // initialize parser and parse rules
            ParseRule expressionRule = new ParseRule(
                ParseMatchType.Expression,
                l => l.MatchType != LexMatchType.Whitespace && l.MatchType != LexMatchType.None,
                l => new ParseResult(ParseStatus.Success, new ASTNode(ParseMatchType.Expression, l))
                );
            ParseRule ignoreRule = new ParseRule(
                ParseMatchType.Ignore,
                l => l.MatchType == LexMatchType.Whitespace,
                l => new ParseResult(ParseStatus.Success, new ASTNode(ParseMatchType.Ignore, l))
                );
            ParseRule unlexedRule = new ParseRule(
                ParseMatchType.Invalid,
                l => l.MatchType == LexMatchType.None,
                l => DetailedParseResult.CreateError(
                    new ASTNode(ParseMatchType.Invalid, l),
                    $"Unlexed token `{l.Text}' detected at character {l.Start}."
                    )
                );

            Parser            myParser = new Parser();
            IParseResultGroup parsedSource;

            // lex the source
            LexResultGroup lexedSource = _testLexer.Lex("\"That\" 1 true 23.9  !");

            // test vigorously
            // Test 1: No rules
            parsedSource = myParser.Parse(lexedSource);

            Assert.IsTrue(parsedSource.Count == 0);

            // Test 2: With rules added
            myParser.AddMultiple(unlexedRule, expressionRule, ignoreRule);
            parsedSource = myParser.Parse(lexedSource);

            Assert.AreEqual("\"That\"", parsedSource[0].Node.Text);
            Assert.IsTrue(parsedSource[0].GetMatchType() == ParseMatchType.Expression);

            Assert.AreEqual("!", parsedSource[8].Node.Text);
            Assert.IsTrue(parsedSource[8].GetMatchType() == ParseMatchType.Invalid);
        }
Exemple #3
0
        public void ProgramRuleTest()
        {
            LexResultGroup lexedSource = _testLexer.Lex("\"I have a \\\"thing\\\" for you.\" 33.45");

            ParseRule expressionRule = new ParseRule(
                l => l.MatchType != LexMatchType.Whitespace && l.MatchType != LexMatchType.None,
                l => new ParseResult(ParseStatus.Success, new ASTNode(ParseMatchType.Expression, l))
                );
            ParseRule ignoreRule = new ParseRule(
                l => l.MatchType == LexMatchType.Whitespace,
                l => new ParseResult(ParseStatus.Success, new ASTNode(ParseMatchType.Ignore, l))
                );
            ParseRule unlexedRule = new ParseRule(
                l => l.MatchType == LexMatchType.None,
                l => DetailedParseResult.CreateError(
                    new ASTNode(ParseMatchType.Invalid, l),
                    $"Unlexed token `{l.Text}' detected at character {l.Start}."
                    )
                );

            ContainerParseRule programRule = new ContainerParseRule(ParseMatchType.Program, LexMatchType.Program);

            programRule.Add(unlexedRule);
            programRule.Add(expressionRule);
            programRule.Add(ignoreRule);

            IParseResult result = programRule.Parse(lexedSource);

            Assert.AreEqual("\"I have a \\\"thing\\\" for you.\"", result.Node[0].Text);
            Assert.IsTrue(result.Node[0].MatchType == ParseMatchType.Expression);

            Assert.AreEqual(" ", result.Node[1].Text);
            Assert.IsTrue(result.Node[1].MatchType == ParseMatchType.Ignore);

            Assert.AreEqual("33.45", result.Node[2].Text);
            Assert.IsTrue(result.Node[2].MatchType == ParseMatchType.Expression);
        }