Search in sources :

Example 81 with IntervalSet

use of org.antlr.v4.runtime.misc.IntervalSet in project antlr4 by antlr.

the class AnalysisPipeline method disjoint.

/** Return whether lookahead sets are disjoint; no lookahead ⇒ not disjoint */
public static boolean disjoint(IntervalSet[] altLook) {
    boolean collision = false;
    IntervalSet combined = new IntervalSet();
    if (altLook == null)
        return false;
    for (IntervalSet look : altLook) {
        // lookahead must've computation failed
        if (look == null)
            return false;
        if (!look.and(combined).isNil()) {
            collision = true;
            break;
        }
        combined.addAll(look);
    }
    return !collision;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Example 82 with IntervalSet

use of org.antlr.v4.runtime.misc.IntervalSet in project antlr4 by antlr.

the class AnalysisPipeline method processLexer.

protected void processLexer() {
    // make sure all non-fragment lexer rules must match at least one symbol
    for (Rule rule : g.rules.values()) {
        if (rule.isFragment()) {
            continue;
        }
        LL1Analyzer analyzer = new LL1Analyzer(g.atn);
        IntervalSet look = analyzer.LOOK(g.atn.ruleToStartState[rule.index], null);
        if (look.contains(Token.EPSILON)) {
            g.tool.errMgr.grammarError(ErrorType.EPSILON_TOKEN, g.fileName, ((GrammarAST) rule.ast.getChild(0)).getToken(), rule.name);
        }
    }
}
Also used : LL1Analyzer(org.antlr.v4.runtime.atn.LL1Analyzer) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Rule(org.antlr.v4.tool.Rule)

Example 83 with IntervalSet

use of org.antlr.v4.runtime.misc.IntervalSet in project antlr4 by antlr.

the class TestExpectedTokens method testFollowIncludedInLeftRecursiveRule.

// Test for https://github.com/antlr/antlr4/issues/1480
// can't reproduce
@Test
public void testFollowIncludedInLeftRecursiveRule() throws Exception {
    String gtext = "grammar T;\n" + "s : expr EOF ;\n" + "expr : L expr R\n" + "     | expr PLUS expr\n" + "     | ID\n" + "     ;\n";
    Grammar g = new Grammar(gtext);
    String atnText = "RuleStart_expr_2->BlockStart_13\n" + "BlockStart_13->s7\n" + "BlockStart_13->s12\n" + "s7-action_1:-1->s8\n" + "s12-ID->BlockEnd_14\n" + "s8-L->s9\n" + "BlockEnd_14->StarLoopEntry_20\n" + "s9-expr->RuleStart_expr_2\n" + "StarLoopEntry_20->StarBlockStart_18\n" + "StarLoopEntry_20->s21\n" + "s10-R->s11\n" + "StarBlockStart_18->s15\n" + "s21->RuleStop_expr_3\n" + "s11->BlockEnd_14\n" + "s15-2 >= _p->s16\n" + "RuleStop_expr_3->s5\n" + "RuleStop_expr_3->s10\n" + "RuleStop_expr_3->BlockEnd_19\n" + "s16-PLUS->s17\n" + "s17-expr->RuleStart_expr_2\n" + "BlockEnd_19->StarLoopBack_22\n" + "StarLoopBack_22->StarLoopEntry_20\n";
    checkRuleATN(g, "expr", atnText);
    ATN atn = g.getATN();
    //		DOTGenerator gen = new DOTGenerator(g);
    //		String dot = gen.getDOT(atn.states.get(2), g.getRuleNames(), false);
    //		System.out.println(dot);
    // Simulate call stack after input '(x' from rule s
    ParserRuleContext callStackFrom_s = new ParserRuleContext(null, 4);
    ParserRuleContext callStackFrom_expr = new ParserRuleContext(callStackFrom_s, 9);
    int afterID = 14;
    IntervalSet tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getTokenNames()));
    // Simulate call stack after input '(x' from within rule expr
    callStackFrom_expr = new ParserRuleContext(null, 9);
    tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getTokenNames()));
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Grammar(org.antlr.v4.tool.Grammar) ATN(org.antlr.v4.runtime.atn.ATN) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Example 84 with IntervalSet

use of org.antlr.v4.runtime.misc.IntervalSet in project antlr4 by antlr.

the class UnicodeDataTemplateController method addUnicodeBinaryPropertyCodesToCodePointRanges.

private static void addUnicodeBinaryPropertyCodesToCodePointRanges(Map<String, IntervalSet> propertyCodePointRanges) {
    for (int property = UProperty.BINARY_START; property < UProperty.BINARY_LIMIT; property++) {
        String propertyName = getShortPropertyName(property);
        IntervalSet intervalSet = new IntervalSet();
        UnicodeSet unicodeSet = new UnicodeSet();
        unicodeSet.applyIntPropertyValue(property, 1);
        for (UnicodeSet.EntryRange range : unicodeSet.ranges()) {
            intervalSet.add(range.codepoint, range.codepointEnd);
        }
        propertyCodePointRanges.put(propertyName, intervalSet);
    }
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) UnicodeSet(com.ibm.icu.text.UnicodeSet)

Example 85 with IntervalSet

use of org.antlr.v4.runtime.misc.IntervalSet in project antlr4 by antlr.

the class UnicodeDataTemplateController method addIntPropertyRanges.

private static void addIntPropertyRanges(int property, String namePrefix, Map<String, IntervalSet> propertyCodePointRanges) {
    for (int propertyValue = UCharacter.getIntPropertyMinValue(property); propertyValue <= UCharacter.getIntPropertyMaxValue(property); propertyValue++) {
        UnicodeSet set = new UnicodeSet();
        set.applyIntPropertyValue(property, propertyValue);
        String propertyName = namePrefix + UCharacter.getPropertyValueName(property, propertyValue, UProperty.NameChoice.SHORT);
        IntervalSet intervalSet = propertyCodePointRanges.get(propertyName);
        if (intervalSet == null) {
            intervalSet = new IntervalSet();
            propertyCodePointRanges.put(propertyName, intervalSet);
        }
        addUnicodeSetToIntervalSet(set, intervalSet);
    }
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) UnicodeSet(com.ibm.icu.text.UnicodeSet)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)84 Test (org.junit.Test)48 ATNState (org.antlr.v4.runtime.atn.ATNState)11 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)10 ATN (org.antlr.v4.runtime.atn.ATN)8 ArrayList (java.util.ArrayList)7 Grammar (org.antlr.v4.tool.Grammar)7 Interval (org.antlr.v4.runtime.misc.Interval)6 SetTransition (org.antlr.v4.runtime.atn.SetTransition)5 UnicodeSet (com.ibm.icu.text.UnicodeSet)4 HashMap (java.util.HashMap)4 Token (org.antlr.runtime.Token)4 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)4 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)4 LinkedHashMap (java.util.LinkedHashMap)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)3 DecisionState (org.antlr.v4.runtime.atn.DecisionState)3 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)3 Transition (org.antlr.v4.runtime.atn.Transition)3