web 3d图形渲染器
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.

134 lines
5.8 KiB

  1. <p align="center">
  2. <img alt="babylon" src="https://raw.githubusercontent.com/babel/logo/master/babylon.png" width="700">
  3. </p>
  4. <p align="center">
  5. Babylon is a JavaScript parser used in <a href="https://github.com/babel/babel">Babel</a>.
  6. </p>
  7. <p align="center">
  8. <a href="https://travis-ci.org/babel/babylon"><img alt="Travis Status" src="https://img.shields.io/travis/babel/babylon/master.svg?style=flat&label=travis"></a>
  9. <a href="https://codecov.io/gh/babel/babylon"><img alt="Codecov Status" src="https://img.shields.io/codecov/c/github/babel/babylon/master.svg?style=flat"></a>
  10. </p>
  11. - The latest ECMAScript version enabled by default (ES2017).
  12. - Comment attachment.
  13. - Support for JSX and Flow.
  14. - Support for experimental language proposals (accepting PRs for anything at least [stage-0](https://github.com/tc39/proposals/blob/master/stage-0-proposals.md)).
  15. ## Credits
  16. Heavily based on [acorn](https://github.com/marijnh/acorn) and [acorn-jsx](https://github.com/RReverser/acorn-jsx),
  17. thanks to the awesome work of [@RReverser](https://github.com/RReverser) and [@marijnh](https://github.com/marijnh).
  18. Significant diversions are expected to occur in the future such as streaming, EBNF definitions, sweet.js integration, interspatial parsing and more.
  19. ## API
  20. ### `babylon.parse(code, [options])`
  21. ### `babylon.parseExpression(code, [options])`
  22. `parse()` parses the provided `code` as an entire ECMAScript program, while
  23. `parseExpression()` tries to parse a single Expression with performance in
  24. mind. When in doubt, use `.parse()`.
  25. ### Options
  26. - **allowImportExportEverywhere**: By default, `import` and `export`
  27. declarations can only appear at a program's top level. Setting this
  28. option to `true` allows them anywhere where a statement is allowed.
  29. - **allowReturnOutsideFunction**: By default, a return statement at
  30. the top level raises an error. Set this to `true` to accept such
  31. code.
  32. - **allowSuperOutsideMethod**: TODO
  33. - **sourceType**: Indicate the mode the code should be parsed in. Can be
  34. either `"script"` or `"module"`.
  35. - **sourceFilename**: Correlate output AST nodes with their source filename. Useful when generating code and source maps from the ASTs of multiple input files.
  36. - **startLine**: By default, the first line of code parsed is treated as line 1. You can provide a line number to alternatively start with. Useful for integration with other source tools.
  37. - **plugins**: Array containing the plugins that you want to enable.
  38. - **strictMode**: TODO
  39. ### Output
  40. Babylon generates AST according to [Babel AST format][].
  41. It is based on [ESTree spec][] with the following deviations:
  42. > There is now an `estree` plugin which reverts these deviations
  43. - [Literal][] token is replaced with [StringLiteral][], [NumericLiteral][], [BooleanLiteral][], [NullLiteral][], [RegExpLiteral][]
  44. - [Property][] token is replaced with [ObjectProperty][] and [ObjectMethod][]
  45. - [MethodDefinition][] is replaced with [ClassMethod][]
  46. - [Program][] and [BlockStatement][] contain additional `directives` field with [Directive][] and [DirectiveLiteral][]
  47. - [ClassMethod][], [ObjectProperty][], and [ObjectMethod][] value property's properties in [FunctionExpression][] is coerced/brought into the main method node.
  48. AST for JSX code is based on [Facebook JSX AST][] with the addition of one node type:
  49. - `JSXText`
  50. [Babel AST format]: https://github.com/babel/babylon/blob/master/ast/spec.md
  51. [ESTree spec]: https://github.com/estree/estree
  52. [Literal]: https://github.com/estree/estree/blob/master/es5.md#literal
  53. [Property]: https://github.com/estree/estree/blob/master/es5.md#property
  54. [MethodDefinition]: https://github.com/estree/estree/blob/master/es2015.md#methoddefinition
  55. [StringLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#stringliteral
  56. [NumericLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#numericliteral
  57. [BooleanLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#booleanliteral
  58. [NullLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#nullliteral
  59. [RegExpLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#regexpliteral
  60. [ObjectProperty]: https://github.com/babel/babylon/blob/master/ast/spec.md#objectproperty
  61. [ObjectMethod]: https://github.com/babel/babylon/blob/master/ast/spec.md#objectmethod
  62. [ClassMethod]: https://github.com/babel/babylon/blob/master/ast/spec.md#classmethod
  63. [Program]: https://github.com/babel/babylon/blob/master/ast/spec.md#programs
  64. [BlockStatement]: https://github.com/babel/babylon/blob/master/ast/spec.md#blockstatement
  65. [Directive]: https://github.com/babel/babylon/blob/master/ast/spec.md#directive
  66. [DirectiveLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#directiveliteral
  67. [FunctionExpression]: https://github.com/babel/babylon/blob/master/ast/spec.md#functionexpression
  68. [Facebook JSX AST]: https://github.com/facebook/jsx/blob/master/AST.md
  69. ### Semver
  70. Babylon follows semver in most situations. The only thing to note is that some spec-compliancy bug fixes may be released under patch versions.
  71. For example: We push a fix to early error on something like [#107](https://github.com/babel/babylon/pull/107) - multiple default exports per file. That would be considered a bug fix even though it would cause a build to fail.
  72. ### Example
  73. ```javascript
  74. require("babylon").parse("code", {
  75. // parse in strict mode and allow module declarations
  76. sourceType: "module",
  77. plugins: [
  78. // enable jsx and flow syntax
  79. "jsx",
  80. "flow"
  81. ]
  82. });
  83. ```
  84. ### Plugins
  85. - `estree`
  86. - `jsx`
  87. - `flow`
  88. - `doExpressions`
  89. - `objectRestSpread`
  90. - `decorators` (Based on an outdated version of the Decorators proposal. Will be removed in a future version of `Babylon`)
  91. - `classProperties`
  92. - `exportExtensions`
  93. - `asyncGenerators`
  94. - `functionBind`
  95. - `functionSent`
  96. - `dynamicImport`
  97. - `templateInvalidEscapes`