-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Shared: Add library for unbound lists #21060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+162
−112
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * Provides logic for representing unbound lists. | ||
| * | ||
| * The lists are represented internally as strings, and generally it should | ||
| * be preferred to instead use a `newtype` representation, but in certain | ||
| * cases where this is not feasible (typically because of performance) unbound | ||
| * lists can be used. | ||
| */ | ||
| overlay[local?] | ||
| module; | ||
|
|
||
| private import Location | ||
|
|
||
| /** Provides the input to `Make`. */ | ||
| signature module InputSig<LocationSig Location> { | ||
| /** An element. */ | ||
| class Element { | ||
| /** Gets a textual representation of this element. */ | ||
| string toString(); | ||
|
|
||
| /** Gets the location of this element. */ | ||
| Location getLocation(); | ||
| } | ||
|
|
||
| /** Gets a unique ID used to identify element `e` amongst all elements. */ | ||
| int getId(Element e); | ||
|
|
||
| /** | ||
| * Gets a textual representation for element `e`, which will be used in the | ||
| * `toString` representation for unbound lists. | ||
| */ | ||
| default string getElementString(Element e) { result = e.toString() } | ||
|
|
||
| /** Gets an optional length limit for unbound lists. */ | ||
| default int getLengthLimit() { none() } | ||
| } | ||
|
|
||
| final private class String = string; | ||
|
|
||
| /** Provides the `UnboundList` implementation. */ | ||
| module Make<LocationSig Location, InputSig<Location> Input> { | ||
| private import Input | ||
| private import codeql.util.DenseRank | ||
|
|
||
| // Use dense ranking to assign compact IDs to elements | ||
| private module DenseRankInput implements DenseRankInputSig { | ||
| class Ranked = Element; | ||
|
|
||
| predicate getRank = getId/1; | ||
| } | ||
|
|
||
| /** Gets the rank of element `e`, which is used internally in the string encoding. */ | ||
| int getRank(Element e) { e = DenseRank<DenseRankInput>::denseRank(result) } | ||
|
|
||
| private string encode(Element e) { result = getRank(e).toString() } | ||
|
|
||
| bindingset[s] | ||
| private Element decode(string s) { encode(result) = s } | ||
|
|
||
| /** | ||
| * An unbound list encoded as a string. | ||
| */ | ||
| class UnboundList extends String { | ||
| bindingset[this] | ||
| UnboundList() { exists(this) } | ||
|
|
||
| /** Gets the `i`th element in this list. */ | ||
| bindingset[this] | ||
| private Element getElement(int i) { result = decode(this.splitAt(".", i)) } | ||
|
|
||
| /** Gets a textual representation of this list. */ | ||
| bindingset[this] | ||
| string toString() { | ||
| result = | ||
| concat(int i, Element e | e = this.getElement(i) | getElementString(e), "." order by i) | ||
| } | ||
|
|
||
| /** Holds if this list is empty. */ | ||
| predicate isEmpty() { this = "" } | ||
|
|
||
| /** Gets the length of this list. */ | ||
| bindingset[this] | ||
| pragma[inline_late] | ||
| int length() { | ||
| // Same as | ||
| // `result = count(this.indexOf("."))` | ||
| // but performs better because it doesn't use an aggregate | ||
| result = this.regexpReplaceAll("[0-9]+", "").length() | ||
| } | ||
|
|
||
| /** Gets the list obtained by appending `suffix` onto this list. */ | ||
| bindingset[this, suffix] | ||
| UnboundList append(UnboundList suffix) { | ||
| result = this + suffix and | ||
| ( | ||
| not exists(getLengthLimit()) | ||
| or | ||
| result.length() <= getLengthLimit() | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Gets the list obtained by appending `suffix` onto this list. | ||
| * | ||
| * Unlike `append`, this predicate has `result` in the binding set, | ||
| * so there is no need to check the length of `result`. | ||
| */ | ||
| bindingset[this, result] | ||
| UnboundList appendInverse(UnboundList suffix) { suffix = result.stripPrefix(this) } | ||
|
|
||
| /** Gets the list obtained by removing `prefix` from this list. */ | ||
| bindingset[this, prefix] | ||
| UnboundList stripPrefix(UnboundList prefix) { this = prefix + result } | ||
|
|
||
| /** Holds if this list starts with `e`, followed by `suffix`. */ | ||
| bindingset[this] | ||
| predicate isCons(Element e, UnboundList suffix) { | ||
| exists(string regexp | regexp = "([0-9]+)\\.(.*)" | | ||
| e = decode(this.regexpCapture(regexp, 1)) and | ||
| suffix = this.regexpCapture(regexp, 2) | ||
| ) | ||
| } | ||
|
|
||
| /** Holds if this list starts with `prefix`, followed by `e`. */ | ||
| bindingset[this] | ||
| predicate isSnoc(UnboundList prefix, Element e) { | ||
| exists(string regexp | regexp = "(|.+\\.)([0-9]+)\\." | | ||
| prefix = this.regexpCapture(regexp, 1) and | ||
| e = decode(this.regexpCapture(regexp, 2)) | ||
| ) | ||
| } | ||
|
|
||
| /** Gets the head of this list, if any. */ | ||
| bindingset[this] | ||
| Element getHead() { result = this.getElement(0) } | ||
| } | ||
|
|
||
| /** Provides predicates for constructing `UnboundList`s. */ | ||
| module UnboundList { | ||
| /** Gets the empty list. */ | ||
| UnboundList nil() { result.isEmpty() } | ||
|
|
||
| /** Gets the singleton list `e`. */ | ||
| UnboundList singleton(Element e) { result = encode(e) + "." } | ||
|
|
||
| /** | ||
| * Gets the list obtained by appending the singleton list `e` | ||
| * onto `suffix`. | ||
| */ | ||
| bindingset[suffix] | ||
| UnboundList cons(Element e, UnboundList suffix) { result = singleton(e).append(suffix) } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.