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 32import * as acorn from ".." 33import {LooseParser} from "./state" 34import "./tokenize" 35import "./parseutil" 36import "./statement" 37import "./expression" 38 39export {LooseParser} from "./state" 40 41acorn.defaultOptions.tabSize = 4 42 43export function parse_dammit(input, options) { 44 let p = new LooseParser(input, options) 45 p.next() 46 return p.parseTopLevel() 47} 48 49acorn.parse_dammit = parse_dammit 50acorn.LooseParser = LooseParser 51 52