Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ module.exports = grammar({
$.extension_expression,
$.lazy_expression,
$._jsx_element,
$.regex,
),

parenthesized_expression: ($) =>
Expand Down Expand Up @@ -752,6 +753,7 @@ module.exports = grammar({
$.number,
$.true,
$.false,
$.regex,
),

variant_pattern: ($) =>
Expand Down Expand Up @@ -1185,6 +1187,30 @@ module.exports = grammar({

extension_identifier: ($) => /[a-zA-Z0-9_\.]+/,

regex: ($) =>
seq(
"/",
field("pattern", $.regex_pattern),
token.immediate(prec(1, "/")),
optional(field("flags", $.regex_flags)),
),

regex_pattern: (_) =>
token.immediate(
prec(
-1,
repeat1(
choice(
seq("[", repeat(choice(seq("\\", /./), /[^\]\n\\]/)), "]"),
seq("\\", /./),
/[^/\\\[\n]/,
),
),
),
),

regex_flags: (_) => token.immediate(/[a-z]+/),
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex_flags pattern allows any sequence of lowercase letters (pattern: /[a-z]+/). While JavaScript/ReScript officially supports only specific flags (g, i, m, s, u, y, d), this lenient validation allows invalid flags like "xyz" to parse without error. Consider whether stricter validation would be beneficial, such as:

regex_flags: (_) => token.immediate(/[gimsuy]+/)

However, the current implementation may be intentionally permissive for forward compatibility with potential new flags. If this is the intended behavior, it's acceptable, but it's worth documenting this design decision.

Suggested change
regex_flags: (_) => token.immediate(/[a-z]+/),
// Only allow officially supported JS/ReScript regex flags: g, i, m, s, u, y, d
regex_flags: (_) => token.immediate(/[gimsuyd]+/),

Copilot uses AI. Check for mistakes.

number: ($) => {
// OCaml: https://github.com/tree-sitter/tree-sitter-ocaml/blob/f1106bf834703f1f2f795da1a3b5f8f40174ffcc/ocaml/grammar.js#L26
const hex_literal = seq(
Expand Down
2 changes: 2 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
] @string


(regex) @string.special

(character) @character
(escape_sequence) @string.escape

Expand Down
5 changes: 4 additions & 1 deletion queries/injections.scm
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
((comment) @injection.content (#set! injection.language "comment"))

; regex literal
(regex
(regex_pattern) @injection.content (#set! injection.language "regex"))

; %re
(extension_expression
(extension_identifier) @_name
Expand All @@ -26,4 +30,3 @@
(#eq? @_name "relay")
(expression_statement
(_ (_) @injection.content (#set! injection.language "graphql") )))

129 changes: 129 additions & 0 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading