Expand regex support to include emojis and binary ops
The `Emoji` property alias is already present, but the actual property is not available since it lives in a new file. This adds that file to the `generate-unicode-categories-json`. The `emoji-data` file follows the same format as the ones we already consume in `generate-unicode-categories-json`, so adding emoji support is fairly easy. his, grammars would need to hard-code a set of unicode ranges in their own regex. The Javascript library `emoji-regex` cannot be used because of #451. For unclear reasons, the characters #, *, and 0-9 are marked as `Emoji=Yes` by `emoji-data.txt`. Because of this, a grammar that wishes to use emojis is likely to want to exclude those characters. For that reason, this change also adds support for binary operations in regexes, e.g. `[\p{Emoji}&&[^#*0-9]]`. Lastly (and perhaps controversially), this change introduces new variables available at grammar compile time, for the major, minor, and patch versions of the tree-sitter CLI used to compile the grammar. This will allow grammars to conditionally adopt these new regex features while remaining backward compatible with older versions of the CLI. Without this part of the change, grammar authors who do not precompile and check-in their `grammar.json` would need to wait for downstream systems to adopt a newer tree-sitter CLI version before they could begin to use these features.
This commit is contained in:
parent
2346570901
commit
8fadf18655
7 changed files with 172 additions and 22 deletions
|
|
@ -276,6 +276,20 @@ impl CharacterSet {
|
|||
}
|
||||
}
|
||||
|
||||
/// Produces a `CharacterSet` containing every character in `self` that is not present in
|
||||
/// `other`.
|
||||
pub fn difference(mut self, mut other: CharacterSet) -> CharacterSet {
|
||||
self.remove_intersection(&mut other);
|
||||
self
|
||||
}
|
||||
|
||||
/// Produces a `CharacterSet` containing every character that is in _exactly one_ of `self` or
|
||||
/// `other`, but is not present in both sets.
|
||||
pub fn symmetric_difference(mut self, mut other: CharacterSet) -> CharacterSet {
|
||||
self.remove_intersection(&mut other);
|
||||
self.add(&other)
|
||||
}
|
||||
|
||||
pub fn iter<'a>(&'a self) -> impl Iterator<Item = u32> + 'a {
|
||||
self.ranges.iter().flat_map(|r| r.clone())
|
||||
}
|
||||
|
|
@ -817,7 +831,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_character_set_remove_intersection() {
|
||||
fn test_character_set_intersection_difference_ops() {
|
||||
struct Row {
|
||||
left: CharacterSet,
|
||||
right: CharacterSet,
|
||||
|
|
@ -942,6 +956,25 @@ mod tests {
|
|||
"row {}b: {:?} - {:?}",
|
||||
i, row.right, row.left
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
row.left.clone().difference(row.right.clone()),
|
||||
row.left_only,
|
||||
"row {}b: {:?} -- {:?}",
|
||||
i,
|
||||
row.left,
|
||||
row.right
|
||||
);
|
||||
|
||||
let symm_difference = row.left_only.clone().add(&mut row.right_only.clone());
|
||||
assert_eq!(
|
||||
row.left.clone().symmetric_difference(row.right.clone()),
|
||||
symm_difference,
|
||||
"row {}b: {:?} ~~ {:?}",
|
||||
i,
|
||||
row.left,
|
||||
row.right
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue