Search in sources :

Example 96 with Grammar

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

the class TestATNConstruction method testParserRuleRefInLexerRule.

@Test
public void testParserRuleRefInLexerRule() throws Exception {
    boolean threwException = false;
    ErrorQueue errorQueue = new ErrorQueue();
    try {
        String gstr = "grammar U;\n" + "a : A;\n" + "A : a;\n";
        Tool tool = new Tool();
        tool.removeListeners();
        tool.addListener(errorQueue);
        assertEquals(0, errorQueue.size());
        GrammarRootAST grammarRootAST = tool.parseGrammarFromString(gstr);
        assertEquals(0, errorQueue.size());
        Grammar g = tool.createGrammar(grammarRootAST);
        assertEquals(0, errorQueue.size());
        g.fileName = "<string>";
        tool.process(g, false);
    } catch (Exception e) {
        threwException = true;
        e.printStackTrace();
    }
    System.out.println(errorQueue);
    assertEquals(1, errorQueue.errors.size());
    assertEquals(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE, errorQueue.errors.get(0).getErrorType());
    assertEquals("[a, A]", Arrays.toString(errorQueue.errors.get(0).getArgs()));
    assertTrue(!threwException);
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool) Test(org.junit.Test)

Example 97 with Grammar

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

the class TestATNConstruction method testAplus.

@Test
public void testAplus() throws Exception {
    Grammar g = new Grammar("parser grammar P;\n" + "a : A+;");
    String expecting = "RuleStart_a_0->PlusBlockStart_3\n" + "PlusBlockStart_3->s2\n" + "s2-A->BlockEnd_4\n" + "BlockEnd_4->PlusLoopBack_5\n" + "PlusLoopBack_5->PlusBlockStart_3\n" + "PlusLoopBack_5->s6\n" + "s6->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s7\n";
    checkRuleATN(g, "a", expecting);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 98 with Grammar

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

the class TestATNSerialization method testLexerUnicodeEscapedSMPNotSetWithRange.

@Test
public void testLexerUnicodeEscapedSMPNotSetWithRange() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : ~('\\u{1F4A9}'|'\\u{1F4AA}'|'\\u{1F441}'|'\\u{1D40F}'..'\\u{1D413}')\n ;");
    String expecting = "max type 1\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:1 1\n" + "mode 0:0\n" + "0:119823..119827, 128065..128065, 128169..128170\n" + "0->1 EPSILON 0,0,0\n" + "1->3 EPSILON 0,0,0\n" + "3->4 NOT_SET 0,0,0\n" + "4->2 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 99 with Grammar

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

the class TestATNSerialization method testLexerUnicodeEscapedBMPNotSet.

@Test
public void testLexerUnicodeEscapedBMPNotSet() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : ~('\\u4E9C'|'\\u4E9D')\n ;");
    String expecting = "max type 1\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:1 1\n" + "mode 0:0\n" + "0:'\\u4E9C'..'\\u4E9D'\n" + "0->1 EPSILON 0,0,0\n" + "1->3 EPSILON 0,0,0\n" + "3->4 NOT_SET 0,0,0\n" + "4->2 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 100 with Grammar

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

the class TestATNSerialization method testLexerTwoRules.

@Test
public void testLexerTwoRules() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n");
    String expecting = "max type 2\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:RULE_START 1\n" + "4:RULE_STOP 1\n" + "5:BASIC 0\n" + "6:BASIC 0\n" + "7:BASIC 1\n" + "8:BASIC 1\n" + "rule 0:1 1\n" + "rule 1:3 2\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "0->3 EPSILON 0,0,0\n" + "1->5 EPSILON 0,0,0\n" + "3->7 EPSILON 0,0,0\n" + "5->6 ATOM 97,0,0\n" + "6->2 EPSILON 0,0,0\n" + "7->8 ATOM 98,0,0\n" + "8->4 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) 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