forked from jasder/antlr
cleaned up interval stuff. moved getSourceInterval to ParserRuleContext; renamed create to of() in Interval.
This commit is contained in:
parent
c6365fb5e2
commit
4ea3c73d1f
|
@ -30,6 +30,7 @@ package org.antlr.v4.runtime;
|
|||
|
||||
import org.antlr.v4.runtime.atn.ATN;
|
||||
import org.antlr.v4.runtime.atn.ATNState;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
import org.antlr.v4.runtime.misc.NotNull;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
@ -291,6 +292,12 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
|
|||
@Override
|
||||
public int getRuleIndex() { return ruleIndex; }
|
||||
|
||||
@Override
|
||||
public Interval getSourceInterval() {
|
||||
return Interval.of(start.getTokenIndex(), stop.getTokenIndex());
|
||||
}
|
||||
|
||||
|
||||
public Symbol getStart() { return start; }
|
||||
public Symbol getStop() { return stop; }
|
||||
|
||||
|
|
|
@ -208,7 +208,12 @@ public class RuleContext implements ParseTree.RuleNode {
|
|||
return invokingState == -1;
|
||||
}
|
||||
|
||||
// satisfy the ParseTree interface
|
||||
// satisfy the ParseTree / SyntaxTree interface
|
||||
|
||||
@Override
|
||||
public Interval getSourceInterval() {
|
||||
return Interval.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RuleContext getRuleContext() { return this; }
|
||||
|
@ -231,14 +236,6 @@ public class RuleContext implements ParseTree.RuleNode {
|
|||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Interval getSourceInterval() {
|
||||
if ( getChildCount()==0 ) return Interval.INVALID;
|
||||
int start = getChild(0).getSourceInterval().a;
|
||||
int stop = getChild(getChildCount()-1).getSourceInterval().b;
|
||||
return new Interval(start, stop);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(ParseTreeVisitor<? extends T> visitor) { return visitor.visitChildren(this); }
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ public class Interval {
|
|||
public static final int INTERVAL_POOL_MAX_VALUE = 1000;
|
||||
|
||||
public static final Interval INVALID = new Interval(-1,-2);
|
||||
public static final Interval EMPTY = new Interval(0,-1); // len 0
|
||||
|
||||
static Interval[] cache = new Interval[INTERVAL_POOL_MAX_VALUE+1];
|
||||
|
||||
|
@ -52,8 +53,7 @@ public class Interval {
|
|||
* Interval object with a..a in it. On Java.g, 218623 IntervalSets
|
||||
* have a..a (set with 1 element).
|
||||
*/
|
||||
public static Interval create(int a, int b) {
|
||||
//return new Interval(a,b);
|
||||
public static Interval of(int a, int b) {
|
||||
// cache just a..a
|
||||
if ( a!=b || a<0 || a>INTERVAL_POOL_MAX_VALUE ) {
|
||||
return new Interval(a,b);
|
||||
|
@ -64,6 +64,14 @@ public class Interval {
|
|||
return cache[a];
|
||||
}
|
||||
|
||||
/** return number of elements between a and b inclusively. x..x is length 1.
|
||||
* if b < a, then length is 0. 9..10 has length 2.
|
||||
*/
|
||||
public int length() {
|
||||
if ( b<a ) return 0;
|
||||
return b-a+1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( o==null ) {
|
||||
|
@ -112,12 +120,12 @@ public class Interval {
|
|||
|
||||
/** Return the interval computed from combining this and other */
|
||||
public Interval union(Interval other) {
|
||||
return Interval.create(Math.min(a,other.a), Math.max(b,other.b));
|
||||
return Interval.of(Math.min(a, other.a), Math.max(b, other.b));
|
||||
}
|
||||
|
||||
/** Return the interval in common between this and o */
|
||||
public Interval intersection(Interval other) {
|
||||
return Interval.create(Math.max(a,other.a), Math.min(b,other.b));
|
||||
return Interval.of(Math.max(a, other.a), Math.min(b, other.b));
|
||||
}
|
||||
|
||||
/** Return the interval with elements from this not in other;
|
||||
|
@ -129,13 +137,13 @@ public class Interval {
|
|||
Interval diff = null;
|
||||
// other.a to left of this.a (or same)
|
||||
if ( other.startsBeforeNonDisjoint(this) ) {
|
||||
diff = Interval.create(Math.max(this.a,other.b+1),
|
||||
this.b);
|
||||
diff = Interval.of(Math.max(this.a, other.b + 1),
|
||||
this.b);
|
||||
}
|
||||
|
||||
// other.a to right of this.a
|
||||
else if ( other.startsAfterNonDisjoint(this) ) {
|
||||
diff = Interval.create(this.a, other.a-1);
|
||||
diff = Interval.of(this.a, other.a - 1);
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
|
|
|
@ -28,9 +28,15 @@
|
|||
*/
|
||||
package org.antlr.v4.runtime.misc;
|
||||
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Set;
|
||||
|
||||
/** A set of integers that relies on ranges being common to do
|
||||
* "run-length-encoded" like compression (if you view an IntSet like
|
||||
|
@ -111,7 +117,7 @@ public class IntervalSet implements IntSet {
|
|||
* {1..5, 6..7, 10..20}. Adding 4..8 yields {1..8, 10..20}.
|
||||
*/
|
||||
public void add(int a, int b) {
|
||||
add(Interval.create(a,b));
|
||||
add(Interval.of(a, b));
|
||||
}
|
||||
|
||||
// copy on write so we can cache a..a intervals and sets of that
|
||||
|
|
Loading…
Reference in New Issue