index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Acorn: Loose parser
  2. //
  3. // This module provides an alternative parser (`parse_dammit`) that
  4. // exposes that same interface as `parse`, but will try to parse
  5. // anything as JavaScript, repairing syntax error the best it can.
  6. // There are circumstances in which it will raise an error and give
  7. // up, but they are very rare. The resulting AST will be a mostly
  8. // valid JavaScript AST (as per the [Mozilla parser API][api], except
  9. // that:
  10. //
  11. // - Return outside functions is allowed
  12. //
  13. // - Label consistency (no conflicts, break only to existing labels)
  14. // is not enforced.
  15. //
  16. // - Bogus Identifier nodes with a name of `"✖"` are inserted whenever
  17. // the parser got too confused to return anything meaningful.
  18. //
  19. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  20. //
  21. // The expected use for this is to *first* try `acorn.parse`, and only
  22. // if that fails switch to `parse_dammit`. The loose parser might
  23. // parse badly indented code incorrectly, so **don't** use it as
  24. // your default parser.
  25. //
  26. // Quite a lot of acorn.js is duplicated here. The alternative was to
  27. // add a *lot* of extra cruft to that file, making it less readable
  28. // and slower. Copying and editing the code allowed me to make
  29. // invasive changes and simplifications without creating a complicated
  30. // tangle.
  31. import {addLooseExports, defaultOptions} from "../index"
  32. import {LooseParser, pluginsLoose} from "./state"
  33. import "./tokenize"
  34. import "./statement"
  35. import "./expression"
  36. export {LooseParser, pluginsLoose} from "./state"
  37. defaultOptions.tabSize = 4
  38. export function parse_dammit(input, options) {
  39. let p = new LooseParser(input, options)
  40. p.next()
  41. return p.parseTopLevel()
  42. }
  43. addLooseExports(parse_dammit, LooseParser, pluginsLoose)