Mirror: The magical sticky regex-based parser generator 🧙

Update README for new features

+47 -2
+47 -2
README.md
··· 248 248 | `(?= )` | `(?= ${/1/})` | A **positive lookahead** checks whether interpolations match, and if so continues the matcher without changing the input. If it matches, it's essentially ignored. | 249 249 | `(?! )` | `(?! ${/1/})` | A **negative lookahead** checks whether interpolations _don't_ match, and if so continues the matcher without changing the input. If the interpolations do match the matcher is aborted. | 250 250 251 + A couple of operators also support "short hands" that allow you to write 252 + lookaheads or non-capturing groups a little quicker. 253 + 254 + | Shorthand | Example | Description | 255 + | --------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 256 + | `:` | `:${/1/}` | A **non-capturing group** is like a regular group, but the interpolations matched inside it don't appear in the parser's output. | 257 + | `=` | `=${/1/}` | A **positive lookahead** checks whether interpolations match, and if so continues the matcher without changing the input. If it matches, it's essentially ignored. | 258 + | `!` | `!${/1/}` | A **negative lookahead** checks whether interpolations _don't_ match, and if so continues the matcher without changing the input. If the interpolations do match the matcher is aborted. | 259 + 251 260 We can combine and compose these operators to create more complex matchers. 252 261 For instance, we can extend the original example to only allow a specific set 253 262 of names by using the `|` operator: ··· 345 354 346 355 We've now entirely changed the output of the parser for this matcher. Given that each 347 356 matcher can change its output, we're free to change the parser's output entirely. 348 - By **returning a falsy value** in this matcher, we can also change the matcher to not have 349 - matched, which would cause other matchers to treat it like a mismatch! 357 + By returning `null` or `undefined` in this matcher, we can also change the matcher 358 + to not have matched, which would cause other matchers to treat it like a mismatch! 350 359 351 360 ```js 352 361 import { match, parse } from 'reghex'; ··· 373 382 374 383 tag(['test'], 'node_name'); 375 384 // ["test", .tag = "node_name"] 385 + ``` 386 + 387 + ### Tagged Template Parsing 388 + 389 + Any grammar in RegHex can also be used to parse a tagged template literal. 390 + A tagged template literal consists of a list of literals alternating with 391 + a list of "interpolations". 392 + 393 + In RegHex we can add an `interpolation` matcher to our grammars to allow it 394 + to parse interpolations in a template literal. 395 + 396 + ```js 397 + import { interpolation } from 'reghex'; 398 + 399 + const anyNumber = interpolation((x) => typeof x === 'number'); 400 + 401 + const num = match('num')` 402 + ${/[+-]?/} ${anyNumber} 403 + `; 404 + 405 + parse(num)`+${42}`; 406 + // ["+", 42, .tag = "num"] 407 + ``` 408 + 409 + This grammar now allows us to match arbitrary values if they're input into the 410 + parser. We can now call our grammar using a tagged template literal themselves 411 + to parse this. 412 + 413 + ```js 414 + import { interpolation } from 'reghex'; 415 + 416 + const anyNumber = interpolation((x) => typeof x === 'number'); 417 + 418 + const num = match('num')` 419 + ${/[+-]?/} ${anyNumber} 420 + `; 376 421 ``` 377 422 378 423 **That's it! May the RegExp be ever in your favor.**