Search in sources :

Example 11 with Interval

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

the class Trees method stripChildrenOutOfRange.

/** Replace any subtree siblings of root that are completely to left
	 *  or right of lookahead range with a CommonToken(Token.INVALID_TYPE,"...")
	 *  node. The source interval for t is not altered to suit smaller range!
	 *
	 *  WARNING: destructive to t.
	 *
	 *  @since 4.5.1
	 */
public static void stripChildrenOutOfRange(ParserRuleContext t, ParserRuleContext root, int startIndex, int stopIndex) {
    if (t == null)
        return;
    for (int i = 0; i < t.getChildCount(); i++) {
        ParseTree child = t.getChild(i);
        Interval range = child.getSourceInterval();
        if (child instanceof ParserRuleContext && (range.b < startIndex || range.a > stopIndex)) {
            if (isAncestorOf(child, root)) {
                // replace only if subtree doesn't have displayed root
                CommonToken abbrev = new CommonToken(Token.INVALID_TYPE, "...");
                t.children.set(i, new TerminalNodeImpl(abbrev));
            }
        }
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonToken(org.antlr.v4.runtime.CommonToken) Interval(org.antlr.v4.runtime.misc.Interval)

Example 12 with Interval

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

the class LexerATNFactory method set.

@Override
public Handle set(GrammarAST associatedAST, List<GrammarAST> alts, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : alts) {
        if (t.getType() == ANTLRParser.RANGE) {
            int a = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(0).getText());
            int b = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(1).getText());
            if (checkRange((GrammarAST) t.getChild(0), (GrammarAST) t.getChild(1), a, b)) {
                checkSetCollision(associatedAST, set, a, b);
                set.add(a, b);
            }
        } else if (t.getType() == ANTLRParser.LEXER_CHAR_SET) {
            set.addAll(getSetFromCharSetLiteral(t));
        } else if (t.getType() == ANTLRParser.STRING_LITERAL) {
            int c = CharSupport.getCharValueFromGrammarCharLiteral(t.getText());
            if (c != -1) {
                checkSetCollision(associatedAST, set, c);
                set.add(c);
            } else {
                g.tool.errMgr.grammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET, g.fileName, t.getToken(), t.getText());
            }
        } else if (t.getType() == ANTLRParser.TOKEN_REF) {
            g.tool.errMgr.grammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET, g.fileName, t.getToken(), t.getText());
        }
    }
    if (invert) {
        left.addTransition(new NotSetTransition(right, set));
    } else {
        Transition transition;
        if (set.getIntervals().size() == 1) {
            Interval interval = set.getIntervals().get(0);
            transition = CodePointTransitions.createWithCodePointRange(right, interval.a, interval.b);
        } else {
            transition = new SetTransition(right, set);
        }
        left.addTransition(transition);
    }
    associatedAST.atnState = left;
    return new Handle(left, right);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) 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) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) Interval(org.antlr.v4.runtime.misc.Interval)

Example 13 with Interval

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

the class CharSupport method getIntervalSetEscapedString.

public static String getIntervalSetEscapedString(IntervalSet intervalSet) {
    StringBuilder buf = new StringBuilder();
    Iterator<Interval> iter = intervalSet.getIntervals().iterator();
    while (iter.hasNext()) {
        Interval interval = iter.next();
        buf.append(getRangeEscapedString(interval.a, interval.b));
        if (iter.hasNext()) {
            buf.append(" | ");
        }
    }
    return buf.toString();
}
Also used : Interval(org.antlr.v4.runtime.misc.Interval)

Example 14 with Interval

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

the class TestParserInterpreter method testEmptyFirstRule.

@Test
public void testEmptyFirstRule() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : x A ;\n" + "x : ;\n", lg);
    ParseTree t = testInterp(lg, g, "s", "a", "(s x a)");
    // s
    assertEquals("0..0", t.getSourceInterval().toString());
    // This gets an empty interval because the stop token is null for x
    // x
    assertEquals("0..-1", t.getChild(0).getSourceInterval().toString());
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 15 with Interval

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

the class TestUnbufferedCharStream method testGetTextInMarkedRange.

@Test
public void testGetTextInMarkedRange() {
    CharStream input = createStream("xyz");
    input.consume();
    int m1 = input.mark();
    assertEquals(1, input.index());
    input.consume();
    input.consume();
    assertEquals("yz", input.getText(new Interval(1, 2)));
}
Also used : CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) Interval(org.antlr.v4.runtime.misc.Interval) Test(org.junit.Test)

Aggregations

Interval (org.antlr.v4.runtime.misc.Interval)14 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 ATNState (org.antlr.v4.runtime.atn.ATNState)3 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)3 DecisionState (org.antlr.v4.runtime.atn.DecisionState)3 SetTransition (org.antlr.v4.runtime.atn.SetTransition)3 Transition (org.antlr.v4.runtime.atn.Transition)3 Test (org.junit.Test)3 LinkedHashMap (java.util.LinkedHashMap)2 CharStream (org.antlr.v4.runtime.CharStream)2 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)2 RangeTransition (org.antlr.v4.runtime.atn.RangeTransition)2 IntegerList (org.antlr.v4.runtime.misc.IntegerList)2 Program (boa.compiler.ast.Program)1 Start (boa.compiler.ast.Start)1 BoaErrorListener (boa.compiler.listeners.BoaErrorListener)1 LexerErrorListener (boa.compiler.listeners.LexerErrorListener)1 ParserErrorListener (boa.compiler.listeners.ParserErrorListener)1