Search in sources :

Example 1 with ErrorType

use of org.antlr.v4.tool.ErrorType in project antlr4 by antlr.

the class TestCompositeGrammars method testImportedTokenVocabIgnoredWithWarning.

@Test
public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar S;\n" + "options {tokenVocab=whatever;}\n" + "tokens { A }\n" + "x : A {System.out.println(\"S.x\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    Object expectedArg = "S";
    ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE;
    GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g.fileName, null, expectedArg);
    checkGrammarSemanticsWarning(equeue, expectedMessage);
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    assertEquals("unexpected warnings: " + equeue, 1, equeue.warnings.size());
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) GrammarSemanticsMessage(org.antlr.v4.tool.GrammarSemanticsMessage) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 2 with ErrorType

use of org.antlr.v4.tool.ErrorType in project antlr4 by antlr.

the class SymbolChecks method checkForLabelConflict.

public void checkForLabelConflict(Rule r, GrammarAST labelID) {
    String name = labelID.getText();
    if (nameToRuleMap.containsKey(name)) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RULE;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (tokenIDs.contains(name)) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_TOKEN;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.args != null && r.args.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_ARG;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.retvals != null && r.retvals.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RETVAL;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.locals != null && r.locals.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_LOCAL;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType)

Example 3 with ErrorType

use of org.antlr.v4.tool.ErrorType in project antlr4 by antlr.

the class BasicSemanticChecks method checkElementIsOuterMostInSingleAlt.

/**
	 Make sure that action is last element in outer alt; here action,
	 a2, z, and zz are bad, but a3 is ok:
	 (RULE A (BLOCK (ALT {action} 'a')))
	 (RULE B (BLOCK (ALT (BLOCK (ALT {a2} 'x') (ALT 'y')) {a3})))
	 (RULE C (BLOCK (ALT 'd' {z}) (ALT 'e' {zz})))
	 */
protected void checkElementIsOuterMostInSingleAlt(GrammarAST tree) {
    CommonTree alt = tree.parent;
    CommonTree blk = alt.parent;
    boolean outerMostAlt = blk.parent.getType() == RULE;
    Tree rule = tree.getAncestor(RULE);
    String fileName = tree.getToken().getInputStream().getSourceName();
    if (!outerMostAlt || blk.getChildCount() > 1) {
        ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE;
        g.tool.errMgr.grammarError(e, fileName, tree.getToken(), rule.getChild(0).getText());
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree)

Example 4 with ErrorType

use of org.antlr.v4.tool.ErrorType in project antlr4 by antlr.

the class ParserATNFactory method createATN.

@Override
public ATN createATN() {
    _createATN(g.rules.values());
    assert atn.maxTokenType == g.getMaxTokenType();
    addRuleFollowLinks();
    addEOFTransitionToStartRules();
    ATNOptimizer.optimize(g, atn);
    for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonClosureBlocks) {
        LL1Analyzer analyzer = new LL1Analyzer(atn);
        ATNState blkStart = pair.b;
        ATNState blkStop = pair.c;
        IntervalSet lookahead = analyzer.LOOK(blkStart, blkStop, null);
        if (lookahead.contains(org.antlr.v4.runtime.Token.EPSILON)) {
            ErrorType errorType = pair.a instanceof LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
            g.tool.errMgr.grammarError(errorType, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
        }
    }
    optionalCheck: for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonOptionalBlocks) {
        int bypassCount = 0;
        for (int i = 0; i < pair.b.getNumberOfTransitions(); i++) {
            ATNState startState = pair.b.transition(i).target;
            if (startState == pair.c) {
                bypassCount++;
                continue;
            }
            LL1Analyzer analyzer = new LL1Analyzer(atn);
            if (analyzer.LOOK(startState, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
                g.tool.errMgr.grammarError(ErrorType.EPSILON_OPTIONAL, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
                continue optionalCheck;
            }
        }
        if (bypassCount != 1) {
            throw new UnsupportedOperationException("Expected optional block with exactly 1 bypass alternative.");
        }
    }
    return atn;
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Triple(org.antlr.v4.runtime.misc.Triple) LL1Analyzer(org.antlr.v4.runtime.atn.LL1Analyzer) ErrorType(org.antlr.v4.tool.ErrorType) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 5 with ErrorType

use of org.antlr.v4.tool.ErrorType in project antlr4 by antlr.

the class AttributeChecks method setAttr.

@Override
public void setAttr(String expr, Token x, Token rhs) {
    if (g.isLexer()) {
        errMgr.grammarError(ErrorType.ATTRIBUTE_IN_LEXER_ACTION, g.fileName, x, x.getText(), expr);
        return;
    }
    if (node.resolver.resolveToAttribute(x.getText(), node) == null) {
        ErrorType errorType = ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE;
        if (node.resolver.resolvesToListLabel(x.getText(), node)) {
            // $ids for ids+=ID etc...
            errorType = ErrorType.ASSIGNMENT_TO_LIST_LABEL;
        }
        errMgr.grammarError(errorType, g.fileName, x, x.getText(), expr);
    }
    new AttributeChecks(g, r, alt, node, rhs).examineAction();
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType)

Aggregations

ErrorType (org.antlr.v4.tool.ErrorType)5 CommonTree (org.antlr.runtime.tree.CommonTree)1 Tree (org.antlr.runtime.tree.Tree)1 ATNState (org.antlr.v4.runtime.atn.ATNState)1 LL1Analyzer (org.antlr.v4.runtime.atn.LL1Analyzer)1 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)1 Triple (org.antlr.v4.runtime.misc.Triple)1 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)1 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)1 Grammar (org.antlr.v4.tool.Grammar)1 GrammarSemanticsMessage (org.antlr.v4.tool.GrammarSemanticsMessage)1 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)1 Rule (org.antlr.v4.tool.Rule)1 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1 Test (org.junit.Test)1