Search in sources :

Example 51 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNInterpreter method testAmbigAltChooseFirstWithFollowingToken.

@Test
public void testAmbigAltChooseFirstWithFollowingToken() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + // first alt
    "a : (A B | A B) C ;");
    checkMatchedAlt(lg, g, "abc", 1);
    checkMatchedAlt(lg, g, "abcd", 1);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 52 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNInterpreter method testSimpleNoBlock.

@Test
public void testSimpleNoBlock() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "a : A B ;");
    checkMatchedAlt(lg, g, "ab", 1);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 53 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNInterpreter method testMustTrackPreviousGoodAlt2WithEOF.

@Test(expected = NoViableAltException.class)
public void testMustTrackPreviousGoodAlt2WithEOF() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "a : (A | A B | A B C) EOF;");
    checkMatchedAlt(lg, g, "a", 1);
    checkMatchedAlt(lg, g, "ab", 2);
    checkMatchedAlt(lg, g, "abc", 3);
    try {
        checkMatchedAlt(lg, g, "abd", 1);
    } catch (NoViableAltException re) {
        assertEquals(2, re.getOffendingToken().getTokenIndex());
        assertEquals(4, re.getOffendingToken().getType());
        throw re;
    }
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 54 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNDeserialization method testNot.

@Test
public void testNot() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {A, B, C}\n" + "a : ~A ;");
    checkDeserializationIsStable(g);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 55 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNConstruction method testAorBplus.

@Test
public void testAorBplus() throws Exception {
    Grammar g = new Grammar("parser grammar P;\n" + "a : (A|B{;})+;");
    String expecting = "RuleStart_a_0->PlusBlockStart_5\n" + "PlusBlockStart_5->s2\n" + "PlusBlockStart_5->s3\n" + "s2-A->BlockEnd_6\n" + "s3-B->s4\n" + "BlockEnd_6->PlusLoopBack_7\n" + "s4-action_0:-1->BlockEnd_6\n" + "PlusLoopBack_7->PlusBlockStart_5\n" + "PlusLoopBack_7->s8\n" + "s8->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s9\n";
    checkRuleATN(g, "a", expecting);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)138 Grammar (org.antlr.v4.tool.Grammar)130 LexerGrammar (org.antlr.v4.tool.LexerGrammar)117 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)52 ParseTree (org.antlr.v4.runtime.tree.ParseTree)39 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)37 ATN (org.antlr.v4.runtime.atn.ATN)19 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)16 ArrayList (java.util.ArrayList)14 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)14 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)14 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)13 RecognitionException (org.antlr.v4.runtime.RecognitionException)11 Parser (org.antlr.v4.runtime.Parser)10 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)10 Lexer (org.antlr.v4.runtime.Lexer)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 CharStream (org.antlr.v4.runtime.CharStream)8 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)8 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)8