Skip to content

Commit 02da0ab

Browse files
Document that token patterns are combinators, not RegExp (#17)
A token pattern is built with the combinator API (seq/oneOf/range/plus/ star/altPattern/…), not a RegExp — `token(/…/)` is a TS2345 type error since the token-pattern IR migration. Add a short note + a RegExp→ combinator conversion table to the README so adopters coming from regex know how to write token config. Prompted by #15.
1 parent a939fc4 commit 02da0ab

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ export default defineGrammar({
269269
});
270270
```
271271

272+
Token patterns are **combinators, not regular expressions**`seq` / `oneOf` / `range` / `noneOf` / `plus` / `star` / `altPattern` / `optPattern` / … assemble a structured pattern IR (regex is a *derived* backend, not the source of truth). A bare `RegExp` is not a valid token pattern: `token(/…/)` is a `TS2345` type error. Coming from regex:
273+
274+
| RegExp | Combinator |
275+
|---|---|
276+
| `/[ \t]+/` | `plus(oneOf(' ', '\t'))` |
277+
| `/[A-Z_][A-Z0-9_]*/` | `seq(oneOf(range('A', 'Z'), '_'), star(oneOf(range('A', 'Z'), range('0', '9'), '_')))` |
278+
| `/"[^"]*"/` | `seq('"', star(noneOf('"')), '"')` |
279+
| `/\d+(\.\d+)?/` | `seq(plus(digit), optPattern(seq('.', plus(digit))))` |
280+
281+
Note `digit` above is just `range('0', '9')` — patterns are plain values you name and reuse, not magic strings.
282+
272283
The parser uses these rules to build a CST. The highlighter reads the same rule **shapes** and infers most scopes structurally — with no per-rule annotation:
273284

274285
- `foo(x)``foo` is `entity.name.function` (from the `$ '(' …` call form)

0 commit comments

Comments
 (0)