Code improvements:

- Replace the containment logic with built in `Location` functionality.
- Generalize `tokenize` to output the tokens that fall within any location.
This commit is contained in:
tiferet
2022-11-30 12:56:59 -08:00
parent a79bdf1cbc
commit 486752d19e
2 changed files with 43 additions and 34 deletions

View File

@@ -226,43 +226,12 @@ class CodexPrompt extends EndpointFeature, TCodexPrompt {
}
/**
* Holds if the location of `node` contains the location of `token`
* Gets the reconstructed source code text for a range of locations.
*/
cached
predicate containsToken(AstNode node, Token token) {
exists(
string file, int node_start_line, int node_start_column, int node_end_line,
int node_end_column, int token_start_line, int token_start_column, int token_end_line,
int token_end_column
|
node.getLocation()
.hasLocationInfo(file, node_start_line, node_start_column, node_end_line, node_end_column) and
token
.getLocation()
.hasLocationInfo(file, token_start_line, token_start_column, token_end_line,
token_end_column) and
(
node_start_line < token_start_line
or
node_start_line = token_start_line and
node_start_column <= token_start_column
) and
(
node_end_line > token_end_line
or
node_end_line = token_end_line and
node_end_column >= token_end_column
)
)
}
/**
* Gets the reconstructed source code text for `node`.
*/
string tokenise(DataFlow::Node node) {
string tokenize(Location location) {
result =
strictconcat(Token token |
containsToken(node.getAstNode(), token)
location.containsLoosely(token.getLocation())
|
token.getValue(),
// Use space as the separator, since that is most likely.
@@ -274,6 +243,13 @@ class CodexPrompt extends EndpointFeature, TCodexPrompt {
token.getLocation().getStartLine(), token.getLocation().getStartColumn()
)
}
/**
* Gets the reconstructed source code text for `node`.
*/
string tokenizeEndpoint(DataFlow::Node node) {
result = tokenize(node.getAstNode().getLocation())
}
}
/**

View File

@@ -40,6 +40,18 @@ class Location extends @location {
)
}
/** Holds if this location starts before or at the same place as location `that`. */
predicate startsBeforeOrWith(Location that) {
exists(File f, int sl1, int sc1, int sl2, int sc2 |
locations_default(this, f, sl1, sc1, _, _) and
locations_default(that, f, sl2, sc2, _, _)
|
sl1 < sl2
or
sl1 = sl2 and sc1 <= sc2
)
}
/** Holds if this location ends after location `that`. */
pragma[inline]
predicate endsAfter(Location that) {
@@ -53,12 +65,33 @@ class Location extends @location {
)
}
/** Holds if this location ends after or at the same place as location `that`. */
pragma[inline]
predicate endsAfterOrWith(Location that) {
exists(File f, int el1, int ec1, int el2, int ec2 |
locations_default(this, f, _, _, el1, ec1) and
locations_default(that, f, _, _, el2, ec2)
|
el1 > el2
or
el1 = el2 and ec1 >= ec2
)
}
/**
* Holds if this location contains location `that`, meaning that it starts
* before and ends after it.
*/
predicate contains(Location that) { this.startsBefore(that) and this.endsAfter(that) }
/**
* Holds if this location contains location `that`, meaning that it starts
* before or at the same place and ends after or at the same place.
*/
predicate containsLoosely(Location that) {
this.startsBeforeOrWith(that) and this.endsAfterOrWith(that)
}
/** Holds if this location is empty. */
predicate isEmpty() { exists(int l, int c | locations_default(this, _, l, c, l, c - 1)) }