regexp-to-ast
Parses a Regular Expression and outputs an AST
Last updated 5 years ago by bd82 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install regexp-to-ast 
SYNC missed versions from official npm registry.

npm version CircleCI Coverage Status Greenkeeper badge

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 = 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:

    // 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 ES5 style and is compatiable with all major browsers and modern node.js versions.

TODO / Limitations

Current Tags

  • 0.5.0                                ...           latest (5 years ago)

13 Versions

  • 0.5.0                                ...           5 years ago
  • 0.4.0                                ...           6 years ago
  • 0.3.5                                ...           6 years ago
  • 0.3.4                                ...           7 years ago
  • 0.3.3                                ...           7 years ago
  • 0.3.2                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.4                                ...           7 years ago
  • 0.2.3                                ...           7 years ago
  • 0.2.2                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (15)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE