AST: add Pattern::getRestIndex

This commit is contained in:
Arthur Baars
2021-02-15 15:22:14 +01:00
parent eee12eecc9
commit 9c5da197ed
3 changed files with 27 additions and 3 deletions

View File

@@ -25,7 +25,12 @@ class VariablePattern extends Pattern {
/**
* A tuple pattern.
*
* This includes both tuple patterns in parameters and assignments.
* This includes both tuple patterns in parameters and assignments. Example patterns:
* ```rb
* a, self.b = value
* (a, b), c[3] = value
* a, b, *rest, c, d = value
* ```
*/
class TuplePattern extends Pattern {
override TuplePattern::Range range;
@@ -35,4 +40,13 @@ class TuplePattern extends Pattern {
/** Gets a sub pattern in this tuple pattern. */
final Pattern getAnElement() { result = this.getElement(_) }
/**
* Gets the index of the pattern with the `*` marker on it, if it exists.
* In the example below the index is `2`.
* ```rb
* a, b, *rest, c, d = value
* ```
*/
final int getRestIndex() { result = range.getRestIndex() }
}

View File

@@ -29,8 +29,6 @@ module AstNode {
or
this instanceof Generated::RestAssignment
or
this = any(Generated::RestAssignment ra).getChild()
or
this instanceof Generated::SymbolArray
or
this instanceof Generated::Interpolation

View File

@@ -20,6 +20,8 @@ predicate explicitAssignmentNode(Generated::AstNode n, Generated::AstNode assign
parent instanceof Generated::DestructuredLeftAssignment
or
parent instanceof Generated::LeftAssignmentList
or
parent instanceof Generated::RestAssignment
)
}
@@ -74,6 +76,14 @@ module TuplePattern {
class Range extends Pattern::Range, Range_ {
Pattern::Range getElement(int i) {
exists(Generated::AstNode c | c = getChild(i) |
result = c.(Generated::RestAssignment).getChild()
or
not c instanceof Generated::RestAssignment and result = c
)
}
private Generated::AstNode getChild(int i) {
result = this.(Generated::DestructuredParameter).getChild(i)
or
result = this.(Generated::DestructuredLeftAssignment).getChild(i)
@@ -81,6 +91,8 @@ module TuplePattern {
result = this.(Generated::LeftAssignmentList).getChild(i)
}
int getRestIndex() { result = unique(int i | getChild(i) instanceof Generated::RestAssignment) }
override Variable getAVariable() { result = this.getElement(_).getAVariable() }
override string toString() { result = "(..., ...)" }