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.

45 lines
2.4 KiB

4 years ago
  1. [![NPM version](https://img.shields.io/npm/v/esprima.svg)](https://www.npmjs.com/package/esprima)
  2. [![npm download](https://img.shields.io/npm/dm/esprima.svg)](https://www.npmjs.com/package/esprima)
  3. [![Build Status](https://img.shields.io/travis/jquery/esprima/master.svg)](https://travis-ci.org/jquery/esprima)
  4. [![Coverage Status](https://img.shields.io/codecov/c/github/jquery/esprima/master.svg)](https://codecov.io/github/jquery/esprima)
  5. **Esprima** ([esprima.org](http://esprima.org), BSD license) is a high performance,
  6. standard-compliant [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm)
  7. parser written in ECMAScript (also popularly known as
  8. [JavaScript](https://en.wikipedia.org/wiki/JavaScript)).
  9. Esprima is created and maintained by [Ariya Hidayat](https://twitter.com/ariyahidayat),
  10. with the help of [many contributors](https://github.com/jquery/esprima/contributors).
  11. ### Features
  12. - Full support for ECMAScript 2017 ([ECMA-262 8th Edition](http://www.ecma-international.org/publications/standards/Ecma-262.htm))
  13. - Sensible [syntax tree format](https://github.com/estree/estree/blob/master/es5.md) as standardized by [ESTree project](https://github.com/estree/estree)
  14. - Experimental support for [JSX](https://facebook.github.io/jsx/), a syntax extension for [React](https://facebook.github.io/react/)
  15. - Optional tracking of syntax node location (index-based and line-column)
  16. - [Heavily tested](http://esprima.org/test/ci.html) (~1500 [unit tests](https://github.com/jquery/esprima/tree/master/test/fixtures) with [full code coverage](https://codecov.io/github/jquery/esprima))
  17. ### API
  18. Esprima can be used to perform [lexical analysis](https://en.wikipedia.org/wiki/Lexical_analysis) (tokenization) or [syntactic analysis](https://en.wikipedia.org/wiki/Parsing) (parsing) of a JavaScript program.
  19. A simple example on Node.js REPL:
  20. ```javascript
  21. > var esprima = require('esprima');
  22. > var program = 'const answer = 42';
  23. > esprima.tokenize(program);
  24. [ { type: 'Keyword', value: 'const' },
  25. { type: 'Identifier', value: 'answer' },
  26. { type: 'Punctuator', value: '=' },
  27. { type: 'Numeric', value: '42' } ]
  28. > esprima.parseScript(program);
  29. { type: 'Program',
  30. body:
  31. [ { type: 'VariableDeclaration',
  32. declarations: [Object],
  33. kind: 'const' } ],
  34. sourceType: 'script' }
  35. ```
  36. For more information, please read the [complete documentation](http://esprima.org/doc).