Search in sources :

Example 36 with Action

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

the class ScopeParser method parseAttributeDef.

/**
	 * For decls like "String foo" or "char *foo32[]" compute the ID
	 * and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
	 * but if the separator is ',' you cannot use ',' in the initvalue
	 * unless you escape use "\," escape.
	 */
public static Attribute parseAttributeDef(ActionAST action, Pair<String, Integer> decl, Grammar g) {
    if (decl.a == null)
        return null;
    Attribute attr = new Attribute();
    int rightEdgeOfDeclarator = decl.a.length() - 1;
    int equalsIndex = decl.a.indexOf('=');
    if (equalsIndex > 0) {
        // everything after the '=' is the init value
        attr.initValue = decl.a.substring(equalsIndex + 1, decl.a.length()).trim();
        rightEdgeOfDeclarator = equalsIndex - 1;
    }
    String declarator = decl.a.substring(0, rightEdgeOfDeclarator + 1);
    Pair<Integer, Integer> p;
    String text = decl.a;
    text = text.replaceAll("::", "");
    if (text.contains(":")) {
        // declarator has type appearing after the name like "x:T"
        p = _parsePostfixDecl(attr, declarator, action, g);
    } else {
        // declarator has type appearing before the name like "T x"
        p = _parsePrefixDecl(attr, declarator, action, g);
    }
    int idStart = p.a;
    int idStop = p.b;
    attr.decl = decl.a;
    if (action != null) {
        String actionText = action.getText();
        int[] lines = new int[actionText.length()];
        int[] charPositionInLines = new int[actionText.length()];
        for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
            lines[i] = line;
            charPositionInLines[i] = col;
            if (actionText.charAt(i) == '\n') {
                line++;
                col = -1;
            }
        }
        int[] charIndexes = new int[actionText.length()];
        for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
            charIndexes[j] = i;
            // skip comments
            if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
                while (i < actionText.length() && actionText.charAt(i) != '\n') {
                    i++;
                }
            }
        }
        int declOffset = charIndexes[decl.b];
        int declLine = lines[declOffset + idStart];
        int line = action.getToken().getLine() + declLine;
        int charPositionInLine = charPositionInLines[declOffset + idStart];
        if (declLine == 0) {
            /* offset for the start position of the ARG_ACTION token, plus 1
				 * since the ARG_ACTION text had the leading '[' stripped before
				 * reaching the scope parser.
				 */
            charPositionInLine += action.getToken().getCharPositionInLine() + 1;
        }
        int offset = ((CommonToken) action.getToken()).getStartIndex();
        attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
        attr.token.setLine(line);
        attr.token.setCharPositionInLine(charPositionInLine);
        assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
    }
    return attr;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) CommonToken(org.antlr.runtime.CommonToken)

Example 37 with Action

use of org.antlr.v4.codegen.model.Action 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 38 with Action

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

the class ParserATNFactory method elemList.

public Handle elemList(List<Handle> els) {
    int n = els.size();
    for (int i = 0; i < n - 1; i++) {
        // hook up elements (visit all but last)
        Handle el = els.get(i);
        // if el is of form o-x->o for x in {rule, action, pred, token, ...}
        // and not last in alt
        Transition tr = null;
        if (el.left.getNumberOfTransitions() == 1)
            tr = el.left.transition(0);
        boolean isRuleTrans = tr instanceof RuleTransition;
        if (el.left.getStateType() == ATNState.BASIC && el.right.getStateType() == ATNState.BASIC && tr != null && (isRuleTrans && ((RuleTransition) tr).followState == el.right || tr.target == el.right)) {
            // we can avoid epsilon edge to next el
            if (isRuleTrans)
                ((RuleTransition) tr).followState = els.get(i + 1).left;
            else
                tr.target = els.get(i + 1).left;
            // we skipped over this state
            atn.removeState(el.right);
        } else {
            // need epsilon if previous block's right end node is complicated
            epsilon(el.right, els.get(i + 1).left);
        }
    }
    Handle first = els.get(0);
    Handle last = els.get(n - 1);
    if (first == null || last == null) {
        g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, "element list has first|last == null");
    }
    return new Handle(first.left, last.right);
}
Also used : RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) Transition(org.antlr.v4.runtime.atn.Transition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition)

Example 39 with Action

use of org.antlr.v4.codegen.model.Action 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 40 with Action

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

the class ActionTranslator method qualifiedAttr.

@Override
public void qualifiedAttr(String expr, Token x, Token y) {
    gen.g.tool.log("action-translator", "qattr " + x + "." + y);
    if (node.resolver.resolveToAttribute(x.getText(), node) != null) {
        // must be a member access to a predefined attribute like $ctx.foo
        attr(expr, x);
        chunks.add(new ActionText(nodeContext, "." + y.getText()));
        return;
    }
    Attribute a = node.resolver.resolveToAttribute(x.getText(), y.getText(), node);
    if (a == null) {
        // Added in response to https://github.com/antlr/antlr4/issues/1211
        gen.g.tool.errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE, gen.g.fileName, x, x.getText(), "rule");
        return;
    }
    switch(a.dict.type) {
        // has to be current rule
        case ARG:
            chunks.add(new ArgRef(nodeContext, y.getText()));
            break;
        case RET:
            chunks.add(new QRetValueRef(nodeContext, getRuleLabel(x.getText()), y.getText()));
            break;
        case PREDEFINED_RULE:
            chunks.add(getRulePropertyRef(x, y));
            break;
        case TOKEN:
            chunks.add(getTokenPropertyRef(x, y));
            break;
    }
}
Also used : ActionText(org.antlr.v4.codegen.model.chunk.ActionText) Attribute(org.antlr.v4.tool.Attribute) ArgRef(org.antlr.v4.codegen.model.chunk.ArgRef) QRetValueRef(org.antlr.v4.codegen.model.chunk.QRetValueRef)

Aggregations

ST (org.stringtemplate.v4.ST)11 ActionAST (org.antlr.v4.tool.ast.ActionAST)9 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)9 Grammar (org.antlr.v4.tool.Grammar)8 LexerGrammar (org.antlr.v4.tool.LexerGrammar)8 STGroup (org.stringtemplate.v4.STGroup)8 ArrayList (java.util.ArrayList)7 ATNFactory (org.antlr.v4.automata.ATNFactory)6 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)6 ATNState (org.antlr.v4.runtime.atn.ATNState)6 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)6 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 Rule (org.antlr.v4.tool.Rule)5 HashMap (java.util.HashMap)4 Action (org.antlr.v4.codegen.model.Action)4