Exemplo n.º 1
0
        public void CheckCustomRoot(Runtime runtime)
        {
            var grammarText =
                $"grammar {TestGrammarName};" +
                "root1: 'V1';" +
                "root2: 'V2';";

            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            workflow.Root = null;
            File.WriteAllText(TestTextName, "V1");
            var             state           = workflow.Process();
            TextParsedState textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);

            workflow.Root   = "root1";
            state           = workflow.Process();
            textParsedState = state as TextParsedState;
            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);

            workflow.Root = "root2";
            File.WriteAllText(TestTextName, "V2");
            state           = workflow.Process();
            textParsedState = state as TextParsedState;
            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);
        }
Exemplo n.º 2
0
        public void CheckListenersAndVisitors(Runtime runtime)
        {
            var grammarText =
                $@"grammar {TestGrammarName};
                t: T;
                T: [a-z]+;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, @"asdf");

            var workflow = new Workflow(grammar)
            {
                GenerateListener = true,
                GenerateVisitor  = true
            };

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var             state           = workflow.Process();
            TextParsedState textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);

            var allFiles = Directory.GetFiles(Path.Combine(ParserGenerator.HelperDirectoryName, TestGrammarName, runtime.ToString()));

            Assert.IsTrue(allFiles.Any(file => file.Contains("listener", StringComparison.OrdinalIgnoreCase)));
            Assert.IsTrue(allFiles.Any(file => file.Contains("visitor", StringComparison.OrdinalIgnoreCase)));
        }
Exemplo n.º 3
0
        private static void CheckCaseInsensitiveWorkflow(Runtime runtime, bool lowerCase)
        {
            char c           = lowerCase ? 'a' : 'A';
            var  grammarText =
                $@"grammar {TestGrammarName};
                start:  A A DIGIT;
                A:      '{c}';
                DIGIT:  [0-9]+;
                WS:     [ \r\n\t]+ -> skip;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            grammar.CaseInsensitiveType = lowerCase ? CaseInsensitiveType.lower : CaseInsensitiveType.UPPER;
            File.WriteAllText(TestTextName, @"A a 1234");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage, state.Exception?.ToString());
            TextParsedState textParsedState = state as TextParsedState;

            Assert.AreEqual(0, textParsedState.Errors.Count, string.Join(Environment.NewLine, textParsedState.Errors));
            Assert.AreEqual("(start A a 1234)", textParsedState.Tree);
        }
Exemplo n.º 4
0
        public void TextParsedStageErrors(Runtime runtime)
        {
            var grammarText =
                $@"grammar {TestGrammarName};
                start: DIGIT+;
                CHAR:  [a-z]+;
                DIGIT: [0-9]+;
                WS:    [ \r\n\t]+ -> skip;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, @"!  asdf  1234");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage, state.Exception?.ToString());

            TextParsedState textParsedState = state as TextParsedState;
            var             textSource      = textParsedState.Text;

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(1, 1, "line 1:0 token recognition error at: '!'", textSource, WorkflowStage.TextParsed),
                new ParsingError(1, 4, "line 1:3 extraneous input 'asdf' expecting DIGIT", textSource, WorkflowStage.TextParsed)
            },
                textParsedState.Errors);
            Assert.AreEqual("(start asdf 1234)", textParsedState.Tree);
        }
Exemplo n.º 5
0
        public void CheckPredictionMode(Runtime runtime)
        {
            var grammarText = $@"
grammar {TestGrammarName};

root
    : (stmt1 | stmt2) EOF
    ;
    
stmt1
    : name
    ;

stmt2
    : 'static' name '.' Id
    ;

name
    : Id ('.' Id)*
    ;

Dot        : '.';
Static     : 'static';
Id         : [A-Za-z]+;
Whitespace : [ \t\r\n]+ -> channel(HIDDEN);
";

            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;
            File.WriteAllText(TestTextName, @"static a.b");

            workflow.PredictionMode = PredictionMode.LL;
            var             llState           = workflow.Process();
            TextParsedState llTextParsedState = llState as TextParsedState;

            Assert.IsNotNull(llTextParsedState);
            Assert.IsFalse(llState.HasErrors);

            workflow.PredictionMode = PredictionMode.SLL;
            var sllState           = workflow.Process();
            var sllTextParsedState = sllState as TextParsedState;

            Assert.IsNotNull(sllTextParsedState);
            Assert.IsTrue(sllTextParsedState.HasErrors);
        }
Exemplo n.º 6
0
        public void CheckLexerOnlyGrammar(Runtime runtime)
        {
            var grammarText =
                $"lexer grammar {TestGrammarName}Lexer;" +
                "T1: 'T1';" +
                "Digit: [0-9]+;" +
                "Space: ' '+ -> channel(HIDDEN);";

            var grammar = GrammarFactory.CreateDefaultLexerAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, "T1 1234");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var             state           = workflow.Process();
            TextParsedState textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);
        }
Exemplo n.º 7
0
        public void TextParsedStageErrors(Runtime runtime)
        {
            var grammarText =
                $@"grammar {TestGrammarName};

root
    : missingToken extraneousToken noViableAlternative mismatchedInput EOF
    ;

missingToken
    : Error LParen RParen Semi
    ;

extraneousToken
    : Error Id Semi
    ;

mismatchedInput
    : Error Id Semi
    ;

noViableAlternative
    : AA BB
    | AA CC
    ;
    
AA: 'aa';
BB: 'bb';
CC: 'cc';
DD: 'dd';
LParen     : '((';
RParen     : '))';
Semi       : ';';
Error      : 'error';
Id         : [A-Za-z][A-Za-z0-9]+;
Whitespace : [ \t\r\n]+ -> channel(HIDDEN);
Comment    : '//' ~[\r\n]* -> channel(HIDDEN);
Number     : [0-9']+;";

            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, @"#                       // token recognition error at: '#'
error (( ;        // missing '))' at ';'
error id1 id2 ;   // extraneous input 'id2' expecting ';'
aa  dd            // no viable alternative at input 'aa  dd'
error 123 456 ;   // mismatched input '123' expecting Id");

            var workflow = new Workflow(grammar)
            {
                Runtime = runtime, TextFileName = TestTextName
            };

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage, state.Exception?.ToString());

            TextParsedState textParsedState = (TextParsedState)state;
            var             textSource      = textParsedState.Text;

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(1, 1, 1, 2, "line 1:0 token recognition error at: '#'", textSource, WorkflowStage.TextParsed),
                new ParsingError(2, 10, 2, 11, "line 2:9 missing '))' at ';'", textSource, WorkflowStage.TextParsed),
                new ParsingError(3, 11, 3, 14, "line 3:10 extraneous input 'id2' expecting ';'", textSource, WorkflowStage.TextParsed),
                new ParsingError(4, 5, 4, 7, "line 4:4 no viable alternative at input 'aa  dd'", textSource, WorkflowStage.TextParsed),
                new ParsingError(5, 7, 5, 10, "line 5:6 mismatched input '123' expecting Id", textSource, WorkflowStage.TextParsed)
            },
                textParsedState.Errors);

            // TODO: unify in different runtimes
            //Assert.AreEqual("(root (missingToken error (( <missing '))'> ;) (extraneousToken error id1 id2 ;) (noViableAlternative aa dd) (mismatchedInput error 123 456 ;) EOF)", textParsedState.Tree);
        }