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