-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create parser combinator library #107
Conversation
Release Notes PreviewThese notes are auto-generated from the conventional commit messages of this PR, and will be re-generated if the commits are updated. Features
Refactors |
Amazing work, thanks for the great documentation. I'll take some notes as I go through this beast. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great I just saw a couple typos in the commit messages
- Commit message of feat(parser): init -> End of line 8 typo of "stings." should be "strings."
- Commit message of feat(parser): init -> Line 11 typo of "RagExp"
[Parser combinator](https://en.wikipedia.org/wiki/Parser_combinator) libraries provide composable parsers used together to construct more complex parsers. This first draft is robust enough for simple DSLs. Notably it does not implement backtracking or a chain combinator, which would allow for more advanced branching. However the TS implementation is very powerful, capable of inferring the return types from complex parsers without explicit compiler hints in userland. The following is a brief overview of the features added. Additionally, the four existing parsers in the repository were re-written using the library - they can serve as examples and documentation, in addition to robust JSDoc comments and test suites. - The base `Parser` class implements core helper methods like `.fallback()`, `into()`, `.not()`, `.and()`, `.or()`, `.optional`, `.ignore`, and `.zeroOrMore`. - Shorthand factory `string()` makes it easy to construct a Parser from one or more strings. - `ArrayParser` with shorthand factory `sequence()` provides a specialized `Parser` with array-specific `.flat`, `.item()`, `.items()`, `.join()`, and `.toObject()`, and specialized handling for parser-like inputs and ignored parsers. - `RegExpParser` with shorthand factory `regexp()` provides a specialized `Parser` around one or more RegExp patterns, with RegExp-specific `.match`, `.group()`, and `.groups()` methods. - Common helpers like `whitespace`, `blankLine`, and `end` are provided. Some are re-exported under `line` (ex. `line.blank`) and `whitespace` (ex. `whitespace.inline`) for convenience.
Thanks! Great catches. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most Excellent work.
Finally done 👏 It's ginormous but a lot of work has been done that should make it easier to review:
More than half of the changes are tests and JSDocs comments.
The PR is divided into 4 commits.
The monolithic
combinator.ts
file has been decomposed as much as possible without impairing maintainability.parser/core.ts
exports foundational types and an abstract classCoreParser
which underpins all others.parser/ignored.ts
andparser/Parser.ts
build on top ofCoreParser
.parser/RegExpParser.ts
builds on top ofParser
.parser/string.ts
,parser/regexp.ts
, andparser/sequence.ts
build on top of the above.parser/named.ts
houses the short, reusable parsers that don't take inputs such aswhitespace
andend
.