Search in sources :

Example 6 with Wildcard

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

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.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 7 with Wildcard

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

the class XPathLexer method nextToken.

@Override
public Token nextToken() {
    _tokenStartCharIndex = _input.index();
    CommonToken t = null;
    while (t == null) {
        switch(_input.LA(1)) {
            case '/':
                consume();
                if (_input.LA(1) == '/') {
                    consume();
                    t = new CommonToken(ANYWHERE, "//");
                } else {
                    t = new CommonToken(ROOT, "/");
                }
                break;
            case '*':
                consume();
                t = new CommonToken(WILDCARD, "*");
                break;
            case '!':
                consume();
                t = new CommonToken(BANG, "!");
                break;
            case '\'':
                String s = matchString();
                t = new CommonToken(STRING, s);
                break;
            case CharStream.EOF:
                return new CommonToken(EOF, "<EOF>");
            default:
                if (isNameStartChar(_input.LA(1))) {
                    String id = matchID();
                    if (Character.isUpperCase(id.charAt(0)))
                        t = new CommonToken(TOKEN_REF, id);
                    else
                        t = new CommonToken(RULE_REF, id);
                } else {
                    throw new LexerNoViableAltException(this, _input, _tokenStartCharIndex, null);
                }
                break;
        }
    }
    t.setStartIndex(_tokenStartCharIndex);
    t.setCharPositionInLine(_tokenStartCharIndex);
    t.setLine(line);
    return t;
}
Also used : LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 8 with Wildcard

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

the class ApplyPathApplicator method enterPoplt_apply_path.

@Override
public void enterPoplt_apply_path(Poplt_apply_pathContext ctx) {
    HierarchyPath applyPathPath = new HierarchyPath();
    String pathQuoted = ctx.path.getText();
    String pathWithoutQuotes = pathQuoted.substring(1, pathQuoted.length() - 1);
    String[] pathComponents = pathWithoutQuotes.split("\\s+");
    for (String pathComponent : pathComponents) {
        boolean isWildcard = pathComponent.charAt(0) == '<';
        if (isWildcard) {
            applyPathPath.addWildcardNode(pathComponent);
        } else {
            applyPathPath.addNode(pathComponent);
        }
    }
    int insertionIndex = _newConfigurationLines.indexOf(_currentSetLine);
    List<ParseTree> newLines = null;
    try {
        newLines = _hierarchy.getApplyPathLines(_currentPath, applyPathPath, _configurationContext);
    } catch (BatfishException e) {
        _w.redFlag("Could not apply path: " + pathQuoted + ": make sure path is terminated by wildcard (e.g. <*>) representing ip(v6) " + "addresses or prefixes");
    }
    if (newLines != null) {
        _newConfigurationLines.addAll(insertionIndex + 1, newLines);
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) HierarchyPath(org.batfish.grammar.flatjuniper.Hierarchy.HierarchyTree.HierarchyPath) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 9 with Wildcard

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

the class ParserATNFactory method wildcard.

/**
 * Build an atom with all possible values in its label.
 */
@NotNull
@Override
public Handle wildcard(@NotNull GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    left.addTransition(new WildcardTransition(right));
    node.atnState = left;
    return new Handle(left, right);
}
Also used : WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 10 with Wildcard

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

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