Search in sources :

Example 16 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class OutputFile method buildNamedActions.

public Map<String, Action> buildNamedActions(Grammar g) {
    Map<String, Action> namedActions = new HashMap<String, Action>();
    for (String name : g.namedActions.keySet()) {
        ActionAST ast = g.namedActions.get(name);
        namedActions.put(name, new Action(factory, ast));
    }
    return namedActions;
}
Also used : HashMap(java.util.HashMap) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 17 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class ActionTranslator method translateActionChunk.

public static List<ActionChunk> translateActionChunk(OutputModelFactory factory, RuleFunction rf, String action, ActionAST node) {
    Token tokenWithinAction = node.token;
    ActionTranslator translator = new ActionTranslator(factory, node);
    translator.rf = rf;
    factory.getGrammar().tool.log("action-translator", "translate " + action);
    String altLabel = node.getAltLabel();
    if (rf != null) {
        translator.nodeContext = rf.ruleCtx;
        if (altLabel != null)
            translator.nodeContext = rf.altLabelCtxs.get(altLabel);
    }
    ANTLRStringStream in = new ANTLRStringStream(action);
    in.setLine(tokenWithinAction.getLine());
    in.setCharPositionInLine(tokenWithinAction.getCharPositionInLine());
    ActionSplitter trigger = new ActionSplitter(in, translator);
    // forces eval, triggers listener methods
    trigger.getActionTokens();
    return translator.chunks;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) ActionSplitter(org.antlr.v4.parse.ActionSplitter) Token(org.antlr.runtime.Token)

Example 18 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class ParserATNFactory method action.

/** Build what amounts to an epsilon transition with an action.
	 *  The action goes into ATN though it is ignored during prediction
	 *  if {@link ActionTransition#actionIndex actionIndex}{@code <0}.
	 */
@Override
public Handle action(ActionAST action) {
    //System.out.println("action: "+action);
    ATNState left = newState(action);
    ATNState right = newState(action);
    ActionTransition a = new ActionTransition(right, currentRule.index);
    left.addTransition(a);
    action.atnState = left;
    return new Handle(left, right);
}
Also used : ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 19 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class RuleFunction method fillNamedActions.

public void fillNamedActions(OutputModelFactory factory, Rule r) {
    if (r.finallyAction != null) {
        finallyAction = new Action(factory, r.finallyAction);
    }
    namedActions = new HashMap<String, Action>();
    for (String name : r.namedActions.keySet()) {
        ActionAST ast = r.namedActions.get(name);
        namedActions.put(name, new Action(factory, ast));
    }
}
Also used : ActionAST(org.antlr.v4.tool.ast.ActionAST)

Aggregations

ActionAST (org.antlr.v4.tool.ast.ActionAST)11 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)5 Rule (org.antlr.v4.tool.Rule)3 PredAST (org.antlr.v4.tool.ast.PredAST)3 HashMap (java.util.HashMap)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CommonToken (org.antlr.runtime.CommonToken)2 Token (org.antlr.runtime.Token)2 Action (org.antlr.v4.codegen.model.Action)2 RuleSempredFunction (org.antlr.v4.codegen.model.RuleSempredFunction)2 ActionSplitter (org.antlr.v4.parse.ActionSplitter)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 Alternative (org.antlr.v4.tool.Alternative)2 Attribute (org.antlr.v4.tool.Attribute)2 Grammar (org.antlr.v4.tool.Grammar)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1