Search in sources :

Example 76 with Lexer

use of org.antlr.v4.runtime.Lexer in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testMustTrackPreviousGoodAlt3.

@Test
public void testMustTrackPreviousGoodAlt3() 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 B | A | A B C ;");
    checkMatchedAlt(lg, g, "a", 2);
    checkMatchedAlt(lg, g, "ab", 1);
    checkMatchedAlt(lg, g, "abc", 3);
    checkMatchedAlt(lg, g, "ad", 2);
    checkMatchedAlt(lg, g, "abd", 1);
    checkMatchedAlt(lg, g, "abcd", 3);
}
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 77 with Lexer

use of org.antlr.v4.runtime.Lexer in project antlr4 by tunnelvisionlabs.

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 78 with Lexer

use of org.antlr.v4.runtime.Lexer in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testPEGAchillesHeel.

@Test
public void testPEGAchillesHeel() 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 | A B ;");
    checkMatchedAlt(lg, g, "a", 1);
    checkMatchedAlt(lg, g, "ab", 2);
    checkMatchedAlt(lg, g, "abc", 2);
}
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 79 with Lexer

use of org.antlr.v4.runtime.Lexer in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testMustTrackPreviousGoodAlt2.

@Test
public void testMustTrackPreviousGoodAlt2() 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 ;");
    checkMatchedAlt(lg, g, "a", 1);
    checkMatchedAlt(lg, g, "ab", 2);
    checkMatchedAlt(lg, g, "abc", 3);
    checkMatchedAlt(lg, g, "ad", 1);
    checkMatchedAlt(lg, g, "abd", 2);
    checkMatchedAlt(lg, g, "abcd", 3);
}
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 80 with Lexer

use of org.antlr.v4.runtime.Lexer in project antlr4 by tunnelvisionlabs.

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)

Aggregations

Test (org.junit.Test)427 LexerGrammar (org.antlr.v4.tool.LexerGrammar)407 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)278 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)145 Grammar (org.antlr.v4.tool.Grammar)125 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)108 CharStream (org.antlr.v4.runtime.CharStream)98 ParseTree (org.antlr.v4.runtime.tree.ParseTree)91 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)56 IOException (java.io.IOException)44 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 Token (org.antlr.v4.runtime.Token)41 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)39 ArrayList (java.util.ArrayList)36 RecognitionException (org.antlr.v4.runtime.RecognitionException)26 StringReader (java.io.StringReader)23 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)23 TokenStream (org.antlr.v4.runtime.TokenStream)23 Lexer (org.antlr.v4.runtime.Lexer)22