Ohm-Management - Projektarbeit B-ME
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.

README.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # Acorn
  2. A tiny, fast JavaScript parser written in JavaScript.
  3. ## Community
  4. Acorn is open source software released under an
  5. [MIT license](https://github.com/acornjs/acorn/blob/master/LICENSE).
  6. You are welcome to
  7. [report bugs](https://github.com/acornjs/acorn/issues) or create pull
  8. requests on [github](https://github.com/acornjs/acorn). For questions
  9. and discussion, please use the
  10. [Tern discussion forum](https://discuss.ternjs.net).
  11. ## Installation
  12. The easiest way to install acorn is from [`npm`](https://www.npmjs.com/):
  13. ```sh
  14. npm install acorn
  15. ```
  16. Alternately, you can download the source and build acorn yourself:
  17. ```sh
  18. git clone https://github.com/acornjs/acorn.git
  19. cd acorn
  20. npm install
  21. ```
  22. ## Interface
  23. **parse**`(input, options)` is the main interface to the library. The
  24. `input` parameter is a string, `options` can be undefined or an object
  25. setting some of the options listed below. The return value will be an
  26. abstract syntax tree object as specified by the [ESTree
  27. spec](https://github.com/estree/estree).
  28. ```javascript
  29. let acorn = require("acorn");
  30. console.log(acorn.parse("1 + 1"));
  31. ```
  32. When encountering a syntax error, the parser will raise a
  33. `SyntaxError` object with a meaningful message. The error object will
  34. have a `pos` property that indicates the string offset at which the
  35. error occurred, and a `loc` object that contains a `{line, column}`
  36. object referring to that same position.
  37. Options can be provided by passing a second argument, which should be
  38. an object containing any of these fields:
  39. - **ecmaVersion**: Indicates the ECMAScript version to parse. Must be
  40. either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018) or 10 (2019, partial
  41. support). This influences support for strict mode, the set of
  42. reserved words, and support for new syntax features. Default is 7.
  43. **NOTE**: Only 'stage 4' (finalized) ECMAScript features are being
  44. implemented by Acorn. Other proposed new features can be implemented
  45. through plugins.
  46. - **sourceType**: Indicate the mode the code should be parsed in. Can be
  47. either `"script"` or `"module"`. This influences global strict mode
  48. and parsing of `import` and `export` declarations.
  49. - **onInsertedSemicolon**: If given a callback, that callback will be
  50. called whenever a missing semicolon is inserted by the parser. The
  51. callback will be given the character offset of the point where the
  52. semicolon is inserted as argument, and if `locations` is on, also a
  53. `{line, column}` object representing this position.
  54. - **onTrailingComma**: Like `onInsertedSemicolon`, but for trailing
  55. commas.
  56. - **allowReserved**: If `false`, using a reserved word will generate
  57. an error. Defaults to `true` for `ecmaVersion` 3, `false` for higher
  58. versions. When given the value `"never"`, reserved words and
  59. keywords can also not be used as property names (as in Internet
  60. Explorer's old parser).
  61. - **allowReturnOutsideFunction**: By default, a return statement at
  62. the top level raises an error. Set this to `true` to accept such
  63. code.
  64. - **allowImportExportEverywhere**: By default, `import` and `export`
  65. declarations can only appear at a program's top level. Setting this
  66. option to `true` allows them anywhere where a statement is allowed.
  67. - **allowAwaitOutsideFunction**: By default, `await` expressions can
  68. only appear inside `async` functions. Setting this option to
  69. `true` allows to have top-level `await` expressions. They are
  70. still not allowed in non-`async` functions, though.
  71. - **allowHashBang**: When this is enabled (off by default), if the
  72. code starts with the characters `#!` (as in a shellscript), the
  73. first line will be treated as a comment.
  74. - **locations**: When `true`, each node has a `loc` object attached
  75. with `start` and `end` subobjects, each of which contains the
  76. one-based line and zero-based column numbers in `{line, column}`
  77. form. Default is `false`.
  78. - **onToken**: If a function is passed for this option, each found
  79. token will be passed in same format as tokens returned from
  80. `tokenizer().getToken()`.
  81. If array is passed, each found token is pushed to it.
  82. Note that you are not allowed to call the parser from the
  83. callback—that will corrupt its internal state.
  84. - **onComment**: If a function is passed for this option, whenever a
  85. comment is encountered the function will be called with the
  86. following parameters:
  87. - `block`: `true` if the comment is a block comment, false if it
  88. is a line comment.
  89. - `text`: The content of the comment.
  90. - `start`: Character offset of the start of the comment.
  91. - `end`: Character offset of the end of the comment.
  92. When the `locations` options is on, the `{line, column}` locations
  93. of the comment’s start and end are passed as two additional
  94. parameters.
  95. If array is passed for this option, each found comment is pushed
  96. to it as object in Esprima format:
  97. ```javascript
  98. {
  99. "type": "Line" | "Block",
  100. "value": "comment text",
  101. "start": Number,
  102. "end": Number,
  103. // If `locations` option is on:
  104. "loc": {
  105. "start": {line: Number, column: Number}
  106. "end": {line: Number, column: Number}
  107. },
  108. // If `ranges` option is on:
  109. "range": [Number, Number]
  110. }
  111. ```
  112. Note that you are not allowed to call the parser from the
  113. callback—that will corrupt its internal state.
  114. - **ranges**: Nodes have their start and end characters offsets
  115. recorded in `start` and `end` properties (directly on the node,
  116. rather than the `loc` object, which holds line/column data. To also
  117. add a
  118. [semi-standardized](https://bugzilla.mozilla.org/show_bug.cgi?id=745678)
  119. `range` property holding a `[start, end]` array with the same
  120. numbers, set the `ranges` option to `true`.
  121. - **program**: It is possible to parse multiple files into a single
  122. AST by passing the tree produced by parsing the first file as the
  123. `program` option in subsequent parses. This will add the toplevel
  124. forms of the parsed file to the "Program" (top) node of an existing
  125. parse tree.
  126. - **sourceFile**: When the `locations` option is `true`, you can pass
  127. this option to add a `source` attribute in every node’s `loc`
  128. object. Note that the contents of this option are not examined or
  129. processed in any way; you are free to use whatever format you
  130. choose.
  131. - **directSourceFile**: Like `sourceFile`, but a `sourceFile` property
  132. will be added (regardless of the `location` option) directly to the
  133. nodes, rather than the `loc` object.
  134. - **preserveParens**: If this option is `true`, parenthesized expressions
  135. are represented by (non-standard) `ParenthesizedExpression` nodes
  136. that have a single `expression` property containing the expression
  137. inside parentheses.
  138. **parseExpressionAt**`(input, offset, options)` will parse a single
  139. expression in a string, and return its AST. It will not complain if
  140. there is more of the string left after the expression.
  141. **tokenizer**`(input, options)` returns an object with a `getToken`
  142. method that can be called repeatedly to get the next token, a `{start,
  143. end, type, value}` object (with added `loc` property when the
  144. `locations` option is enabled and `range` property when the `ranges`
  145. option is enabled). When the token's type is `tokTypes.eof`, you
  146. should stop calling the method, since it will keep returning that same
  147. token forever.
  148. In ES6 environment, returned result can be used as any other
  149. protocol-compliant iterable:
  150. ```javascript
  151. for (let token of acorn.tokenizer(str)) {
  152. // iterate over the tokens
  153. }
  154. // transform code to array of tokens:
  155. var tokens = [...acorn.tokenizer(str)];
  156. ```
  157. **tokTypes** holds an object mapping names to the token type objects
  158. that end up in the `type` properties of tokens.
  159. **getLineInfo**`(input, offset)` can be used to get a `{line,
  160. column}` object for a given program string and offset.
  161. ### The `Parser` class
  162. Instances of the **`Parser`** class contain all the state and logic
  163. that drives a parse. It has static methods `parse`,
  164. `parseExpressionAt`, and `tokenizer` that match the top-level
  165. functions by the same name.
  166. When extending the parser with plugins, you need to call these methods
  167. on the extended version of the class. To extend a parser with plugins,
  168. you can use its static `extend` method.
  169. ```javascript
  170. var acorn = require("acorn");
  171. var jsx = require("acorn-jsx");
  172. var JSXParser = acorn.Parser.extend(jsx());
  173. JSXParser.parse("foo(<bar/>)");
  174. ```
  175. The `extend` method takes any number of plugin values, and returns a
  176. new `Parser` class that includes the extra parser logic provided by
  177. the plugins.
  178. ## Command line interface
  179. The `bin/acorn` utility can be used to parse a file from the command
  180. line. It accepts as arguments its input file and the following
  181. options:
  182. - `--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|--ecma10`: Sets the ECMAScript version
  183. to parse. Default is version 9.
  184. - `--module`: Sets the parsing mode to `"module"`. Is set to `"script"` otherwise.
  185. - `--locations`: Attaches a "loc" object to each node with "start" and
  186. "end" subobjects, each of which contains the one-based line and
  187. zero-based column numbers in `{line, column}` form.
  188. - `--allow-hash-bang`: If the code starts with the characters #! (as
  189. in a shellscript), the first line will be treated as a comment.
  190. - `--compact`: No whitespace is used in the AST output.
  191. - `--silent`: Do not output the AST, just return the exit status.
  192. - `--help`: Print the usage information and quit.
  193. The utility spits out the syntax tree as JSON data.
  194. ## Existing plugins
  195. - [`acorn-jsx`](https://github.com/RReverser/acorn-jsx): Parse [Facebook JSX syntax extensions](https://github.com/facebook/jsx)
  196. Plugins for ECMAScript proposals:
  197. - [`acorn-stage3`](https://github.com/acornjs/acorn-stage3): Parse most stage 3 proposals, bundling:
  198. - [`acorn-async-iteration`](https://github.com/acornjs/acorn-async-iteration): Parse [async iteration proposal](https://github.com/tc39/proposal-async-iteration)
  199. - [`acorn-bigint`](https://github.com/acornjs/acorn-bigint): Parse [BigInt proposal](https://github.com/tc39/proposal-bigint)
  200. - [`acorn-class-fields`](https://github.com/acornjs/acorn-class-fields): Parse [class fields proposal](https://github.com/tc39/proposal-class-fields)
  201. - [`acorn-dynamic-import`](https://github.com/kesne/acorn-dynamic-import): Parse [import() proposal](https://github.com/tc39/proposal-dynamic-import)
  202. - [`acorn-import-meta`](https://github.com/acornjs/acorn-import-meta): Parse [import.meta proposal](https://github.com/tc39/proposal-import-meta)
  203. - [`acorn-numeric-separator`](https://github.com/acornjs/acorn-numeric-separator): Parse [numeric separator proposal](https://github.com/tc39/proposal-numeric-separator)
  204. - [`acorn-private-methods`](https://github.com/acornjs/acorn-private-methods): parse [private methods, getters and setters proposal](https://github.com/tc39/proposal-private-methods)n