[HN Gopher] Implementing Regular Expressions in TypeScript Types...
___________________________________________________________________
Implementing Regular Expressions in TypeScript Types (Badly)
Author : skalt
Score : 59 points
Date : 2024-10-17 01:41 UTC (3 days ago)
(HTM) web link (skalt.github.io)
(TXT) w3m dump (skalt.github.io)
| penguin359 wrote:
| Having compile-time RegEx's would certainly fix some issues I've
| had with them in Python. As they are just buried in plain
| strings, there's no validation of their syntax until you hit the
| line of code that tried to use them only to discover that Python
| 3.12 has changed the syntax and what was once working is now
| broken. I'd love to see this validated at compile-time/load-time
| of any language to avoid those unexpected surprises.
| ygra wrote:
| C# has compile-time regex, which are turned into fairly
| reasonable code doing the matching you can even inspect. The
| main goal isn't to have the syntax verified during compilation
| (technically it happens even during editing the code), but
| rather to reduce either dependencies on complex code at
| runtime, while still maintaining good performance by using the
| complex regex.
| nsonha wrote:
| This article isn't about compile-time regex (which is in many
| languages, including javascript) though? It's compile-time
| regex MATCHING.
| asicsp wrote:
| If you need to detect syntax issues, you can use
| `re.compile()`. For example, before Python 3.11, you'll get an
| error: >>> re.compile(r'\w++')
| re.error: multiple repeat at position 3
|
| To use such a compiled pattern, you can call the method on this
| object instead of `re`. For example: >>> word
| = re.compile(r'\w+') >>> word.findall('hello-there!')
| ['hello', 'there']
| hombre_fatal wrote:
| The existent of re.compile is compatible with and doesn't
| address their complaint.
| drowsspa wrote:
| If you compile them outside the functions they will throw when
| imported
| JonChesterfield wrote:
| It's regular expressions via derivatives which is sanely
| representable in functional programming fashion. Matching a
| single string doesn't need the caching layers for termination,
| you could do this in C++ templates if you have the patience. Nice
| to see it in typescript syntax.
| jonstewart wrote:
| Hana Dusikova did this in C++ several years ago, with her CTRE
| library.
|
| https://github.com/hanickadot/compile-time-regular-expressio...
|
| Of course, it has all the usual tradeoffs of compile-time
| template programming.
| gpderetta wrote:
| Also relevant Boost.Expressive, by Eric Niebler, from 2007.
| clarkdale wrote:
| Derivatives approach is great and works if you're matching on
| some pattern like \w+, but could it work with "or" characters
| like (abc|def) or patterns like [a-z]+\d+
| evolveyourmind wrote:
| Nice! My version: https://github.com/desi-ivanov/ts-regexp
| skalt wrote:
| That's a great concise implementation! I'm glad you also went
| with the parse-and-interpret-an-AST approach: it seems like the
| right design. Your `Reconstruct` type is a great idea! I might
| copy that.
___________________________________________________________________
(page generated 2024-10-20 23:02 UTC)