retsac
v0.17.0
Published
Text lexer and parser.
Downloads
226
Maintainers
Readme
Retsac
[!WARNING] This project is still in early development stage, the API may change frequently.
Text lexer and parser. Compiler frontend framework.
This can be used to fast prototype your own programming language compiler/translator frontend, or parse your domain specific language.
Try it online in the playground.
Installation
yarn add retsac
Features
- The Lexer, yield token from the text input string.
- Regex support. See examples below.
- Built-in util functions.
- JavaScript's string literal, numeric literal, integer literal, identifier, etc.
- JSON's string literal, numeric literal.
- Support custom functions.
- The Parser, co-work with the lexer and produce an AST (Abstract Syntax Tree).
- ELR (Expectational LR) parser.
- Meta characters like
+*?
when defining a grammar rule. - Conflict detection, try to auto resolve conflicts.
- Query children nodes by using
$('name')
instead ofchildren[index]
. - Top-down traverse the AST.
- Bottom-up reduce data.
- Expect lexer to yield specific token type and/or content.
- Try to re-lex the input if parsing failed.
- DFA serialization & hydration to accelerate future building.
- Meta characters like
- Serializable AST object to co-work with other tools (e.g. compiler backend libs like LLVM).
- ELR (Expectational LR) parser.
- Strict type checking with TypeScript.
- This is amazing, you'd better try this out by yourself.
Resources
- ~~Documentation & API reference~~ (deprecated, working on a new one).
- A demo programming language which compiles to WebAssembly.
- Build tmLanguage.json file in TypeScript with
tmlb
. - Compose
RegExp
in JavaScript in a readable and maintainable way withr-compose
.
Examples
JSON Parser
In this example, we use AdvancedBuilder
to define grammar rules with +*?
, define top-down traversers using traverser
, and query nodes in grammar rules using $
and $$
.
All conflicts are auto resolved.
Calculator
In this example, we use reducer
to define bottom-up data reducers, so we can get the result when the AST is built.
There are conflicts introduced by those grammar rules, we use the high-level resolver API priority
to resolve them.
Contribute
All issues and pull requests are highly welcomed.