AST: add ParenthesizedExpr

This commit is contained in:
Arthur Baars
2021-02-04 11:35:55 +01:00
parent 61d9669655
commit 2035bc4d3a
5 changed files with 97 additions and 52 deletions

View File

@@ -124,7 +124,15 @@ class ExprSequence extends Expr {
override string getAPrimaryQlClass() { result = "ExprSequence" }
override string toString() { result = "...; ..." }
override string toString() {
exists(int c | c = this.getNumberOfExpressions() |
c = 0 and result = ";"
or
c = 1 and result = this.getExpr(0).toString()
or
c > 1 and result = "...; ..."
)
}
/** Gets the `n`th expression in this sequence. */
final Expr getExpr(int n) { result = range.getExpr(n) }
@@ -151,6 +159,35 @@ class BodyStatement extends ExprSequence {
override BodyStatement::Range range;
}
/**
* A parenthesized expression sequence, typically containing a single expression:
* ```rb
* (x + 1)
* ```
* However, they can also contain multiple expressions (the value of the parenthesized
* expression is the last expression):
* ```rb
* (foo; bar)
* ```
* or even an empty sequence (value is `nil`):
* ```rb
* ()
* ```
*/
class ParenthesizedExpr extends ExprSequence, @parenthesized_statements {
final override ParenthesizedExpr::Range range;
final override string getAPrimaryQlClass() { result = "ParenthesizedExpr" }
final override string toString() {
exists(int c | c = this.getNumberOfExpressions() |
c = 0 and result = "()"
or
c > 0 and result = "(" + ExprSequence.super.toString() + ")"
)
}
}
/**
* A scope resolution, typically used to access constants defined in a class or
* module.

View File

@@ -172,6 +172,14 @@ module BodyStatement {
abstract class Range extends ExprSequence::Range { }
}
module ParenthesizedExpr {
class Range extends ExprSequence::Range, @parenthesized_statements {
final override Generated::ParenthesizedStatements generated;
final override Expr getExpr(int n) { result = generated.getChild(n) }
}
}
module ThenExpr {
class Range extends ExprSequence::Range, @then {
final override Generated::Then generated;