Skip to content
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

fix digit escape in highlight regex for constants #90

Merged
merged 1 commit into from
Oct 17, 2021
Merged

fix digit escape in highlight regex for constants #90

merged 1 commit into from
Oct 17, 2021

Conversation

the-mikedavis
Copy link
Contributor

Checklist (:heavy_check_mark:)
  • All tests pass in CI.
  • There are sufficient tests for the new fix/feature. (added one 👍)
  • Grammar rules have not been renamed unless absolutely necessary. (N/A)
  • The conflicts section hasn't grown too much. (N/A)
  • The parser size hasn't grown too much (check the value of STATE_COUNT in src/parser.c). (N/A)

I believe this regex escape "\d" actually needs to be "\\d" to become the digit escape.

pies.java

enum Pies {
  APPLE,
  PUMPKIN,
  PI_314
}

query1.scm

((identifier) @constant
 (#match? @constant "^_*[A-Z][A-Z\d_]+"))

query2.scm

((identifier) @constant
 (#match? @constant "^_*[A-Z][A-Z\d_]+$"))

query3.scm

((identifier) @constant
 (#match? @constant "^_*[A-Z][A-Z\\d_]+$"))

reproduction

$ tree-sitter query query1.scm pies.java
pies.java
  pattern: 0
    capture: constant, start: (1, 2), text: "APPLE"
  pattern: 0
    capture: constant, start: (2, 2), text: "PUMPKIN"
  pattern: 0
    capture: constant, start: (3, 2), text: "PI_314"

$ tree-sitter query query2.scm pies.java
pies.java
  pattern: 0
    capture: constant, start: (1, 2), text: "APPLE"
  pattern: 0
    capture: constant, start: (2, 2), text: "PUMPKIN"

$ tree-sitter query query3.scm pies.java
pies.java
  pattern: 0
    capture: constant, start: (1, 2), text: "APPLE"
  pattern: 0
    capture: constant, start: (2, 2), text: "PUMPKIN"
  pattern: 0
    capture: constant, start: (3, 2), text: "PI_314"

query1.scm is the current query rule, and it works but only because it doesn't end in $, so in theory other characters could end up matching the query than digits. query2.scm shows that the \d escape doesn't work: it fails to match the constant with digits. query3.scm switches to \\d with the trailing $ and it works again!

It looks like there's one of these \ds in the php queries (pr incoming...) but elsewhere there're \\ds (e.g. this ruby query)

@maxbrunsfeld
Copy link
Contributor

Thanks!

@maxbrunsfeld maxbrunsfeld merged commit ed3a87f into tree-sitter:master Oct 17, 2021
@the-mikedavis the-mikedavis deleted the fix-regex-digit-escape branch October 17, 2021 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants