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);
}
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;
}
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());
}
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;
}
}
Aggregations