JS: introduce RegExpSequence::nextElement and previousElement

This commit is contained in:
Esben Sparre Andreasen
2020-02-17 23:20:25 +01:00
parent 8a9587fc91
commit e8938fb466

View File

@@ -76,40 +76,22 @@ class RegExpTerm extends Locatable, @regexpterm {
/** Gets the regular expression term that is matched (textually) before this one, if any. */
RegExpTerm getPredecessor() {
exists(RegExpTerm parent | parent = getParent() |
if parent instanceof RegExpSequence
then
exists(RegExpSequence seq, int i |
seq = parent and
seq.getChild(i) = this
|
seq.getChild(i - 1) = result
or
i = 0 and result = seq.getPredecessor()
)
else (
not parent instanceof RegExpSubPattern and
result = parent.getPredecessor()
)
result = parent.(RegExpSequence).previousElement(this)
or
not exists(parent.(RegExpSequence).previousElement(this)) and
not parent instanceof RegExpSubPattern and
result = parent.getPredecessor()
)
}
/** Gets the regular expression term that is matched (textually) after this one, if any. */
RegExpTerm getSuccessor() {
exists(RegExpTerm parent | parent = getParent() |
if parent instanceof RegExpSequence
then
exists(RegExpSequence seq, int i |
seq = parent and
seq.getChild(i) = this
|
seq.getChild(i + 1) = result
or
i = seq.getNumChild() - 1 and result = seq.getSuccessor()
)
else (
not parent instanceof RegExpSubPattern and
result = parent.getSuccessor()
)
result = parent.(RegExpSequence).nextElement(this)
or
not exists(parent.(RegExpSequence).nextElement(this)) and
not parent instanceof RegExpSubPattern and
result = parent.getSuccessor()
)
}
@@ -328,6 +310,19 @@ class RegExpSequence extends RegExpTerm, @regexp_seq {
or
result = getChild(i).getConstantValue() + getConstantValue(i+1)
}
/** Gets the element preceding `element` in this sequence. */
RegExpTerm previousElement(RegExpTerm element) {
element = nextElement(result)
}
/** Gets the element following `element` in this sequence. */
RegExpTerm nextElement(RegExpTerm element) {
exists(int i |
element = this.getChild(i) and
result = this.getChild(i + 1)
)
}
}
/**