Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/desi-ivanov/ts-regexp
Type-Level Regular Expressions matching with TypeScript
https://github.com/desi-ivanov/ts-regexp
Last synced: 12 days ago
JSON representation
Type-Level Regular Expressions matching with TypeScript
- Host: GitHub
- URL: https://github.com/desi-ivanov/ts-regexp
- Owner: desi-ivanov
- License: mit
- Created: 2021-12-06T07:45:07.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-14T15:46:46.000Z (over 1 year ago)
- Last Synced: 2024-06-02T10:35:53.829Z (5 months ago)
- Language: TypeScript
- Homepage:
- Size: 28.3 KB
- Stars: 22
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-regexp
Regular expressions matching at compile time with TypeScript.
Try it out in the [TS Playground](https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcBawoTgXjgIkFAifAbgChRJYFlUB5AO1T30FbgUi8aeJFOAZQCWAcwC2AQ1wFx+OAB8CAI1kL8AYxUEAJpvzBdAM13COnKj1pwAShixSA3jRQAuAgC9MEWQF9ylbk6oVozMcI68wK74EEw+flzUEdYAgjAQogA8AMIAfA6BUWoAFuJQpHDFpa5ZcL5mAUlW-ACumQBiADRwAOJ5eOG0UQDOreUANsAGMK5tJHBQIkXTPbXx5oHW-MAAjhmdPX1hBQRDO+OTy7Pzi8vdq-WJlk0wpXuHAy4nL2VzUDP3DwsfAACqVTgBZDL8Q78OCgGDABhaIZwAAGAAoACT2QQMAyYazAIYwbyoshwOAAfjgoKgEIyViJMDy8MRyLgAG1cfioHAADIXLrcgmMiS4gC65IpVMJYoYcJACKRKNRAEpscLeYziQAmUlS6Uy7UwHUKpXs-D4A2GmUcgVTLqWyU26WuY2m1nKtEAKg1eJFTIAzPqXRTqRznq97cyusbA87Q26mR7FWyVQBqP08wnE4Nk0My2n0uMs1Nern+rU3IWV2XiXE6hMF8NNVoZaOxm45WPAO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nwAPFuNC+BTl4Qnfh5cm6kJEY3Su4jZ5SK4YGKQdChgZAEWXEhwj66qObufLeFjNGWBgEfHRfC9EGKUcY0RaiJGbwvpYzK0jUAPzsfIhxiijG1xMWItxb8rF8AMAQtBxQFGCOUSE1xMDB7ePAX42JAT4nONMeowBKSkgoPSUUOJhihHBJcWYvJKTOHWIAKxFIUZXb0YT8nWIAGwNICU0lprSZEAHZOn6Kbs0ge4hemoBgAADkGaXbpoyxkRImQAThmcMnpYzqkFPqXIjJQy5lKEWdlDpOzildLrmErehyDADJOY085joNBeKtgAemeT0Rm8pJBQGAMIdypQFRgG+UMIYggYhwCyWQ9EFDVRdAAO5FEEMUQ8KIK41zgOmWisLBAwCKHAEFogwAGQMIIYARkhiIAYC8EAXQyCvIQMUvF9VcTCDgIoOAWgIBEifBAeAihgBjBiCytI9LUCeQYL88QwhUAQAMCK+YPy-m8koECkFMQAB0cBaVvJ0tgtQMAujiVoPwNQCwwDwF-PpZodsuU4rtjExORDSnKPIZQmBV9BAom2hqrVcBxLFO+QYaAwAujiGBSIBgzK4CSD0mMeKzUJnYGjaUQQE04Ayo2Mwx1TjIXQpbm6vIPqPVwFhVgcVUakRyoiAAWliWoAA1gSWyggwrlqMjirAsKUSGr4AAchWj25F3L4ChpBcIBgqbhVJB7dtHtPru2oD7Q1AdRaGA8qjWGsdE7sBTpnT6n1zQGDt1EKIREPAGWBo8hALFZaDJMAQNgU4qBsU+rvnCKAfx7UsLBJ1AxQTRG5rwuE+e8U5hkCAA)!## Syntax
The syntax of regular expressions is defined as follows:
- `∅` is a regular expression denoting the empty language
- `ε` is a regular expression denoting the language with just the empty string
- Given a symbol `r` of the alphabet, the atom regular expression is defined as `r`
- Given two regular expressions `F` and `G`, the union is defined as `F+G`
- Given two regular expressions `F` and `G`, the concatenation is defined as `FG`
- Given a regular expression `F`, the star is defined as `F*`## Parsing
In the [Parse.ts](src/Parse.ts) file there is an implementation of a parser that can derive the AST following the above syntax.
It takes a raw string passed as a literal type and concretely produces a composition of the following types:
```ts
type Sigma = "a" | "b" | "c" | "d" | "e" | "f" | "g";
type Zero = "∅";
type One = "ε";type RZero = { type: "zero" };
type ROne = { type: "one" };
type RAtom = { type: "char"; char: C };
type RSum = { type: "sum"; left: F; right: G };
type RSeq = { type: "seq"; left: F; right: G };
type RStar = { type: "star"; r: F };
```
For instance, given a regular expression like `(a)(b)`, the parser derives the following AST:
```ts
type r = Parse<"(a)(b)">
type parsed = RSeq, RAtom<"b">>
```## Matching
The matching is available in [Match.ts](src/Match.ts) and uses derivatives on regular expression to match the strings.For example, given the regular expression `(a)*`, TypeScript infers that the regular expression matches the strings ` `, `a`, `aa`, `aaa`, `aaaa`, but doesn't match the strings `b`, `ba`, `c`:
```ts
type t5 = Matches<"(a)*", ""> // true
type t6 = Matches<"(a)*", "a"> // true
type t7 = Matches<"(a)*", "aa"> // true
type t8 = Matches<"(a)*", "aaa"> // true
type t9 = Matches<"(a)*", "aaaa"> // truetype f5 = Matches<"(a)*", "b"> // false
type f6 = Matches<"(a)*", "ba"> // false
type f7 = Matches<"(a)*", "c"> // false
```
Another example for `(ab)* + c` (the syntax is a bit heavy due to limitations of the current parser implementation):
```ts
type t1 = Matches<"((((a)(b))*)+(c))", "c"> // true
type t2 = Matches<"((((a)(b))*)+(c))", "ab"> // true
type t3 = Matches<"((((a)(b))*)+(c))", "abababab"> // true
type t4 = Matches<"((((a)(b))*)+(c))", "ababababab"> // truetype f1 = Matches<"((((a)(b))*)+(c))", "b"> // false
type f2 = Matches<"((((a)(b))*)+(c))", "ba"> // false
type f3 = Matches<"((((a)(b))*)+(c))", "ca"> // false
type f4 = Matches<"((((a)(b))*)+(c))", "ac"> // false
```# License
MIT