index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Acorn is a tiny, fast JavaScript parser written in JavaScript.
  2. //
  3. // Acorn was written by Marijn Haverbeke, Ingvar Stepanyan, and
  4. // various contributors and released under an MIT license.
  5. //
  6. // Git repositories for Acorn are available at
  7. //
  8. // http://marijnhaverbeke.nl/git/acorn
  9. // https://github.com/ternjs/acorn.git
  10. //
  11. // Please use the [github bug tracker][ghbt] to report issues.
  12. //
  13. // [ghbt]: https://github.com/ternjs/acorn/issues
  14. //
  15. // This file defines the main parser interface. The library also comes
  16. // with a [error-tolerant parser][dammit] and an
  17. // [abstract syntax tree walker][walk], defined in other files.
  18. //
  19. // [dammit]: acorn_loose.js
  20. // [walk]: util/walk.js
  21. import {Parser} from "./state"
  22. import "./parseutil"
  23. import "./statement"
  24. import "./lval"
  25. import "./expression"
  26. import "./location"
  27. export {Parser, plugins} from "./state"
  28. export {defaultOptions} from "./options"
  29. export {Position, SourceLocation, getLineInfo} from "./locutil"
  30. export {Node} from "./node"
  31. export {TokenType, types as tokTypes, keywords as keywordTypes} from "./tokentype"
  32. export {TokContext, types as tokContexts} from "./tokencontext"
  33. export {isIdentifierChar, isIdentifierStart} from "./identifier"
  34. export {Token} from "./tokenize"
  35. export {isNewLine, lineBreak, lineBreakG} from "./whitespace"
  36. export const version = "4.0.11"
  37. // The main exported interface (under `self.acorn` when in the
  38. // browser) is a `parse` function that takes a code string and
  39. // returns an abstract syntax tree as specified by [Mozilla parser
  40. // API][api].
  41. //
  42. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  43. export function parse(input, options) {
  44. return new Parser(options, input).parse()
  45. }
  46. // This function tries to parse a single expression at a given
  47. // offset in a string. Useful for parsing mixed-language formats
  48. // that embed JavaScript expressions.
  49. export function parseExpressionAt(input, pos, options) {
  50. let p = new Parser(options, input, pos)
  51. p.nextToken()
  52. return p.parseExpression()
  53. }
  54. // Acorn is organized as a tokenizer and a recursive-descent parser.
  55. // The `tokenizer` export provides an interface to the tokenizer.
  56. export function tokenizer(input, options) {
  57. return new Parser(options, input)
  58. }
  59. // This is a terrible kludge to support the existing, pre-ES6
  60. // interface where the loose parser module retroactively adds exports
  61. // to this module.
  62. export let parse_dammit, LooseParser, pluginsLoose
  63. export function addLooseExports(parse, Parser, plugins) {
  64. parse_dammit = parse
  65. LooseParser = Parser
  66. pluginsLoose = plugins
  67. }