Search in sources :

Example 16 with Decl

use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.

the class Choice method addCodeForLookaheadTempVar.

public TestSetInline addCodeForLookaheadTempVar(IntervalSet look) {
    List<SrcOp> testOps = factory.getLL1Test(look, ast);
    TestSetInline expr = Utils.find(testOps, TestSetInline.class);
    if (expr != null) {
        Decl d = new TokenTypeDecl(factory, expr.varName);
        factory.getCurrentRuleFunction().addLocalDecl(d);
        CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
        addPreambleOp(nextType);
    }
    return expr;
}
Also used : TokenTypeDecl(org.antlr.v4.codegen.model.decl.TokenTypeDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenTypeDecl(org.antlr.v4.codegen.model.decl.TokenTypeDecl)

Example 17 with Decl

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

the class ScopeParser method parse.

public static AttributeDict parse(ActionAST action, String s, char separator, Grammar g) {
    AttributeDict dict = new AttributeDict();
    List<Pair<String, Integer>> decls = splitDecls(s, separator);
    for (Pair<String, Integer> decl : decls) {
        if (decl.a.trim().length() > 0) {
            Attribute a = parseAttributeDef(action, decl, g);
            dict.add(a);
        }
    }
    return dict;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) AttributeDict(org.antlr.v4.tool.AttributeDict) Pair(org.antlr.v4.runtime.misc.Pair)

Example 18 with Decl

use of org.antlr.v4.codegen.model.decl.Decl 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 19 with Decl

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

the class ParserFactory method set.

@Override
public List<SrcOp> set(GrammarAST setAST, GrammarAST labelAST, boolean invert) {
    MatchSet matchOp;
    if (invert)
        matchOp = new MatchNotSet(this, setAST);
    else
        matchOp = new MatchSet(this, setAST);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            defineImplicitLabel(setAST, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(setAST.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(setAST.getAltLabel(), d);
        }
    }
    if (controller.needsImplicitLabel(setAST, matchOp))
        defineImplicitLabel(setAST, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : MatchSet(org.antlr.v4.codegen.model.MatchSet) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) MatchNotSet(org.antlr.v4.codegen.model.MatchNotSet) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 20 with Decl

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

the class ParserFactory method defineImplicitLabel.

// support
public void defineImplicitLabel(GrammarAST ast, LabeledOp op) {
    Decl d;
    if (ast.getType() == ANTLRParser.SET || ast.getType() == ANTLRParser.WILDCARD) {
        String implLabel = gen.getTarget().getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
        d = getTokenLabelDecl(implLabel);
        ((TokenDecl) d).isImplicit = true;
    } else if (ast.getType() == ANTLRParser.RULE_REF) {
        // a rule reference?
        Rule r = g.getRule(ast.getText());
        String implLabel = gen.getTarget().getImplicitRuleLabel(ast.getText());
        String ctxName = gen.getTarget().getRuleFunctionContextStructName(r);
        d = new RuleContextDecl(this, implLabel, ctxName);
        ((RuleContextDecl) d).isImplicit = true;
    } else {
        String implLabel = gen.getTarget().getImplicitTokenLabel(ast.getText());
        d = getTokenLabelDecl(implLabel);
        ((TokenDecl) d).isImplicit = true;
    }
    op.getLabels().add(d);
    // all labels must be in scope struct in case we exec action out of context
    getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
}
Also used : TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) InvokeRule(org.antlr.v4.codegen.model.InvokeRule) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl)

Aggregations

Decl (org.antlr.v4.codegen.model.decl.Decl)18 RuleContextDecl (org.antlr.v4.codegen.model.decl.RuleContextDecl)10 TokenDecl (org.antlr.v4.codegen.model.decl.TokenDecl)10 TokenListDecl (org.antlr.v4.codegen.model.decl.TokenListDecl)10 AltLabelStructDecl (org.antlr.v4.codegen.model.decl.AltLabelStructDecl)8 ArrayList (java.util.ArrayList)6 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)6 AttributeDecl (org.antlr.v4.codegen.model.decl.AttributeDecl)6 ContextRuleGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl)6 ContextRuleListGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl)6 ContextRuleListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl)6 ContextTokenGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl)6 ContextTokenListGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl)6 ContextTokenListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl)6 StructDecl (org.antlr.v4.codegen.model.decl.StructDecl)6 LeftRecursiveRuleFunction (org.antlr.v4.codegen.model.LeftRecursiveRuleFunction)4 RuleFunction (org.antlr.v4.codegen.model.RuleFunction)4 Attribute (org.antlr.v4.tool.Attribute)4 Rule (org.antlr.v4.tool.Rule)4 AltAST (org.antlr.v4.tool.ast.AltAST)4