Search in sources :

Example 61 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNLexerInterpreter method testEOFInSetAtEndOfLineComment.

/** only positive sets like (EOF|'\n') can match EOF and not in wildcard or ~foo sets
	 *  EOF matches but does not advance cursor.
	 */
@Test
public void testEOFInSetAtEndOfLineComment() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "CMT : '//' .* (EOF|'\\n') ;\n");
    String expecting = "CMT, EOF";
    checkLexerMatches(lg, "//", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 62 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNLexerInterpreter method testLexerEscapeInString.

@Test
public void testLexerEscapeInString() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "STR : '[' ('~' ']' | .)* ']' ;\n");
    checkLexerMatches(lg, "[a~]b]", "STR, EOF");
    checkLexerMatches(lg, "[a]", "STR, EOF");
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 63 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNLexerInterpreter method testLexerSetUnicodeBMP.

@Test
public void testLexerSetUnicodeBMP() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : ('愛'|'愜')\n ;");
    String expecting = "ID, EOF";
    checkLexerMatches(lg, "愛", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 64 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNLexerInterpreter method testGreedyBetweenRules.

@Test
public void testGreedyBetweenRules() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : '<a>' ;\n" + "B : '<' .+ '>' ;\n");
    String expecting = "B, EOF";
    checkLexerMatches(lg, "<a><x>", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 65 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNParserPrediction method testEmptyInput.

@Test
public void testEmptyInput() 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 | ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "a", "" };
    String[] dfa = { "s0-'a'->:s1=>1\n", "s0-EOF->:s2=>2\n" + "s0-'a'->:s1=>1\n" };
    checkDFAConstruction(lg, g, decision, inputs, dfa);
}
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

LexerGrammar (org.antlr.v4.tool.LexerGrammar)305 Test (org.junit.Test)304 Grammar (org.antlr.v4.tool.Grammar)183 ATN (org.antlr.v4.runtime.atn.ATN)65 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)53 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)47 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)29 Rule (org.antlr.v4.tool.Rule)25 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)21 STGroupString (org.stringtemplate.v4.STGroupString)21 ArrayList (java.util.ArrayList)18 ATNState (org.antlr.v4.runtime.atn.ATNState)18 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)17 ST (org.stringtemplate.v4.ST)17 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15