Search in sources :

Example 16 with LexerAction

use of org.antlr.v4.runtime.atn.LexerAction in project antlr4 by antlr.

the class LexerATNFactory method createLexerAction.

private LexerAction createLexerAction(GrammarAST ID, GrammarAST arg) {
    String command = ID.getText();
    checkCommands(command, ID.getToken());
    if ("skip".equals(command) && arg == null) {
        return LexerSkipAction.INSTANCE;
    } else if ("more".equals(command) && arg == null) {
        return LexerMoreAction.INSTANCE;
    } else if ("popMode".equals(command) && arg == null) {
        return LexerPopModeAction.INSTANCE;
    } else if ("mode".equals(command) && arg != null) {
        String modeName = arg.getText();
        Integer mode = getModeConstantValue(modeName, arg.getToken());
        if (mode == null) {
            return null;
        }
        return new LexerModeAction(mode);
    } else if ("pushMode".equals(command) && arg != null) {
        String modeName = arg.getText();
        Integer mode = getModeConstantValue(modeName, arg.getToken());
        if (mode == null) {
            return null;
        }
        return new LexerPushModeAction(mode);
    } else if ("type".equals(command) && arg != null) {
        String typeName = arg.getText();
        Integer type = getTokenConstantValue(typeName, arg.getToken());
        if (type == null) {
            return null;
        }
        return new LexerTypeAction(type);
    } else if ("channel".equals(command) && arg != null) {
        String channelName = arg.getText();
        Integer channel = getChannelConstantValue(channelName, arg.getToken());
        if (channel == null) {
            return null;
        }
        return new LexerChannelAction(channel);
    } else {
        return null;
    }
}
Also used : LexerPushModeAction(org.antlr.v4.runtime.atn.LexerPushModeAction) LexerChannelAction(org.antlr.v4.runtime.atn.LexerChannelAction) LexerTypeAction(org.antlr.v4.runtime.atn.LexerTypeAction) LexerModeAction(org.antlr.v4.runtime.atn.LexerModeAction)

Example 17 with LexerAction

use of org.antlr.v4.runtime.atn.LexerAction in project antlr4 by antlr.

the class LexerATNFactory method createATN.

@Override
public ATN createATN() {
    // BUILD ALL START STATES (ONE PER MODE)
    Set<String> modes = ((LexerGrammar) g).modes.keySet();
    for (String modeName : modes) {
        // create s0, start state; implied Tokens rule node
        TokensStartState startState = newState(TokensStartState.class, null);
        atn.modeNameToStartState.put(modeName, startState);
        atn.modeToStartState.add(startState);
        atn.defineDecisionState(startState);
    }
    // INIT ACTION, RULE->TOKEN_TYPE MAP
    atn.ruleToTokenType = new int[g.rules.size()];
    for (Rule r : g.rules.values()) {
        atn.ruleToTokenType[r.index] = g.getTokenType(r.name);
    }
    // CREATE ATN FOR EACH RULE
    _createATN(g.rules.values());
    atn.lexerActions = new LexerAction[indexToActionMap.size()];
    for (Map.Entry<Integer, LexerAction> entry : indexToActionMap.entrySet()) {
        atn.lexerActions[entry.getKey()] = entry.getValue();
    }
    // LINK MODE START STATE TO EACH TOKEN RULE
    for (String modeName : modes) {
        List<Rule> rules = ((LexerGrammar) g).modes.get(modeName);
        TokensStartState startState = atn.modeNameToStartState.get(modeName);
        for (Rule r : rules) {
            if (!r.isFragment()) {
                RuleStartState s = atn.ruleToStartState[r.index];
                epsilon(startState, s);
            }
        }
    }
    ATNOptimizer.optimize(g, atn);
    return atn;
}
Also used : TokensStartState(org.antlr.v4.runtime.atn.TokensStartState) LexerAction(org.antlr.v4.runtime.atn.LexerAction) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

LexerAction (org.antlr.v4.runtime.atn.LexerAction)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)5 ActionAST (org.antlr.v4.tool.ast.ActionAST)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 RangeAST (org.antlr.v4.tool.ast.RangeAST)4 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)4 ST (org.stringtemplate.v4.ST)4 ATNState (org.antlr.v4.runtime.atn.ATNState)3 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)3 LexerChannelAction (org.antlr.v4.runtime.atn.LexerChannelAction)3 LexerCustomAction (org.antlr.v4.runtime.atn.LexerCustomAction)3 LexerModeAction (org.antlr.v4.runtime.atn.LexerModeAction)3 LexerTypeAction (org.antlr.v4.runtime.atn.LexerTypeAction)3 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)3 IntegerList (org.antlr.v4.runtime.misc.IntegerList)3 InvalidClassException (java.io.InvalidClassException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2