# regexp-to-ast

> Parses a Regular Expression and outputs an AST

Latest version **0.5.0** (published 2019-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install regexp-to-ast
pnpm add regexp-to-ast
yarn add regexp-to-ast
bun add regexp-to-ast
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2019-12-11 |
| First published | 2018-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 41.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 28 |
| Author | Shahar Soel |
| Maintainers | bd82 |
| Keywords | regExp, parser, regular expression |

## Links

- npm: https://www.npmjs.com/package/regexp-to-ast
- Repository: https://github.com/bd82/regexp-to-ast
- Homepage: https://github.com/bd82/regexp-to-ast#readme
- Issues: https://github.com/bd82/regexp-to-ast/issues
- npm.io page: https://npm.io/package/regexp-to-ast

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2019-12-11
- 0.4.0 — 2019-03-15
- 0.3.5 — 2018-07-12
- 0.3.4 — 2018-06-16
- 0.3.3 — 2018-06-09
- 0.3.2 — 2018-06-09
- 0.3.1 — 2018-06-09
- 0.3.0 — 2018-06-09
- 0.2.4 — 2018-06-05
- 0.2.3 — 2018-06-05
- 0.2.2 — 2018-04-09
- 0.2.0 — 2018-04-07
- 0.1.0 — 2018-04-07

## README

[![npm version](https://badge.fury.io/js/regexp-to-ast.svg)](https://badge.fury.io/js/regexp-to-ast)
[![CircleCI](https://circleci.com/gh/bd82/regexp-to-ast.svg?style=svg)](https://circleci.com/gh/bd82/regexp-to-ast)
[![Coverage Status](https://coveralls.io/repos/github/bd82/regexp-to-ast/badge.svg?branch=master)](https://coveralls.io/github/bd82/regexp-to-ast?branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/bd82/regexp-to-ast.svg)](https://greenkeeper.io/)

# 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](https://github.com/bd82/regexp-to-ast/blob/master/api.d.ts) is defined as a TypeScript definition file.

## Usage

-   Parsing to an AST:

    ```javascript
    const RegExpParser = require("regexp-to-ast").RegExpParser
    const regexpParser = new RegExpParser.parser()

    // from a regexp text
    const astOutput = regexpParser.pattern("/a|b|c/g")

    // text from regexp instance.
    const input2 = /a|b/.toString()
    // The same parser instance can be reused
    const anotherAstOutput = regexpParser.pattern(input2)
    ```

-   Visiting the AST:

    ```javascript
    // parse to an AST as before.
    const { RegExpParser, BaseRegExpVisitor } = require("regexp-to-ast")
    const regexpParser = new RegExpParser.parser()
    const regExpAst = regexpParser.pattern("/a|b|c/g")

    // Override the visitor methods to add your logic.
    class MyRegExpVisitor extends BaseRegExpVisitor {
        visitPattern(node) {}

        visitFlags(node) {}

        visitDisjunction(node) {}

        visitAlternative(node) {}

        // Assertion
        visitStartAnchor(node) {}

        visitEndAnchor(node) {}

        visitWordBoundary(node) {}

        visitNonWordBoundary(node) {}

        visitLookahead(node) {}

        visitNegativeLookahead(node) {}

        // atoms
        visitCharacter(node) {}

        visitSet(node) {}

        visitGroup(node) {}

        visitGroupBackReference(node) {}

        visitQuantifier(node) {}
    }

    const myVisitor = new MyRegExpVisitor()
    myVisitor.visit(regExpAst)
    // extract visit results from the visitor state.
    ```

## Compatibility

This library is written in ES**5** style and is compatiable with all major browsers and **modern** node.js versions.

## TODO / Limitations

-   Use polyFill for [string.prototype.at](https://github.com/mathiasbynens/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"](https://hackernoon.com/the-madness-of-parsing-real-world-javascript-regexps-d9ee336df983) are supported.
-   Support deprecated octal escapes

---
_Source: https://npm.io/package/regexp-to-ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
