UNPKG

5.48 kBMarkdownView Raw
1# regexpp
2
3[![npm version](https://img.shields.io/npm/v/regexpp.svg)](https://www.npmjs.com/package/regexpp)
4[![Downloads/month](https://img.shields.io/npm/dm/regexpp.svg)](http://www.npmtrends.com/regexpp)
5[![Build Status](https://github.com/mysticatea/regexpp/workflows/CI/badge.svg)](https://github.com/mysticatea/regexpp/actions)
6[![codecov](https://codecov.io/gh/mysticatea/regexpp/branch/master/graph/badge.svg)](https://codecov.io/gh/mysticatea/regexpp)
7[![Dependency Status](https://david-dm.org/mysticatea/regexpp.svg)](https://david-dm.org/mysticatea/regexpp)
8
9A regular expression parser for ECMAScript.
10
11## 💿 Installation
12
13```bash
14$ npm install regexpp
15```
16
17- require Node.js 8 or newer.
18
19## 📖 Usage
20
21```ts
22import {
23 AST,
24 RegExpParser,
25 RegExpValidator,
26 RegExpVisitor,
27 parseRegExpLiteral,
28 validateRegExpLiteral,
29 visitRegExpAST
30} from "regexpp"
31```
32
33### parseRegExpLiteral(source, options?)
34
35Parse a given regular expression literal then make AST object.
36
37This is equivalent to `new RegExpParser(options).parseLiteral(source)`.
38
39- **Parameters:**
40 - `source` (`string | RegExp`) The source code to parse.
41 - `options?` ([`RegExpParser.Options`]) The options to parse.
42- **Return:**
43 - The AST of the regular expression.
44
45### validateRegExpLiteral(source, options?)
46
47Validate a given regular expression literal.
48
49This is equivalent to `new RegExpValidator(options).validateLiteral(source)`.
50
51- **Parameters:**
52 - `source` (`string`) The source code to validate.
53 - `options?` ([`RegExpValidator.Options`]) The options to validate.
54
55### visitRegExpAST(ast, handlers)
56
57Visit each node of a given AST.
58
59This is equivalent to `new RegExpVisitor(handlers).visit(ast)`.
60
61- **Parameters:**
62 - `ast` ([`AST.Node`]) The AST to visit.
63 - `handlers` ([`RegExpVisitor.Handlers`]) The callbacks.
64
65### RegExpParser
66
67#### new RegExpParser(options?)
68
69- **Parameters:**
70 - `options?` ([`RegExpParser.Options`]) The options to parse.
71
72#### parser.parseLiteral(source, start?, end?)
73
74Parse a regular expression literal.
75
76- **Parameters:**
77 - `source` (`string`) The source code to parse. E.g. `"/abc/g"`.
78 - `start?` (`number`) The start index in the source code. Default is `0`.
79 - `end?` (`number`) The end index in the source code. Default is `source.length`.
80- **Return:**
81 - The AST of the regular expression.
82
83#### parser.parsePattern(source, start?, end?, uFlag?)
84
85Parse a regular expression pattern.
86
87- **Parameters:**
88 - `source` (`string`) The source code to parse. E.g. `"abc"`.
89 - `start?` (`number`) The start index in the source code. Default is `0`.
90 - `end?` (`number`) The end index in the source code. Default is `source.length`.
91 - `uFlag?` (`boolean`) The flag to enable Unicode mode.
92- **Return:**
93 - The AST of the regular expression pattern.
94
95#### parser.parseFlags(source, start?, end?)
96
97Parse a regular expression flags.
98
99- **Parameters:**
100 - `source` (`string`) The source code to parse. E.g. `"gim"`.
101 - `start?` (`number`) The start index in the source code. Default is `0`.
102 - `end?` (`number`) The end index in the source code. Default is `source.length`.
103- **Return:**
104 - The AST of the regular expression flags.
105
106### RegExpValidator
107
108#### new RegExpValidator(options)
109
110- **Parameters:**
111 - `options` ([`RegExpValidator.Options`]) The options to validate.
112
113#### validator.validateLiteral(source, start, end)
114
115Validate a regular expression literal.
116
117- **Parameters:**
118 - `source` (`string`) The source code to validate.
119 - `start?` (`number`) The start index in the source code. Default is `0`.
120 - `end?` (`number`) The end index in the source code. Default is `source.length`.
121
122#### validator.validatePattern(source, start, end, uFlag)
123
124Validate a regular expression pattern.
125
126- **Parameters:**
127 - `source` (`string`) The source code to validate.
128 - `start?` (`number`) The start index in the source code. Default is `0`.
129 - `end?` (`number`) The end index in the source code. Default is `source.length`.
130 - `uFlag?` (`boolean`) The flag to enable Unicode mode.
131
132#### validator.validateFlags(source, start, end)
133
134Validate a regular expression flags.
135
136- **Parameters:**
137 - `source` (`string`) The source code to validate.
138 - `start?` (`number`) The start index in the source code. Default is `0`.
139 - `end?` (`number`) The end index in the source code. Default is `source.length`.
140
141### RegExpVisitor
142
143#### new RegExpVisitor(handlers)
144
145- **Parameters:**
146 - `handlers` ([`RegExpVisitor.Handlers`]) The callbacks.
147
148#### visitor.visit(ast)
149
150Validate a regular expression literal.
151
152- **Parameters:**
153 - `ast` ([`AST.Node`]) The AST to visit.
154
155## 📰 Changelog
156
157- [GitHub Releases](https://github.com/mysticatea/regexpp/releases)
158
159## 🍻 Contributing
160
161Welcome contributing!
162
163Please use GitHub's Issues/PRs.
164
165### Development Tools
166
167- `npm test` runs tests and measures coverage.
168- `npm run build` compiles TypeScript source code to `index.js`, `index.js.map`, and `index.d.ts`.
169- `npm run clean` removes the temporary files which are created by `npm test` and `npm run build`.
170- `npm run lint` runs ESLint.
171- `npm run update:test` updates test fixtures.
172- `npm run update:ids` updates `src/unicode/ids.ts`.
173- `npm run watch` runs tests with `--watch` option.
174
175[`AST.Node`]: src/ast.ts#L4
176[`RegExpParser.Options`]: src/parser.ts#L539
177[`RegExpValidator.Options`]: src/validator.ts#L127
178[`RegExpVisitor.Handlers`]: src/visitor.ts#L204