regexp-to-ast
Reads a JavaScript Regular Expression literal(text) and outputs an Abstract Syntax Tree.
Installation
- npm
npm install regexp-to-ast
- Browser
<script src="https://unpkg.com/regexp-to-ast/lib/parser.js"></script>
API
The API is defined as a TypeScript definition file.
Usage
-
Parsing to an AST:
const RegExpParser = RegExpParserconst regexpParser =// from a regexp textconst astOutput = regexpParser// text from regexp instance.const input2 = /a|b/// The same parser instance can be reusedconst anotherAstOutput = regexpParser -
Visiting the AST:
// parse to an AST as before.const RegExpParser BaseRegExpVisitor =const regexpParser =const regExpAst = regexpParser// Override the visitor methods to add your logic.{}{}{}{}// Assertion{}{}{}{}{}{}// atoms{}{}{}{}{}const myVisitor =myVisitor// extract visit results from the visitor state.
Compatibility
This library is written in ES5 style and is compatiable with all major browsers and modern node.js versions.
TODO / Limitations
- Use polyFill for string.prototype.at to support unicode characters outside BMP.
- Descriptive error messages.
- Position information in error messages.
- Support unicode flag escapes.
- Ensure edge cases described in "The madness of parsing real world JavaScript regexps" are supported.
- Support deprecated octal escapes