-
Notifications
You must be signed in to change notification settings - Fork 16
/
acorn.javascript_cli.txt
206 lines (175 loc) · 12.7 KB
/
acorn.javascript_cli.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
ACORN
ALTERNATIVES ==> # - esprima:
# - from jQuery, historic main parser.
# - uses ESTree
# - least support for ES6 syntax
# - not maintained
# - acorn (preferred):
# - uses ESTree
# - most utilities functions, good plugin system
# - no support for recent ES features
# - loose parsing
# - espree (preferred with ESLint plugins):
# - used by ESLint
# - built on top of acorn but:
# - different OPTS
# - produces Esprima-compatible output for comments, locations and TOKENs
# - @babel/parser (preferred with Babel plugins):
# - used by Babel
# - fork of acorn
# - uses slightly different ESTree
# - supports Flow, TypeScript, ES proposals
# - least utilities functions
# - most active
# - typescript-estree:
# - uses TSESTree, used by typescript-eslint (former tslint)
# - supports TypeScript
# - meriyah
# - based on cherow
# - buntis:
# - based on cherow
# - TypeScript focused
# - project too young at the moment
# - cherow: not maintained
# - esprit: not maintained
VERSION ==> #8.11.3
#Browser, Node.js or CLI
#JavaScript parser and tokenizer
AST FORMAT ==> #ESTree
SPEED ==> #Tokenizer: 4000 tokens per ms
#Parser: 2600 nodes per ms
#(Using jQuery source code)
/=+===============================+=\
/ : : \
)==: PARSER :==(
\ :_______________________________: /
\=+===============================+=/
ACORN.parse('JS'[, OPTS]) #Parse JavaScript.
->PROGRAM_NODE #Errors raise SyntaxError, with ERROR:
# - pos NUM (offset)
# - loc.line|column NUM
#OPTS:
# - ecmaVersion 3|5, 6-14 or 2015-2024 or 'latest' (required)
# - sourceType 'script|module' (def: 'script')
# - allowReturnOutsideFunction BOOL (def: false): top-level return
# - allowAwaitOutsideFunction BOOL (def: true if >=es2022): top-level await
# - allowSuperOutsideMethod BOOL (def: false): top-level `super`
# - allowImportExportEverywhere BOOL (def: false): non-top-level import|export
# - allowReserved BOOL|'never' (def: false): allow VAR that is a reserved keywords (true|false) or a OBJ.VAR ('never')
# - allowHashBang BOOL (def: true if ecmaVersion >= 2023)
# - preserveParens BOOL (def: false): parse 'ParenthesizedExpression' nodes
# - checkPrivateFields BOOL (def: true): check private PROPs are only used where allowed
# - onToken(TOKEN):
# - called by tokenizer
# - can also be an ARR to push arguments to it
# - onComment(BOOL, STR, START_OFFSET_NUM, END_OFFSET_NUM):
# - called on comments
# - BOOL is // vs /* */
# - can also be an ARR to push arguments to it
# - with a slighly different format mimicking Esprima, e.g. to use with Escodegen
# - onInsertedSemicolon(NUM, POSITION): called when found a line with missing semicolon
# - onTrailingComma(NUM, POSITION): called on trailing commas
# - program PROGRAM2_NODE: prepend PROGRAM2.body to PROGRAM.body
#Positions:
# - line-wise (if OPTS.locations true, def false):
# - POSITION is OBJ.line|column
# - NODE|TOKEN.loc: start|end POSITION
# - NODE|TOKEN[.loc].sourceFile VAL: using OPTS.[direct]sourceFile VAL
# - ACORN.getLineInfo('JS', START_OFFSET)->POSITION
# - non-line-wise:
# - NODE|TOKEN.start|end START|END_OFFSET (always defined)
# - NODE|TOKEN.range [START_OFFSET, END_OFFSET] (if OPTS.ranges true, def: false)
ACORN.parseExpressionAt #Same but for an expression, not a full file.
('JS', OFFSET_NUM[, OPTS]) #Ignores next expressions|statements
->EXPR_NODE #A bit faster
ACORN-LOOSE.parse('JS'[, OPTS])
->PROGRAM_NODE
/=+===============================+=\
/ : : \
)==: TOKENIZER :==(
\ :_______________________________: /
\=+===============================+=/
ACORN.tokenizer('JS'[, OPTS])
->TOKENIZER #
TOKENIZER #TOKEN_ITERATOR. Stops at ACORN.tokTypes.eof
TOKENIZER.getToken()->TOKEN #When reached ACORN.tokTypes.eof, keeps returning it.
TOKEN.value #STR
TOKEN.type #TOKEN_TYPE
ACORN.tokTypes.TYPE #TOKEN_TYPE where TYPE can be:
# eof
# _null
# _true _false logicalOR logicalAND bitwiseOR question equality relational
# num plusMin modulo star slash starstar bitwiseXOR bitwiseAND bitShift
# string template backQuote dollarBraceL
# regexp
# name eq assign incDec _var _const _delete _export _import
# bracketL bracketR braceL braceR parenL parenR
# comma semi colon dot
# _if _else _do _while _break _continue _for _in ellipsis
# _switch _case _default
# _try _catch _finally _throw _debugger
# arrow _function _return _new
# _this _super _class _extends
# _instanceof _typeof _void
# _with
# prefix
#"name" is VAR, and also some keywords including let, of, yield
TOKEN_TYPE.label #Name STR:
# eof
# null
# true false || && | ? ==/!= </>
# num +/- % * / ** ^ & <</>>
# string template ` ${
# regexp
# name = _= ++/-- var const delete export import
# [ ] { } ( )
# , ; : .
# if else do while break continue for in ...
# switch case default
# try catch finally throw debugger
# => function return new
# this super class extends
# instanceof typeof void
# with
# prefix
/=+===============================+=\
/ : : \
)==: CLI :==(
\ :_______________________________: /
\=+===============================+=/
acorn [FILE...] #CLI. Calls ACORN.parse() -> console.log()
#Def FILE: stdin
#Exit code 1 if error
--tokenize #Same with ACORN.tokenize()
--silent #Do not console.log(), only use exit code
--compact #Do not indent JSON output
--ecma3|5|6|7|8
--module
--allow-hash-bang
--allow-await-outside-function
--locations #ACORN.parse|tokenize() OPTS
/=+===============================+=\
/ : : \
)==: PLUGINS :==(
\ :_______________________________: /
\=+===============================+=/
ACORN.Parser #PARSER. Same as ACORN, except with additional method extend()
PARSER.acorn #ACORN
PARSER.extend(PLUGIN...) #
ACORN-STAGE3 ##PLUGIN bundling the following PLUGINs: class-fields, private-methods, static-class-features
##Version 4.0.0
ACORN-CLASS-FIELDS ##PLUGIN for class { VAR [= VAL] }
##Version 1.0.0
ACORN-PRIVATE-CLASS-ELEMENTS ##PLUGIN for class { #VAR }
##New token type: privateName
##Version 1.0.0
ACORN-PRIVATE-METHODS ##PLUGIN for class { #FUNC() }
##New token type: privateName
##Version 1.0.0
ACORN-STATIC-CLASS-FEATURES ##PLUGIN for class { static VAR [= VAL] }
##Version 1.0.0
ACORN-JSX() ##PLUGIN for JSX (see JSX AST doc)
##New token types: jsxName, jsxText, jsxTagStart, jsxTagEnd
##Version 5.3.2