You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 line
5.4 KiB

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