Search in sources :

Example 1 with Wildcard

use of org.antlr.v4.codegen.model.Wildcard in project antlr4 by tunnelvisionlabs.

the class TestATNSerialization method testLexerWildcardWithMode.

@Test
public void testLexerWildcardWithMode() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : 'a'..'z'+ ;\n" + "mode CMT;" + "COMMENT : '*/' {skip(); popMode();} ;\n" + "JUNK : . {more();} ;\n");
    String expecting = "max type 3\n" + "0:TOKEN_START -1\n" + "1:TOKEN_START -1\n" + "2:RULE_START 0\n" + "3:RULE_STOP 0\n" + "4:RULE_START 1\n" + "5:RULE_STOP 1\n" + "6:RULE_START 2\n" + "7:RULE_STOP 2\n" + "8:BASIC 0\n" + "9:PLUS_BLOCK_START 0 10\n" + "10:BLOCK_END 0\n" + "11:PLUS_LOOP_BACK 0\n" + "12:LOOP_END 0 11\n" + "13:BASIC 1\n" + "14:BASIC 1\n" + "15:BASIC 1\n" + "16:BASIC 1\n" + "17:BASIC 1\n" + "18:BASIC 2\n" + "19:BASIC 2\n" + "20:BASIC 2\n" + "rule 0:2 1\n" + "rule 1:4 2\n" + "rule 2:6 3\n" + "mode 0:0\n" + "mode 1:1\n" + "0->2 EPSILON 0,0,0\n" + "1->4 EPSILON 0,0,0\n" + "1->6 EPSILON 0,0,0\n" + "2->9 EPSILON 0,0,0\n" + "4->13 EPSILON 0,0,0\n" + "6->18 EPSILON 0,0,0\n" + "8->10 RANGE 97,122,0\n" + "9->8 EPSILON 0,0,0\n" + "10->11 EPSILON 0,0,0\n" + "11->9 EPSILON 0,0,0\n" + "11->12 EPSILON 0,0,0\n" + "12->3 EPSILON 0,0,0\n" + "13->14 ATOM 42,0,0\n" + "14->15 ATOM 47,0,0\n" + "15->16 EPSILON 0,0,0\n" + "16->17 ACTION 1,0,0\n" + "17->5 EPSILON 0,0,0\n" + "18->19 WILDCARD 0,0,0\n" + "19->20 ACTION 2,1,0\n" + "20->7 EPSILON 0,0,0\n" + "0:0\n" + "1:1\n" + "2:11\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getRuleNames()), 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 2 with Wildcard

use of org.antlr.v4.codegen.model.Wildcard in project antlr4 by tunnelvisionlabs.

the class TestATNSerialization method testWildcard.

@Test
public void testWildcard() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {A, B, C}\n" + "a : . ;");
    String expecting = "max type 3\n" + "0:RULE_START 0\n" + "1:RULE_STOP 0\n" + "2:BASIC 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:0\n" + "0->2 EPSILON 0,0,0\n" + "2->3 WILDCARD 0,0,0\n" + "3->1 EPSILON 0,0,0\n";
    ATN atn = createATN(g, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(g.getRuleNames()), Arrays.asList(g.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) Test(org.junit.Test)

Example 3 with Wildcard

use of org.antlr.v4.codegen.model.Wildcard in project antlr4 by tunnelvisionlabs.

the class ParserFactory method wildcard.

@Override
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) {
    Wildcard wild = new Wildcard(this, ast);
    // TODO: dup with tokenRef
    if (labelAST != null) {
        String label = labelAST.getText();
        Decl d = getTokenLabelDecl(label);
        wild.labels.add(d);
        getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            TokenListDecl l = getTokenListLabelDecl(label);
            getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), l);
        }
    }
    if (controller.needsImplicitLabel(ast, wild))
        defineImplicitLabel(ast, wild);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(wild, labelAST);
    return list(wild, listLabelOp);
}
Also used : Wildcard(org.antlr.v4.codegen.model.Wildcard) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 4 with Wildcard

use of org.antlr.v4.codegen.model.Wildcard in project antlr4 by antlr.

the class TestATNSerialization method testWildcard.

@Test
public void testWildcard() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {A, B, C}\n" + "a : . ;");
    String expecting = "max type 3\n" + "0:RULE_START 0\n" + "1:RULE_STOP 0\n" + "2:BASIC 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:0\n" + "0->2 EPSILON 0,0,0\n" + "2->3 WILDCARD 0,0,0\n" + "3->1 EPSILON 0,0,0\n";
    ATN atn = createATN(g, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(g.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) Test(org.junit.Test)

Example 5 with Wildcard

use of org.antlr.v4.codegen.model.Wildcard 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)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 Test (org.junit.Test)6 ATN (org.antlr.v4.runtime.atn.ATN)4 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)2 Wildcard (org.antlr.v4.codegen.model.Wildcard)2 Decl (org.antlr.v4.codegen.model.decl.Decl)2 RuleContextDecl (org.antlr.v4.codegen.model.decl.RuleContextDecl)2 TokenDecl (org.antlr.v4.codegen.model.decl.TokenDecl)2 TokenListDecl (org.antlr.v4.codegen.model.decl.TokenListDecl)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 WildcardTransition (org.antlr.v4.runtime.atn.WildcardTransition)2 Grammar (org.antlr.v4.tool.Grammar)2 CommonToken (org.antlr.v4.runtime.CommonToken)1 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)1 NotNull (org.antlr.v4.runtime.misc.NotNull)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 BatfishException (org.batfish.common.BatfishException)1 HierarchyPath (org.batfish.grammar.flatjuniper.Hierarchy.HierarchyTree.HierarchyPath)1