Search in sources :

Example 6 with LexerAction

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

the class LexerATNFactory method action.

@Override
public Handle action(ActionAST action) {
    int ruleIndex = currentRule.index;
    int actionIndex = g.lexerActions.get(action);
    LexerCustomAction lexerAction = new LexerCustomAction(ruleIndex, actionIndex);
    return action(action, lexerAction);
}
Also used : LexerCustomAction(org.antlr.v4.runtime.atn.LexerCustomAction)

Example 7 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)

Example 8 with LexerAction

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

the class LexerATNFactory method lexerCommand.

@Override
public Handle lexerCommand(GrammarAST ID) {
    LexerAction lexerAction = createLexerAction(ID, null);
    if (lexerAction != null) {
        return action(ID, lexerAction);
    }
    // fall back to standard action generation for the command
    ST cmdST = codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText()) + "Command");
    if (cmdST == null) {
        g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
        return epsilon(ID);
    }
    if (cmdST.impl.formalArguments != null && cmdST.impl.formalArguments.containsKey("arg")) {
        g.tool.errMgr.grammarError(ErrorType.MISSING_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText());
        return epsilon(ID);
    }
    return action(cmdST.render());
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) RangeAST(org.antlr.v4.tool.ast.RangeAST) ST(org.stringtemplate.v4.ST) LexerAction(org.antlr.v4.runtime.atn.LexerAction)

Example 9 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)

Aggregations

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