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));
}
}
}
}
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);
}
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();
}
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());
}
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)));
}
Aggregations