Search in sources :

Example 81 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testArbitraryLeftPrefix.

@Test
public void testArbitraryLeftPrefix() 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 | A+ C ;");
    checkMatchedAlt(lg, g, "aac", 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 82 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testAmbigAltChooseFirst.

@Test
public void testAmbigAltChooseFirst() 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 ;");
    checkMatchedAlt(lg, g, "ab", 1);
    checkMatchedAlt(lg, g, "abc", 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 83 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testCommonLeftPrefix.

@Test
public void testCommonLeftPrefix() 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 | A C ;");
    checkMatchedAlt(lg, g, "ab", 1);
    checkMatchedAlt(lg, g, "ac", 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 84 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method testMustTrackPreviousGoodAlt.

@Test
public void testMustTrackPreviousGoodAlt() 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, "ac", 1);
    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 85 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class BaseTest method createATN.

protected ATN createATN(Grammar g, boolean useSerializer) {
    if (g.atn == null) {
        semanticProcess(g);
        assertEquals(0, g.tool.getNumErrors());
        ParserATNFactory f;
        if (g.isLexer()) {
            f = new LexerATNFactory((LexerGrammar) g);
        } else {
            f = new ParserATNFactory(g);
        }
        g.atn = f.createATN();
        assertEquals(0, g.tool.getNumErrors());
    }
    ATN atn = g.atn;
    if (useSerializer) {
        char[] serialized = ATNSerializer.getSerializedAsChars(atn, Arrays.asList(g.getRuleNames()));
        return new ATNDeserializer().deserialize(serialized);
    }
    return atn;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)444 Test (org.junit.Test)410 Grammar (org.antlr.v4.tool.Grammar)140 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)120 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)100 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)72 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)58 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)26 ParseTree (org.antlr.v4.runtime.tree.ParseTree)24 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 CharStream (org.antlr.v4.runtime.CharStream)20 TokenStream (org.antlr.v4.runtime.TokenStream)17 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)14 STGroupString (org.stringtemplate.v4.STGroupString)14 ArrayList (java.util.ArrayList)13 ATNFactory (org.antlr.v4.automata.ATNFactory)12 DFA (org.antlr.v4.runtime.dfa.DFA)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12