Ruby: Move regex/non-regex split into TAstNode to convey disjointness

This commit is contained in:
Tom Hvitved
2022-01-19 09:22:01 +01:00
parent 4f7f92490a
commit f02aeafef1
2 changed files with 51 additions and 30 deletions

View File

@@ -274,12 +274,10 @@ class StringComponent extends AstNode, TStringComponent {
* "foo#{ bar() } baz"
* ```
*/
class StringTextComponent extends StringComponent, TStringTextComponent {
class StringTextComponent extends StringComponent, TStringTextComponentNonRegexp {
private Ruby::Token g;
StringTextComponent() {
this = TStringTextComponent(g) and not g.getParent() instanceof Ruby::Regex
}
StringTextComponent() { this = TStringTextComponentNonRegexp(g) }
final override string toString() { result = g.getValue() }
@@ -291,12 +289,10 @@ class StringTextComponent extends StringComponent, TStringTextComponent {
/**
* An escape sequence component of a string or string-like literal.
*/
class StringEscapeSequenceComponent extends StringComponent, TStringEscapeSequenceComponent {
class StringEscapeSequenceComponent extends StringComponent, TStringEscapeSequenceComponentNonRegexp {
private Ruby::EscapeSequence g;
StringEscapeSequenceComponent() {
this = TStringEscapeSequenceComponent(g) and not g.getParent() instanceof Ruby::Regex
}
StringEscapeSequenceComponent() { this = TStringEscapeSequenceComponentNonRegexp(g) }
final override string toString() { result = g.getValue() }
@@ -309,12 +305,10 @@ class StringEscapeSequenceComponent extends StringComponent, TStringEscapeSequen
* An interpolation expression component of a string or string-like literal.
*/
class StringInterpolationComponent extends StringComponent, StmtSequence,
TStringInterpolationComponent {
TStringInterpolationComponentNonRegexp {
private Ruby::Interpolation g;
StringInterpolationComponent() {
this = TStringInterpolationComponent(g) and not g.getParent() instanceof Ruby::Regex
}
StringInterpolationComponent() { this = TStringInterpolationComponentNonRegexp(g) }
final override string toString() { result = "#{...}" }
@@ -325,14 +319,15 @@ class StringInterpolationComponent extends StringComponent, StmtSequence,
final override string getAPrimaryQlClass() { result = "StringInterpolationComponent" }
}
private class TRegExpComponent =
TStringTextComponentRegexp or TStringEscapeSequenceComponentRegexp or
TStringInterpolationComponentRegexp;
/**
* The base class for a component of a regular expression literal.
*/
class RegExpComponent extends AstNode, TStringComponent {
private RegExpLiteral parent;
RegExpComponent() { toGenerated(this).getParent() = toGenerated(parent) }
class RegExpComponent extends AstNode, TRegExpComponent {
/** Gets the source text for this regex component, if any. */
string getValueText() { none() }
}
@@ -348,10 +343,10 @@ class RegExpComponent extends AstNode, TStringComponent {
* "foo#{ bar() } baz"
* ```
*/
class RegExpTextComponent extends RegExpComponent, TStringTextComponent {
class RegExpTextComponent extends RegExpComponent, TStringTextComponentRegexp {
private Ruby::Token g;
RegExpTextComponent() { this = TStringTextComponent(g) and g.getParent() instanceof Ruby::Regex }
RegExpTextComponent() { this = TStringTextComponentRegexp(g) }
final override string toString() { result = g.getValue() }
@@ -367,10 +362,10 @@ class RegExpTextComponent extends RegExpComponent, TStringTextComponent {
/**
* An escape sequence component of a regex literal.
*/
class RegExpEscapeSequenceComponent extends RegExpComponent, TStringEscapeSequenceComponent {
class RegExpEscapeSequenceComponent extends RegExpComponent, TStringEscapeSequenceComponentRegexp {
private Ruby::EscapeSequence g;
RegExpEscapeSequenceComponent() { this = TStringEscapeSequenceComponent(g) }
RegExpEscapeSequenceComponent() { this = TStringEscapeSequenceComponentRegexp(g) }
final override string toString() { result = g.getValue() }
@@ -387,10 +382,10 @@ class RegExpEscapeSequenceComponent extends RegExpComponent, TStringEscapeSequen
* An interpolation expression component of a regex literal.
*/
class RegExpInterpolationComponent extends RegExpComponent, StmtSequence,
TStringInterpolationComponent {
TStringInterpolationComponentRegexp {
private Ruby::Interpolation g;
RegExpInterpolationComponent() { this = TStringInterpolationComponent(g) }
RegExpInterpolationComponent() { this = TStringInterpolationComponentRegexp(g) }
final override string toString() { result = "#{...}" }

View File

@@ -272,10 +272,25 @@ private module Cached {
TStmtSequenceSynth(AST::AstNode parent, int i) { mkSynthChild(StmtSequenceKind(), parent, i) } or
TStringArrayLiteral(Ruby::StringArray g) or
TStringConcatenation(Ruby::ChainedString g) or
TStringEscapeSequenceComponent(Ruby::EscapeSequence g) or
TStringInterpolationComponent(Ruby::Interpolation g) or
TStringTextComponent(Ruby::Token g) {
g instanceof Ruby::StringContent or g instanceof Ruby::HeredocContent
TStringEscapeSequenceComponentNonRegexp(Ruby::EscapeSequence g) {
not g.getParent() instanceof Ruby::Regex
} or
TStringEscapeSequenceComponentRegexp(Ruby::EscapeSequence g) {
g.getParent() instanceof Ruby::Regex
} or
TStringInterpolationComponentNonRegexp(Ruby::Interpolation g) {
not g.getParent() instanceof Ruby::Regex
} or
TStringInterpolationComponentRegexp(Ruby::Interpolation g) {
g.getParent() instanceof Ruby::Regex
} or
TStringTextComponentNonRegexp(Ruby::Token g) {
(g instanceof Ruby::StringContent or g instanceof Ruby::HeredocContent) and
not g.getParent() instanceof Ruby::Regex
} or
TStringTextComponentRegexp(Ruby::Token g) {
(g instanceof Ruby::StringContent or g instanceof Ruby::HeredocContent) and
g.getParent() instanceof Ruby::Regex
} or
TSubExprReal(Ruby::Binary g) { g instanceof @ruby_binary_minus } or
TSubExprSynth(AST::AstNode parent, int i) { mkSynthChild(SubExprKind(), parent, i) } or
@@ -489,9 +504,12 @@ private module Cached {
n = TSplatParameter(result) or
n = TStringArrayLiteral(result) or
n = TStringConcatenation(result) or
n = TStringEscapeSequenceComponent(result) or
n = TStringInterpolationComponent(result) or
n = TStringTextComponent(result) or
n = TStringEscapeSequenceComponentNonRegexp(result) or
n = TStringEscapeSequenceComponentRegexp(result) or
n = TStringInterpolationComponentNonRegexp(result) or
n = TStringInterpolationComponentRegexp(result) or
n = TStringTextComponentNonRegexp(result) or
n = TStringTextComponentRegexp(result) or
n = TSubExprReal(result) or
n = TSubshellLiteral(result) or
n = TSymbolArrayLiteral(result) or
@@ -680,6 +698,14 @@ class TIntegerLiteral = TIntegerLiteralReal or TIntegerLiteralSynth;
class TBooleanLiteral = TTrueLiteral or TFalseLiteral;
class TStringTextComponent = TStringTextComponentNonRegexp or TStringTextComponentRegexp;
class TStringEscapeSequenceComponent =
TStringEscapeSequenceComponentNonRegexp or TStringEscapeSequenceComponentRegexp;
class TStringInterpolationComponent =
TStringInterpolationComponentNonRegexp or TStringInterpolationComponentRegexp;
class TStringComponent =
TStringTextComponent or TStringEscapeSequenceComponent or TStringInterpolationComponent;