]> code.delx.au - gnu-emacs-elpa/blob - js2-mode.el
js2-node-externs: Add querystring
[gnu-emacs-elpa] / js2-mode.el
1 ;;; js2-mode.el --- Improved JavaScript editing mode
2
3 ;; Copyright (C) 2009, 2011-2015 Free Software Foundation, Inc.
4
5 ;; Author: Steve Yegge <steve.yegge@gmail.com>
6 ;; mooz <stillpedant@gmail.com>
7 ;; Dmitry Gutov <dgutov@yandex.ru>
8 ;; URL: https://github.com/mooz/js2-mode/
9 ;; http://code.google.com/p/js2-mode/
10 ;; Version: 20150202
11 ;; Keywords: languages, javascript
12 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
13
14 ;; This file is part of GNU Emacs.
15
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
20
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28
29 ;;; Commentary:
30
31 ;; This JavaScript editing mode supports:
32
33 ;; - strict recognition of the Ecma-262 language standard
34 ;; - support for most Rhino and SpiderMonkey extensions from 1.5 and up
35 ;; - parsing support for ECMAScript for XML (E4X, ECMA-357)
36 ;; - accurate syntax highlighting using a recursive-descent parser
37 ;; - on-the-fly reporting of syntax errors and strict-mode warnings
38 ;; - undeclared-variable warnings using a configurable externs framework
39 ;; - "bouncing" line indentation to choose among alternate indentation points
40 ;; - smart line-wrapping within comments and strings
41 ;; - code folding:
42 ;; - show some or all function bodies as {...}
43 ;; - show some or all block comments as /*...*/
44 ;; - context-sensitive menu bar and popup menus
45 ;; - code browsing using the `imenu' package
46 ;; - many customization options
47
48 ;; Installation:
49 ;;
50 ;; To install it as your major mode for JavaScript editing:
51
52 ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
53
54 ;; Alternatively, to install it as a minor mode just for JavaScript linting,
55 ;; you must add it to the appropriate major-mode hook. Normally this would be:
56
57 ;; (add-hook 'js-mode-hook 'js2-minor-mode)
58
59 ;; You may also want to hook it in for shell scripts running via node.js:
60
61 ;; (add-to-list 'interpreter-mode-alist '("node" . js2-mode))
62
63 ;; To customize how it works:
64 ;; M-x customize-group RET js2-mode RET
65
66 ;; Notes:
67
68 ;; This mode includes a port of Mozilla Rhino's scanner, parser and
69 ;; symbol table. Ideally it should stay in sync with Rhino, keeping
70 ;; `js2-mode' current as the EcmaScript language standard evolves.
71
72 ;; Unlike cc-engine based language modes, js2-mode's line-indentation is not
73 ;; customizable. It is a surprising amount of work to support customizable
74 ;; indentation. The current compromise is that the tab key lets you cycle among
75 ;; various likely indentation points, similar to the behavior of python-mode.
76
77 ;; This mode does not yet work with "multi-mode" modes such as `mmm-mode'
78 ;; and `mumamo', although it could be made to do so with some effort.
79 ;; This means that `js2-mode' is currently only useful for editing JavaScript
80 ;; files, and not for editing JavaScript within <script> tags or templates.
81
82 ;; The project page on GitHub is used for development and issue tracking.
83 ;; The original homepage at Google Code has outdated information and is mostly
84 ;; unmaintained.
85
86 ;;; Code:
87
88 (require 'cl-lib)
89 (require 'imenu)
90 (require 'cc-cmds) ; for `c-fill-paragraph'
91
92 (eval-and-compile
93 (require 'cc-mode) ; (only) for `c-populate-syntax-table'
94 (require 'cc-engine)) ; for `c-paragraph-start' et. al.
95
96 (defvar electric-layout-rules)
97
98 ;;; Externs (variables presumed to be defined by the host system)
99
100 (defvar js2-ecma-262-externs
101 (mapcar 'symbol-name
102 '(Array Boolean Date Error EvalError Function Infinity JSON
103 Math NaN Number Object RangeError ReferenceError RegExp
104 String SyntaxError TypeError URIError
105 decodeURI decodeURIComponent encodeURI
106 encodeURIComponent escape eval isFinite isNaN
107 parseFloat parseInt undefined unescape))
108 "Ecma-262 externs. Included in `js2-externs' by default.")
109
110 (defvar js2-browser-externs
111 (mapcar 'symbol-name
112 '(;; DOM level 1
113 Attr CDATASection CharacterData Comment DOMException
114 DOMImplementation Document DocumentFragment
115 DocumentType Element Entity EntityReference
116 ExceptionCode NamedNodeMap Node NodeList Notation
117 ProcessingInstruction Text
118
119 ;; DOM level 2
120 HTMLAnchorElement HTMLAppletElement HTMLAreaElement
121 HTMLBRElement HTMLBaseElement HTMLBaseFontElement
122 HTMLBodyElement HTMLButtonElement HTMLCollection
123 HTMLDListElement HTMLDirectoryElement HTMLDivElement
124 HTMLDocument HTMLElement HTMLFieldSetElement
125 HTMLFontElement HTMLFormElement HTMLFrameElement
126 HTMLFrameSetElement HTMLHRElement HTMLHeadElement
127 HTMLHeadingElement HTMLHtmlElement HTMLIFrameElement
128 HTMLImageElement HTMLInputElement HTMLIsIndexElement
129 HTMLLIElement HTMLLabelElement HTMLLegendElement
130 HTMLLinkElement HTMLMapElement HTMLMenuElement
131 HTMLMetaElement HTMLModElement HTMLOListElement
132 HTMLObjectElement HTMLOptGroupElement
133 HTMLOptionElement HTMLOptionsCollection
134 HTMLParagraphElement HTMLParamElement HTMLPreElement
135 HTMLQuoteElement HTMLScriptElement HTMLSelectElement
136 HTMLStyleElement HTMLTableCaptionElement
137 HTMLTableCellElement HTMLTableColElement
138 HTMLTableElement HTMLTableRowElement
139 HTMLTableSectionElement HTMLTextAreaElement
140 HTMLTitleElement HTMLUListElement
141
142 ;; DOM level 3
143 DOMConfiguration DOMError DOMException
144 DOMImplementationList DOMImplementationSource
145 DOMLocator DOMStringList NameList TypeInfo
146 UserDataHandler
147
148 ;; Window
149 window alert confirm document java navigator prompt screen
150 self top requestAnimationFrame cancelAnimationFrame
151
152 ;; W3C CSS
153 CSSCharsetRule CSSFontFace CSSFontFaceRule
154 CSSImportRule CSSMediaRule CSSPageRule
155 CSSPrimitiveValue CSSProperties CSSRule CSSRuleList
156 CSSStyleDeclaration CSSStyleRule CSSStyleSheet
157 CSSValue CSSValueList Counter DOMImplementationCSS
158 DocumentCSS DocumentStyle ElementCSSInlineStyle
159 LinkStyle MediaList RGBColor Rect StyleSheet
160 StyleSheetList ViewCSS
161
162 ;; W3C Event
163 EventListener EventTarget Event DocumentEvent UIEvent
164 MouseEvent MutationEvent KeyboardEvent
165
166 ;; W3C Range
167 DocumentRange Range RangeException
168
169 ;; W3C XML
170 XPathResult XMLHttpRequest
171
172 ;; console object. Provided by at least Chrome and Firefox.
173 console))
174 "Browser externs.
175 You can cause these to be included or excluded with the custom
176 variable `js2-include-browser-externs'.")
177
178 (defvar js2-rhino-externs
179 (mapcar 'symbol-name
180 '(Packages importClass importPackage com org java
181 ;; Global object (shell) externs.
182 defineClass deserialize doctest gc help load
183 loadClass print quit readFile readUrl runCommand seal
184 serialize spawn sync toint32 version))
185 "Mozilla Rhino externs.
186 Set `js2-include-rhino-externs' to t to include them.")
187
188 (defvar js2-node-externs
189 (mapcar 'symbol-name
190 '(__dirname __filename Buffer clearInterval clearTimeout require
191 console exports global module process setInterval setTimeout
192 querystring))
193 "Node.js externs.
194 Set `js2-include-node-externs' to t to include them.")
195
196 (defvar js2-typed-array-externs
197 (mapcar 'symbol-name
198 '(ArrayBuffer Uint8ClampedArray DataView
199 Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array
200 Float32Array Float64Array))
201 "Khronos typed array externs. Available in most modern browsers and
202 in node.js >= 0.6. If `js2-include-node-externs' or `js2-include-browser-externs'
203 are enabled, these will also be included.")
204
205 (defvar js2-harmony-externs
206 (mapcar 'symbol-name
207 '(Map Promise Proxy Reflect Set Symbol WeakMap WeakSet))
208 "ES6 externs. If `js2-include-browser-externs' is enabled and
209 `js2-language-version' is sufficiently high, these will be included.")
210
211 ;;; Variables
212
213 (defun js2-mark-safe-local (name pred)
214 "Make the variable NAME buffer-local and mark it as safe file-local
215 variable with predicate PRED."
216 (make-variable-buffer-local name)
217 (put name 'safe-local-variable pred))
218
219 (defcustom js2-highlight-level 2
220 "Amount of syntax highlighting to perform.
221 0 or a negative value means none.
222 1 adds basic syntax highlighting.
223 2 adds highlighting of some Ecma built-in properties.
224 3 adds highlighting of many Ecma built-in functions."
225 :group 'js2-mode
226 :type '(choice (const :tag "None" 0)
227 (const :tag "Basic" 1)
228 (const :tag "Include Properties" 2)
229 (const :tag "Include Functions" 3)))
230
231 (defvar js2-mode-dev-mode-p nil
232 "Non-nil if running in development mode. Normally nil.")
233
234 (defgroup js2-mode nil
235 "An improved JavaScript mode."
236 :group 'languages)
237
238 (defcustom js2-basic-offset (if (and (boundp 'c-basic-offset)
239 (numberp c-basic-offset))
240 c-basic-offset
241 4)
242 "Number of spaces to indent nested statements.
243 Similar to `c-basic-offset'."
244 :group 'js2-mode
245 :type 'integer)
246 (js2-mark-safe-local 'js2-basic-offset 'integerp)
247
248 (defcustom js2-bounce-indent-p nil
249 "Non-nil to have indent-line function choose among alternatives.
250 If nil, the indent-line function will indent to a predetermined column
251 based on heuristic guessing. If non-nil, then if the current line is
252 already indented to that predetermined column, indenting will choose
253 another likely column and indent to that spot. Repeated invocation of
254 the indent-line function will cycle among the computed alternatives.
255 See the function `js2-bounce-indent' for details. When it is non-nil,
256 js2-mode also binds `js2-bounce-indent-backwards' to Shift-Tab."
257 :type 'boolean
258 :group 'js2-mode)
259
260 (defcustom js2-pretty-multiline-declarations t
261 "Non-nil to line up multiline declarations vertically:
262
263 var a = 10,
264 b = 20,
265 c = 30;
266
267 If the value is t, and the first assigned value in the
268 declaration is a function/array/object literal spanning several
269 lines, it won't be indented additionally:
270
271 var o = { var bar = 2,
272 foo: 3 vs. o = {
273 }, foo: 3
274 bar = 2; };
275
276 If the value is `all', it will always be indented additionally:
277
278 var o = {
279 foo: 3
280 };
281
282 var o = {
283 foo: 3
284 },
285 bar = 2;
286
287 If the value is `dynamic', it will be indented additionally only
288 if the declaration contains more than one variable:
289
290 var o = {
291 foo: 3
292 };
293
294 var o = {
295 foo: 3
296 },
297 bar = 2;"
298 :group 'js2-mode
299 :type 'symbol)
300 (js2-mark-safe-local 'js2-pretty-multiline-declarations 'symbolp)
301
302 (defcustom js2-indent-switch-body nil
303 "When nil, case labels are indented on the same level as the
304 containing switch statement. Otherwise, all lines inside
305 switch statement body are indented one additional level."
306 :type 'boolean
307 :group 'js2-mode)
308 (js2-mark-safe-local 'js2-indent-case-same-as-switch 'booleanp)
309
310 (defcustom js2-idle-timer-delay 0.2
311 "Delay in secs before re-parsing after user makes changes.
312 Multiplied by `js2-dynamic-idle-timer-adjust', which see."
313 :type 'number
314 :group 'js2-mode)
315 (make-variable-buffer-local 'js2-idle-timer-delay)
316
317 (defcustom js2-dynamic-idle-timer-adjust 0
318 "Positive to adjust `js2-idle-timer-delay' based on file size.
319 The idea is that for short files, parsing is faster so we can be
320 more responsive to user edits without interfering with editing.
321 The buffer length in characters (typically bytes) is divided by
322 this value and used to multiply `js2-idle-timer-delay' for the
323 buffer. For example, a 21k file and 10k adjust yields 21k/10k
324 == 2, so js2-idle-timer-delay is multiplied by 2.
325 If `js2-dynamic-idle-timer-adjust' is 0 or negative,
326 `js2-idle-timer-delay' is not dependent on the file size."
327 :type 'number
328 :group 'js2-mode)
329
330 (defcustom js2-concat-multiline-strings t
331 "When non-nil, `js2-line-break' in mid-string will make it a
332 string concatenation. When `eol', the '+' will be inserted at the
333 end of the line, otherwise, at the beginning of the next line."
334 :type '(choice (const t) (const eol) (const nil))
335 :group 'js2-mode)
336
337 (defcustom js2-mode-show-parse-errors t
338 "True to highlight parse errors."
339 :type 'boolean
340 :group 'js2-mode)
341
342 (defcustom js2-mode-show-strict-warnings t
343 "Non-nil to emit Ecma strict-mode warnings.
344 Some of the warnings can be individually disabled by other flags,
345 even if this flag is non-nil."
346 :type 'boolean
347 :group 'js2-mode)
348
349 (defcustom js2-strict-trailing-comma-warning t
350 "Non-nil to warn about trailing commas in array literals.
351 Ecma-262-5.1 allows them, but older versions of IE raise an error."
352 :type 'boolean
353 :group 'js2-mode)
354
355 (defcustom js2-strict-missing-semi-warning t
356 "Non-nil to warn about semicolon auto-insertion after statement.
357 Technically this is legal per Ecma-262, but some style guides disallow
358 depending on it."
359 :type 'boolean
360 :group 'js2-mode)
361
362 (defcustom js2-missing-semi-one-line-override nil
363 "Non-nil to permit missing semicolons in one-line functions.
364 In one-liner functions such as `function identity(x) {return x}'
365 people often omit the semicolon for a cleaner look. If you are
366 such a person, you can suppress the missing-semicolon warning
367 by setting this variable to t."
368 :type 'boolean
369 :group 'js2-mode)
370
371 (defcustom js2-strict-inconsistent-return-warning t
372 "Non-nil to warn about mixing returns with value-returns.
373 It's perfectly legal to have a `return' and a `return foo' in the
374 same function, but it's often an indicator of a bug, and it also
375 interferes with type inference (in systems that support it.)"
376 :type 'boolean
377 :group 'js2-mode)
378
379 (defcustom js2-strict-cond-assign-warning t
380 "Non-nil to warn about expressions like if (a = b).
381 This often should have been '==' instead of '='. If the warning
382 is enabled, you can suppress it on a per-expression basis by
383 parenthesizing the expression, e.g. if ((a = b)) ..."
384 :type 'boolean
385 :group 'js2-mode)
386
387 (defcustom js2-strict-var-redeclaration-warning t
388 "Non-nil to warn about redeclaring variables in a script or function."
389 :type 'boolean
390 :group 'js2-mode)
391
392 (defcustom js2-strict-var-hides-function-arg-warning t
393 "Non-nil to warn about a var decl hiding a function argument."
394 :type 'boolean
395 :group 'js2-mode)
396
397 (defcustom js2-skip-preprocessor-directives nil
398 "Non-nil to treat lines beginning with # as comments.
399 Useful for viewing Mozilla JavaScript source code."
400 :type 'boolean
401 :group 'js2-mode)
402
403 (defcustom js2-language-version 200
404 "Configures what JavaScript language version to recognize.
405 Currently versions 150, 160, 170, 180 and 200 are supported,
406 corresponding to JavaScript 1.5, 1.6, 1.7, 1.8 and 2.0 (Harmony),
407 respectively. In a nutshell, 1.6 adds E4X support, 1.7 adds let,
408 yield, and Array comprehensions, and 1.8 adds function closures."
409 :type 'integer
410 :group 'js2-mode)
411
412 (defcustom js2-instanceof-has-side-effects nil
413 "If non-nil, treats the instanceof operator as having side effects.
414 This is useful for xulrunner apps."
415 :type 'boolean
416 :group 'js2-mode)
417
418 (defcustom js2-move-point-on-right-click t
419 "Non-nil to move insertion point when you right-click.
420 This makes right-click context menu behavior a bit more intuitive,
421 since menu operations generally apply to the point. The exception
422 is if there is a region selection, in which case the point does -not-
423 move, so cut/copy/paste can work properly.
424
425 Note that IntelliJ moves the point, and Eclipse leaves it alone,
426 so this behavior is customizable."
427 :group 'js2-mode
428 :type 'boolean)
429
430 (defcustom js2-allow-rhino-new-expr-initializer t
431 "Non-nil to support a Rhino's experimental syntactic construct.
432
433 Rhino supports the ability to follow a `new' expression with an object
434 literal, which is used to set additional properties on the new object
435 after calling its constructor. Syntax:
436
437 new <expr> [ ( arglist ) ] [initializer]
438
439 Hence, this expression:
440
441 new Object {a: 1, b: 2}
442
443 results in an Object with properties a=1 and b=2. This syntax is
444 apparently not configurable in Rhino - it's currently always enabled,
445 as of Rhino version 1.7R2."
446 :type 'boolean
447 :group 'js2-mode)
448
449 (defcustom js2-allow-member-expr-as-function-name nil
450 "Non-nil to support experimental Rhino syntax for function names.
451
452 Rhino supports an experimental syntax configured via the Rhino Context
453 setting `allowMemberExprAsFunctionName'. The experimental syntax is:
454
455 function <member-expr> ( [ arg-list ] ) { <body> }
456
457 Where member-expr is a non-parenthesized 'member expression', which
458 is anything at the grammar level of a new-expression or lower, meaning
459 any expression that does not involve infix or unary operators.
460
461 When <member-expr> is not a simple identifier, then it is syntactic
462 sugar for assigning the anonymous function to the <member-expr>. Hence,
463 this code:
464
465 function a.b().c[2] (x, y) { ... }
466
467 is rewritten as:
468
469 a.b().c[2] = function(x, y) {...}
470
471 which doesn't seem particularly useful, but Rhino permits it."
472 :type 'boolean
473 :group 'js2-mode)
474
475 ;; scanner variables
476
477 (defmacro js2-deflocal (name value &optional comment)
478 "Define a buffer-local variable NAME with VALUE and COMMENT."
479 (declare (debug defvar) (doc-string 3))
480 `(progn
481 (defvar ,name ,value ,comment)
482 (make-variable-buffer-local ',name)))
483
484 (defvar js2-EOF_CHAR -1
485 "Represents end of stream. Distinct from js2-EOF token type.")
486
487 ;; I originally used symbols to represent tokens, but Rhino uses
488 ;; ints and then sets various flag bits in them, so ints it is.
489 ;; The upshot is that we need a `js2-' prefix in front of each name.
490 (defvar js2-ERROR -1)
491 (defvar js2-EOF 0)
492 (defvar js2-EOL 1)
493 (defvar js2-ENTERWITH 2) ; begin interpreter bytecodes
494 (defvar js2-LEAVEWITH 3)
495 (defvar js2-RETURN 4)
496 (defvar js2-GOTO 5)
497 (defvar js2-IFEQ 6)
498 (defvar js2-IFNE 7)
499 (defvar js2-SETNAME 8)
500 (defvar js2-BITOR 9)
501 (defvar js2-BITXOR 10)
502 (defvar js2-BITAND 11)
503 (defvar js2-EQ 12)
504 (defvar js2-NE 13)
505 (defvar js2-LT 14)
506 (defvar js2-LE 15)
507 (defvar js2-GT 16)
508 (defvar js2-GE 17)
509 (defvar js2-LSH 18)
510 (defvar js2-RSH 19)
511 (defvar js2-URSH 20)
512 (defvar js2-ADD 21) ; infix plus
513 (defvar js2-SUB 22) ; infix minus
514 (defvar js2-MUL 23)
515 (defvar js2-DIV 24)
516 (defvar js2-MOD 25)
517 (defvar js2-NOT 26)
518 (defvar js2-BITNOT 27)
519 (defvar js2-POS 28) ; unary plus
520 (defvar js2-NEG 29) ; unary minus
521 (defvar js2-NEW 30)
522 (defvar js2-DELPROP 31)
523 (defvar js2-TYPEOF 32)
524 (defvar js2-GETPROP 33)
525 (defvar js2-GETPROPNOWARN 34)
526 (defvar js2-SETPROP 35)
527 (defvar js2-GETELEM 36)
528 (defvar js2-SETELEM 37)
529 (defvar js2-CALL 38)
530 (defvar js2-NAME 39) ; an identifier
531 (defvar js2-NUMBER 40)
532 (defvar js2-STRING 41)
533 (defvar js2-NULL 42)
534 (defvar js2-THIS 43)
535 (defvar js2-FALSE 44)
536 (defvar js2-TRUE 45)
537 (defvar js2-SHEQ 46) ; shallow equality (===)
538 (defvar js2-SHNE 47) ; shallow inequality (!==)
539 (defvar js2-REGEXP 48)
540 (defvar js2-BINDNAME 49)
541 (defvar js2-THROW 50)
542 (defvar js2-RETHROW 51) ; rethrow caught exception: catch (e if ) uses it
543 (defvar js2-IN 52)
544 (defvar js2-INSTANCEOF 53)
545 (defvar js2-LOCAL_LOAD 54)
546 (defvar js2-GETVAR 55)
547 (defvar js2-SETVAR 56)
548 (defvar js2-CATCH_SCOPE 57)
549 (defvar js2-ENUM_INIT_KEYS 58) ; FIXME: what are these?
550 (defvar js2-ENUM_INIT_VALUES 59)
551 (defvar js2-ENUM_INIT_ARRAY 60)
552 (defvar js2-ENUM_NEXT 61)
553 (defvar js2-ENUM_ID 62)
554 (defvar js2-THISFN 63)
555 (defvar js2-RETURN_RESULT 64) ; to return previously stored return result
556 (defvar js2-ARRAYLIT 65) ; array literal
557 (defvar js2-OBJECTLIT 66) ; object literal
558 (defvar js2-GET_REF 67) ; *reference
559 (defvar js2-SET_REF 68) ; *reference = something
560 (defvar js2-DEL_REF 69) ; delete reference
561 (defvar js2-REF_CALL 70) ; f(args) = something or f(args)++
562 (defvar js2-REF_SPECIAL 71) ; reference for special properties like __proto
563 (defvar js2-YIELD 72) ; JS 1.7 yield pseudo keyword
564
565 ;; XML support
566 (defvar js2-DEFAULTNAMESPACE 73)
567 (defvar js2-ESCXMLATTR 74)
568 (defvar js2-ESCXMLTEXT 75)
569 (defvar js2-REF_MEMBER 76) ; Reference for x.@y, x..y etc.
570 (defvar js2-REF_NS_MEMBER 77) ; Reference for x.ns::y, x..ns::y etc.
571 (defvar js2-REF_NAME 78) ; Reference for @y, @[y] etc.
572 (defvar js2-REF_NS_NAME 79) ; Reference for ns::y, @ns::y@[y] etc.
573
574 (defvar js2-first-bytecode js2-ENTERWITH)
575 (defvar js2-last-bytecode js2-REF_NS_NAME)
576
577 (defvar js2-TRY 80)
578 (defvar js2-SEMI 81) ; semicolon
579 (defvar js2-LB 82) ; left and right brackets
580 (defvar js2-RB 83)
581 (defvar js2-LC 84) ; left and right curly-braces
582 (defvar js2-RC 85)
583 (defvar js2-LP 86) ; left and right parens
584 (defvar js2-RP 87)
585 (defvar js2-COMMA 88) ; comma operator
586
587 (defvar js2-ASSIGN 89) ; simple assignment (=)
588 (defvar js2-ASSIGN_BITOR 90) ; |=
589 (defvar js2-ASSIGN_BITXOR 91) ; ^=
590 (defvar js2-ASSIGN_BITAND 92) ; &=
591 (defvar js2-ASSIGN_LSH 93) ; <<=
592 (defvar js2-ASSIGN_RSH 94) ; >>=
593 (defvar js2-ASSIGN_URSH 95) ; >>>=
594 (defvar js2-ASSIGN_ADD 96) ; +=
595 (defvar js2-ASSIGN_SUB 97) ; -=
596 (defvar js2-ASSIGN_MUL 98) ; *=
597 (defvar js2-ASSIGN_DIV 99) ; /=
598 (defvar js2-ASSIGN_MOD 100) ; %=
599
600 (defvar js2-first-assign js2-ASSIGN)
601 (defvar js2-last-assign js2-ASSIGN_MOD)
602
603 (defvar js2-HOOK 101) ; conditional (?:)
604 (defvar js2-COLON 102)
605 (defvar js2-OR 103) ; logical or (||)
606 (defvar js2-AND 104) ; logical and (&&)
607 (defvar js2-INC 105) ; increment/decrement (++ --)
608 (defvar js2-DEC 106)
609 (defvar js2-DOT 107) ; member operator (.)
610 (defvar js2-FUNCTION 108) ; function keyword
611 (defvar js2-EXPORT 109) ; export keyword
612 (defvar js2-IMPORT 110) ; import keyword
613 (defvar js2-IF 111) ; if keyword
614 (defvar js2-ELSE 112) ; else keyword
615 (defvar js2-SWITCH 113) ; switch keyword
616 (defvar js2-CASE 114) ; case keyword
617 (defvar js2-DEFAULT 115) ; default keyword
618 (defvar js2-WHILE 116) ; while keyword
619 (defvar js2-DO 117) ; do keyword
620 (defvar js2-FOR 118) ; for keyword
621 (defvar js2-BREAK 119) ; break keyword
622 (defvar js2-CONTINUE 120) ; continue keyword
623 (defvar js2-VAR 121) ; var keyword
624 (defvar js2-WITH 122) ; with keyword
625 (defvar js2-CATCH 123) ; catch keyword
626 (defvar js2-FINALLY 124) ; finally keyword
627 (defvar js2-VOID 125) ; void keyword
628 (defvar js2-RESERVED 126) ; reserved keywords
629
630 (defvar js2-EMPTY 127)
631
632 ;; Types used for the parse tree - never returned by scanner.
633
634 (defvar js2-BLOCK 128) ; statement block
635 (defvar js2-LABEL 129) ; label
636 (defvar js2-TARGET 130)
637 (defvar js2-LOOP 131)
638 (defvar js2-EXPR_VOID 132) ; expression statement in functions
639 (defvar js2-EXPR_RESULT 133) ; expression statement in scripts
640 (defvar js2-JSR 134)
641 (defvar js2-SCRIPT 135) ; top-level node for entire script
642 (defvar js2-TYPEOFNAME 136) ; for typeof(simple-name)
643 (defvar js2-USE_STACK 137)
644 (defvar js2-SETPROP_OP 138) ; x.y op= something
645 (defvar js2-SETELEM_OP 139) ; x[y] op= something
646 (defvar js2-LOCAL_BLOCK 140)
647 (defvar js2-SET_REF_OP 141) ; *reference op= something
648
649 ;; For XML support:
650 (defvar js2-DOTDOT 142) ; member operator (..)
651 (defvar js2-COLONCOLON 143) ; namespace::name
652 (defvar js2-XML 144) ; XML type
653 (defvar js2-DOTQUERY 145) ; .() -- e.g., x.emps.emp.(name == "terry")
654 (defvar js2-XMLATTR 146) ; @
655 (defvar js2-XMLEND 147)
656
657 ;; Optimizer-only tokens
658 (defvar js2-TO_OBJECT 148)
659 (defvar js2-TO_DOUBLE 149)
660
661 (defvar js2-GET 150) ; JS 1.5 get pseudo keyword
662 (defvar js2-SET 151) ; JS 1.5 set pseudo keyword
663 (defvar js2-LET 152) ; JS 1.7 let pseudo keyword
664 (defvar js2-CONST 153)
665 (defvar js2-SETCONST 154)
666 (defvar js2-SETCONSTVAR 155)
667 (defvar js2-ARRAYCOMP 156)
668 (defvar js2-LETEXPR 157)
669 (defvar js2-WITHEXPR 158)
670 (defvar js2-DEBUGGER 159)
671
672 (defvar js2-COMMENT 160)
673 (defvar js2-TRIPLEDOT 161) ; for rest parameter
674 (defvar js2-ARROW 162) ; function arrow (=>)
675 (defvar js2-CLASS 163)
676 (defvar js2-EXTENDS 164)
677 (defvar js2-SUPER 165)
678 (defvar js2-TEMPLATE_HEAD 166) ; part of template literal before substitution
679 (defvar js2-NO_SUBS_TEMPLATE 167) ; template literal without substitutions
680 (defvar js2-TAGGED_TEMPLATE 168) ; tagged template literal
681
682 (defconst js2-num-tokens (1+ js2-TAGGED_TEMPLATE))
683
684 (defconst js2-debug-print-trees nil)
685
686 ;; Rhino accepts any string or stream as input. Emacs character
687 ;; processing works best in buffers, so we'll assume the input is a
688 ;; buffer. JavaScript strings can be copied into temp buffers before
689 ;; scanning them.
690
691 ;; Buffer-local variables yield much cleaner code than using `defstruct'.
692 ;; They're the Emacs equivalent of instance variables, more or less.
693
694 (js2-deflocal js2-ts-dirty-line nil
695 "Token stream buffer-local variable.
696 Indicates stuff other than whitespace since start of line.")
697
698 (js2-deflocal js2-ts-hit-eof nil
699 "Token stream buffer-local variable.")
700
701 ;; FIXME: Unused.
702 (js2-deflocal js2-ts-line-start 0
703 "Token stream buffer-local variable.")
704
705 (js2-deflocal js2-ts-lineno 1
706 "Token stream buffer-local variable.")
707
708 ;; FIXME: Unused.
709 (js2-deflocal js2-ts-line-end-char -1
710 "Token stream buffer-local variable.")
711
712 (js2-deflocal js2-ts-cursor 1 ; emacs buffers are 1-indexed
713 "Token stream buffer-local variable.
714 Current scan position.")
715
716 ;; FIXME: Unused.
717 (js2-deflocal js2-ts-is-xml-attribute nil
718 "Token stream buffer-local variable.")
719
720 (js2-deflocal js2-ts-xml-is-tag-content nil
721 "Token stream buffer-local variable.")
722
723 (js2-deflocal js2-ts-xml-open-tags-count 0
724 "Token stream buffer-local variable.")
725
726 (js2-deflocal js2-ts-string-buffer nil
727 "Token stream buffer-local variable.
728 List of chars built up while scanning various tokens.")
729
730 (cl-defstruct (js2-token
731 (:constructor nil)
732 (:constructor make-js2-token (beg)))
733 "Value returned from the token stream."
734 (type js2-EOF)
735 (beg 1)
736 (end -1)
737 (string "")
738 number
739 regexp-flags
740 comment-type
741 follows-eol-p)
742
743 ;; Have to call `js2-init-scanner' to initialize the values.
744 (js2-deflocal js2-ti-tokens nil)
745 (js2-deflocal js2-ti-tokens-cursor nil)
746 (js2-deflocal js2-ti-lookahead nil)
747
748 (cl-defstruct (js2-ts-state
749 (:constructor make-js2-ts-state (&key (lineno js2-ts-lineno)
750 (cursor js2-ts-cursor)
751 (tokens (copy-sequence js2-ti-tokens))
752 (tokens-cursor js2-ti-tokens-cursor)
753 (lookahead js2-ti-lookahead))))
754 lineno
755 cursor
756 tokens
757 tokens-cursor
758 lookahead)
759
760 ;;; Parser variables
761
762 (js2-deflocal js2-parsed-errors nil
763 "List of errors produced during scanning/parsing.")
764
765 (js2-deflocal js2-parsed-warnings nil
766 "List of warnings produced during scanning/parsing.")
767
768 (js2-deflocal js2-recover-from-parse-errors t
769 "Non-nil to continue parsing after a syntax error.
770
771 In recovery mode, the AST will be built in full, and any error
772 nodes will be flagged with appropriate error information. If
773 this flag is nil, a syntax error will result in an error being
774 signaled.
775
776 The variable is automatically buffer-local, because different
777 modes that use the parser will need different settings.")
778
779 (js2-deflocal js2-parse-hook nil
780 "List of callbacks for receiving parsing progress.")
781
782 (defvar js2-parse-finished-hook nil
783 "List of callbacks to notify when parsing finishes.
784 Not called if parsing was interrupted.")
785
786 (js2-deflocal js2-is-eval-code nil
787 "True if we're evaluating code in a string.
788 If non-nil, the tokenizer will record the token text, and the AST nodes
789 will record their source text. Off by default for IDE modes, since the
790 text is available in the buffer.")
791
792 (defvar js2-parse-ide-mode t
793 "Non-nil if the parser is being used for `js2-mode'.
794 If non-nil, the parser will set text properties for fontification
795 and the syntax table. The value should be nil when using the
796 parser as a frontend to an interpreter or byte compiler.")
797
798 ;;; Parser instance variables (buffer-local vars for js2-parse)
799
800 (defconst js2-ti-after-eol (lsh 1 16)
801 "Flag: first token of the source line.")
802
803 ;; Inline Rhino's CompilerEnvirons vars as buffer-locals.
804
805 (js2-deflocal js2-compiler-generate-debug-info t)
806 (js2-deflocal js2-compiler-use-dynamic-scope nil)
807 (js2-deflocal js2-compiler-reserved-keywords-as-identifier nil)
808 (js2-deflocal js2-compiler-xml-available t)
809 (js2-deflocal js2-compiler-optimization-level 0)
810 (js2-deflocal js2-compiler-generating-source t)
811 (js2-deflocal js2-compiler-strict-mode nil)
812 (js2-deflocal js2-compiler-report-warning-as-error nil)
813 (js2-deflocal js2-compiler-generate-observer-count nil)
814 (js2-deflocal js2-compiler-activation-names nil)
815
816 ;; SKIP: sourceURI
817
818 ;; There's a compileFunction method in Context.java - may need it.
819 (js2-deflocal js2-called-by-compile-function nil
820 "True if `js2-parse' was called by `js2-compile-function'.
821 Will only be used when we finish implementing the interpreter.")
822
823 ;; SKIP: ts (we just call `js2-init-scanner' and use its vars)
824
825 ;; SKIP: node factory - we're going to just call functions directly,
826 ;; and eventually go to a unified AST format.
827
828 (js2-deflocal js2-nesting-of-function 0)
829
830 (js2-deflocal js2-recorded-identifiers nil
831 "Tracks identifiers found during parsing.")
832
833 (js2-deflocal js2-is-in-destructuring nil
834 "True while parsing destructuring expression.")
835
836 (defcustom js2-global-externs nil
837 "A list of any extern names you'd like to consider always declared.
838 This list is global and is used by all `js2-mode' files.
839 You can create buffer-local externs list using `js2-additional-externs'.
840
841 There is also a buffer-local variable `js2-default-externs',
842 which is initialized by default to include the Ecma-262 externs
843 and the standard browser externs. The three lists are all
844 checked during highlighting."
845 :type 'list
846 :group 'js2-mode)
847
848 (js2-deflocal js2-default-externs nil
849 "Default external declarations.
850
851 These are currently only used for highlighting undeclared variables,
852 which only worries about top-level (unqualified) references.
853 As js2-mode's processing improves, we will flesh out this list.
854
855 The initial value is set to `js2-ecma-262-externs', unless some
856 of the `js2-include-?-externs' variables are set to t, in which
857 case the browser, Rhino and/or Node.js externs are also included.
858
859 See `js2-additional-externs' for more information.")
860
861 (defcustom js2-include-browser-externs t
862 "Non-nil to include browser externs in the master externs list.
863 If you work on JavaScript files that are not intended for browsers,
864 such as Mozilla Rhino server-side JavaScript, set this to nil.
865 See `js2-additional-externs' for more information about externs."
866 :type 'boolean
867 :group 'js2-mode)
868
869 (defcustom js2-include-rhino-externs nil
870 "Non-nil to include Mozilla Rhino externs in the master externs list.
871 See `js2-additional-externs' for more information about externs."
872 :type 'boolean
873 :group 'js2-mode)
874
875 (defcustom js2-include-node-externs nil
876 "Non-nil to include Node.js externs in the master externs list.
877 See `js2-additional-externs' for more information about externs."
878 :type 'boolean
879 :group 'js2-mode)
880
881 (js2-deflocal js2-additional-externs nil
882 "A buffer-local list of additional external declarations.
883 It is used to decide whether variables are considered undeclared
884 for purposes of highlighting.
885
886 Each entry is a Lisp string. The string should be the fully qualified
887 name of an external entity. All externs should be added to this list,
888 so that as js2-mode's processing improves it can take advantage of them.
889
890 You may want to declare your externs in three ways.
891 First, you can add externs that are valid for all your JavaScript files.
892 You should probably do this by adding them to `js2-global-externs', which
893 is a global list used for all js2-mode files.
894
895 Next, you can add a function to `js2-init-hook' that adds additional
896 externs appropriate for the specific file, perhaps based on its path.
897 These should go in `js2-additional-externs', which is buffer-local.
898
899 Third, you can use JSLint's global declaration, as long as
900 `js2-include-jslint-globals' is non-nil, which see.
901
902 Finally, you can add a function to `js2-post-parse-callbacks',
903 which is called after parsing completes, and `js2-mode-ast' is bound to
904 the root of the parse tree. At this stage you can set up an AST
905 node visitor using `js2-visit-ast' and examine the parse tree
906 for specific import patterns that may imply the existence of
907 other externs, possibly tied to your build system. These should also
908 be added to `js2-additional-externs'.
909
910 Your post-parse callback may of course also use the simpler and
911 faster (but perhaps less robust) approach of simply scanning the
912 buffer text for your imports, using regular expressions.")
913
914 ;; SKIP: decompiler
915 ;; SKIP: encoded-source
916
917 ;;; The following variables are per-function and should be saved/restored
918 ;;; during function parsing...
919
920 (js2-deflocal js2-current-script-or-fn nil)
921 (js2-deflocal js2-current-scope nil)
922 (js2-deflocal js2-nesting-of-with 0)
923 (js2-deflocal js2-label-set nil
924 "An alist mapping label names to nodes.")
925
926 (js2-deflocal js2-loop-set nil)
927 (js2-deflocal js2-loop-and-switch-set nil)
928 (js2-deflocal js2-has-return-value nil)
929 (js2-deflocal js2-end-flags 0)
930
931 ;;; ...end of per function variables
932
933 ;; These flags enumerate the possible ways a statement/function can
934 ;; terminate. These flags are used by endCheck() and by the Parser to
935 ;; detect inconsistent return usage.
936 ;;
937 ;; END_UNREACHED is reserved for code paths that are assumed to always be
938 ;; able to execute (example: throw, continue)
939 ;;
940 ;; END_DROPS_OFF indicates if the statement can transfer control to the
941 ;; next one. Statement such as return dont. A compound statement may have
942 ;; some branch that drops off control to the next statement.
943 ;;
944 ;; END_RETURNS indicates that the statement can return (without arguments)
945 ;; END_RETURNS_VALUE indicates that the statement can return a value.
946 ;;
947 ;; A compound statement such as
948 ;; if (condition) {
949 ;; return value;
950 ;; }
951 ;; Will be detected as (END_DROPS_OFF | END_RETURN_VALUE) by endCheck()
952
953 (defconst js2-end-unreached #x0)
954 (defconst js2-end-drops-off #x1)
955 (defconst js2-end-returns #x2)
956 (defconst js2-end-returns-value #x4)
957
958 ;; Rhino awkwardly passes a statementLabel parameter to the
959 ;; statementHelper() function, the main statement parser, which
960 ;; is then used by quite a few of the sub-parsers. We just make
961 ;; it a buffer-local variable and make sure it's cleaned up properly.
962 (js2-deflocal js2-labeled-stmt nil) ; type `js2-labeled-stmt-node'
963
964 ;; Similarly, Rhino passes an inForInit boolean through about half
965 ;; the expression parsers. We use a dynamically-scoped variable,
966 ;; which makes it easier to funcall the parsers individually without
967 ;; worrying about whether they take the parameter or not.
968 (js2-deflocal js2-in-for-init nil)
969 (js2-deflocal js2-temp-name-counter 0)
970 (js2-deflocal js2-parse-stmt-count 0)
971
972 (defsubst js2-get-next-temp-name ()
973 (format "$%d" (cl-incf js2-temp-name-counter)))
974
975 (defvar js2-parse-interruptable-p t
976 "Set this to nil to force parse to continue until finished.
977 This will mostly be useful for interpreters.")
978
979 (defvar js2-statements-per-pause 50
980 "Pause after this many statements to check for user input.
981 If user input is pending, stop the parse and discard the tree.
982 This makes for a smoother user experience for large files.
983 You may have to wait a second or two before the highlighting
984 and error-reporting appear, but you can always type ahead if
985 you wish. This appears to be more or less how Eclipse, IntelliJ
986 and other editors work.")
987
988 (js2-deflocal js2-record-comments t
989 "Instructs the scanner to record comments in `js2-scanned-comments'.")
990
991 (js2-deflocal js2-scanned-comments nil
992 "List of all comments from the current parse.")
993
994 (defcustom js2-mode-indent-inhibit-undo nil
995 "Non-nil to disable collection of Undo information when indenting lines.
996 Some users have requested this behavior. It's nil by default because
997 other Emacs modes don't work this way."
998 :type 'boolean
999 :group 'js2-mode)
1000
1001 (defcustom js2-mode-indent-ignore-first-tab nil
1002 "If non-nil, ignore first TAB keypress if we look indented properly.
1003 It's fairly common for users to navigate to an already-indented line
1004 and press TAB for reassurance that it's been indented. For this class
1005 of users, we want the first TAB press on a line to be ignored if the
1006 line is already indented to one of the precomputed alternatives.
1007
1008 This behavior is only partly implemented. If you TAB-indent a line,
1009 navigate to another line, and then navigate back, it fails to clear
1010 the last-indented variable, so it thinks you've already hit TAB once,
1011 and performs the indent. A full solution would involve getting on the
1012 point-motion hooks for the entire buffer. If we come across another
1013 use cases that requires watching point motion, I'll consider doing it.
1014
1015 If you set this variable to nil, then the TAB key will always change
1016 the indentation of the current line, if more than one alternative
1017 indentation spot exists."
1018 :type 'boolean
1019 :group 'js2-mode)
1020
1021 (defvar js2-indent-hook nil
1022 "A hook for user-defined indentation rules.
1023
1024 Functions on this hook should expect two arguments: (LIST INDEX)
1025 The LIST argument is the list of computed indentation points for
1026 the current line. INDEX is the list index of the indentation point
1027 that `js2-bounce-indent' plans to use. If INDEX is nil, then the
1028 indent function is not going to change the current line indentation.
1029
1030 If a hook function on this list returns a non-nil value, then
1031 `js2-bounce-indent' assumes the hook function has performed its own
1032 indentation, and will do nothing. If all hook functions on the list
1033 return nil, then `js2-bounce-indent' will use its computed indentation
1034 and reindent the line.
1035
1036 When hook functions on this hook list are called, the variable
1037 `js2-mode-ast' may or may not be set, depending on whether the
1038 parse tree is available. If the variable is nil, you can pass a
1039 callback to `js2-mode-wait-for-parse', and your callback will be
1040 called after the new parse tree is built. This can take some time
1041 in large files.")
1042
1043 (defface js2-warning
1044 `((((class color) (background light))
1045 (:underline "orange"))
1046 (((class color) (background dark))
1047 (:underline "orange"))
1048 (t (:underline t)))
1049 "Face for JavaScript warnings."
1050 :group 'js2-mode)
1051
1052 (defface js2-error
1053 `((((class color) (background light))
1054 (:foreground "red"))
1055 (((class color) (background dark))
1056 (:foreground "red"))
1057 (t (:foreground "red")))
1058 "Face for JavaScript errors."
1059 :group 'js2-mode)
1060
1061 (defface js2-jsdoc-tag
1062 '((t :foreground "SlateGray"))
1063 "Face used to highlight @whatever tags in jsdoc comments."
1064 :group 'js2-mode)
1065
1066 (defface js2-jsdoc-type
1067 '((t :foreground "SteelBlue"))
1068 "Face used to highlight {FooBar} types in jsdoc comments."
1069 :group 'js2-mode)
1070
1071 (defface js2-jsdoc-value
1072 '((t :foreground "PeachPuff3"))
1073 "Face used to highlight tag values in jsdoc comments."
1074 :group 'js2-mode)
1075
1076 (defface js2-function-param
1077 '((t :foreground "SeaGreen"))
1078 "Face used to highlight function parameters in javascript."
1079 :group 'js2-mode)
1080
1081 (defface js2-function-call
1082 '((t :inherit default))
1083 "Face used to highlight function name in calls."
1084 :group 'js2-mode)
1085
1086 (defface js2-instance-member
1087 '((t :foreground "DarkOrchid"))
1088 "Face used to highlight instance variables in javascript.
1089 Not currently used."
1090 :group 'js2-mode)
1091
1092 (defface js2-private-member
1093 '((t :foreground "PeachPuff3"))
1094 "Face used to highlight calls to private methods in javascript.
1095 Not currently used."
1096 :group 'js2-mode)
1097
1098 (defface js2-private-function-call
1099 '((t :foreground "goldenrod"))
1100 "Face used to highlight calls to private functions in javascript.
1101 Not currently used."
1102 :group 'js2-mode)
1103
1104 (defface js2-jsdoc-html-tag-name
1105 '((((class color) (min-colors 88) (background light))
1106 (:foreground "rosybrown"))
1107 (((class color) (min-colors 8) (background dark))
1108 (:foreground "yellow"))
1109 (((class color) (min-colors 8) (background light))
1110 (:foreground "magenta")))
1111 "Face used to highlight jsdoc html tag names"
1112 :group 'js2-mode)
1113
1114 (defface js2-jsdoc-html-tag-delimiter
1115 '((((class color) (min-colors 88) (background light))
1116 (:foreground "dark khaki"))
1117 (((class color) (min-colors 8) (background dark))
1118 (:foreground "green"))
1119 (((class color) (min-colors 8) (background light))
1120 (:foreground "green")))
1121 "Face used to highlight brackets in jsdoc html tags."
1122 :group 'js2-mode)
1123
1124 (defface js2-external-variable
1125 '((t :foreground "orange"))
1126 "Face used to highlight undeclared variable identifiers.")
1127
1128 (defcustom js2-init-hook nil
1129 "List of functions to be called after `js2-mode' or
1130 `js2-minor-mode' has initialized all variables, before parsing
1131 the buffer for the first time."
1132 :type 'hook
1133 :group 'js2-mode
1134 :version "20130608")
1135
1136 (defcustom js2-post-parse-callbacks nil
1137 "List of callback functions invoked after parsing finishes.
1138 Currently, the main use for this function is to add synthetic
1139 declarations to `js2-recorded-identifiers', which see."
1140 :type 'hook
1141 :group 'js2-mode)
1142
1143 (defcustom js2-build-imenu-callbacks nil
1144 "List of functions called during Imenu index generation.
1145 It's a good place to add additional entries to it, using
1146 `js2-record-imenu-entry'."
1147 :type 'hook
1148 :group 'js2-mode)
1149
1150 (defcustom js2-highlight-external-variables t
1151 "Non-nil to highlight undeclared variable identifiers.
1152 An undeclared variable is any variable not declared with var or let
1153 in the current scope or any lexically enclosing scope. If you use
1154 such a variable, then you are either expecting it to originate from
1155 another file, or you've got a potential bug."
1156 :type 'boolean
1157 :group 'js2-mode)
1158
1159 (defcustom js2-warn-about-unused-function-arguments nil
1160 "Non-nil to treat function arguments like declared-but-unused variables."
1161 :type 'booleanp
1162 :group 'js2-mode)
1163
1164 (defcustom js2-include-jslint-globals t
1165 "Non-nil to include the identifiers from JSLint global
1166 declaration (see http://www.jslint.com/lint.html#global) in the
1167 buffer-local externs list. See `js2-additional-externs' for more
1168 information."
1169 :type 'boolean
1170 :group 'js2-mode)
1171
1172 (defvar js2-mode-map
1173 (let ((map (make-sparse-keymap)))
1174 (define-key map [mouse-1] #'js2-mode-show-node)
1175 (define-key map (kbd "M-j") #'js2-line-break)
1176 (define-key map (kbd "C-c C-e") #'js2-mode-hide-element)
1177 (define-key map (kbd "C-c C-s") #'js2-mode-show-element)
1178 (define-key map (kbd "C-c C-a") #'js2-mode-show-all)
1179 (define-key map (kbd "C-c C-f") #'js2-mode-toggle-hide-functions)
1180 (define-key map (kbd "C-c C-t") #'js2-mode-toggle-hide-comments)
1181 (define-key map (kbd "C-c C-o") #'js2-mode-toggle-element)
1182 (define-key map (kbd "C-c C-w") #'js2-mode-toggle-warnings-and-errors)
1183 (define-key map [down-mouse-3] #'js2-down-mouse-3)
1184 (when js2-bounce-indent-p
1185 (define-key map (kbd "<backtab>") #'js2-indent-bounce-backwards))
1186
1187 (define-key map [menu-bar javascript]
1188 (cons "JavaScript" (make-sparse-keymap "JavaScript")))
1189
1190 (define-key map [menu-bar javascript customize-js2-mode]
1191 '(menu-item "Customize js2-mode" js2-mode-customize
1192 :help "Customize the behavior of this mode"))
1193
1194 (define-key map [menu-bar javascript js2-force-refresh]
1195 '(menu-item "Force buffer refresh" js2-mode-reset
1196 :help "Re-parse the buffer from scratch"))
1197
1198 (define-key map [menu-bar javascript separator-2]
1199 '("--"))
1200
1201 (define-key map [menu-bar javascript next-error]
1202 '(menu-item "Next warning or error" next-error
1203 :enabled (and js2-mode-ast
1204 (or (js2-ast-root-errors js2-mode-ast)
1205 (js2-ast-root-warnings js2-mode-ast)))
1206 :help "Move to next warning or error"))
1207
1208 (define-key map [menu-bar javascript display-errors]
1209 '(menu-item "Show errors and warnings" js2-mode-display-warnings-and-errors
1210 :visible (not js2-mode-show-parse-errors)
1211 :help "Turn on display of warnings and errors"))
1212
1213 (define-key map [menu-bar javascript hide-errors]
1214 '(menu-item "Hide errors and warnings" js2-mode-hide-warnings-and-errors
1215 :visible js2-mode-show-parse-errors
1216 :help "Turn off display of warnings and errors"))
1217
1218 (define-key map [menu-bar javascript separator-1]
1219 '("--"))
1220
1221 (define-key map [menu-bar javascript js2-toggle-function]
1222 '(menu-item "Show/collapse element" js2-mode-toggle-element
1223 :help "Hide or show function body or comment"))
1224
1225 (define-key map [menu-bar javascript show-comments]
1226 '(menu-item "Show block comments" js2-mode-toggle-hide-comments
1227 :visible js2-mode-comments-hidden
1228 :help "Expand all hidden block comments"))
1229
1230 (define-key map [menu-bar javascript hide-comments]
1231 '(menu-item "Hide block comments" js2-mode-toggle-hide-comments
1232 :visible (not js2-mode-comments-hidden)
1233 :help "Show block comments as /*...*/"))
1234
1235 (define-key map [menu-bar javascript show-all-functions]
1236 '(menu-item "Show function bodies" js2-mode-toggle-hide-functions
1237 :visible js2-mode-functions-hidden
1238 :help "Expand all hidden function bodies"))
1239
1240 (define-key map [menu-bar javascript hide-all-functions]
1241 '(menu-item "Hide function bodies" js2-mode-toggle-hide-functions
1242 :visible (not js2-mode-functions-hidden)
1243 :help "Show {...} for all top-level function bodies"))
1244
1245 map)
1246 "Keymap used in `js2-mode' buffers.")
1247
1248 (defconst js2-mode-identifier-re "[[:alpha:]_$][[:alnum:]_$]*")
1249
1250 (defvar js2-mode-//-comment-re "^\\(\\s-*\\)//.+"
1251 "Matches a //-comment line. Must be first non-whitespace on line.
1252 First match-group is the leading whitespace.")
1253
1254 (defvar js2-mode-hook nil)
1255
1256 (js2-deflocal js2-mode-ast nil "Private variable.")
1257 (js2-deflocal js2-mode-parse-timer nil "Private variable.")
1258 (js2-deflocal js2-mode-buffer-dirty-p nil "Private variable.")
1259 (js2-deflocal js2-mode-parsing nil "Private variable.")
1260 (js2-deflocal js2-mode-node-overlay nil)
1261
1262 (defvar js2-mode-show-overlay js2-mode-dev-mode-p
1263 "Debug: Non-nil to highlight AST nodes on mouse-down.")
1264
1265 (js2-deflocal js2-mode-fontifications nil "Private variable")
1266 (js2-deflocal js2-mode-deferred-properties nil "Private variable")
1267 (js2-deflocal js2-imenu-recorder nil "Private variable")
1268 (js2-deflocal js2-imenu-function-map nil "Private variable")
1269
1270 (defvar js2-paragraph-start
1271 "\\(@[[:alpha:]]+\\>\\|$\\)")
1272
1273 ;; Note that we also set a 'c-in-sws text property in html comments,
1274 ;; so that `c-forward-sws' and `c-backward-sws' work properly.
1275 (defvar js2-syntactic-ws-start
1276 "\\s \\|/[*/]\\|[\n\r]\\|\\\\[\n\r]\\|\\s!\\|<!--\\|^\\s-*-->")
1277
1278 (defvar js2-syntactic-ws-end
1279 "\\s \\|[\n\r/]\\|\\s!")
1280
1281 (defvar js2-syntactic-eol
1282 (concat "\\s *\\(/\\*[^*\n\r]*"
1283 "\\(\\*+[^*\n\r/][^*\n\r]*\\)*"
1284 "\\*+/\\s *\\)*"
1285 "\\(//\\|/\\*[^*\n\r]*"
1286 "\\(\\*+[^*\n\r/][^*\n\r]*\\)*$"
1287 "\\|\\\\$\\|$\\)")
1288 "Copied from `java-mode'. Needed for some cc-engine functions.")
1289
1290 (defvar js2-comment-prefix-regexp
1291 "//+\\|\\**")
1292
1293 (defvar js2-comment-start-skip
1294 "\\(//+\\|/\\*+\\)\\s *")
1295
1296 (defvar js2-mode-verbose-parse-p js2-mode-dev-mode-p
1297 "Non-nil to emit status messages during parsing.")
1298
1299 (defvar js2-mode-functions-hidden nil "Private variable.")
1300 (defvar js2-mode-comments-hidden nil "Private variable.")
1301
1302 (defvar js2-mode-syntax-table
1303 (let ((table (make-syntax-table)))
1304 (c-populate-syntax-table table)
1305 (modify-syntax-entry ?` "\"" table)
1306 table)
1307 "Syntax table used in `js2-mode' buffers.")
1308
1309 (defvar js2-mode-abbrev-table nil
1310 "Abbrev table in use in `js2-mode' buffers.")
1311 (define-abbrev-table 'js2-mode-abbrev-table ())
1312
1313 (defvar js2-mode-pending-parse-callbacks nil
1314 "List of functions waiting to be notified that parse is finished.")
1315
1316 (defvar js2-mode-last-indented-line -1)
1317
1318 ;;; Localizable error and warning messages
1319
1320 ;; Messages are copied from Rhino's Messages.properties.
1321 ;; Many of the Java-specific messages have been elided.
1322 ;; Add any js2-specific ones at the end, so we can keep
1323 ;; this file synced with changes to Rhino's.
1324
1325 (defvar js2-message-table
1326 (make-hash-table :test 'equal :size 250)
1327 "Contains localized messages for `js2-mode'.")
1328
1329 ;; TODO(stevey): construct this table at compile-time.
1330 (defmacro js2-msg (key &rest strings)
1331 `(puthash ,key (concat ,@strings)
1332 js2-message-table))
1333
1334 (defun js2-get-msg (msg-key)
1335 "Look up a localized message.
1336 MSG-KEY is a list of (MSG ARGS). If the message takes parameters,
1337 the correct number of ARGS must be provided."
1338 (let* ((key (if (listp msg-key) (car msg-key) msg-key))
1339 (args (if (listp msg-key) (cdr msg-key)))
1340 (msg (gethash key js2-message-table)))
1341 (if msg
1342 (apply #'format msg args)
1343 key))) ; default to showing the key
1344
1345 (js2-msg "msg.dup.parms"
1346 "Duplicate parameter name '%s'.")
1347
1348 (js2-msg "msg.too.big.jump"
1349 "Program too complex: jump offset too big.")
1350
1351 (js2-msg "msg.too.big.index"
1352 "Program too complex: internal index exceeds 64K limit.")
1353
1354 (js2-msg "msg.while.compiling.fn"
1355 "Encountered code generation error while compiling function '%s': %s")
1356
1357 (js2-msg "msg.while.compiling.script"
1358 "Encountered code generation error while compiling script: %s")
1359
1360 ;; Context
1361 (js2-msg "msg.ctor.not.found"
1362 "Constructor for '%s' not found.")
1363
1364 (js2-msg "msg.not.ctor"
1365 "'%s' is not a constructor.")
1366
1367 ;; FunctionObject
1368 (js2-msg "msg.varargs.ctor"
1369 "Method or constructor '%s' must be static "
1370 "with the signature (Context cx, Object[] args, "
1371 "Function ctorObj, boolean inNewExpr) "
1372 "to define a variable arguments constructor.")
1373
1374 (js2-msg "msg.varargs.fun"
1375 "Method '%s' must be static with the signature "
1376 "(Context cx, Scriptable thisObj, Object[] args, Function funObj) "
1377 "to define a variable arguments function.")
1378
1379 (js2-msg "msg.incompat.call"
1380 "Method '%s' called on incompatible object.")
1381
1382 (js2-msg "msg.bad.parms"
1383 "Unsupported parameter type '%s' in method '%s'.")
1384
1385 (js2-msg "msg.bad.method.return"
1386 "Unsupported return type '%s' in method '%s'.")
1387
1388 (js2-msg "msg.bad.ctor.return"
1389 "Construction of objects of type '%s' is not supported.")
1390
1391 (js2-msg "msg.no.overload"
1392 "Method '%s' occurs multiple times in class '%s'.")
1393
1394 (js2-msg "msg.method.not.found"
1395 "Method '%s' not found in '%s'.")
1396
1397 ;; IRFactory
1398
1399 (js2-msg "msg.bad.for.in.lhs"
1400 "Invalid left-hand side of for..in loop.")
1401
1402 (js2-msg "msg.mult.index"
1403 "Only one variable allowed in for..in loop.")
1404
1405 (js2-msg "msg.bad.for.in.destruct"
1406 "Left hand side of for..in loop must be an array of "
1407 "length 2 to accept key/value pair.")
1408
1409 (js2-msg "msg.cant.convert"
1410 "Can't convert to type '%s'.")
1411
1412 (js2-msg "msg.bad.assign.left"
1413 "Invalid assignment left-hand side.")
1414
1415 (js2-msg "msg.bad.decr"
1416 "Invalid decrement operand.")
1417
1418 (js2-msg "msg.bad.incr"
1419 "Invalid increment operand.")
1420
1421 (js2-msg "msg.bad.yield"
1422 "yield must be in a function.")
1423
1424 (js2-msg "msg.yield.parenthesized"
1425 "yield expression must be parenthesized.")
1426
1427 ;; NativeGlobal
1428 (js2-msg "msg.cant.call.indirect"
1429 "Function '%s' must be called directly, and not by way of a "
1430 "function of another name.")
1431
1432 (js2-msg "msg.eval.nonstring"
1433 "Calling eval() with anything other than a primitive "
1434 "string value will simply return the value. "
1435 "Is this what you intended?")
1436
1437 (js2-msg "msg.eval.nonstring.strict"
1438 "Calling eval() with anything other than a primitive "
1439 "string value is not allowed in strict mode.")
1440
1441 (js2-msg "msg.bad.destruct.op"
1442 "Invalid destructuring assignment operator")
1443
1444 ;; NativeCall
1445 (js2-msg "msg.only.from.new"
1446 "'%s' may only be invoked from a `new' expression.")
1447
1448 (js2-msg "msg.deprec.ctor"
1449 "The '%s' constructor is deprecated.")
1450
1451 ;; NativeFunction
1452 (js2-msg "msg.no.function.ref.found"
1453 "no source found to decompile function reference %s")
1454
1455 (js2-msg "msg.arg.isnt.array"
1456 "second argument to Function.prototype.apply must be an array")
1457
1458 ;; NativeGlobal
1459 (js2-msg "msg.bad.esc.mask"
1460 "invalid string escape mask")
1461
1462 ;; NativeRegExp
1463 (js2-msg "msg.bad.quant"
1464 "Invalid quantifier %s")
1465
1466 (js2-msg "msg.overlarge.backref"
1467 "Overly large back reference %s")
1468
1469 (js2-msg "msg.overlarge.min"
1470 "Overly large minimum %s")
1471
1472 (js2-msg "msg.overlarge.max"
1473 "Overly large maximum %s")
1474
1475 (js2-msg "msg.zero.quant"
1476 "Zero quantifier %s")
1477
1478 (js2-msg "msg.max.lt.min"
1479 "Maximum %s less than minimum")
1480
1481 (js2-msg "msg.unterm.quant"
1482 "Unterminated quantifier %s")
1483
1484 (js2-msg "msg.unterm.paren"
1485 "Unterminated parenthetical %s")
1486
1487 (js2-msg "msg.unterm.class"
1488 "Unterminated character class %s")
1489
1490 (js2-msg "msg.bad.range"
1491 "Invalid range in character class.")
1492
1493 (js2-msg "msg.trail.backslash"
1494 "Trailing \\ in regular expression.")
1495
1496 (js2-msg "msg.re.unmatched.right.paren"
1497 "unmatched ) in regular expression.")
1498
1499 (js2-msg "msg.no.regexp"
1500 "Regular expressions are not available.")
1501
1502 (js2-msg "msg.bad.backref"
1503 "back-reference exceeds number of capturing parentheses.")
1504
1505 (js2-msg "msg.bad.regexp.compile"
1506 "Only one argument may be specified if the first "
1507 "argument to RegExp.prototype.compile is a RegExp object.")
1508
1509 ;; Parser
1510 (js2-msg "msg.got.syntax.errors"
1511 "Compilation produced %s syntax errors.")
1512
1513 (js2-msg "msg.var.redecl"
1514 "TypeError: redeclaration of var %s.")
1515
1516 (js2-msg "msg.const.redecl"
1517 "TypeError: redeclaration of const %s.")
1518
1519 (js2-msg "msg.let.redecl"
1520 "TypeError: redeclaration of variable %s.")
1521
1522 (js2-msg "msg.parm.redecl"
1523 "TypeError: redeclaration of formal parameter %s.")
1524
1525 (js2-msg "msg.fn.redecl"
1526 "TypeError: redeclaration of function %s.")
1527
1528 (js2-msg "msg.let.decl.not.in.block"
1529 "SyntaxError: let declaration not directly within block")
1530
1531 (js2-msg "msg.mod.import.decl.at.top.level"
1532 "SyntaxError: import declarations may only appear at the top level")
1533
1534 (js2-msg "msg.mod.as.after.reserved.word"
1535 "SyntaxError: missing keyword 'as' after reserved word %s")
1536
1537 (js2-msg "msg.mod.rc.after.import.spec.list"
1538 "SyntaxError: missing '}' after module specifier list")
1539
1540 (js2-msg "msg.mod.from.after.import.spec.set"
1541 "SyntaxError: missing keyword 'from' after import specifier set")
1542
1543 (js2-msg "msg.mod.declaration.after.import"
1544 "SyntaxError: missing declaration after 'import' keyword")
1545
1546 (js2-msg "msg.mod.spec.after.from"
1547 "SyntaxError: missing module specifier after 'from' keyword")
1548
1549 (js2-msg "msg.mod.export.decl.at.top.level"
1550 "SyntaxError: export declarations may only appear at top level")
1551
1552 (js2-msg "msg.mod.rc.after.export.spec.list"
1553 "SyntaxError: missing '}' after export specifier list")
1554
1555 ;; NodeTransformer
1556 (js2-msg "msg.dup.label"
1557 "duplicated label")
1558
1559 (js2-msg "msg.undef.label"
1560 "undefined label")
1561
1562 (js2-msg "msg.bad.break"
1563 "unlabelled break must be inside loop or switch")
1564
1565 (js2-msg "msg.continue.outside"
1566 "continue must be inside loop")
1567
1568 (js2-msg "msg.continue.nonloop"
1569 "continue can only use labels of iteration statements")
1570
1571 (js2-msg "msg.bad.throw.eol"
1572 "Line terminator is not allowed between the throw "
1573 "keyword and throw expression.")
1574
1575 (js2-msg "msg.unnamed.function.stmt" ; added by js2-mode
1576 "function statement requires a name")
1577
1578 (js2-msg "msg.no.paren.parms"
1579 "missing ( before function parameters.")
1580
1581 (js2-msg "msg.no.parm"
1582 "missing formal parameter")
1583
1584 (js2-msg "msg.no.paren.after.parms"
1585 "missing ) after formal parameters")
1586
1587 (js2-msg "msg.no.default.after.default.param" ; added by js2-mode
1588 "parameter without default follows parameter with default")
1589
1590 (js2-msg "msg.param.after.rest" ; added by js2-mode
1591 "parameter after rest parameter")
1592
1593 (js2-msg "msg.bad.arrow.args" ; added by js2-mode
1594 "invalid arrow-function arguments (parentheses around the arrow-function may help)")
1595
1596 (js2-msg "msg.no.brace.body"
1597 "missing '{' before function body")
1598
1599 (js2-msg "msg.no.brace.after.body"
1600 "missing } after function body")
1601
1602 (js2-msg "msg.no.paren.cond"
1603 "missing ( before condition")
1604
1605 (js2-msg "msg.no.paren.after.cond"
1606 "missing ) after condition")
1607
1608 (js2-msg "msg.no.semi.stmt"
1609 "missing ; before statement")
1610
1611 (js2-msg "msg.missing.semi"
1612 "missing ; after statement")
1613
1614 (js2-msg "msg.no.name.after.dot"
1615 "missing name after . operator")
1616
1617 (js2-msg "msg.no.name.after.coloncolon"
1618 "missing name after :: operator")
1619
1620 (js2-msg "msg.no.name.after.dotdot"
1621 "missing name after .. operator")
1622
1623 (js2-msg "msg.no.name.after.xmlAttr"
1624 "missing name after .@")
1625
1626 (js2-msg "msg.no.bracket.index"
1627 "missing ] in index expression")
1628
1629 (js2-msg "msg.no.paren.switch"
1630 "missing ( before switch expression")
1631
1632 (js2-msg "msg.no.paren.after.switch"
1633 "missing ) after switch expression")
1634
1635 (js2-msg "msg.no.brace.switch"
1636 "missing '{' before switch body")
1637
1638 (js2-msg "msg.bad.switch"
1639 "invalid switch statement")
1640
1641 (js2-msg "msg.no.colon.case"
1642 "missing : after case expression")
1643
1644 (js2-msg "msg.double.switch.default"
1645 "double default label in the switch statement")
1646
1647 (js2-msg "msg.no.while.do"
1648 "missing while after do-loop body")
1649
1650 (js2-msg "msg.no.paren.for"
1651 "missing ( after for")
1652
1653 (js2-msg "msg.no.semi.for"
1654 "missing ; after for-loop initializer")
1655
1656 (js2-msg "msg.no.semi.for.cond"
1657 "missing ; after for-loop condition")
1658
1659 (js2-msg "msg.in.after.for.name"
1660 "missing in or of after for")
1661
1662 (js2-msg "msg.no.paren.for.ctrl"
1663 "missing ) after for-loop control")
1664
1665 (js2-msg "msg.no.paren.with"
1666 "missing ( before with-statement object")
1667
1668 (js2-msg "msg.no.paren.after.with"
1669 "missing ) after with-statement object")
1670
1671 (js2-msg "msg.no.paren.after.let"
1672 "missing ( after let")
1673
1674 (js2-msg "msg.no.paren.let"
1675 "missing ) after variable list")
1676
1677 (js2-msg "msg.no.curly.let"
1678 "missing } after let statement")
1679
1680 (js2-msg "msg.bad.return"
1681 "invalid return")
1682
1683 (js2-msg "msg.no.brace.block"
1684 "missing } in compound statement")
1685
1686 (js2-msg "msg.bad.label"
1687 "invalid label")
1688
1689 (js2-msg "msg.bad.var"
1690 "missing variable name")
1691
1692 (js2-msg "msg.bad.var.init"
1693 "invalid variable initialization")
1694
1695 (js2-msg "msg.no.colon.cond"
1696 "missing : in conditional expression")
1697
1698 (js2-msg "msg.no.paren.arg"
1699 "missing ) after argument list")
1700
1701 (js2-msg "msg.no.bracket.arg"
1702 "missing ] after element list")
1703
1704 (js2-msg "msg.bad.prop"
1705 "invalid property id")
1706
1707 (js2-msg "msg.no.colon.prop"
1708 "missing : after property id")
1709
1710 (js2-msg "msg.no.brace.prop"
1711 "missing } after property list")
1712
1713 (js2-msg "msg.no.paren"
1714 "missing ) in parenthetical")
1715
1716 (js2-msg "msg.reserved.id"
1717 "'%s' is a reserved identifier")
1718
1719 (js2-msg "msg.no.paren.catch"
1720 "missing ( before catch-block condition")
1721
1722 (js2-msg "msg.bad.catchcond"
1723 "invalid catch block condition")
1724
1725 (js2-msg "msg.catch.unreachable"
1726 "any catch clauses following an unqualified catch are unreachable")
1727
1728 (js2-msg "msg.no.brace.try"
1729 "missing '{' before try block")
1730
1731 (js2-msg "msg.no.brace.catchblock"
1732 "missing '{' before catch-block body")
1733
1734 (js2-msg "msg.try.no.catchfinally"
1735 "'try' without 'catch' or 'finally'")
1736
1737 (js2-msg "msg.no.return.value"
1738 "function %s does not always return a value")
1739
1740 (js2-msg "msg.anon.no.return.value"
1741 "anonymous function does not always return a value")
1742
1743 (js2-msg "msg.return.inconsistent"
1744 "return statement is inconsistent with previous usage")
1745
1746 (js2-msg "msg.generator.returns"
1747 "TypeError: legacy generator function '%s' returns a value")
1748
1749 (js2-msg "msg.anon.generator.returns"
1750 "TypeError: anonymous legacy generator function returns a value")
1751
1752 (js2-msg "msg.syntax"
1753 "syntax error")
1754
1755 (js2-msg "msg.unexpected.eof"
1756 "Unexpected end of file")
1757
1758 (js2-msg "msg.XML.bad.form"
1759 "illegally formed XML syntax")
1760
1761 (js2-msg "msg.XML.not.available"
1762 "XML runtime not available")
1763
1764 (js2-msg "msg.too.deep.parser.recursion"
1765 "Too deep recursion while parsing")
1766
1767 (js2-msg "msg.no.side.effects"
1768 "Code has no side effects")
1769
1770 (js2-msg "msg.extra.trailing.comma"
1771 "Trailing comma is not supported in some browsers")
1772
1773 (js2-msg "msg.array.trailing.comma"
1774 "Trailing comma yields different behavior across browsers")
1775
1776 (js2-msg "msg.equal.as.assign"
1777 (concat "Test for equality (==) mistyped as assignment (=)?"
1778 " (parenthesize to suppress warning)"))
1779
1780 (js2-msg "msg.var.hides.arg"
1781 "Variable %s hides argument")
1782
1783 (js2-msg "msg.destruct.assign.no.init"
1784 "Missing = in destructuring declaration")
1785
1786 ;; ScriptRuntime
1787 (js2-msg "msg.no.properties"
1788 "%s has no properties.")
1789
1790 (js2-msg "msg.invalid.iterator"
1791 "Invalid iterator value")
1792
1793 (js2-msg "msg.iterator.primitive"
1794 "__iterator__ returned a primitive value")
1795
1796 (js2-msg "msg.assn.create.strict"
1797 "Assignment to undeclared variable %s")
1798
1799 (js2-msg "msg.undeclared.variable" ; added by js2-mode
1800 "Undeclared variable or function '%s'")
1801
1802 (js2-msg "msg.unused.variable" ; added by js2-mode
1803 "Unused variable or function '%s'")
1804
1805 (js2-msg "msg.uninitialized.variable" ; added by js2-mode
1806 "Variable '%s' referenced but never initialized")
1807
1808 (js2-msg "msg.ref.undefined.prop"
1809 "Reference to undefined property '%s'")
1810
1811 (js2-msg "msg.prop.not.found"
1812 "Property %s not found.")
1813
1814 (js2-msg "msg.invalid.type"
1815 "Invalid JavaScript value of type %s")
1816
1817 (js2-msg "msg.primitive.expected"
1818 "Primitive type expected (had %s instead)")
1819
1820 (js2-msg "msg.namespace.expected"
1821 "Namespace object expected to left of :: (found %s instead)")
1822
1823 (js2-msg "msg.null.to.object"
1824 "Cannot convert null to an object.")
1825
1826 (js2-msg "msg.undef.to.object"
1827 "Cannot convert undefined to an object.")
1828
1829 (js2-msg "msg.cyclic.value"
1830 "Cyclic %s value not allowed.")
1831
1832 (js2-msg "msg.is.not.defined"
1833 "'%s' is not defined.")
1834
1835 (js2-msg "msg.undef.prop.read"
1836 "Cannot read property '%s' from %s")
1837
1838 (js2-msg "msg.undef.prop.write"
1839 "Cannot set property '%s' of %s to '%s'")
1840
1841 (js2-msg "msg.undef.prop.delete"
1842 "Cannot delete property '%s' of %s")
1843
1844 (js2-msg "msg.undef.method.call"
1845 "Cannot call method '%s' of %s")
1846
1847 (js2-msg "msg.undef.with"
1848 "Cannot apply 'with' to %s")
1849
1850 (js2-msg "msg.isnt.function"
1851 "%s is not a function, it is %s.")
1852
1853 (js2-msg "msg.isnt.function.in"
1854 "Cannot call property %s in object %s. "
1855 "It is not a function, it is '%s'.")
1856
1857 (js2-msg "msg.function.not.found"
1858 "Cannot find function %s.")
1859
1860 (js2-msg "msg.function.not.found.in"
1861 "Cannot find function %s in object %s.")
1862
1863 (js2-msg "msg.isnt.xml.object"
1864 "%s is not an xml object.")
1865
1866 (js2-msg "msg.no.ref.to.get"
1867 "%s is not a reference to read reference value.")
1868
1869 (js2-msg "msg.no.ref.to.set"
1870 "%s is not a reference to set reference value to %s.")
1871
1872 (js2-msg "msg.no.ref.from.function"
1873 "Function %s can not be used as the left-hand "
1874 "side of assignment or as an operand of ++ or -- operator.")
1875
1876 (js2-msg "msg.bad.default.value"
1877 "Object's getDefaultValue() method returned an object.")
1878
1879 (js2-msg "msg.instanceof.not.object"
1880 "Can't use instanceof on a non-object.")
1881
1882 (js2-msg "msg.instanceof.bad.prototype"
1883 "'prototype' property of %s is not an object.")
1884
1885 (js2-msg "msg.bad.radix"
1886 "illegal radix %s.")
1887
1888 ;; ScriptableObject
1889 (js2-msg "msg.default.value"
1890 "Cannot find default value for object.")
1891
1892 (js2-msg "msg.zero.arg.ctor"
1893 "Cannot load class '%s' which has no zero-parameter constructor.")
1894
1895 (js2-msg "msg.ctor.multiple.parms"
1896 "Can't define constructor or class %s since more than "
1897 "one constructor has multiple parameters.")
1898
1899 (js2-msg "msg.extend.scriptable"
1900 "%s must extend ScriptableObject in order to define property %s.")
1901
1902 (js2-msg "msg.bad.getter.parms"
1903 "In order to define a property, getter %s must have zero "
1904 "parameters or a single ScriptableObject parameter.")
1905
1906 (js2-msg "msg.obj.getter.parms"
1907 "Expected static or delegated getter %s to take "
1908 "a ScriptableObject parameter.")
1909
1910 (js2-msg "msg.getter.static"
1911 "Getter and setter must both be static or neither be static.")
1912
1913 (js2-msg "msg.setter.return"
1914 "Setter must have void return type: %s")
1915
1916 (js2-msg "msg.setter2.parms"
1917 "Two-parameter setter must take a ScriptableObject as "
1918 "its first parameter.")
1919
1920 (js2-msg "msg.setter1.parms"
1921 "Expected single parameter setter for %s")
1922
1923 (js2-msg "msg.setter2.expected"
1924 "Expected static or delegated setter %s to take two parameters.")
1925
1926 (js2-msg "msg.setter.parms"
1927 "Expected either one or two parameters for setter.")
1928
1929 (js2-msg "msg.setter.bad.type"
1930 "Unsupported parameter type '%s' in setter '%s'.")
1931
1932 (js2-msg "msg.add.sealed"
1933 "Cannot add a property to a sealed object: %s.")
1934
1935 (js2-msg "msg.remove.sealed"
1936 "Cannot remove a property from a sealed object: %s.")
1937
1938 (js2-msg "msg.modify.sealed"
1939 "Cannot modify a property of a sealed object: %s.")
1940
1941 (js2-msg "msg.modify.readonly"
1942 "Cannot modify readonly property: %s.")
1943
1944 ;; TokenStream
1945 (js2-msg "msg.missing.exponent"
1946 "missing exponent")
1947
1948 (js2-msg "msg.caught.nfe"
1949 "number format error")
1950
1951 (js2-msg "msg.unterminated.string.lit"
1952 "unterminated string literal")
1953
1954 (js2-msg "msg.unterminated.comment"
1955 "unterminated comment")
1956
1957 (js2-msg "msg.unterminated.re.lit"
1958 "unterminated regular expression literal")
1959
1960 (js2-msg "msg.invalid.re.flag"
1961 "invalid flag after regular expression")
1962
1963 (js2-msg "msg.no.re.input.for"
1964 "no input for %s")
1965
1966 (js2-msg "msg.illegal.character"
1967 "illegal character")
1968
1969 (js2-msg "msg.invalid.escape"
1970 "invalid Unicode escape sequence")
1971
1972 (js2-msg "msg.bad.namespace"
1973 "not a valid default namespace statement. "
1974 "Syntax is: default xml namespace = EXPRESSION;")
1975
1976 ;; TokensStream warnings
1977 (js2-msg "msg.bad.octal.literal"
1978 "illegal octal literal digit %s; "
1979 "interpreting it as a decimal digit")
1980
1981 (js2-msg "msg.missing.hex.digits"
1982 "missing hexadecimal digits after '0x'")
1983
1984 (js2-msg "msg.missing.binary.digits"
1985 "missing binary digits after '0b'")
1986
1987 (js2-msg "msg.missing.octal.digits"
1988 "missing octal digits after '0o'")
1989
1990 (js2-msg "msg.script.is.not.constructor"
1991 "Script objects are not constructors.")
1992
1993 ;; Arrays
1994 (js2-msg "msg.arraylength.bad"
1995 "Inappropriate array length.")
1996
1997 ;; Arrays
1998 (js2-msg "msg.arraylength.too.big"
1999 "Array length %s exceeds supported capacity limit.")
2000
2001 ;; URI
2002 (js2-msg "msg.bad.uri"
2003 "Malformed URI sequence.")
2004
2005 ;; Number
2006 (js2-msg "msg.bad.precision"
2007 "Precision %s out of range.")
2008
2009 ;; NativeGenerator
2010 (js2-msg "msg.send.newborn"
2011 "Attempt to send value to newborn generator")
2012
2013 (js2-msg "msg.already.exec.gen"
2014 "Already executing generator")
2015
2016 (js2-msg "msg.StopIteration.invalid"
2017 "StopIteration may not be changed to an arbitrary object.")
2018
2019 ;; Interpreter
2020 (js2-msg "msg.yield.closing"
2021 "Yield from closing generator")
2022
2023 ;; Classes
2024 (js2-msg "msg.unnamed.class.stmt" ; added by js2-mode
2025 "class statement requires a name")
2026
2027 (js2-msg "msg.class.unexpected.comma" ; added by js2-mode
2028 "unexpected ',' between class properties")
2029
2030 (js2-msg "msg.unexpected.static" ; added by js2-mode
2031 "unexpected 'static'")
2032
2033 (js2-msg "msg.missing.extends" ; added by js2-mode
2034 "name is required after extends")
2035
2036 (js2-msg "msg.no.brace.class" ; added by js2-mode
2037 "missing '{' before class body")
2038
2039 (js2-msg "msg.missing.computed.rb" ; added by js2-mode
2040 "missing ']' after computed property expression")
2041
2042 ;;; Tokens Buffer
2043
2044 (defconst js2-ti-max-lookahead 2)
2045 (defconst js2-ti-ntokens (1+ js2-ti-max-lookahead))
2046
2047 (defun js2-new-token (offset)
2048 (let ((token (make-js2-token (+ offset js2-ts-cursor))))
2049 (setq js2-ti-tokens-cursor (mod (1+ js2-ti-tokens-cursor) js2-ti-ntokens))
2050 (aset js2-ti-tokens js2-ti-tokens-cursor token)
2051 token))
2052
2053 (defsubst js2-current-token ()
2054 (aref js2-ti-tokens js2-ti-tokens-cursor))
2055
2056 (defsubst js2-current-token-string ()
2057 (js2-token-string (js2-current-token)))
2058
2059 (defsubst js2-current-token-type ()
2060 (js2-token-type (js2-current-token)))
2061
2062 (defsubst js2-current-token-beg ()
2063 (js2-token-beg (js2-current-token)))
2064
2065 (defsubst js2-current-token-end ()
2066 (js2-token-end (js2-current-token)))
2067
2068 (defun js2-current-token-len ()
2069 (let ((token (js2-current-token)))
2070 (- (js2-token-end token)
2071 (js2-token-beg token))))
2072
2073 (defun js2-ts-seek (state)
2074 (setq js2-ts-lineno (js2-ts-state-lineno state)
2075 js2-ts-cursor (js2-ts-state-cursor state)
2076 js2-ti-tokens (js2-ts-state-tokens state)
2077 js2-ti-tokens-cursor (js2-ts-state-tokens-cursor state)
2078 js2-ti-lookahead (js2-ts-state-lookahead state)))
2079
2080 ;;; Utilities
2081
2082 (defun js2-delete-if (predicate list)
2083 "Remove all items satisfying PREDICATE in LIST."
2084 (cl-loop for item in list
2085 if (not (funcall predicate item))
2086 collect item))
2087
2088 (defun js2-position (element list)
2089 "Find 0-indexed position of ELEMENT in LIST comparing with `eq'.
2090 Returns nil if element is not found in the list."
2091 (let ((count 0)
2092 found)
2093 (while (and list (not found))
2094 (if (eq element (car list))
2095 (setq found t)
2096 (setq count (1+ count)
2097 list (cdr list))))
2098 (if found count)))
2099
2100 (defun js2-find-if (predicate list)
2101 "Find first item satisfying PREDICATE in LIST."
2102 (let (result)
2103 (while (and list (not result))
2104 (if (funcall predicate (car list))
2105 (setq result (car list)))
2106 (setq list (cdr list)))
2107 result))
2108
2109 (defmacro js2-time (form)
2110 "Evaluate FORM, discard result, and return elapsed time in sec."
2111 (declare (debug t))
2112 (let ((beg (make-symbol "--js2-time-beg--")))
2113 `(let ((,beg (current-time)))
2114 ,form
2115 (/ (truncate (* (- (float-time (current-time))
2116 (float-time ,beg))
2117 10000))
2118 10000.0))))
2119
2120 (defsubst js2-same-line (pos)
2121 "Return t if POS is on the same line as current point."
2122 (and (>= pos (point-at-bol))
2123 (<= pos (point-at-eol))))
2124
2125 (defun js2-code-bug ()
2126 "Signal an error when we encounter an unexpected code path."
2127 (error "failed assertion"))
2128
2129 (defsubst js2-record-text-property (beg end prop value)
2130 "Record a text property to set when parsing finishes."
2131 (push (list beg end prop value) js2-mode-deferred-properties))
2132
2133 ;; I'd like to associate errors with nodes, but for now the
2134 ;; easiest thing to do is get the context info from the last token.
2135 (defun js2-record-parse-error (msg &optional arg pos len)
2136 (push (list (list msg arg)
2137 (or pos (js2-current-token-beg))
2138 (or len (js2-current-token-len)))
2139 js2-parsed-errors))
2140
2141 (defun js2-report-error (msg &optional msg-arg pos len)
2142 "Signal a syntax error or record a parse error."
2143 (if js2-recover-from-parse-errors
2144 (js2-record-parse-error msg msg-arg pos len)
2145 (signal 'js2-syntax-error
2146 (list msg
2147 js2-ts-lineno
2148 (save-excursion
2149 (goto-char js2-ts-cursor)
2150 (current-column))
2151 js2-ts-hit-eof))))
2152
2153 (defun js2-report-warning (msg &optional msg-arg pos len face)
2154 (if js2-compiler-report-warning-as-error
2155 (js2-report-error msg msg-arg pos len)
2156 (push (list (list msg msg-arg)
2157 (or pos (js2-current-token-beg))
2158 (or len (js2-current-token-len))
2159 face)
2160 js2-parsed-warnings)))
2161
2162 (defun js2-add-strict-warning (msg-id &optional msg-arg beg end)
2163 (if js2-compiler-strict-mode
2164 (js2-report-warning msg-id msg-arg beg
2165 (and beg end (- end beg)))))
2166
2167 (put 'js2-syntax-error 'error-conditions
2168 '(error syntax-error js2-syntax-error))
2169 (put 'js2-syntax-error 'error-message "Syntax error")
2170
2171 (put 'js2-parse-error 'error-conditions
2172 '(error parse-error js2-parse-error))
2173 (put 'js2-parse-error 'error-message "Parse error")
2174
2175 (defmacro js2-clear-flag (flags flag)
2176 `(setq ,flags (logand ,flags (lognot ,flag))))
2177
2178 (defmacro js2-set-flag (flags flag)
2179 "Logical-or FLAG into FLAGS."
2180 `(setq ,flags (logior ,flags ,flag)))
2181
2182 (defsubst js2-flag-set-p (flags flag)
2183 (/= 0 (logand flags flag)))
2184
2185 (defsubst js2-flag-not-set-p (flags flag)
2186 (zerop (logand flags flag)))
2187
2188 (defmacro js2-with-underscore-as-word-syntax (&rest body)
2189 "Evaluate BODY with the _ character set to be word-syntax."
2190 (declare (indent 0) (debug t))
2191 (let ((old-syntax (make-symbol "old-syntax")))
2192 `(let ((,old-syntax (string (char-syntax ?_))))
2193 (unwind-protect
2194 (progn
2195 (modify-syntax-entry ?_ "w" js2-mode-syntax-table)
2196 ,@body)
2197 (modify-syntax-entry ?_ ,old-syntax js2-mode-syntax-table)))))
2198
2199 ;;; AST struct and function definitions
2200
2201 ;; flags for ast node property 'member-type (used for e4x operators)
2202 (defvar js2-property-flag #x1 "Property access: element is valid name.")
2203 (defvar js2-attribute-flag #x2 "x.@y or x..@y.")
2204 (defvar js2-descendants-flag #x4 "x..y or x..@i.")
2205
2206 (defsubst js2-relpos (pos anchor)
2207 "Convert POS to be relative to ANCHOR.
2208 If POS is nil, returns nil."
2209 (and pos (- pos anchor)))
2210
2211 (defun js2-make-pad (indent)
2212 (if (zerop indent)
2213 ""
2214 (make-string (* indent js2-basic-offset) ? )))
2215
2216 (defun js2-visit-ast (node callback)
2217 "Visit every node in ast NODE with visitor CALLBACK.
2218
2219 CALLBACK is a function that takes two arguments: (NODE END-P). It is
2220 called twice: once to visit the node, and again after all the node's
2221 children have been processed. The END-P argument is nil on the first
2222 call and non-nil on the second call. The return value of the callback
2223 affects the traversal: if non-nil, the children of NODE are processed.
2224 If the callback returns nil, or if the node has no children, then the
2225 callback is called immediately with a non-nil END-P argument.
2226
2227 The node traversal is approximately lexical-order, although there
2228 are currently no guarantees around this."
2229 (when node
2230 (let ((vfunc (get (aref node 0) 'js2-visitor)))
2231 ;; visit the node
2232 (when (funcall callback node nil)
2233 ;; visit the kids
2234 (cond
2235 ((eq vfunc 'js2-visit-none)
2236 nil) ; don't even bother calling it
2237 ;; Each AST node type has to define a `js2-visitor' function
2238 ;; that takes a node and a callback, and calls `js2-visit-ast'
2239 ;; on each child of the node.
2240 (vfunc
2241 (funcall vfunc node callback))
2242 (t
2243 (error "%s does not define a visitor-traversal function"
2244 (aref node 0)))))
2245 ;; call the end-visit
2246 (funcall callback node t))))
2247
2248 (cl-defstruct (js2-node
2249 (:constructor nil)) ; abstract
2250 "Base AST node type."
2251 (type -1) ; token type
2252 (pos -1) ; start position of this AST node in parsed input
2253 (len 1) ; num characters spanned by the node
2254 props ; optional node property list (an alist)
2255 parent) ; link to parent node; null for root
2256
2257 (defsubst js2-node-get-prop (node prop &optional default)
2258 (or (cadr (assoc prop (js2-node-props node))) default))
2259
2260 (defsubst js2-node-set-prop (node prop value)
2261 (setf (js2-node-props node)
2262 (cons (list prop value) (js2-node-props node))))
2263
2264 (defun js2-fixup-starts (n nodes)
2265 "Adjust the start positions of NODES to be relative to N.
2266 Any node in the list may be nil, for convenience."
2267 (dolist (node nodes)
2268 (when node
2269 (setf (js2-node-pos node) (- (js2-node-pos node)
2270 (js2-node-pos n))))))
2271
2272 (defun js2-node-add-children (parent &rest nodes)
2273 "Set parent node of NODES to PARENT, and return PARENT.
2274 Does nothing if we're not recording parent links.
2275 If any given node in NODES is nil, doesn't record that link."
2276 (js2-fixup-starts parent nodes)
2277 (dolist (node nodes)
2278 (and node
2279 (setf (js2-node-parent node) parent))))
2280
2281 ;; Non-recursive since it's called a frightening number of times.
2282 (defun js2-node-abs-pos (n)
2283 (let ((pos (js2-node-pos n)))
2284 (while (setq n (js2-node-parent n))
2285 (setq pos (+ pos (js2-node-pos n))))
2286 pos))
2287
2288 (defsubst js2-node-abs-end (n)
2289 "Return absolute buffer position of end of N."
2290 (+ (js2-node-abs-pos n) (js2-node-len n)))
2291
2292 ;; It's important to make sure block nodes have a Lisp list for the
2293 ;; child nodes, to limit printing recursion depth in an AST that
2294 ;; otherwise consists of defstruct vectors. Emacs will crash printing
2295 ;; a sufficiently large vector tree.
2296
2297 (cl-defstruct (js2-block-node
2298 (:include js2-node)
2299 (:constructor nil)
2300 (:constructor make-js2-block-node (&key (type js2-BLOCK)
2301 (pos (js2-current-token-beg))
2302 len
2303 props
2304 kids)))
2305 "A block of statements."
2306 kids) ; a Lisp list of the child statement nodes
2307
2308 (put 'cl-struct-js2-block-node 'js2-visitor 'js2-visit-block)
2309 (put 'cl-struct-js2-block-node 'js2-printer 'js2-print-block)
2310
2311 (defun js2-visit-block (ast callback)
2312 "Visit the `js2-block-node' children of AST."
2313 (dolist (kid (js2-block-node-kids ast))
2314 (js2-visit-ast kid callback)))
2315
2316 (defun js2-print-block (n i)
2317 (let ((pad (js2-make-pad i)))
2318 (insert pad "{\n")
2319 (dolist (kid (js2-block-node-kids n))
2320 (js2-print-ast kid (1+ i)))
2321 (insert pad "}")))
2322
2323 (cl-defstruct (js2-scope
2324 (:include js2-block-node)
2325 (:constructor nil)
2326 (:constructor make-js2-scope (&key (type js2-BLOCK)
2327 (pos (js2-current-token-beg))
2328 len
2329 kids)))
2330 ;; The symbol-table is a LinkedHashMap<String,Symbol> in Rhino.
2331 ;; I don't have one of those handy, so I'll use an alist for now.
2332 ;; It's as fast as an emacs hashtable for up to about 50 elements,
2333 ;; and is much lighter-weight to construct (both CPU and mem).
2334 ;; The keys are interned strings (symbols) for faster lookup.
2335 ;; Should switch to hybrid alist/hashtable eventually.
2336 symbol-table ; an alist of (symbol . js2-symbol)
2337 parent-scope ; a `js2-scope'
2338 top) ; top-level `js2-scope' (script/function)
2339
2340 (put 'cl-struct-js2-scope 'js2-visitor 'js2-visit-block)
2341 (put 'cl-struct-js2-scope 'js2-printer 'js2-print-none)
2342
2343 (defun js2-node-get-enclosing-scope (node)
2344 "Return the innermost `js2-scope' node surrounding NODE.
2345 Returns nil if there is no enclosing scope node."
2346 (while (and (setq node (js2-node-parent node))
2347 (not (js2-scope-p node))))
2348 node)
2349
2350 (defun js2-get-defining-scope (scope name &optional point)
2351 "Search up scope chain from SCOPE looking for NAME, a string or symbol.
2352 Returns `js2-scope' in which NAME is defined, or nil if not found.
2353
2354 If POINT is non-nil, and if the found declaration type is
2355 `js2-LET', also check that the declaration node is before POINT."
2356 (let ((sym (if (symbolp name)
2357 name
2358 (intern name)))
2359 result
2360 (continue t))
2361 (while (and scope continue)
2362 (if (or
2363 (let ((entry (cdr (assq sym (js2-scope-symbol-table scope)))))
2364 (and entry
2365 (or (not point)
2366 (not (eq js2-LET (js2-symbol-decl-type entry)))
2367 (>= point
2368 (js2-node-abs-pos (js2-symbol-ast-node entry))))))
2369 (and (eq sym 'arguments)
2370 (js2-function-node-p scope)))
2371 (setq continue nil
2372 result scope)
2373 (setq scope (js2-scope-parent-scope scope))))
2374 result))
2375
2376 (defun js2-scope-get-symbol (scope name)
2377 "Return symbol table entry for NAME in SCOPE.
2378 NAME can be a string or symbol. Returns a `js2-symbol' or nil if not found."
2379 (and (js2-scope-symbol-table scope)
2380 (cdr (assq (if (symbolp name)
2381 name
2382 (intern name))
2383 (js2-scope-symbol-table scope)))))
2384
2385 (defun js2-scope-put-symbol (scope name symbol)
2386 "Enter SYMBOL into symbol-table for SCOPE under NAME.
2387 NAME can be a Lisp symbol or string. SYMBOL is a `js2-symbol'."
2388 (let* ((table (js2-scope-symbol-table scope))
2389 (sym (if (symbolp name) name (intern name)))
2390 (entry (assq sym table)))
2391 (if entry
2392 (setcdr entry symbol)
2393 (push (cons sym symbol)
2394 (js2-scope-symbol-table scope)))))
2395
2396 (cl-defstruct (js2-symbol
2397 (:constructor nil)
2398 (:constructor make-js2-symbol (decl-type name &optional ast-node)))
2399 "A symbol table entry."
2400 ;; One of js2-FUNCTION, js2-LP (for parameters), js2-VAR,
2401 ;; js2-LET, or js2-CONST
2402 decl-type
2403 name ; string
2404 ast-node) ; a `js2-node'
2405
2406 (cl-defstruct (js2-error-node
2407 (:include js2-node)
2408 (:constructor nil) ; silence emacs21 byte-compiler
2409 (:constructor make-js2-error-node (&key (type js2-ERROR)
2410 (pos (js2-current-token-beg))
2411 len)))
2412 "AST node representing a parse error.")
2413
2414 (put 'cl-struct-js2-error-node 'js2-visitor 'js2-visit-none)
2415 (put 'cl-struct-js2-error-node 'js2-printer 'js2-print-none)
2416
2417 (cl-defstruct (js2-script-node
2418 (:include js2-scope)
2419 (:constructor nil)
2420 (:constructor make-js2-script-node (&key (type js2-SCRIPT)
2421 (pos (js2-current-token-beg))
2422 len
2423 ;; FIXME: What are those?
2424 var-decls
2425 fun-decls)))
2426 functions ; Lisp list of nested functions
2427 regexps ; Lisp list of (string . flags)
2428 symbols ; alist (every symbol gets unique index)
2429 (param-count 0)
2430 var-names ; vector of string names
2431 consts ; bool-vector matching var-decls
2432 (temp-number 0)) ; for generating temp variables
2433
2434 (put 'cl-struct-js2-script-node 'js2-visitor 'js2-visit-block)
2435 (put 'cl-struct-js2-script-node 'js2-printer 'js2-print-script)
2436
2437 (defun js2-print-script (node indent)
2438 (dolist (kid (js2-block-node-kids node))
2439 (js2-print-ast kid indent)))
2440
2441 (cl-defstruct (js2-ast-root
2442 (:include js2-script-node)
2443 (:constructor nil)
2444 (:constructor make-js2-ast-root (&key (type js2-SCRIPT)
2445 (pos (js2-current-token-beg))
2446 len
2447 buffer)))
2448 "The root node of a js2 AST."
2449 buffer ; the source buffer from which the code was parsed
2450 comments ; a Lisp list of comments, ordered by start position
2451 errors ; a Lisp list of errors found during parsing
2452 warnings ; a Lisp list of warnings found during parsing
2453 node-count) ; number of nodes in the tree, including the root
2454
2455 (put 'cl-struct-js2-ast-root 'js2-visitor 'js2-visit-ast-root)
2456 (put 'cl-struct-js2-ast-root 'js2-printer 'js2-print-script)
2457
2458 (defun js2-visit-ast-root (ast callback)
2459 (dolist (kid (js2-ast-root-kids ast))
2460 (js2-visit-ast kid callback))
2461 (dolist (comment (js2-ast-root-comments ast))
2462 (js2-visit-ast comment callback)))
2463
2464 (cl-defstruct (js2-comment-node
2465 (:include js2-node)
2466 (:constructor nil)
2467 (:constructor make-js2-comment-node (&key (type js2-COMMENT)
2468 (pos (js2-current-token-beg))
2469 len
2470 format)))
2471 format) ; 'line, 'block, 'jsdoc or 'html
2472
2473 (put 'cl-struct-js2-comment-node 'js2-visitor 'js2-visit-none)
2474 (put 'cl-struct-js2-comment-node 'js2-printer 'js2-print-comment)
2475
2476 (defun js2-print-comment (n i)
2477 ;; We really ought to link end-of-line comments to their nodes.
2478 ;; Or maybe we could add a new comment type, 'endline.
2479 (insert (js2-make-pad i)
2480 (js2-node-string n)))
2481
2482 (cl-defstruct (js2-expr-stmt-node
2483 (:include js2-node)
2484 (:constructor nil)
2485 (:constructor make-js2-expr-stmt-node (&key (type js2-EXPR_VOID)
2486 (pos js2-ts-cursor)
2487 len
2488 expr)))
2489 "An expression statement."
2490 expr)
2491
2492 (defsubst js2-expr-stmt-node-set-has-result (node)
2493 "Change NODE type to `js2-EXPR_RESULT'. Used for code generation."
2494 (setf (js2-node-type node) js2-EXPR_RESULT))
2495
2496 (put 'cl-struct-js2-expr-stmt-node 'js2-visitor 'js2-visit-expr-stmt-node)
2497 (put 'cl-struct-js2-expr-stmt-node 'js2-printer 'js2-print-expr-stmt-node)
2498
2499 (defun js2-visit-expr-stmt-node (n v)
2500 (js2-visit-ast (js2-expr-stmt-node-expr n) v))
2501
2502 (defun js2-print-expr-stmt-node (n indent)
2503 (js2-print-ast (js2-expr-stmt-node-expr n) indent)
2504 (insert ";\n"))
2505
2506 (cl-defstruct (js2-loop-node
2507 (:include js2-scope)
2508 (:constructor nil))
2509 "Abstract supertype of loop nodes."
2510 body ; a `js2-block-node'
2511 lp ; position of left-paren, nil if omitted
2512 rp) ; position of right-paren, nil if omitted
2513
2514 (cl-defstruct (js2-do-node
2515 (:include js2-loop-node)
2516 (:constructor nil)
2517 (:constructor make-js2-do-node (&key (type js2-DO)
2518 (pos (js2-current-token-beg))
2519 len
2520 body
2521 condition
2522 while-pos
2523 lp
2524 rp)))
2525 "AST node for do-loop."
2526 condition ; while (expression)
2527 while-pos) ; buffer position of 'while' keyword
2528
2529 (put 'cl-struct-js2-do-node 'js2-visitor 'js2-visit-do-node)
2530 (put 'cl-struct-js2-do-node 'js2-printer 'js2-print-do-node)
2531
2532 (defun js2-visit-do-node (n v)
2533 (js2-visit-ast (js2-do-node-body n) v)
2534 (js2-visit-ast (js2-do-node-condition n) v))
2535
2536 (defun js2-print-do-node (n i)
2537 (let ((pad (js2-make-pad i)))
2538 (insert pad "do {\n")
2539 (dolist (kid (js2-block-node-kids (js2-do-node-body n)))
2540 (js2-print-ast kid (1+ i)))
2541 (insert pad "} while (")
2542 (js2-print-ast (js2-do-node-condition n) 0)
2543 (insert ");\n")))
2544
2545 (cl-defstruct (js2-export-node
2546 (:include js2-node)
2547 (:constructor nil)
2548 (:constructor make-js2-export-node (&key (type js2-EXPORT)
2549 (pos (js2-current-token-beg))
2550 len
2551 exports-list
2552 from-clause
2553 declaration
2554 default)))
2555 "AST node for an export statement. There are many things that can be exported,
2556 so many of its properties will be nil.
2557 "
2558 exports-list ; lisp list of js2-export-binding-node to export
2559 from-clause ; js2-from-clause-node for re-exporting symbols from another module
2560 declaration ; js2-var-decl-node (var, let, const) or js2-class-node
2561 default) ; js2-function-node or js2-assign-node
2562
2563 (put 'cl-struct-js2-export-node 'js2-visitor 'js2-visit-export-node)
2564 (put 'cl-struct-js2-export-node 'js2-printer 'js2-print-export-node)
2565
2566 (defun js2-visit-export-node (n v)
2567 (let ((exports-list (js2-export-node-exports-list n))
2568 (from (js2-export-node-from-clause n))
2569 (declaration (js2-export-node-declaration n))
2570 (default (js2-export-node-default n)))
2571 (when exports-list
2572 (dolist (export exports-list)
2573 (js2-visit-ast export v)))
2574 (when from
2575 (js2-visit-ast from v))
2576 (when declaration
2577 (js2-visit-ast declaration v))
2578 (when default
2579 (js2-visit-ast default v))))
2580
2581 (defun js2-print-export-node (n i)
2582 (let ((pad (js2-make-pad i))
2583 (exports-list (js2-export-node-exports-list n))
2584 (from (js2-export-node-from-clause n))
2585 (declaration (js2-export-node-declaration n))
2586 (default (js2-export-node-default n)))
2587 (insert pad "export ")
2588 (cond
2589 (default
2590 (insert "default ")
2591 (js2-print-ast default i))
2592 (declaration
2593 (js2-print-ast declaration i))
2594 ((and exports-list from)
2595 (js2-print-named-imports exports-list)
2596 (insert " ")
2597 (js2-print-from-clause from))
2598 (from
2599 (insert "* ")
2600 (js2-print-from-clause from))
2601 (exports-list
2602 (js2-print-named-imports exports-list)))
2603 (insert ";\n")))
2604
2605 (cl-defstruct (js2-while-node
2606 (:include js2-loop-node)
2607 (:constructor nil)
2608 (:constructor make-js2-while-node (&key (type js2-WHILE)
2609 (pos (js2-current-token-beg))
2610 len body
2611 condition lp
2612 rp)))
2613 "AST node for while-loop."
2614 condition) ; while-condition
2615
2616 (put 'cl-struct-js2-while-node 'js2-visitor 'js2-visit-while-node)
2617 (put 'cl-struct-js2-while-node 'js2-printer 'js2-print-while-node)
2618
2619 (defun js2-visit-while-node (n v)
2620 (js2-visit-ast (js2-while-node-condition n) v)
2621 (js2-visit-ast (js2-while-node-body n) v))
2622
2623 (defun js2-print-while-node (n i)
2624 (let ((pad (js2-make-pad i)))
2625 (insert pad "while (")
2626 (js2-print-ast (js2-while-node-condition n) 0)
2627 (insert ") {\n")
2628 (js2-print-body (js2-while-node-body n) (1+ i))
2629 (insert pad "}\n")))
2630
2631 (cl-defstruct (js2-for-node
2632 (:include js2-loop-node)
2633 (:constructor nil)
2634 (:constructor make-js2-for-node (&key (type js2-FOR)
2635 (pos js2-ts-cursor)
2636 len body init
2637 condition
2638 update lp rp)))
2639 "AST node for a C-style for-loop."
2640 init ; initialization expression
2641 condition ; loop condition
2642 update) ; update clause
2643
2644 (put 'cl-struct-js2-for-node 'js2-visitor 'js2-visit-for-node)
2645 (put 'cl-struct-js2-for-node 'js2-printer 'js2-print-for-node)
2646
2647 (defun js2-visit-for-node (n v)
2648 (js2-visit-ast (js2-for-node-init n) v)
2649 (js2-visit-ast (js2-for-node-condition n) v)
2650 (js2-visit-ast (js2-for-node-update n) v)
2651 (js2-visit-ast (js2-for-node-body n) v))
2652
2653 (defun js2-print-for-node (n i)
2654 (let ((pad (js2-make-pad i)))
2655 (insert pad "for (")
2656 (js2-print-ast (js2-for-node-init n) 0)
2657 (insert "; ")
2658 (js2-print-ast (js2-for-node-condition n) 0)
2659 (insert "; ")
2660 (js2-print-ast (js2-for-node-update n) 0)
2661 (insert ") {\n")
2662 (js2-print-body (js2-for-node-body n) (1+ i))
2663 (insert pad "}\n")))
2664
2665 (cl-defstruct (js2-for-in-node
2666 (:include js2-loop-node)
2667 (:constructor nil)
2668 (:constructor make-js2-for-in-node (&key (type js2-FOR)
2669 (pos js2-ts-cursor)
2670 len body
2671 iterator
2672 object
2673 in-pos
2674 each-pos
2675 foreach-p forof-p
2676 lp rp)))
2677 "AST node for a for..in loop."
2678 iterator ; [var] foo in ...
2679 object ; object over which we're iterating
2680 in-pos ; buffer position of 'in' keyword
2681 each-pos ; buffer position of 'each' keyword, if foreach-p
2682 foreach-p ; t if it's a for-each loop
2683 forof-p) ; t if it's a for-of loop
2684
2685 (put 'cl-struct-js2-for-in-node 'js2-visitor 'js2-visit-for-in-node)
2686 (put 'cl-struct-js2-for-in-node 'js2-printer 'js2-print-for-in-node)
2687
2688 (defun js2-visit-for-in-node (n v)
2689 (js2-visit-ast (js2-for-in-node-iterator n) v)
2690 (js2-visit-ast (js2-for-in-node-object n) v)
2691 (js2-visit-ast (js2-for-in-node-body n) v))
2692
2693 (defun js2-print-for-in-node (n i)
2694 (let ((pad (js2-make-pad i))
2695 (foreach (js2-for-in-node-foreach-p n))
2696 (forof (js2-for-in-node-forof-p n)))
2697 (insert pad "for ")
2698 (if foreach
2699 (insert "each "))
2700 (insert "(")
2701 (js2-print-ast (js2-for-in-node-iterator n) 0)
2702 (insert (if forof " of " " in "))
2703 (js2-print-ast (js2-for-in-node-object n) 0)
2704 (insert ") {\n")
2705 (js2-print-body (js2-for-in-node-body n) (1+ i))
2706 (insert pad "}\n")))
2707
2708 (cl-defstruct (js2-return-node
2709 (:include js2-node)
2710 (:constructor nil)
2711 (:constructor make-js2-return-node (&key (type js2-RETURN)
2712 (pos js2-ts-cursor)
2713 len
2714 retval)))
2715 "AST node for a return statement."
2716 retval) ; expression to return, or 'undefined
2717
2718 (put 'cl-struct-js2-return-node 'js2-visitor 'js2-visit-return-node)
2719 (put 'cl-struct-js2-return-node 'js2-printer 'js2-print-return-node)
2720
2721 (defun js2-visit-return-node (n v)
2722 (js2-visit-ast (js2-return-node-retval n) v))
2723
2724 (defun js2-print-return-node (n i)
2725 (insert (js2-make-pad i) "return")
2726 (when (js2-return-node-retval n)
2727 (insert " ")
2728 (js2-print-ast (js2-return-node-retval n) 0))
2729 (insert ";\n"))
2730
2731 (cl-defstruct (js2-if-node
2732 (:include js2-node)
2733 (:constructor nil)
2734 (:constructor make-js2-if-node (&key (type js2-IF)
2735 (pos js2-ts-cursor)
2736 len condition
2737 then-part
2738 else-pos
2739 else-part lp
2740 rp)))
2741 "AST node for an if-statement."
2742 condition ; expression
2743 then-part ; statement or block
2744 else-pos ; optional buffer position of 'else' keyword
2745 else-part ; optional statement or block
2746 lp ; position of left-paren, nil if omitted
2747 rp) ; position of right-paren, nil if omitted
2748
2749 (put 'cl-struct-js2-if-node 'js2-visitor 'js2-visit-if-node)
2750 (put 'cl-struct-js2-if-node 'js2-printer 'js2-print-if-node)
2751
2752 (defun js2-visit-if-node (n v)
2753 (js2-visit-ast (js2-if-node-condition n) v)
2754 (js2-visit-ast (js2-if-node-then-part n) v)
2755 (js2-visit-ast (js2-if-node-else-part n) v))
2756
2757 (defun js2-print-if-node (n i)
2758 (let ((pad (js2-make-pad i))
2759 (then-part (js2-if-node-then-part n))
2760 (else-part (js2-if-node-else-part n)))
2761 (insert pad "if (")
2762 (js2-print-ast (js2-if-node-condition n) 0)
2763 (insert ") {\n")
2764 (js2-print-body then-part (1+ i))
2765 (insert pad "}")
2766 (cond
2767 ((not else-part)
2768 (insert "\n"))
2769 ((js2-if-node-p else-part)
2770 (insert " else ")
2771 (js2-print-body else-part i))
2772 (t
2773 (insert " else {\n")
2774 (js2-print-body else-part (1+ i))
2775 (insert pad "}\n")))))
2776
2777 (cl-defstruct (js2-export-binding-node
2778 (:include js2-node)
2779 (:constructor nil)
2780 (:constructor make-js2-export-binding-node (&key (type -1)
2781 pos
2782 len
2783 local-name
2784 extern-name)))
2785 "AST node for an external symbol binding.
2786 It contains a local-name node which is the name of the value in the
2787 current scope, and extern-name which is the name of the value in the
2788 imported or exported scope. By default these are the same, but if the
2789 name is aliased as in {foo as bar}, it would have an extern-name node
2790 containing 'foo' and a local-name node containing 'bar'."
2791 local-name ; js2-name-node with the variable name in this scope
2792 extern-name) ; js2-name-node with the value name in the exporting module
2793
2794 (put 'cl-struct-js2-export-binding-node 'js2-printer 'js2-print-extern-binding)
2795 (put 'cl-struct-js2-export-binding-node 'js2-visitor 'js2-visit-extern-binding)
2796
2797 (defun js2-visit-extern-binding (n v)
2798 "Visit an extern binding node. First visit the local-name, and, if
2799 different, visit the extern-name."
2800 (let ((local-name (js2-export-binding-node-local-name n))
2801 (extern-name (js2-export-binding-node-extern-name n)))
2802 (when local-name
2803 (js2-visit-ast local-name v))
2804 (when (not (equal local-name extern-name))
2805 (js2-visit-ast extern-name v))))
2806
2807 (defun js2-print-extern-binding (n _i)
2808 "Print a representation of a single extern binding. E.g. 'foo' or
2809 'foo as bar'."
2810 (let ((local-name (js2-export-binding-node-local-name n))
2811 (extern-name (js2-export-binding-node-extern-name n)))
2812 (insert (js2-name-node-name extern-name))
2813 (when (not (equal local-name extern-name))
2814 (insert " as ")
2815 (insert (js2-name-node-name local-name)))))
2816
2817
2818 (cl-defstruct (js2-import-node
2819 (:include js2-node)
2820 (:constructor nil)
2821 (:constructor make-js2-import-node (&key (type js2-IMPORT)
2822 (pos (js2-current-token-beg))
2823 len
2824 import
2825 from
2826 module-id)))
2827 "AST node for an import statement. It follows the form
2828
2829 import ModuleSpecifier;
2830 import ImportClause FromClause;"
2831 import ; js2-import-clause-node specifying which names are to imported.
2832 from ; js2-from-clause-node indicating the module from which to import.
2833 module-id) ; module-id of the import. E.g. 'src/mylib'.
2834
2835 (put 'cl-struct-js2-import-node 'js2-printer 'js2-print-import)
2836 (put 'cl-struct-js2-import-node 'js2-visitor 'js2-visit-import)
2837
2838 (defun js2-visit-import (n v)
2839 (let ((import-clause (js2-import-node-import n))
2840 (from-clause (js2-import-node-from n)))
2841 (when import-clause
2842 (js2-visit-ast import-clause v))
2843 (when from-clause
2844 (js2-visit-ast from-clause v))))
2845
2846 (defun js2-print-import (n i)
2847 "Prints a representation of the import node"
2848 (let ((pad (js2-make-pad i))
2849 (import-clause (js2-import-node-import n))
2850 (from-clause (js2-import-node-from n))
2851 (module-id (js2-import-node-module-id n)))
2852 (insert pad "import ")
2853 (if import-clause
2854 (progn
2855 (js2-print-import-clause import-clause)
2856 (insert " ")
2857 (js2-print-from-clause from-clause))
2858 (insert "'")
2859 (insert module-id)
2860 (insert "'"))
2861 (insert ";\n")))
2862
2863 (cl-defstruct (js2-import-clause-node
2864 (:include js2-node)
2865 (:constructor nil)
2866 (:constructor make-js2-import-clause-node (&key (type -1)
2867 pos
2868 len
2869 namespace-import
2870 named-imports
2871 default-binding)))
2872 "AST node corresponding to the import clause of an import statement. This is
2873 the portion of the import that bindings names from the external context to the
2874 local context."
2875 namespace-import ; js2-namespace-import-node. E.g. '* as lib'
2876 named-imports ; lisp list of js2-export-binding-node for all named imports.
2877 default-binding) ; js2-export-binding-node for the default import binding
2878
2879 (put 'cl-struct-js2-import-clause-node 'js2-visitor 'js2-visit-import-clause)
2880 (put 'cl-struct-js2-import-clause-node 'js2-printer 'js2-print-import-clause)
2881
2882 (defun js2-visit-import-clause (n v)
2883 (let ((ns-import (js2-import-clause-node-namespace-import n))
2884 (named-imports (js2-import-clause-node-named-imports n))
2885 (default (js2-import-clause-node-default-binding n)))
2886 (when ns-import
2887 (js2-visit-ast ns-import v))
2888 (when named-imports
2889 (dolist (import named-imports)
2890 (js2-visit-ast import v)))
2891 (when default
2892 (js2-visit-ast default v))))
2893
2894 (defun js2-print-import-clause (n)
2895 (let ((ns-import (js2-import-clause-node-namespace-import n))
2896 (named-imports (js2-import-clause-node-named-imports n))
2897 (default (js2-import-clause-node-default-binding n)))
2898 (cond
2899 ((and default ns-import)
2900 (js2-print-ast default)
2901 (insert ", ")
2902 (js2-print-namespace-import ns-import))
2903 ((and default named-imports)
2904 (js2-print-ast default)
2905 (insert ", ")
2906 (js2-print-named-imports named-imports))
2907 (default
2908 (js2-print-ast default))
2909 (ns-import
2910 (js2-print-namespace-import ns-import))
2911 (named-imports
2912 (js2-print-named-imports named-imports)))))
2913
2914 (defun js2-print-namespace-import (node)
2915 (insert "* as ")
2916 (insert (js2-name-node-name (js2-namespace-import-node-name node))))
2917
2918 (defun js2-print-named-imports (imports)
2919 (insert "{")
2920 (let ((len (length imports))
2921 (n 0))
2922 (while (< n len)
2923 (js2-print-extern-binding (nth n imports) 0)
2924 (unless (= n (- len 1))
2925 (insert ", "))
2926 (setq n (+ n 1))))
2927 (insert "}"))
2928
2929 (cl-defstruct (js2-namespace-import-node
2930 (:include js2-node)
2931 (:constructor nil)
2932 (:constructor make-js2-namespace-import-node (&key (type -1)
2933 pos
2934 len
2935 name)))
2936 "AST node for a complete namespace import.
2937 E.g. the '* as lib' expression in:
2938
2939 import * as lib from 'src/lib'
2940
2941 It contains a single name node referring to the bound name."
2942 name) ; js2-name-node of the bound name.
2943
2944 (defun js2-visit-namespace-import (n v)
2945 (js2-visit-ast (js2-namespace-import-node-name n) v))
2946
2947 (put 'cl-struct-js2-namespace-import-node 'js2-visitor 'js2-visit-namespace-import)
2948 (put 'cl-struct-js2-namespace-import-node 'js2-printer 'js2-print-namespace-import)
2949
2950 (cl-defstruct (js2-from-clause-node
2951 (:include js2-node)
2952 (:constructor nil)
2953 (:constructor make-js2-from-clause-node (&key (type js2-NAME)
2954 pos
2955 len
2956 module-id
2957 metadata-p)))
2958 "AST node for the from clause in an import or export statement.
2959 E.g. from 'my/module'. It can refere to either an external module, or to the
2960 modules metadata itself."
2961 module-id ; string containing the module specifier.
2962 metadata-p) ; true if this clause refers to the module's metadata
2963
2964 (put 'cl-struct-js2-from-clause-node 'js2-visitor 'js2-visit-none)
2965 (put 'cl-struct-js2-from-clause-node 'js2-printer 'js2-print-from-clause)
2966
2967 (defun js2-print-from-clause (n)
2968 (insert "from ")
2969 (if (js2-from-clause-node-metadata-p n)
2970 (insert "this module")
2971 (insert "'")
2972 (insert (js2-from-clause-node-module-id n))
2973 (insert "'")))
2974
2975 (cl-defstruct (js2-try-node
2976 (:include js2-node)
2977 (:constructor nil)
2978 (:constructor make-js2-try-node (&key (type js2-TRY)
2979 (pos js2-ts-cursor)
2980 len
2981 try-block
2982 catch-clauses
2983 finally-block)))
2984 "AST node for a try-statement."
2985 try-block
2986 catch-clauses ; a Lisp list of `js2-catch-node'
2987 finally-block) ; a `js2-finally-node'
2988
2989 (put 'cl-struct-js2-try-node 'js2-visitor 'js2-visit-try-node)
2990 (put 'cl-struct-js2-try-node 'js2-printer 'js2-print-try-node)
2991
2992 (defun js2-visit-try-node (n v)
2993 (js2-visit-ast (js2-try-node-try-block n) v)
2994 (dolist (clause (js2-try-node-catch-clauses n))
2995 (js2-visit-ast clause v))
2996 (js2-visit-ast (js2-try-node-finally-block n) v))
2997
2998 (defun js2-print-try-node (n i)
2999 (let ((pad (js2-make-pad i))
3000 (catches (js2-try-node-catch-clauses n))
3001 (finally (js2-try-node-finally-block n)))
3002 (insert pad "try {\n")
3003 (js2-print-body (js2-try-node-try-block n) (1+ i))
3004 (insert pad "}")
3005 (when catches
3006 (dolist (catch catches)
3007 (js2-print-ast catch i)))
3008 (if finally
3009 (js2-print-ast finally i)
3010 (insert "\n"))))
3011
3012 (cl-defstruct (js2-catch-node
3013 (:include js2-scope)
3014 (:constructor nil)
3015 (:constructor make-js2-catch-node (&key (type js2-CATCH)
3016 (pos js2-ts-cursor)
3017 len
3018 param
3019 guard-kwd
3020 guard-expr
3021 lp rp)))
3022 "AST node for a catch clause."
3023 param ; destructuring form or simple name node
3024 guard-kwd ; relative buffer position of "if" in "catch (x if ...)"
3025 guard-expr ; catch condition, a `js2-node'
3026 lp ; buffer position of left-paren, nil if omitted
3027 rp) ; buffer position of right-paren, nil if omitted
3028
3029 (put 'cl-struct-js2-catch-node 'js2-visitor 'js2-visit-catch-node)
3030 (put 'cl-struct-js2-catch-node 'js2-printer 'js2-print-catch-node)
3031
3032 (defun js2-visit-catch-node (n v)
3033 (js2-visit-ast (js2-catch-node-param n) v)
3034 (when (js2-catch-node-guard-kwd n)
3035 (js2-visit-ast (js2-catch-node-guard-expr n) v))
3036 (js2-visit-block n v))
3037
3038 (defun js2-print-catch-node (n i)
3039 (let ((pad (js2-make-pad i))
3040 (guard-kwd (js2-catch-node-guard-kwd n))
3041 (guard-expr (js2-catch-node-guard-expr n)))
3042 (insert " catch (")
3043 (js2-print-ast (js2-catch-node-param n) 0)
3044 (when guard-kwd
3045 (insert " if ")
3046 (js2-print-ast guard-expr 0))
3047 (insert ") {\n")
3048 (js2-print-body n (1+ i))
3049 (insert pad "}")))
3050
3051 (cl-defstruct (js2-finally-node
3052 (:include js2-node)
3053 (:constructor nil)
3054 (:constructor make-js2-finally-node (&key (type js2-FINALLY)
3055 (pos js2-ts-cursor)
3056 len body)))
3057 "AST node for a finally clause."
3058 body) ; a `js2-node', often but not always a block node
3059
3060 (put 'cl-struct-js2-finally-node 'js2-visitor 'js2-visit-finally-node)
3061 (put 'cl-struct-js2-finally-node 'js2-printer 'js2-print-finally-node)
3062
3063 (defun js2-visit-finally-node (n v)
3064 (js2-visit-ast (js2-finally-node-body n) v))
3065
3066 (defun js2-print-finally-node (n i)
3067 (let ((pad (js2-make-pad i)))
3068 (insert " finally {\n")
3069 (js2-print-body (js2-finally-node-body n) (1+ i))
3070 (insert pad "}\n")))
3071
3072 (cl-defstruct (js2-switch-node
3073 (:include js2-node)
3074 (:constructor nil)
3075 (:constructor make-js2-switch-node (&key (type js2-SWITCH)
3076 (pos js2-ts-cursor)
3077 len
3078 discriminant
3079 cases lp
3080 rp)))
3081 "AST node for a switch statement."
3082 discriminant ; a `js2-node' (switch expression)
3083 cases ; a Lisp list of `js2-case-node'
3084 lp ; position of open-paren for discriminant, nil if omitted
3085 rp) ; position of close-paren for discriminant, nil if omitted
3086
3087 (put 'cl-struct-js2-switch-node 'js2-visitor 'js2-visit-switch-node)
3088 (put 'cl-struct-js2-switch-node 'js2-printer 'js2-print-switch-node)
3089
3090 (defun js2-visit-switch-node (n v)
3091 (js2-visit-ast (js2-switch-node-discriminant n) v)
3092 (dolist (c (js2-switch-node-cases n))
3093 (js2-visit-ast c v)))
3094
3095 (defun js2-print-switch-node (n i)
3096 (let ((pad (js2-make-pad i))
3097 (cases (js2-switch-node-cases n)))
3098 (insert pad "switch (")
3099 (js2-print-ast (js2-switch-node-discriminant n) 0)
3100 (insert ") {\n")
3101 (dolist (case cases)
3102 (js2-print-ast case i))
3103 (insert pad "}\n")))
3104
3105 (cl-defstruct (js2-case-node
3106 (:include js2-block-node)
3107 (:constructor nil)
3108 (:constructor make-js2-case-node (&key (type js2-CASE)
3109 (pos js2-ts-cursor)
3110 len kids expr)))
3111 "AST node for a case clause of a switch statement."
3112 expr) ; the case expression (nil for default)
3113
3114 (put 'cl-struct-js2-case-node 'js2-visitor 'js2-visit-case-node)
3115 (put 'cl-struct-js2-case-node 'js2-printer 'js2-print-case-node)
3116
3117 (defun js2-visit-case-node (n v)
3118 (js2-visit-ast (js2-case-node-expr n) v)
3119 (js2-visit-block n v))
3120
3121 (defun js2-print-case-node (n i)
3122 (let ((pad (js2-make-pad i))
3123 (expr (js2-case-node-expr n)))
3124 (insert pad)
3125 (if (null expr)
3126 (insert "default:\n")
3127 (insert "case ")
3128 (js2-print-ast expr 0)
3129 (insert ":\n"))
3130 (dolist (kid (js2-case-node-kids n))
3131 (js2-print-ast kid (1+ i)))))
3132
3133 (cl-defstruct (js2-throw-node
3134 (:include js2-node)
3135 (:constructor nil)
3136 (:constructor make-js2-throw-node (&key (type js2-THROW)
3137 (pos js2-ts-cursor)
3138 len expr)))
3139 "AST node for a throw statement."
3140 expr) ; the expression to throw
3141
3142 (put 'cl-struct-js2-throw-node 'js2-visitor 'js2-visit-throw-node)
3143 (put 'cl-struct-js2-throw-node 'js2-printer 'js2-print-throw-node)
3144
3145 (defun js2-visit-throw-node (n v)
3146 (js2-visit-ast (js2-throw-node-expr n) v))
3147
3148 (defun js2-print-throw-node (n i)
3149 (insert (js2-make-pad i) "throw ")
3150 (js2-print-ast (js2-throw-node-expr n) 0)
3151 (insert ";\n"))
3152
3153 (cl-defstruct (js2-with-node
3154 (:include js2-node)
3155 (:constructor nil)
3156 (:constructor make-js2-with-node (&key (type js2-WITH)
3157 (pos js2-ts-cursor)
3158 len object
3159 body lp rp)))
3160 "AST node for a with-statement."
3161 object
3162 body
3163 lp ; buffer position of left-paren around object, nil if omitted
3164 rp) ; buffer position of right-paren around object, nil if omitted
3165
3166 (put 'cl-struct-js2-with-node 'js2-visitor 'js2-visit-with-node)
3167 (put 'cl-struct-js2-with-node 'js2-printer 'js2-print-with-node)
3168
3169 (defun js2-visit-with-node (n v)
3170 (js2-visit-ast (js2-with-node-object n) v)
3171 (js2-visit-ast (js2-with-node-body n) v))
3172
3173 (defun js2-print-with-node (n i)
3174 (let ((pad (js2-make-pad i)))
3175 (insert pad "with (")
3176 (js2-print-ast (js2-with-node-object n) 0)
3177 (insert ") {\n")
3178 (js2-print-body (js2-with-node-body n) (1+ i))
3179 (insert pad "}\n")))
3180
3181 (cl-defstruct (js2-label-node
3182 (:include js2-node)
3183 (:constructor nil)
3184 (:constructor make-js2-label-node (&key (type js2-LABEL)
3185 (pos js2-ts-cursor)
3186 len name)))
3187 "AST node for a statement label or case label."
3188 name ; a string
3189 loop) ; for validating and code-generating continue-to-label
3190
3191 (put 'cl-struct-js2-label-node 'js2-visitor 'js2-visit-none)
3192 (put 'cl-struct-js2-label-node 'js2-printer 'js2-print-label)
3193
3194 (defun js2-print-label (n i)
3195 (insert (js2-make-pad i)
3196 (js2-label-node-name n)
3197 ":\n"))
3198
3199 (cl-defstruct (js2-labeled-stmt-node
3200 (:include js2-node)
3201 (:constructor nil)
3202 ;; type needs to be in `js2-side-effecting-tokens' to avoid spurious
3203 ;; no-side-effects warnings, hence js2-EXPR_RESULT.
3204 (:constructor make-js2-labeled-stmt-node (&key (type js2-EXPR_RESULT)
3205 (pos js2-ts-cursor)
3206 len labels stmt)))
3207 "AST node for a statement with one or more labels.
3208 Multiple labels for a statement are collapsed into the labels field."
3209 labels ; Lisp list of `js2-label-node'
3210 stmt) ; the statement these labels are for
3211
3212 (put 'cl-struct-js2-labeled-stmt-node 'js2-visitor 'js2-visit-labeled-stmt)
3213 (put 'cl-struct-js2-labeled-stmt-node 'js2-printer 'js2-print-labeled-stmt)
3214
3215 (defun js2-get-label-by-name (lbl-stmt name)
3216 "Return a `js2-label-node' by NAME from LBL-STMT's labels list.
3217 Returns nil if no such label is in the list."
3218 (let ((label-list (js2-labeled-stmt-node-labels lbl-stmt))
3219 result)
3220 (while (and label-list (not result))
3221 (if (string= (js2-label-node-name (car label-list)) name)
3222 (setq result (car label-list))
3223 (setq label-list (cdr label-list))))
3224 result))
3225
3226 (defun js2-visit-labeled-stmt (n v)
3227 (dolist (label (js2-labeled-stmt-node-labels n))
3228 (js2-visit-ast label v))
3229 (js2-visit-ast (js2-labeled-stmt-node-stmt n) v))
3230
3231 (defun js2-print-labeled-stmt (n i)
3232 (dolist (label (js2-labeled-stmt-node-labels n))
3233 (js2-print-ast label i))
3234 (js2-print-ast (js2-labeled-stmt-node-stmt n) i))
3235
3236 (defun js2-labeled-stmt-node-contains (node label)
3237 "Return t if NODE contains LABEL in its label set.
3238 NODE is a `js2-labels-node'. LABEL is an identifier."
3239 (cl-loop for nl in (js2-labeled-stmt-node-labels node)
3240 if (string= label (js2-label-node-name nl))
3241 return t
3242 finally return nil))
3243
3244 (defsubst js2-labeled-stmt-node-add-label (node label)
3245 "Add a `js2-label-node' to the label set for this statement."
3246 (setf (js2-labeled-stmt-node-labels node)
3247 (nconc (js2-labeled-stmt-node-labels node) (list label))))
3248
3249 (cl-defstruct (js2-jump-node
3250 (:include js2-node)
3251 (:constructor nil))
3252 "Abstract supertype of break and continue nodes."
3253 label ; `js2-name-node' for location of label identifier, if present
3254 target) ; target js2-labels-node or loop/switch statement
3255
3256 (defun js2-visit-jump-node (n v)
3257 ;; We don't visit the target, since it's a back-link.
3258 (js2-visit-ast (js2-jump-node-label n) v))
3259
3260 (cl-defstruct (js2-break-node
3261 (:include js2-jump-node)
3262 (:constructor nil)
3263 (:constructor make-js2-break-node (&key (type js2-BREAK)
3264 (pos js2-ts-cursor)
3265 len label target)))
3266 "AST node for a break statement.
3267 The label field is a `js2-name-node', possibly nil, for the named label
3268 if provided. E.g. in 'break foo', it represents 'foo'. The target field
3269 is the target of the break - a label node or enclosing loop/switch statement.")
3270
3271 (put 'cl-struct-js2-break-node 'js2-visitor 'js2-visit-jump-node)
3272 (put 'cl-struct-js2-break-node 'js2-printer 'js2-print-break-node)
3273
3274 (defun js2-print-break-node (n i)
3275 (insert (js2-make-pad i) "break")
3276 (when (js2-break-node-label n)
3277 (insert " ")
3278 (js2-print-ast (js2-break-node-label n) 0))
3279 (insert ";\n"))
3280
3281 (cl-defstruct (js2-continue-node
3282 (:include js2-jump-node)
3283 (:constructor nil)
3284 (:constructor make-js2-continue-node (&key (type js2-CONTINUE)
3285 (pos js2-ts-cursor)
3286 len label target)))
3287 "AST node for a continue statement.
3288 The label field is the user-supplied enclosing label name, a `js2-name-node'.
3289 It is nil if continue specifies no label. The target field is the jump target:
3290 a `js2-label-node' or the innermost enclosing loop.")
3291
3292 (put 'cl-struct-js2-continue-node 'js2-visitor 'js2-visit-jump-node)
3293 (put 'cl-struct-js2-continue-node 'js2-printer 'js2-print-continue-node)
3294
3295 (defun js2-print-continue-node (n i)
3296 (insert (js2-make-pad i) "continue")
3297 (when (js2-continue-node-label n)
3298 (insert " ")
3299 (js2-print-ast (js2-continue-node-label n) 0))
3300 (insert ";\n"))
3301
3302 (cl-defstruct (js2-function-node
3303 (:include js2-script-node)
3304 (:constructor nil)
3305 (:constructor make-js2-function-node (&key (type js2-FUNCTION)
3306 (pos js2-ts-cursor)
3307 len
3308 (ftype 'FUNCTION)
3309 (form 'FUNCTION_STATEMENT)
3310 (name "")
3311 params rest-p
3312 body
3313 generator-type
3314 lp rp)))
3315 "AST node for a function declaration.
3316 The `params' field is a Lisp list of nodes. Each node is either a simple
3317 `js2-name-node', or if it's a destructuring-assignment parameter, a
3318 `js2-array-node' or `js2-object-node'."
3319 ftype ; FUNCTION, GETTER or SETTER
3320 form ; FUNCTION_{STATEMENT|EXPRESSION|ARROW}
3321 name ; function name (a `js2-name-node', or nil if anonymous)
3322 params ; a Lisp list of destructuring forms or simple name nodes
3323 rest-p ; if t, the last parameter is rest parameter
3324 body ; a `js2-block-node' or expression node (1.8 only)
3325 lp ; position of arg-list open-paren, or nil if omitted
3326 rp ; position of arg-list close-paren, or nil if omitted
3327 ignore-dynamic ; ignore value of the dynamic-scope flag (interpreter only)
3328 needs-activation ; t if we need an activation object for this frame
3329 generator-type ; STAR, LEGACY, COMPREHENSION or nil
3330 member-expr) ; nonstandard Ecma extension from Rhino
3331
3332 (put 'cl-struct-js2-function-node 'js2-visitor 'js2-visit-function-node)
3333 (put 'cl-struct-js2-function-node 'js2-printer 'js2-print-function-node)
3334
3335 (defun js2-visit-function-node (n v)
3336 (js2-visit-ast (js2-function-node-name n) v)
3337 (dolist (p (js2-function-node-params n))
3338 (js2-visit-ast p v))
3339 (js2-visit-ast (js2-function-node-body n) v))
3340
3341 (defun js2-print-function-node (n i)
3342 (let* ((pad (js2-make-pad i))
3343 (getter (js2-node-get-prop n 'GETTER_SETTER))
3344 (name (or (js2-function-node-name n)
3345 (js2-function-node-member-expr n)))
3346 (params (js2-function-node-params n))
3347 (arrow (eq (js2-function-node-form n) 'FUNCTION_ARROW))
3348 (rest-p (js2-function-node-rest-p n))
3349 (body (js2-function-node-body n))
3350 (expr (not (eq (js2-function-node-form n) 'FUNCTION_STATEMENT))))
3351 (unless (or getter arrow)
3352 (insert pad "function")
3353 (when (eq (js2-function-node-generator-type n) 'STAR)
3354 (insert "*")))
3355 (when name
3356 (insert " ")
3357 (js2-print-ast name 0))
3358 (insert "(")
3359 (cl-loop with len = (length params)
3360 for param in params
3361 for count from 1
3362 do
3363 (when (and rest-p (= count len))
3364 (insert "..."))
3365 (js2-print-ast param 0)
3366 (when (< count len)
3367 (insert ", ")))
3368 (insert ") ")
3369 (when arrow
3370 (insert "=> "))
3371 (insert "{")
3372 ;; TODO: fix this to be smarter about indenting, etc.
3373 (unless expr
3374 (insert "\n"))
3375 (if (js2-block-node-p body)
3376 (js2-print-body body (1+ i))
3377 (js2-print-ast body 0))
3378 (insert pad "}")
3379 (unless expr
3380 (insert "\n"))))
3381
3382 (defun js2-function-name (node)
3383 "Return function name for NODE, a `js2-function-node', or nil if anonymous."
3384 (and (js2-function-node-name node)
3385 (js2-name-node-name (js2-function-node-name node))))
3386
3387 ;; Having this be an expression node makes it more flexible.
3388 ;; There are IDE contexts, such as indentation in a for-loop initializer,
3389 ;; that work better if you assume it's an expression. Whenever we have
3390 ;; a standalone var/const declaration, we just wrap with an expr stmt.
3391 ;; Eclipse apparently screwed this up and now has two versions, expr and stmt.
3392 (cl-defstruct (js2-var-decl-node
3393 (:include js2-node)
3394 (:constructor nil)
3395 (:constructor make-js2-var-decl-node (&key (type js2-VAR)
3396 (pos (js2-current-token-beg))
3397 len kids
3398 decl-type)))
3399 "AST node for a variable declaration list (VAR, CONST or LET).
3400 The node bounds differ depending on the declaration type. For VAR or
3401 CONST declarations, the bounds include the var/const keyword. For LET
3402 declarations, the node begins at the position of the first child."
3403 kids ; a Lisp list of `js2-var-init-node' structs.
3404 decl-type) ; js2-VAR, js2-CONST or js2-LET
3405
3406 (put 'cl-struct-js2-var-decl-node 'js2-visitor 'js2-visit-var-decl)
3407 (put 'cl-struct-js2-var-decl-node 'js2-printer 'js2-print-var-decl)
3408
3409 (defun js2-visit-var-decl (n v)
3410 (dolist (kid (js2-var-decl-node-kids n))
3411 (js2-visit-ast kid v)))
3412
3413 (defun js2-print-var-decl (n i)
3414 (let ((pad (js2-make-pad i))
3415 (tt (js2-var-decl-node-decl-type n)))
3416 (insert pad)
3417 (insert (cond
3418 ((= tt js2-VAR) "var ")
3419 ((= tt js2-LET) "let ")
3420 ((= tt js2-CONST) "const ")
3421 (t
3422 (error "malformed var-decl node"))))
3423 (cl-loop with kids = (js2-var-decl-node-kids n)
3424 with len = (length kids)
3425 for kid in kids
3426 for count from 1
3427 do
3428 (js2-print-ast kid 0)
3429 (if (< count len)
3430 (insert ", ")))))
3431
3432 (cl-defstruct (js2-var-init-node
3433 (:include js2-node)
3434 (:constructor nil)
3435 (:constructor make-js2-var-init-node (&key (type js2-VAR)
3436 (pos js2-ts-cursor)
3437 len target
3438 initializer)))
3439 "AST node for a variable declaration.
3440 The type field will be js2-CONST for a const decl."
3441 target ; `js2-name-node', `js2-object-node', or `js2-array-node'
3442 initializer) ; initializer expression, a `js2-node'
3443
3444 (put 'cl-struct-js2-var-init-node 'js2-visitor 'js2-visit-var-init-node)
3445 (put 'cl-struct-js2-var-init-node 'js2-printer 'js2-print-var-init-node)
3446
3447 (defun js2-visit-var-init-node (n v)
3448 (js2-visit-ast (js2-var-init-node-target n) v)
3449 (js2-visit-ast (js2-var-init-node-initializer n) v))
3450
3451 (defun js2-print-var-init-node (n i)
3452 (let ((pad (js2-make-pad i))
3453 (name (js2-var-init-node-target n))
3454 (init (js2-var-init-node-initializer n)))
3455 (insert pad)
3456 (js2-print-ast name 0)
3457 (when init
3458 (insert " = ")
3459 (js2-print-ast init 0))))
3460
3461 (cl-defstruct (js2-cond-node
3462 (:include js2-node)
3463 (:constructor nil)
3464 (:constructor make-js2-cond-node (&key (type js2-HOOK)
3465 (pos js2-ts-cursor)
3466 len
3467 test-expr
3468 true-expr
3469 false-expr
3470 q-pos c-pos)))
3471 "AST node for the ternary operator"
3472 test-expr
3473 true-expr
3474 false-expr
3475 q-pos ; buffer position of ?
3476 c-pos) ; buffer position of :
3477
3478 (put 'cl-struct-js2-cond-node 'js2-visitor 'js2-visit-cond-node)
3479 (put 'cl-struct-js2-cond-node 'js2-printer 'js2-print-cond-node)
3480
3481 (defun js2-visit-cond-node (n v)
3482 (js2-visit-ast (js2-cond-node-test-expr n) v)
3483 (js2-visit-ast (js2-cond-node-true-expr n) v)
3484 (js2-visit-ast (js2-cond-node-false-expr n) v))
3485
3486 (defun js2-print-cond-node (n i)
3487 (let ((pad (js2-make-pad i)))
3488 (insert pad)
3489 (js2-print-ast (js2-cond-node-test-expr n) 0)
3490 (insert " ? ")
3491 (js2-print-ast (js2-cond-node-true-expr n) 0)
3492 (insert " : ")
3493 (js2-print-ast (js2-cond-node-false-expr n) 0)))
3494
3495 (cl-defstruct (js2-infix-node
3496 (:include js2-node)
3497 (:constructor nil)
3498 (:constructor make-js2-infix-node (&key type
3499 (pos js2-ts-cursor)
3500 len op-pos
3501 left right)))
3502 "Represents infix expressions.
3503 Includes assignment ops like `|=', and the comma operator.
3504 The type field inherited from `js2-node' holds the operator."
3505 op-pos ; buffer position where operator begins
3506 left ; any `js2-node'
3507 right) ; any `js2-node'
3508
3509 (put 'cl-struct-js2-infix-node 'js2-visitor 'js2-visit-infix-node)
3510 (put 'cl-struct-js2-infix-node 'js2-printer 'js2-print-infix-node)
3511
3512 (defun js2-visit-infix-node (n v)
3513 (js2-visit-ast (js2-infix-node-left n) v)
3514 (js2-visit-ast (js2-infix-node-right n) v))
3515
3516 (defconst js2-operator-tokens
3517 (let ((table (make-hash-table :test 'eq))
3518 (tokens
3519 (list (cons js2-IN "in")
3520 (cons js2-TYPEOF "typeof")
3521 (cons js2-INSTANCEOF "instanceof")
3522 (cons js2-DELPROP "delete")
3523 (cons js2-COMMA ",")
3524 (cons js2-COLON ":")
3525 (cons js2-OR "||")
3526 (cons js2-AND "&&")
3527 (cons js2-INC "++")
3528 (cons js2-DEC "--")
3529 (cons js2-BITOR "|")
3530 (cons js2-BITXOR "^")
3531 (cons js2-BITAND "&")
3532 (cons js2-EQ "==")
3533 (cons js2-NE "!=")
3534 (cons js2-LT "<")
3535 (cons js2-LE "<=")
3536 (cons js2-GT ">")
3537 (cons js2-GE ">=")
3538 (cons js2-LSH "<<")
3539 (cons js2-RSH ">>")
3540 (cons js2-URSH ">>>")
3541 (cons js2-ADD "+") ; infix plus
3542 (cons js2-SUB "-") ; infix minus
3543 (cons js2-MUL "*")
3544 (cons js2-DIV "/")
3545 (cons js2-MOD "%")
3546 (cons js2-NOT "!")
3547 (cons js2-BITNOT "~")
3548 (cons js2-POS "+") ; unary plus
3549 (cons js2-NEG "-") ; unary minus
3550 (cons js2-TRIPLEDOT "...")
3551 (cons js2-SHEQ "===") ; shallow equality
3552 (cons js2-SHNE "!==") ; shallow inequality
3553 (cons js2-ASSIGN "=")
3554 (cons js2-ASSIGN_BITOR "|=")
3555 (cons js2-ASSIGN_BITXOR "^=")
3556 (cons js2-ASSIGN_BITAND "&=")
3557 (cons js2-ASSIGN_LSH "<<=")
3558 (cons js2-ASSIGN_RSH ">>=")
3559 (cons js2-ASSIGN_URSH ">>>=")
3560 (cons js2-ASSIGN_ADD "+=")
3561 (cons js2-ASSIGN_SUB "-=")
3562 (cons js2-ASSIGN_MUL "*=")
3563 (cons js2-ASSIGN_DIV "/=")
3564 (cons js2-ASSIGN_MOD "%="))))
3565 (cl-loop for (k . v) in tokens do
3566 (puthash k v table))
3567 table))
3568
3569 (defun js2-print-infix-node (n i)
3570 (let* ((tt (js2-node-type n))
3571 (op (gethash tt js2-operator-tokens)))
3572 (unless op
3573 (error "unrecognized infix operator %s" (js2-node-type n)))
3574 (insert (js2-make-pad i))
3575 (js2-print-ast (js2-infix-node-left n) 0)
3576 (unless (= tt js2-COMMA)
3577 (insert " "))
3578 (insert op)
3579 (insert " ")
3580 (js2-print-ast (js2-infix-node-right n) 0)))
3581
3582 (cl-defstruct (js2-assign-node
3583 (:include js2-infix-node)
3584 (:constructor nil)
3585 (:constructor make-js2-assign-node (&key type
3586 (pos js2-ts-cursor)
3587 len op-pos
3588 left right)))
3589 "Represents any assignment.
3590 The type field holds the actual assignment operator.")
3591
3592 (put 'cl-struct-js2-assign-node 'js2-visitor 'js2-visit-infix-node)
3593 (put 'cl-struct-js2-assign-node 'js2-printer 'js2-print-infix-node)
3594
3595 (cl-defstruct (js2-unary-node
3596 (:include js2-node)
3597 (:constructor nil)
3598 (:constructor make-js2-unary-node (&key type ; required
3599 (pos js2-ts-cursor)
3600 len operand)))
3601 "AST node type for unary operator nodes.
3602 The type field can be NOT, BITNOT, POS, NEG, INC, DEC,
3603 TYPEOF, DELPROP or TRIPLEDOT. For INC or DEC, a 'postfix node
3604 property is added if the operator follows the operand."
3605 operand) ; a `js2-node' expression
3606
3607 (put 'cl-struct-js2-unary-node 'js2-visitor 'js2-visit-unary-node)
3608 (put 'cl-struct-js2-unary-node 'js2-printer 'js2-print-unary-node)
3609
3610 (defun js2-visit-unary-node (n v)
3611 (js2-visit-ast (js2-unary-node-operand n) v))
3612
3613 (defun js2-print-unary-node (n i)
3614 (let* ((tt (js2-node-type n))
3615 (op (gethash tt js2-operator-tokens))
3616 (postfix (js2-node-get-prop n 'postfix)))
3617 (unless op
3618 (error "unrecognized unary operator %s" tt))
3619 (insert (js2-make-pad i))
3620 (unless postfix
3621 (insert op))
3622 (if (or (= tt js2-TYPEOF)
3623 (= tt js2-DELPROP))
3624 (insert " "))
3625 (js2-print-ast (js2-unary-node-operand n) 0)
3626 (when postfix
3627 (insert op))))
3628
3629 (cl-defstruct (js2-let-node
3630 (:include js2-scope)
3631 (:constructor nil)
3632 (:constructor make-js2-let-node (&key (type js2-LETEXPR)
3633 (pos (js2-current-token-beg))
3634 len vars body
3635 lp rp)))
3636 "AST node for a let expression or a let statement.
3637 Note that a let declaration such as let x=6, y=7 is a `js2-var-decl-node'."
3638 vars ; a `js2-var-decl-node'
3639 body ; a `js2-node' representing the expression or body block
3640 lp
3641 rp)
3642
3643 (put 'cl-struct-js2-let-node 'js2-visitor 'js2-visit-let-node)
3644 (put 'cl-struct-js2-let-node 'js2-printer 'js2-print-let-node)
3645
3646 (defun js2-visit-let-node (n v)
3647 (js2-visit-ast (js2-let-node-vars n) v)
3648 (js2-visit-ast (js2-let-node-body n) v))
3649
3650 (defun js2-print-let-node (n i)
3651 (insert (js2-make-pad i) "let (")
3652 (let ((p (point)))
3653 (js2-print-ast (js2-let-node-vars n) 0)
3654 (delete-region p (+ p 4)))
3655 (insert ") ")
3656 (js2-print-ast (js2-let-node-body n) i))
3657
3658 (cl-defstruct (js2-keyword-node
3659 (:include js2-node)
3660 (:constructor nil)
3661 (:constructor make-js2-keyword-node (&key type
3662 (pos (js2-current-token-beg))
3663 (len (- js2-ts-cursor pos)))))
3664 "AST node representing a literal keyword such as `null'.
3665 Used for `null', `this', `true', `false' and `debugger'.
3666 The node type is set to js2-NULL, js2-THIS, etc.")
3667
3668 (put 'cl-struct-js2-keyword-node 'js2-visitor 'js2-visit-none)
3669 (put 'cl-struct-js2-keyword-node 'js2-printer 'js2-print-keyword-node)
3670
3671 (defun js2-print-keyword-node (n i)
3672 (insert (js2-make-pad i)
3673 (let ((tt (js2-node-type n)))
3674 (cond
3675 ((= tt js2-THIS) "this")
3676 ((= tt js2-SUPER) "super")
3677 ((= tt js2-NULL) "null")
3678 ((= tt js2-TRUE) "true")
3679 ((= tt js2-FALSE) "false")
3680 ((= tt js2-DEBUGGER) "debugger")
3681 (t (error "Invalid keyword literal type: %d" tt))))))
3682
3683 (defsubst js2-this-or-super-node-p (node)
3684 "Return t if NODE is a `js2-literal-node' of type js2-THIS or js2-SUPER."
3685 (let ((type (js2-node-type node)))
3686 (or (eq type js2-THIS) (eq type js2-SUPER))))
3687
3688 (cl-defstruct (js2-new-node
3689 (:include js2-node)
3690 (:constructor nil)
3691 (:constructor make-js2-new-node (&key (type js2-NEW)
3692 (pos (js2-current-token-beg))
3693 len target
3694 args initializer
3695 lp rp)))
3696 "AST node for new-expression such as new Foo()."
3697 target ; an identifier or reference
3698 args ; a Lisp list of argument nodes
3699 lp ; position of left-paren, nil if omitted
3700 rp ; position of right-paren, nil if omitted
3701 initializer) ; experimental Rhino syntax: optional `js2-object-node'
3702
3703 (put 'cl-struct-js2-new-node 'js2-visitor 'js2-visit-new-node)
3704 (put 'cl-struct-js2-new-node 'js2-printer 'js2-print-new-node)
3705
3706 (defun js2-visit-new-node (n v)
3707 (js2-visit-ast (js2-new-node-target n) v)
3708 (dolist (arg (js2-new-node-args n))
3709 (js2-visit-ast arg v))
3710 (js2-visit-ast (js2-new-node-initializer n) v))
3711
3712 (defun js2-print-new-node (n i)
3713 (insert (js2-make-pad i) "new ")
3714 (js2-print-ast (js2-new-node-target n))
3715 (insert "(")
3716 (js2-print-list (js2-new-node-args n))
3717 (insert ")")
3718 (when (js2-new-node-initializer n)
3719 (insert " ")
3720 (js2-print-ast (js2-new-node-initializer n))))
3721
3722 (cl-defstruct (js2-name-node
3723 (:include js2-node)
3724 (:constructor nil)
3725 (:constructor make-js2-name-node (&key (type js2-NAME)
3726 (pos (js2-current-token-beg))
3727 (len (- js2-ts-cursor
3728 (js2-current-token-beg)))
3729 (name (js2-current-token-string)))))
3730 "AST node for a JavaScript identifier"
3731 name ; a string
3732 scope) ; a `js2-scope' (optional, used for codegen)
3733
3734 (put 'cl-struct-js2-name-node 'js2-visitor 'js2-visit-none)
3735 (put 'cl-struct-js2-name-node 'js2-printer 'js2-print-name-node)
3736
3737 (defun js2-print-name-node (n i)
3738 (insert (js2-make-pad i)
3739 (js2-name-node-name n)))
3740
3741 (defsubst js2-name-node-length (node)
3742 "Return identifier length of NODE, a `js2-name-node'.
3743 Returns 0 if NODE is nil or its identifier field is nil."
3744 (if node
3745 (length (js2-name-node-name node))
3746 0))
3747
3748 (cl-defstruct (js2-number-node
3749 (:include js2-node)
3750 (:constructor nil)
3751 (:constructor make-js2-number-node (&key (type js2-NUMBER)
3752 (pos (js2-current-token-beg))
3753 (len (- js2-ts-cursor
3754 (js2-current-token-beg)))
3755 (value (js2-current-token-string))
3756 (num-value (js2-token-number
3757 (js2-current-token))))))
3758 "AST node for a number literal."
3759 value ; the original string, e.g. "6.02e23"
3760 num-value) ; the parsed number value
3761
3762 (put 'cl-struct-js2-number-node 'js2-visitor 'js2-visit-none)
3763 (put 'cl-struct-js2-number-node 'js2-printer 'js2-print-number-node)
3764
3765 (defun js2-print-number-node (n i)
3766 (insert (js2-make-pad i)
3767 (number-to-string (js2-number-node-num-value n))))
3768
3769 (cl-defstruct (js2-regexp-node
3770 (:include js2-node)
3771 (:constructor nil)
3772 (:constructor make-js2-regexp-node (&key (type js2-REGEXP)
3773 (pos (js2-current-token-beg))
3774 (len (- js2-ts-cursor
3775 (js2-current-token-beg)))
3776 value flags)))
3777 "AST node for a regular expression literal."
3778 value ; the regexp string, without // delimiters
3779 flags) ; a string of flags, e.g. `mi'.
3780
3781 (put 'cl-struct-js2-regexp-node 'js2-visitor 'js2-visit-none)
3782 (put 'cl-struct-js2-regexp-node 'js2-printer 'js2-print-regexp)
3783
3784 (defun js2-print-regexp (n i)
3785 (insert (js2-make-pad i)
3786 "/"
3787 (js2-regexp-node-value n)
3788 "/")
3789 (if (js2-regexp-node-flags n)
3790 (insert (js2-regexp-node-flags n))))
3791
3792 (cl-defstruct (js2-string-node
3793 (:include js2-node)
3794 (:constructor nil)
3795 (:constructor make-js2-string-node (&key (type js2-STRING)
3796 (pos (js2-current-token-beg))
3797 (len (- js2-ts-cursor
3798 (js2-current-token-beg)))
3799 (value (js2-current-token-string)))))
3800 "String literal.
3801 Escape characters are not evaluated; e.g. \n is 2 chars in value field.
3802 You can tell the quote type by looking at the first character."
3803 value) ; the characters of the string, including the quotes
3804
3805 (put 'cl-struct-js2-string-node 'js2-visitor 'js2-visit-none)
3806 (put 'cl-struct-js2-string-node 'js2-printer 'js2-print-string-node)
3807
3808 (defun js2-print-string-node (n i)
3809 (insert (js2-make-pad i)
3810 (js2-node-string n)))
3811
3812 (cl-defstruct (js2-template-node
3813 (:include js2-node)
3814 (:constructor nil)
3815 (:constructor make-js2-template-node (&key (type js2-TEMPLATE_HEAD)
3816 beg len kids)))
3817 "Template literal."
3818 kids) ; `js2-string-node' is used for string segments, other nodes
3819 ; for substitutions inside.
3820
3821 (put 'cl-struct-js2-template-node 'js2-visitor 'js2-visit-template)
3822 (put 'cl-struct-js2-template-node 'js2-printer 'js2-print-template)
3823
3824 (defun js2-visit-template (n callback)
3825 (dolist (kid (js2-template-node-kids n))
3826 (js2-visit-ast kid callback)))
3827
3828 (defun js2-print-template (n i)
3829 (insert (js2-make-pad i))
3830 (dolist (kid (js2-template-node-kids n))
3831 (if (js2-string-node-p kid)
3832 (insert (js2-node-string kid))
3833 (js2-print-ast kid))))
3834
3835 (cl-defstruct (js2-tagged-template-node
3836 (:include js2-node)
3837 (:constructor nil)
3838 (:constructor make-js2-tagged-template-node (&key (type js2-TAGGED_TEMPLATE)
3839 beg len tag template)))
3840 "Tagged template literal."
3841 tag ; `js2-node' with the tag expression.
3842 template) ; `js2-template-node' with the template.
3843
3844 (put 'cl-struct-js2-tagged-template-node 'js2-visitor 'js2-visit-tagged-template)
3845 (put 'cl-struct-js2-tagged-template-node 'js2-printer 'js2-print-tagged-template)
3846
3847 (defun js2-visit-tagged-template (n callback)
3848 (js2-visit-ast (js2-tagged-template-node-tag n) callback)
3849 (js2-visit-ast (js2-tagged-template-node-template n) callback))
3850
3851 (defun js2-print-tagged-template (n i)
3852 (insert (js2-make-pad i))
3853 (js2-print-ast (js2-tagged-template-node-tag n))
3854 (js2-print-ast (js2-tagged-template-node-template n)))
3855
3856 (cl-defstruct (js2-array-node
3857 (:include js2-node)
3858 (:constructor nil)
3859 (:constructor make-js2-array-node (&key (type js2-ARRAYLIT)
3860 (pos js2-ts-cursor)
3861 len elems)))
3862 "AST node for an array literal."
3863 elems) ; list of expressions. [foo,,bar] yields a nil middle element.
3864
3865 (put 'cl-struct-js2-array-node 'js2-visitor 'js2-visit-array-node)
3866 (put 'cl-struct-js2-array-node 'js2-printer 'js2-print-array-node)
3867
3868 (defun js2-visit-array-node (n v)
3869 (dolist (e (js2-array-node-elems n))
3870 (js2-visit-ast e v))) ; Can be nil; e.g. [a, ,b].
3871
3872 (defun js2-print-array-node (n i)
3873 (insert (js2-make-pad i) "[")
3874 (let ((elems (js2-array-node-elems n)))
3875 (js2-print-list elems)
3876 (when (and elems (null (car (last elems))))
3877 (insert ",")))
3878 (insert "]"))
3879
3880 (cl-defstruct (js2-class-node
3881 (:include js2-node)
3882 (:constructor nil)
3883 (:constructor make-js2-class-node (&key (type js2-CLASS)
3884 (pos js2-ts-cursor)
3885 (form 'CLASS_STATEMENT)
3886 (name "")
3887 extends len elems)))
3888 "AST node for an class expression.
3889 `elems' is a list of `js2-object-prop-node', and `extends' is an
3890 optional `js2-expr-node'"
3891 form ; CLASS_{STATEMENT|EXPRESSION}
3892 name ; class name (a `js2-node-name', or nil if anonymous)
3893 extends ; class heritage (a `js2-expr-node', or nil if none)
3894 elems)
3895
3896 (put 'cl-struct-js2-class-node 'js2-visitor 'js2-visit-class-node)
3897 (put 'cl-struct-js2-class-node 'js2-printer 'js2-print-class-node)
3898
3899 (defun js2-visit-class-node (n v)
3900 (js2-visit-ast (js2-class-node-name n) v)
3901 (js2-visit-ast (js2-class-node-extends n) v)
3902 (dolist (e (js2-class-node-elems n))
3903 (js2-visit-ast e v)))
3904
3905 (defun js2-print-class-node (n i)
3906 (let* ((pad (js2-make-pad i))
3907 (name (js2-class-node-name n))
3908 (extends (js2-class-node-extends n))
3909 (elems (js2-class-node-elems n)))
3910 (insert pad "class")
3911 (when name
3912 (insert " ")
3913 (js2-print-ast name 0))
3914 (when extends
3915 (insert " extends ")
3916 (js2-print-ast extends))
3917 (insert " {")
3918 (dolist (elem elems)
3919 (insert "\n")
3920 (if (js2-node-get-prop elem 'STATIC)
3921 (progn (insert (js2-make-pad (1+ i)) "static ")
3922 (js2-print-ast elem 0)) ;; TODO(sdh): indentation isn't quite right
3923 (js2-print-ast elem (1+ i))))
3924 (insert "\n" pad "}")))
3925
3926 (cl-defstruct (js2-object-node
3927 (:include js2-node)
3928 (:constructor nil)
3929 (:constructor make-js2-object-node (&key (type js2-OBJECTLIT)
3930 (pos js2-ts-cursor)
3931 len
3932 elems)))
3933 "AST node for an object literal expression.
3934 `elems' is a list of `js2-object-prop-node'."
3935 elems)
3936
3937 (put 'cl-struct-js2-object-node 'js2-visitor 'js2-visit-object-node)
3938 (put 'cl-struct-js2-object-node 'js2-printer 'js2-print-object-node)
3939
3940 (defun js2-visit-object-node (n v)
3941 (dolist (e (js2-object-node-elems n))
3942 (js2-visit-ast e v)))
3943
3944 (defun js2-print-object-node (n i)
3945 (insert (js2-make-pad i) "{")
3946 (js2-print-list (js2-object-node-elems n))
3947 (insert "}"))
3948
3949 (cl-defstruct (js2-object-prop-node
3950 (:include js2-infix-node)
3951 (:constructor nil)
3952 (:constructor make-js2-object-prop-node (&key (type js2-COLON)
3953 (pos js2-ts-cursor)
3954 len left
3955 right op-pos)))
3956 "AST node for an object literal prop:value entry.
3957 The `left' field is the property: a name node, string node or number node.
3958 The `right' field is a `js2-node' representing the initializer value.
3959 If the property is abbreviated, the node's `SHORTHAND' property is non-nil
3960 and both fields have the same value.")
3961
3962 (put 'cl-struct-js2-object-prop-node 'js2-visitor 'js2-visit-infix-node)
3963 (put 'cl-struct-js2-object-prop-node 'js2-printer 'js2-print-object-prop-node)
3964
3965 (defun js2-print-object-prop-node (n i)
3966 (let* ((left (js2-object-prop-node-left n))
3967 (right (js2-object-prop-node-right n))
3968 (computed (not (or (js2-string-node-p left)
3969 (js2-number-node-p left)
3970 (js2-name-node-p left)))))
3971 (insert (js2-make-pad i))
3972 (if computed
3973 (insert "["))
3974 (js2-print-ast left 0)
3975 (if computed
3976 (insert "]"))
3977 (if (not (js2-node-get-prop n 'SHORTHAND))
3978 (progn
3979 (insert ": ")
3980 (js2-print-ast right 0)))))
3981
3982 (cl-defstruct (js2-getter-setter-node
3983 (:include js2-infix-node)
3984 (:constructor nil)
3985 (:constructor make-js2-getter-setter-node (&key type ; GET, SET, or FUNCTION
3986 (pos js2-ts-cursor)
3987 len left right)))
3988 "AST node for a getter/setter property in an object literal.
3989 The `left' field is the `js2-name-node' naming the getter/setter prop.
3990 The `right' field is always an anonymous `js2-function-node' with a node
3991 property `GETTER_SETTER' set to js2-GET, js2-SET, or js2-FUNCTION. ")
3992
3993 (put 'cl-struct-js2-getter-setter-node 'js2-visitor 'js2-visit-infix-node)
3994 (put 'cl-struct-js2-getter-setter-node 'js2-printer 'js2-print-getter-setter)
3995
3996 (defun js2-print-getter-setter (n i)
3997 (let* ((pad (js2-make-pad i))
3998 (left (js2-getter-setter-node-left n))
3999 (right (js2-getter-setter-node-right n))
4000 (computed (not (or (js2-string-node-p left)
4001 (js2-number-node-p left)
4002 (js2-name-node-p left)))))
4003 (insert pad)
4004 (if (/= (js2-node-type n) js2-FUNCTION)
4005 (insert (if (= (js2-node-type n) js2-GET) "get " "set ")))
4006 (when (and (js2-function-node-p right)
4007 (eq 'STAR (js2-function-node-generator-type right)))
4008 (insert "*"))
4009 (when computed
4010 (insert "["))
4011 (js2-print-ast left 0)
4012 (when computed
4013 (insert "]"))
4014 (js2-print-ast right 0)))
4015
4016 (cl-defstruct (js2-prop-get-node
4017 (:include js2-infix-node)
4018 (:constructor nil)
4019 (:constructor make-js2-prop-get-node (&key (type js2-GETPROP)
4020 (pos js2-ts-cursor)
4021 len left right)))
4022 "AST node for a dotted property reference, e.g. foo.bar or foo().bar")
4023
4024 (put 'cl-struct-js2-prop-get-node 'js2-visitor 'js2-visit-prop-get-node)
4025 (put 'cl-struct-js2-prop-get-node 'js2-printer 'js2-print-prop-get-node)
4026
4027 (defun js2-visit-prop-get-node (n v)
4028 (js2-visit-ast (js2-prop-get-node-left n) v)
4029 (js2-visit-ast (js2-prop-get-node-right n) v))
4030
4031 (defun js2-print-prop-get-node (n i)
4032 (insert (js2-make-pad i))
4033 (js2-print-ast (js2-prop-get-node-left n) 0)
4034 (insert ".")
4035 (js2-print-ast (js2-prop-get-node-right n) 0))
4036
4037 (cl-defstruct (js2-elem-get-node
4038 (:include js2-node)
4039 (:constructor nil)
4040 (:constructor make-js2-elem-get-node (&key (type js2-GETELEM)
4041 (pos js2-ts-cursor)
4042 len target element
4043 lb rb)))
4044 "AST node for an array index expression such as foo[bar]."
4045 target ; a `js2-node' - the expression preceding the "."
4046 element ; a `js2-node' - the expression in brackets
4047 lb ; position of left-bracket, nil if omitted
4048 rb) ; position of right-bracket, nil if omitted
4049
4050 (put 'cl-struct-js2-elem-get-node 'js2-visitor 'js2-visit-elem-get-node)
4051 (put 'cl-struct-js2-elem-get-node 'js2-printer 'js2-print-elem-get-node)
4052
4053 (defun js2-visit-elem-get-node (n v)
4054 (js2-visit-ast (js2-elem-get-node-target n) v)
4055 (js2-visit-ast (js2-elem-get-node-element n) v))
4056
4057 (defun js2-print-elem-get-node (n i)
4058 (insert (js2-make-pad i))
4059 (js2-print-ast (js2-elem-get-node-target n) 0)
4060 (insert "[")
4061 (js2-print-ast (js2-elem-get-node-element n) 0)
4062 (insert "]"))
4063
4064 (cl-defstruct (js2-call-node
4065 (:include js2-node)
4066 (:constructor nil)
4067 (:constructor make-js2-call-node (&key (type js2-CALL)
4068 (pos js2-ts-cursor)
4069 len target args
4070 lp rp)))
4071 "AST node for a JavaScript function call."
4072 target ; a `js2-node' evaluating to the function to call
4073 args ; a Lisp list of `js2-node' arguments
4074 lp ; position of open-paren, or nil if missing
4075 rp) ; position of close-paren, or nil if missing
4076
4077 (put 'cl-struct-js2-call-node 'js2-visitor 'js2-visit-call-node)
4078 (put 'cl-struct-js2-call-node 'js2-printer 'js2-print-call-node)
4079
4080 (defun js2-visit-call-node (n v)
4081 (js2-visit-ast (js2-call-node-target n) v)
4082 (dolist (arg (js2-call-node-args n))
4083 (js2-visit-ast arg v)))
4084
4085 (defun js2-print-call-node (n i)
4086 (insert (js2-make-pad i))
4087 (js2-print-ast (js2-call-node-target n) 0)
4088 (insert "(")
4089 (js2-print-list (js2-call-node-args n))
4090 (insert ")"))
4091
4092 (cl-defstruct (js2-yield-node
4093 (:include js2-node)
4094 (:constructor nil)
4095 (:constructor make-js2-yield-node (&key (type js2-YIELD)
4096 (pos js2-ts-cursor)
4097 len value star-p)))
4098 "AST node for yield statement or expression."
4099 star-p ; whether it's yield*
4100 value) ; optional: value to be yielded
4101
4102 (put 'cl-struct-js2-yield-node 'js2-visitor 'js2-visit-yield-node)
4103 (put 'cl-struct-js2-yield-node 'js2-printer 'js2-print-yield-node)
4104
4105 (defun js2-visit-yield-node (n v)
4106 (js2-visit-ast (js2-yield-node-value n) v))
4107
4108 (defun js2-print-yield-node (n i)
4109 (insert (js2-make-pad i))
4110 (insert "yield")
4111 (when (js2-yield-node-star-p n)
4112 (insert "*"))
4113 (when (js2-yield-node-value n)
4114 (insert " ")
4115 (js2-print-ast (js2-yield-node-value n) 0)))
4116
4117 (cl-defstruct (js2-paren-node
4118 (:include js2-node)
4119 (:constructor nil)
4120 (:constructor make-js2-paren-node (&key (type js2-LP)
4121 (pos js2-ts-cursor)
4122 len expr)))
4123 "AST node for a parenthesized expression.
4124 In particular, used when the parens are syntactically optional,
4125 as opposed to required parens such as those enclosing an if-conditional."
4126 expr) ; `js2-node'
4127
4128 (put 'cl-struct-js2-paren-node 'js2-visitor 'js2-visit-paren-node)
4129 (put 'cl-struct-js2-paren-node 'js2-printer 'js2-print-paren-node)
4130
4131 (defun js2-visit-paren-node (n v)
4132 (js2-visit-ast (js2-paren-node-expr n) v))
4133
4134 (defun js2-print-paren-node (n i)
4135 (insert (js2-make-pad i))
4136 (insert "(")
4137 (js2-print-ast (js2-paren-node-expr n) 0)
4138 (insert ")"))
4139
4140 (cl-defstruct (js2-comp-node
4141 (:include js2-scope)
4142 (:constructor nil)
4143 (:constructor make-js2-comp-node (&key (type js2-ARRAYCOMP)
4144 (pos js2-ts-cursor)
4145 len result
4146 loops filters
4147 form)))
4148 "AST node for an Array comprehension such as [[x,y] for (x in foo) for (y in bar)]."
4149 result ; result expression (just after left-bracket)
4150 loops ; a Lisp list of `js2-comp-loop-node'
4151 filters ; a Lisp list of guard/filter expressions
4152 form ; ARRAY, LEGACY_ARRAY or STAR_GENERATOR
4153 ; SpiderMonkey also supports "legacy generator expressions", but we dont.
4154 )
4155
4156 (put 'cl-struct-js2-comp-node 'js2-visitor 'js2-visit-comp-node)
4157 (put 'cl-struct-js2-comp-node 'js2-printer 'js2-print-comp-node)
4158
4159 (defun js2-visit-comp-node (n v)
4160 (js2-visit-ast (js2-comp-node-result n) v)
4161 (dolist (l (js2-comp-node-loops n))
4162 (js2-visit-ast l v))
4163 (dolist (f (js2-comp-node-filters n))
4164 (js2-visit-ast f v)))
4165
4166 (defun js2-print-comp-node (n i)
4167 (let ((pad (js2-make-pad i))
4168 (result (js2-comp-node-result n))
4169 (loops (js2-comp-node-loops n))
4170 (filters (js2-comp-node-filters n))
4171 (legacy-p (eq (js2-comp-node-form n) 'LEGACY_ARRAY))
4172 (gen-p (eq (js2-comp-node-form n) 'STAR_GENERATOR)))
4173 (insert pad (if gen-p "(" "["))
4174 (when legacy-p
4175 (js2-print-ast result 0))
4176 (dolist (l loops)
4177 (when legacy-p
4178 (insert " "))
4179 (js2-print-ast l 0)
4180 (unless legacy-p
4181 (insert " ")))
4182 (dolist (f filters)
4183 (when legacy-p
4184 (insert " "))
4185 (insert "if (")
4186 (js2-print-ast f 0)
4187 (insert ")")
4188 (unless legacy-p
4189 (insert " ")))
4190 (unless legacy-p
4191 (js2-print-ast result 0))
4192 (insert (if gen-p ")" "]"))))
4193
4194 (cl-defstruct (js2-comp-loop-node
4195 (:include js2-for-in-node)
4196 (:constructor nil)
4197 (:constructor make-js2-comp-loop-node (&key (type js2-FOR)
4198 (pos js2-ts-cursor)
4199 len iterator
4200 object in-pos
4201 foreach-p
4202 each-pos
4203 forof-p
4204 lp rp)))
4205 "AST subtree for each 'for (foo in bar)' loop in an array comprehension.")
4206
4207 (put 'cl-struct-js2-comp-loop-node 'js2-visitor 'js2-visit-comp-loop)
4208 (put 'cl-struct-js2-comp-loop-node 'js2-printer 'js2-print-comp-loop)
4209
4210 (defun js2-visit-comp-loop (n v)
4211 (js2-visit-ast (js2-comp-loop-node-iterator n) v)
4212 (js2-visit-ast (js2-comp-loop-node-object n) v))
4213
4214 (defun js2-print-comp-loop (n _i)
4215 (insert "for ")
4216 (when (js2-comp-loop-node-foreach-p n) (insert "each "))
4217 (insert "(")
4218 (js2-print-ast (js2-comp-loop-node-iterator n) 0)
4219 (insert (if (js2-comp-loop-node-forof-p n)
4220 " of " " in "))
4221 (js2-print-ast (js2-comp-loop-node-object n) 0)
4222 (insert ")"))
4223
4224 (cl-defstruct (js2-empty-expr-node
4225 (:include js2-node)
4226 (:constructor nil)
4227 (:constructor make-js2-empty-expr-node (&key (type js2-EMPTY)
4228 (pos (js2-current-token-beg))
4229 len)))
4230 "AST node for an empty expression.")
4231
4232 (put 'cl-struct-js2-empty-expr-node 'js2-visitor 'js2-visit-none)
4233 (put 'cl-struct-js2-empty-expr-node 'js2-printer 'js2-print-none)
4234
4235 (cl-defstruct (js2-xml-node
4236 (:include js2-block-node)
4237 (:constructor nil)
4238 (:constructor make-js2-xml-node (&key (type js2-XML)
4239 (pos (js2-current-token-beg))
4240 len kids)))
4241 "AST node for initial parse of E4X literals.
4242 The kids field is a list of XML fragments, each a `js2-string-node' or
4243 a `js2-xml-js-expr-node'. Equivalent to Rhino's XmlLiteral node.")
4244
4245 (put 'cl-struct-js2-xml-node 'js2-visitor 'js2-visit-block)
4246 (put 'cl-struct-js2-xml-node 'js2-printer 'js2-print-xml-node)
4247
4248 (defun js2-print-xml-node (n i)
4249 (dolist (kid (js2-xml-node-kids n))
4250 (js2-print-ast kid i)))
4251
4252 (cl-defstruct (js2-xml-js-expr-node
4253 (:include js2-xml-node)
4254 (:constructor nil)
4255 (:constructor make-js2-xml-js-expr-node (&key (type js2-XML)
4256 (pos js2-ts-cursor)
4257 len expr)))
4258 "AST node for an embedded JavaScript {expression} in an E4X literal.
4259 The start and end fields correspond to the curly-braces."
4260 expr) ; a `js2-expr-node' of some sort
4261
4262 (put 'cl-struct-js2-xml-js-expr-node 'js2-visitor 'js2-visit-xml-js-expr)
4263 (put 'cl-struct-js2-xml-js-expr-node 'js2-printer 'js2-print-xml-js-expr)
4264
4265 (defun js2-visit-xml-js-expr (n v)
4266 (js2-visit-ast (js2-xml-js-expr-node-expr n) v))
4267
4268 (defun js2-print-xml-js-expr (n i)
4269 (insert (js2-make-pad i))
4270 (insert "{")
4271 (js2-print-ast (js2-xml-js-expr-node-expr n) 0)
4272 (insert "}"))
4273
4274 (cl-defstruct (js2-xml-dot-query-node
4275 (:include js2-infix-node)
4276 (:constructor nil)
4277 (:constructor make-js2-xml-dot-query-node (&key (type js2-DOTQUERY)
4278 (pos js2-ts-cursor)
4279 op-pos len left
4280 right rp)))
4281 "AST node for an E4X foo.(bar) filter expression.
4282 Note that the left-paren is automatically the character immediately
4283 following the dot (.) in the operator. No whitespace is permitted
4284 between the dot and the lp by the scanner."
4285 rp)
4286
4287 (put 'cl-struct-js2-xml-dot-query-node 'js2-visitor 'js2-visit-infix-node)
4288 (put 'cl-struct-js2-xml-dot-query-node 'js2-printer 'js2-print-xml-dot-query)
4289
4290 (defun js2-print-xml-dot-query (n i)
4291 (insert (js2-make-pad i))
4292 (js2-print-ast (js2-xml-dot-query-node-left n) 0)
4293 (insert ".(")
4294 (js2-print-ast (js2-xml-dot-query-node-right n) 0)
4295 (insert ")"))
4296
4297 (cl-defstruct (js2-xml-ref-node
4298 (:include js2-node)
4299 (:constructor nil)) ; abstract
4300 "Base type for E4X XML attribute-access or property-get expressions.
4301 Such expressions can take a variety of forms. The general syntax has
4302 three parts:
4303
4304 - (optional) an @ (specifying an attribute access)
4305 - (optional) a namespace (a `js2-name-node') and double-colon
4306 - (required) either a `js2-name-node' or a bracketed [expression]
4307
4308 The property-name expressions (examples: ns::name, @name) are
4309 represented as `js2-xml-prop-ref' nodes. The bracketed-expression
4310 versions (examples: ns::[name], @[name]) become `js2-xml-elem-ref' nodes.
4311
4312 This node type (or more specifically, its subclasses) will sometimes
4313 be the right-hand child of a `js2-prop-get-node' or a
4314 `js2-infix-node' of type `js2-DOTDOT', the .. xml-descendants operator.
4315 The `js2-xml-ref-node' may also be a standalone primary expression with
4316 no explicit target, which is valid in certain expression contexts such as
4317
4318 company..employee.(@id < 100)
4319
4320 in this case, the @id is a `js2-xml-ref' that is part of an infix '<'
4321 expression whose parent is a `js2-xml-dot-query-node'."
4322 namespace
4323 at-pos
4324 colon-pos)
4325
4326 (defsubst js2-xml-ref-node-attr-access-p (node)
4327 "Return non-nil if this expression began with an @-token."
4328 (and (numberp (js2-xml-ref-node-at-pos node))
4329 (cl-plusp (js2-xml-ref-node-at-pos node))))
4330
4331 (cl-defstruct (js2-xml-prop-ref-node
4332 (:include js2-xml-ref-node)
4333 (:constructor nil)
4334 (:constructor make-js2-xml-prop-ref-node (&key (type js2-REF_NAME)
4335 (pos (js2-current-token-beg))
4336 len propname
4337 namespace at-pos
4338 colon-pos)))
4339 "AST node for an E4X XML [expr] property-ref expression.
4340 The JavaScript syntax is an optional @, an optional ns::, and a name.
4341
4342 [ '@' ] [ name '::' ] name
4343
4344 Examples include name, ns::name, ns::*, *::name, *::*, @attr, @ns::attr,
4345 @ns::*, @*::attr, @*::*, and @*.
4346
4347 The node starts at the @ token, if present. Otherwise it starts at the
4348 namespace name. The node bounds extend through the closing right-bracket,
4349 or if it is missing due to a syntax error, through the end of the index
4350 expression."
4351 propname)
4352
4353 (put 'cl-struct-js2-xml-prop-ref-node 'js2-visitor 'js2-visit-xml-prop-ref-node)
4354 (put 'cl-struct-js2-xml-prop-ref-node 'js2-printer 'js2-print-xml-prop-ref-node)
4355
4356 (defun js2-visit-xml-prop-ref-node (n v)
4357 (js2-visit-ast (js2-xml-prop-ref-node-namespace n) v)
4358 (js2-visit-ast (js2-xml-prop-ref-node-propname n) v))
4359
4360 (defun js2-print-xml-prop-ref-node (n i)
4361 (insert (js2-make-pad i))
4362 (if (js2-xml-ref-node-attr-access-p n)
4363 (insert "@"))
4364 (when (js2-xml-prop-ref-node-namespace n)
4365 (js2-print-ast (js2-xml-prop-ref-node-namespace n) 0)
4366 (insert "::"))
4367 (if (js2-xml-prop-ref-node-propname n)
4368 (js2-print-ast (js2-xml-prop-ref-node-propname n) 0)))
4369
4370 (cl-defstruct (js2-xml-elem-ref-node
4371 (:include js2-xml-ref-node)
4372 (:constructor nil)
4373 (:constructor make-js2-xml-elem-ref-node (&key (type js2-REF_MEMBER)
4374 (pos (js2-current-token-beg))
4375 len expr lb rb
4376 namespace at-pos
4377 colon-pos)))
4378 "AST node for an E4X XML [expr] member-ref expression.
4379 Syntax:
4380
4381 [ '@' ] [ name '::' ] '[' expr ']'
4382
4383 Examples include ns::[expr], @ns::[expr], @[expr], *::[expr] and @*::[expr].
4384
4385 Note that the form [expr] (i.e. no namespace or attribute-qualifier)
4386 is not a legal E4X XML element-ref expression, since it's already used
4387 for standard JavaScript element-get array indexing. Hence, a
4388 `js2-xml-elem-ref-node' always has either the attribute-qualifier, a
4389 non-nil namespace node, or both.
4390
4391 The node starts at the @ token, if present. Otherwise it starts
4392 at the namespace name. The node bounds extend through the closing
4393 right-bracket, or if it is missing due to a syntax error, through the
4394 end of the index expression."
4395 expr ; the bracketed index expression
4396 lb
4397 rb)
4398
4399 (put 'cl-struct-js2-xml-elem-ref-node 'js2-visitor 'js2-visit-xml-elem-ref-node)
4400 (put 'cl-struct-js2-xml-elem-ref-node 'js2-printer 'js2-print-xml-elem-ref-node)
4401
4402 (defun js2-visit-xml-elem-ref-node (n v)
4403 (js2-visit-ast (js2-xml-elem-ref-node-namespace n) v)
4404 (js2-visit-ast (js2-xml-elem-ref-node-expr n) v))
4405
4406 (defun js2-print-xml-elem-ref-node (n i)
4407 (insert (js2-make-pad i))
4408 (if (js2-xml-ref-node-attr-access-p n)
4409 (insert "@"))
4410 (when (js2-xml-elem-ref-node-namespace n)
4411 (js2-print-ast (js2-xml-elem-ref-node-namespace n) 0)
4412 (insert "::"))
4413 (insert "[")
4414 (if (js2-xml-elem-ref-node-expr n)
4415 (js2-print-ast (js2-xml-elem-ref-node-expr n) 0))
4416 (insert "]"))
4417
4418 ;;; Placeholder nodes for when we try parsing the XML literals structurally.
4419
4420 (cl-defstruct (js2-xml-start-tag-node
4421 (:include js2-xml-node)
4422 (:constructor nil)
4423 (:constructor make-js2-xml-start-tag-node (&key (type js2-XML)
4424 (pos js2-ts-cursor)
4425 len name attrs kids
4426 empty-p)))
4427 "AST node for an XML start-tag. Not currently used.
4428 The `kids' field is a Lisp list of child content nodes."
4429 name ; a `js2-xml-name-node'
4430 attrs ; a Lisp list of `js2-xml-attr-node'
4431 empty-p) ; t if this is an empty element such as <foo bar="baz"/>
4432
4433 (put 'cl-struct-js2-xml-start-tag-node 'js2-visitor 'js2-visit-xml-start-tag)
4434 (put 'cl-struct-js2-xml-start-tag-node 'js2-printer 'js2-print-xml-start-tag)
4435
4436 (defun js2-visit-xml-start-tag (n v)
4437 (js2-visit-ast (js2-xml-start-tag-node-name n) v)
4438 (dolist (attr (js2-xml-start-tag-node-attrs n))
4439 (js2-visit-ast attr v))
4440 (js2-visit-block n v))
4441
4442 (defun js2-print-xml-start-tag (n i)
4443 (insert (js2-make-pad i) "<")
4444 (js2-print-ast (js2-xml-start-tag-node-name n) 0)
4445 (when (js2-xml-start-tag-node-attrs n)
4446 (insert " ")
4447 (js2-print-list (js2-xml-start-tag-node-attrs n) " "))
4448 (insert ">"))
4449
4450 ;; I -think- I'm going to make the parent node the corresponding start-tag,
4451 ;; and add the end-tag to the kids list of the parent as well.
4452 (cl-defstruct (js2-xml-end-tag-node
4453 (:include js2-xml-node)
4454 (:constructor nil)
4455 (:constructor make-js2-xml-end-tag-node (&key (type js2-XML)
4456 (pos js2-ts-cursor)
4457 len name)))
4458 "AST node for an XML end-tag. Not currently used."
4459 name) ; a `js2-xml-name-node'
4460
4461 (put 'cl-struct-js2-xml-end-tag-node 'js2-visitor 'js2-visit-xml-end-tag)
4462 (put 'cl-struct-js2-xml-end-tag-node 'js2-printer 'js2-print-xml-end-tag)
4463
4464 (defun js2-visit-xml-end-tag (n v)
4465 (js2-visit-ast (js2-xml-end-tag-node-name n) v))
4466
4467 (defun js2-print-xml-end-tag (n i)
4468 (insert (js2-make-pad i))
4469 (insert "</")
4470 (js2-print-ast (js2-xml-end-tag-node-name n) 0)
4471 (insert ">"))
4472
4473 (cl-defstruct (js2-xml-name-node
4474 (:include js2-xml-node)
4475 (:constructor nil)
4476 (:constructor make-js2-xml-name-node (&key (type js2-XML)
4477 (pos js2-ts-cursor)
4478 len namespace kids)))
4479 "AST node for an E4X XML name. Not currently used.
4480 Any XML name can be qualified with a namespace, hence the namespace field.
4481 Further, any E4X name can be comprised of arbitrary JavaScript {} expressions.
4482 The kids field is a list of `js2-name-node' and `js2-xml-js-expr-node'.
4483 For a simple name, the kids list has exactly one node, a `js2-name-node'."
4484 namespace) ; a `js2-string-node'
4485
4486 (put 'cl-struct-js2-xml-name-node 'js2-visitor 'js2-visit-xml-name-node)
4487 (put 'cl-struct-js2-xml-name-node 'js2-printer 'js2-print-xml-name-node)
4488
4489 (defun js2-visit-xml-name-node (n v)
4490 (js2-visit-ast (js2-xml-name-node-namespace n) v))
4491
4492 (defun js2-print-xml-name-node (n i)
4493 (insert (js2-make-pad i))
4494 (when (js2-xml-name-node-namespace n)
4495 (js2-print-ast (js2-xml-name-node-namespace n) 0)
4496 (insert "::"))
4497 (dolist (kid (js2-xml-name-node-kids n))
4498 (js2-print-ast kid 0)))
4499
4500 (cl-defstruct (js2-xml-pi-node
4501 (:include js2-xml-node)
4502 (:constructor nil)
4503 (:constructor make-js2-xml-pi-node (&key (type js2-XML)
4504 (pos js2-ts-cursor)
4505 len name attrs)))
4506 "AST node for an E4X XML processing instruction. Not currently used."
4507 name ; a `js2-xml-name-node'
4508 attrs) ; a list of `js2-xml-attr-node'
4509
4510 (put 'cl-struct-js2-xml-pi-node 'js2-visitor 'js2-visit-xml-pi-node)
4511 (put 'cl-struct-js2-xml-pi-node 'js2-printer 'js2-print-xml-pi-node)
4512
4513 (defun js2-visit-xml-pi-node (n v)
4514 (js2-visit-ast (js2-xml-pi-node-name n) v)
4515 (dolist (attr (js2-xml-pi-node-attrs n))
4516 (js2-visit-ast attr v)))
4517
4518 (defun js2-print-xml-pi-node (n i)
4519 (insert (js2-make-pad i) "<?")
4520 (js2-print-ast (js2-xml-pi-node-name n))
4521 (when (js2-xml-pi-node-attrs n)
4522 (insert " ")
4523 (js2-print-list (js2-xml-pi-node-attrs n)))
4524 (insert "?>"))
4525
4526 (cl-defstruct (js2-xml-cdata-node
4527 (:include js2-xml-node)
4528 (:constructor nil)
4529 (:constructor make-js2-xml-cdata-node (&key (type js2-XML)
4530 (pos js2-ts-cursor)
4531 len content)))
4532 "AST node for a CDATA escape section. Not currently used."
4533 content) ; a `js2-string-node' with node-property 'quote-type 'cdata
4534
4535 (put 'cl-struct-js2-xml-cdata-node 'js2-visitor 'js2-visit-xml-cdata-node)
4536 (put 'cl-struct-js2-xml-cdata-node 'js2-printer 'js2-print-xml-cdata-node)
4537
4538 (defun js2-visit-xml-cdata-node (n v)
4539 (js2-visit-ast (js2-xml-cdata-node-content n) v))
4540
4541 (defun js2-print-xml-cdata-node (n i)
4542 (insert (js2-make-pad i))
4543 (js2-print-ast (js2-xml-cdata-node-content n)))
4544
4545 (cl-defstruct (js2-xml-attr-node
4546 (:include js2-xml-node)
4547 (:constructor nil)
4548 (:constructor make-js2-attr-node (&key (type js2-XML)
4549 (pos js2-ts-cursor)
4550 len name value
4551 eq-pos quote-type)))
4552 "AST node representing a foo='bar' XML attribute value. Not yet used."
4553 name ; a `js2-xml-name-node'
4554 value ; a `js2-xml-name-node'
4555 eq-pos ; buffer position of "=" sign
4556 quote-type) ; 'single or 'double
4557
4558 (put 'cl-struct-js2-xml-attr-node 'js2-visitor 'js2-visit-xml-attr-node)
4559 (put 'cl-struct-js2-xml-attr-node 'js2-printer 'js2-print-xml-attr-node)
4560
4561 (defun js2-visit-xml-attr-node (n v)
4562 (js2-visit-ast (js2-xml-attr-node-name n) v)
4563 (js2-visit-ast (js2-xml-attr-node-value n) v))
4564
4565 (defun js2-print-xml-attr-node (n i)
4566 (let ((quote (if (eq (js2-xml-attr-node-quote-type n) 'single)
4567 "'"
4568 "\"")))
4569 (insert (js2-make-pad i))
4570 (js2-print-ast (js2-xml-attr-node-name n) 0)
4571 (insert "=" quote)
4572 (js2-print-ast (js2-xml-attr-node-value n) 0)
4573 (insert quote)))
4574
4575 (cl-defstruct (js2-xml-text-node
4576 (:include js2-xml-node)
4577 (:constructor nil)
4578 (:constructor make-js2-text-node (&key (type js2-XML)
4579 (pos js2-ts-cursor)
4580 len content)))
4581 "AST node for an E4X XML text node. Not currently used."
4582 content) ; a Lisp list of `js2-string-node' and `js2-xml-js-expr-node'
4583
4584 (put 'cl-struct-js2-xml-text-node 'js2-visitor 'js2-visit-xml-text-node)
4585 (put 'cl-struct-js2-xml-text-node 'js2-printer 'js2-print-xml-text-node)
4586
4587 (defun js2-visit-xml-text-node (n v)
4588 (js2-visit-ast (js2-xml-text-node-content n) v))
4589
4590 (defun js2-print-xml-text-node (n i)
4591 (insert (js2-make-pad i))
4592 (dolist (kid (js2-xml-text-node-content n))
4593 (js2-print-ast kid)))
4594
4595 (cl-defstruct (js2-xml-comment-node
4596 (:include js2-xml-node)
4597 (:constructor nil)
4598 (:constructor make-js2-xml-comment-node (&key (type js2-XML)
4599 (pos js2-ts-cursor)
4600 len)))
4601 "AST node for E4X XML comment. Not currently used.")
4602
4603 (put 'cl-struct-js2-xml-comment-node 'js2-visitor 'js2-visit-none)
4604 (put 'cl-struct-js2-xml-comment-node 'js2-printer 'js2-print-xml-comment)
4605
4606 (defun js2-print-xml-comment (n i)
4607 (insert (js2-make-pad i)
4608 (js2-node-string n)))
4609
4610 ;;; Node utilities
4611
4612 (defsubst js2-node-line (n)
4613 "Fetch the source line number at the start of node N.
4614 This is O(n) in the length of the source buffer; use prudently."
4615 (1+ (count-lines (point-min) (js2-node-abs-pos n))))
4616
4617 (defsubst js2-block-node-kid (n i)
4618 "Return child I of node N, or nil if there aren't that many."
4619 (nth i (js2-block-node-kids n)))
4620
4621 (defsubst js2-block-node-first (n)
4622 "Return first child of block node N, or nil if there is none."
4623 (cl-first (js2-block-node-kids n)))
4624
4625 (defun js2-node-root (n)
4626 "Return the root of the AST containing N.
4627 If N has no parent pointer, returns N."
4628 (let ((parent (js2-node-parent n)))
4629 (if parent
4630 (js2-node-root parent)
4631 n)))
4632
4633 (defsubst js2-node-short-name (n)
4634 "Return the short name of node N as a string, e.g. `js2-if-node'."
4635 (substring (symbol-name (aref n 0))
4636 (length "cl-struct-")))
4637
4638 (defun js2-node-child-list (node)
4639 "Return the child list for NODE, a Lisp list of nodes.
4640 Works for block nodes, array nodes, obj literals, funarg lists,
4641 var decls and try nodes (for catch clauses). Note that you should call
4642 `js2-block-node-kids' on the function body for the body statements.
4643 Returns nil for zero-length child lists or unsupported nodes."
4644 (cond
4645 ((js2-function-node-p node)
4646 (js2-function-node-params node))
4647 ((js2-block-node-p node)
4648 (js2-block-node-kids node))
4649 ((js2-try-node-p node)
4650 (js2-try-node-catch-clauses node))
4651 ((js2-array-node-p node)
4652 (js2-array-node-elems node))
4653 ((js2-object-node-p node)
4654 (js2-object-node-elems node))
4655 ((js2-call-node-p node)
4656 (js2-call-node-args node))
4657 ((js2-new-node-p node)
4658 (js2-new-node-args node))
4659 ((js2-var-decl-node-p node)
4660 (js2-var-decl-node-kids node))
4661 (t
4662 nil)))
4663
4664 (defun js2-node-set-child-list (node kids)
4665 "Set the child list for NODE to KIDS."
4666 (cond
4667 ((js2-function-node-p node)
4668 (setf (js2-function-node-params node) kids))
4669 ((js2-block-node-p node)
4670 (setf (js2-block-node-kids node) kids))
4671 ((js2-try-node-p node)
4672 (setf (js2-try-node-catch-clauses node) kids))
4673 ((js2-array-node-p node)
4674 (setf (js2-array-node-elems node) kids))
4675 ((js2-object-node-p node)
4676 (setf (js2-object-node-elems node) kids))
4677 ((js2-call-node-p node)
4678 (setf (js2-call-node-args node) kids))
4679 ((js2-new-node-p node)
4680 (setf (js2-new-node-args node) kids))
4681 ((js2-var-decl-node-p node)
4682 (setf (js2-var-decl-node-kids node) kids))
4683 (t
4684 (error "Unsupported node type: %s" (js2-node-short-name node))))
4685 kids)
4686
4687 ;; All because Common Lisp doesn't support multiple inheritance for defstructs.
4688 (defconst js2-paren-expr-nodes
4689 '(cl-struct-js2-comp-loop-node
4690 cl-struct-js2-comp-node
4691 cl-struct-js2-call-node
4692 cl-struct-js2-catch-node
4693 cl-struct-js2-do-node
4694 cl-struct-js2-elem-get-node
4695 cl-struct-js2-for-in-node
4696 cl-struct-js2-for-node
4697 cl-struct-js2-function-node
4698 cl-struct-js2-if-node
4699 cl-struct-js2-let-node
4700 cl-struct-js2-new-node
4701 cl-struct-js2-paren-node
4702 cl-struct-js2-switch-node
4703 cl-struct-js2-while-node
4704 cl-struct-js2-with-node
4705 cl-struct-js2-xml-dot-query-node)
4706 "Node types that can have a parenthesized child expression.
4707 In particular, nodes that respond to `js2-node-lp' and `js2-node-rp'.")
4708
4709 (defsubst js2-paren-expr-node-p (node)
4710 "Return t for nodes that typically have a parenthesized child expression.
4711 Useful for computing the indentation anchors for arg-lists and conditions.
4712 Note that it may return a false positive, for instance when NODE is
4713 a `js2-new-node' and there are no arguments or parentheses."
4714 (memq (aref node 0) js2-paren-expr-nodes))
4715
4716 ;; Fake polymorphism... yech.
4717 (defun js2-node-lp (node)
4718 "Return relative left-paren position for NODE, if applicable.
4719 For `js2-elem-get-node' structs, returns left-bracket position.
4720 Note that the position may be nil in the case of a parse error."
4721 (cond
4722 ((js2-elem-get-node-p node)
4723 (js2-elem-get-node-lb node))
4724 ((js2-loop-node-p node)
4725 (js2-loop-node-lp node))
4726 ((js2-function-node-p node)
4727 (js2-function-node-lp node))
4728 ((js2-if-node-p node)
4729 (js2-if-node-lp node))
4730 ((js2-new-node-p node)
4731 (js2-new-node-lp node))
4732 ((js2-call-node-p node)
4733 (js2-call-node-lp node))
4734 ((js2-paren-node-p node)
4735 0)
4736 ((js2-switch-node-p node)
4737 (js2-switch-node-lp node))
4738 ((js2-catch-node-p node)
4739 (js2-catch-node-lp node))
4740 ((js2-let-node-p node)
4741 (js2-let-node-lp node))
4742 ((js2-comp-node-p node)
4743 0)
4744 ((js2-with-node-p node)
4745 (js2-with-node-lp node))
4746 ((js2-xml-dot-query-node-p node)
4747 (1+ (js2-infix-node-op-pos node)))
4748 (t
4749 (error "Unsupported node type: %s" (js2-node-short-name node)))))
4750
4751 ;; Fake polymorphism... blech.
4752 (defun js2-node-rp (node)
4753 "Return relative right-paren position for NODE, if applicable.
4754 For `js2-elem-get-node' structs, returns right-bracket position.
4755 Note that the position may be nil in the case of a parse error."
4756 (cond
4757 ((js2-elem-get-node-p node)
4758 (js2-elem-get-node-rb node))
4759 ((js2-loop-node-p node)
4760 (js2-loop-node-rp node))
4761 ((js2-function-node-p node)
4762 (js2-function-node-rp node))
4763 ((js2-if-node-p node)
4764 (js2-if-node-rp node))
4765 ((js2-new-node-p node)
4766 (js2-new-node-rp node))
4767 ((js2-call-node-p node)
4768 (js2-call-node-rp node))
4769 ((js2-paren-node-p node)
4770 (1- (js2-node-len node)))
4771 ((js2-switch-node-p node)
4772 (js2-switch-node-rp node))
4773 ((js2-catch-node-p node)
4774 (js2-catch-node-rp node))
4775 ((js2-let-node-p node)
4776 (js2-let-node-rp node))
4777 ((js2-comp-node-p node)
4778 (1- (js2-node-len node)))
4779 ((js2-with-node-p node)
4780 (js2-with-node-rp node))
4781 ((js2-xml-dot-query-node-p node)
4782 (1+ (js2-xml-dot-query-node-rp node)))
4783 (t
4784 (error "Unsupported node type: %s" (js2-node-short-name node)))))
4785
4786 (defsubst js2-node-first-child (node)
4787 "Return the first element of `js2-node-child-list' for NODE."
4788 (car (js2-node-child-list node)))
4789
4790 (defsubst js2-node-last-child (node)
4791 "Return the last element of `js2-node-last-child' for NODE."
4792 (car (last (js2-node-child-list node))))
4793
4794 (defun js2-node-prev-sibling (node)
4795 "Return the previous statement in parent.
4796 Works for parents supported by `js2-node-child-list'.
4797 Returns nil if NODE is not in the parent, or PARENT is
4798 not a supported node, or if NODE is the first child."
4799 (let* ((p (js2-node-parent node))
4800 (kids (js2-node-child-list p))
4801 (sib (car kids)))
4802 (while (and kids
4803 (not (eq node (cadr kids))))
4804 (setq kids (cdr kids)
4805 sib (car kids)))
4806 sib))
4807
4808 (defun js2-node-next-sibling (node)
4809 "Return the next statement in parent block.
4810 Returns nil if NODE is not in the block, or PARENT is not
4811 a block node, or if NODE is the last statement."
4812 (let* ((p (js2-node-parent node))
4813 (kids (js2-node-child-list p)))
4814 (while (and kids
4815 (not (eq node (car kids))))
4816 (setq kids (cdr kids)))
4817 (cadr kids)))
4818
4819 (defun js2-node-find-child-before (pos parent &optional after)
4820 "Find the last child that starts before POS in parent.
4821 If AFTER is non-nil, returns first child starting after POS.
4822 POS is an absolute buffer position. PARENT is any node
4823 supported by `js2-node-child-list'.
4824 Returns nil if no applicable child is found."
4825 (let ((kids (if (js2-function-node-p parent)
4826 (js2-block-node-kids (js2-function-node-body parent))
4827 (js2-node-child-list parent)))
4828 (beg (js2-node-abs-pos (if (js2-function-node-p parent)
4829 (js2-function-node-body parent)
4830 parent)))
4831 kid result fn
4832 (continue t))
4833 (setq fn (if after '>= '<))
4834 (while (and kids continue)
4835 (setq kid (car kids))
4836 (if (funcall fn (+ beg (js2-node-pos kid)) pos)
4837 (setq result kid
4838 continue (not after))
4839 (setq continue after))
4840 (setq kids (cdr kids)))
4841 result))
4842
4843 (defun js2-node-find-child-after (pos parent)
4844 "Find first child that starts after POS in parent.
4845 POS is an absolute buffer position. PARENT is any node
4846 supported by `js2-node-child-list'.
4847 Returns nil if no applicable child is found."
4848 (js2-node-find-child-before pos parent 'after))
4849
4850 (defun js2-node-replace-child (pos parent new-node)
4851 "Replace node at index POS in PARENT with NEW-NODE.
4852 Only works for parents supported by `js2-node-child-list'."
4853 (let ((kids (js2-node-child-list parent))
4854 (i 0))
4855 (while (< i pos)
4856 (setq kids (cdr kids)
4857 i (1+ i)))
4858 (setcar kids new-node)
4859 (js2-node-add-children parent new-node)))
4860
4861 (defun js2-node-buffer (n)
4862 "Return the buffer associated with AST N.
4863 Returns nil if the buffer is not set as a property on the root
4864 node, or if parent links were not recorded during parsing."
4865 (let ((root (js2-node-root n)))
4866 (and root
4867 (js2-ast-root-p root)
4868 (js2-ast-root-buffer root))))
4869
4870 (defun js2-block-node-push (n kid)
4871 "Push js2-node KID onto the end of js2-block-node N's child list.
4872 KID is always added to the -end- of the kids list.
4873 Function also calls `js2-node-add-children' to add the parent link."
4874 (let ((kids (js2-node-child-list n)))
4875 (if kids
4876 (setcdr kids (nconc (cdr kids) (list kid)))
4877 (js2-node-set-child-list n (list kid)))
4878 (js2-node-add-children n kid)))
4879
4880 (defun js2-node-string (node)
4881 (with-current-buffer (or (js2-node-buffer node)
4882 (error "No buffer available for node %s" node))
4883 (let ((pos (js2-node-abs-pos node)))
4884 (buffer-substring-no-properties pos (+ pos (js2-node-len node))))))
4885
4886 ;; Container for storing the node we're looking for in a traversal.
4887 (js2-deflocal js2-discovered-node nil)
4888
4889 ;; Keep track of absolute node position during traversals.
4890 (js2-deflocal js2-visitor-offset nil)
4891
4892 (js2-deflocal js2-node-search-point nil)
4893
4894 (when js2-mode-dev-mode-p
4895 (defun js2-find-node-at-point ()
4896 (interactive)
4897 (let ((node (js2-node-at-point)))
4898 (message "%s" (or node "No node found at point"))))
4899 (defun js2-node-name-at-point ()
4900 (interactive)
4901 (let ((node (js2-node-at-point)))
4902 (message "%s" (if node
4903 (js2-node-short-name node)
4904 "No node found at point.")))))
4905
4906 (defun js2-node-at-point (&optional pos skip-comments)
4907 "Return AST node at POS, a buffer position, defaulting to current point.
4908 The `js2-mode-ast' variable must be set to the current parse tree.
4909 Signals an error if the AST (`js2-mode-ast') is nil.
4910 Always returns a node - if it can't find one, it returns the root.
4911 If SKIP-COMMENTS is non-nil, comment nodes are ignored."
4912 (let ((ast js2-mode-ast)
4913 result)
4914 (unless ast
4915 (error "No JavaScript AST available"))
4916 ;; Look through comments first, since they may be inside nodes that
4917 ;; would otherwise report a match.
4918 (setq pos (or pos (point))
4919 result (if (> pos (js2-node-abs-end ast))
4920 ast
4921 (if (not skip-comments)
4922 (js2-comment-at-point pos))))
4923 (unless result
4924 (setq js2-discovered-node nil
4925 js2-visitor-offset 0
4926 js2-node-search-point pos)
4927 (unwind-protect
4928 (catch 'js2-visit-done
4929 (js2-visit-ast ast #'js2-node-at-point-visitor))
4930 (setq js2-visitor-offset nil
4931 js2-node-search-point nil))
4932 (setq result js2-discovered-node))
4933 ;; may have found a comment beyond end of last child node,
4934 ;; since visiting the ast-root looks at the comment-list last.
4935 (if (and skip-comments
4936 (js2-comment-node-p result))
4937 (setq result nil))
4938 (or result js2-mode-ast)))
4939
4940 (defun js2-node-at-point-visitor (node end-p)
4941 (let ((rel-pos (js2-node-pos node))
4942 abs-pos
4943 abs-end
4944 (point js2-node-search-point))
4945 (cond
4946 (end-p
4947 ;; this evaluates to a non-nil return value, even if it's zero
4948 (cl-decf js2-visitor-offset rel-pos))
4949 ;; we already looked for comments before visiting, and don't want them now
4950 ((js2-comment-node-p node)
4951 nil)
4952 (t
4953 (setq abs-pos (cl-incf js2-visitor-offset rel-pos)
4954 ;; we only want to use the node if the point is before
4955 ;; the last character position in the node, so we decrement
4956 ;; the absolute end by 1.
4957 abs-end (+ abs-pos (js2-node-len node) -1))
4958 (cond
4959 ;; If this node starts after search-point, stop the search.
4960 ((> abs-pos point)
4961 (throw 'js2-visit-done nil))
4962 ;; If this node ends before the search-point, don't check kids.
4963 ((> point abs-end)
4964 nil)
4965 (t
4966 ;; Otherwise point is within this node, possibly in a child.
4967 (setq js2-discovered-node node)
4968 t)))))) ; keep processing kids to look for more specific match
4969
4970 (defsubst js2-block-comment-p (node)
4971 "Return non-nil if NODE is a comment node of format `jsdoc' or `block'."
4972 (and (js2-comment-node-p node)
4973 (memq (js2-comment-node-format node) '(jsdoc block))))
4974
4975 ;; TODO: put the comments in a vector and binary-search them instead
4976 (defun js2-comment-at-point (&optional pos)
4977 "Look through scanned comment nodes for one containing POS.
4978 POS is a buffer position that defaults to current point.
4979 Function returns nil if POS was not in any comment node."
4980 (let ((ast js2-mode-ast)
4981 (x (or pos (point)))
4982 beg end)
4983 (unless ast
4984 (error "No JavaScript AST available"))
4985 (catch 'done
4986 ;; Comments are stored in lexical order.
4987 (dolist (comment (js2-ast-root-comments ast) nil)
4988 (setq beg (js2-node-abs-pos comment)
4989 end (+ beg (js2-node-len comment)))
4990 (if (and (>= x beg)
4991 (<= x end))
4992 (throw 'done comment))))))
4993
4994 (defun js2-mode-find-parent-fn (node)
4995 "Find function enclosing NODE.
4996 Returns nil if NODE is not inside a function."
4997 (setq node (js2-node-parent node))
4998 (while (and node (not (js2-function-node-p node)))
4999 (setq node (js2-node-parent node)))
5000 (and (js2-function-node-p node) node))
5001
5002 (defun js2-mode-find-enclosing-fn (node)
5003 "Find function or root enclosing NODE."
5004 (if (js2-ast-root-p node)
5005 node
5006 (setq node (js2-node-parent node))
5007 (while (not (or (js2-ast-root-p node)
5008 (js2-function-node-p node)))
5009 (setq node (js2-node-parent node)))
5010 node))
5011
5012 (defun js2-mode-find-enclosing-node (beg end)
5013 "Find node fully enclosing BEG and END."
5014 (let ((node (js2-node-at-point beg))
5015 pos
5016 (continue t))
5017 (while continue
5018 (if (or (js2-ast-root-p node)
5019 (and
5020 (<= (setq pos (js2-node-abs-pos node)) beg)
5021 (>= (+ pos (js2-node-len node)) end)))
5022 (setq continue nil)
5023 (setq node (js2-node-parent node))))
5024 node))
5025
5026 (defun js2-node-parent-script-or-fn (node)
5027 "Find script or function immediately enclosing NODE.
5028 If NODE is the ast-root, returns nil."
5029 (if (js2-ast-root-p node)
5030 nil
5031 (setq node (js2-node-parent node))
5032 (while (and node (not (or (js2-function-node-p node)
5033 (js2-script-node-p node))))
5034 (setq node (js2-node-parent node)))
5035 node))
5036
5037 (defun js2-node-is-descendant (node ancestor)
5038 "Return t if NODE is a descendant of ANCESTOR."
5039 (while (and node
5040 (not (eq node ancestor)))
5041 (setq node (js2-node-parent node)))
5042 node)
5043
5044 ;;; visitor infrastructure
5045
5046 (defun js2-visit-none (_node _callback)
5047 "Visitor for AST node that have no node children."
5048 nil)
5049
5050 (defun js2-print-none (_node _indent)
5051 "Visitor for AST node with no printed representation.")
5052
5053 (defun js2-print-body (node indent)
5054 "Print a statement, or a block without braces."
5055 (if (js2-block-node-p node)
5056 (dolist (kid (js2-block-node-kids node))
5057 (js2-print-ast kid indent))
5058 (js2-print-ast node indent)))
5059
5060 (defun js2-print-list (args &optional delimiter)
5061 (cl-loop with len = (length args)
5062 for arg in args
5063 for count from 1
5064 do
5065 (when arg (js2-print-ast arg 0))
5066 (if (< count len)
5067 (insert (or delimiter ", ")))))
5068
5069 (defun js2-print-tree (ast)
5070 "Prints an AST to the current buffer.
5071 Makes `js2-ast-parent-nodes' available to the printer functions."
5072 (let ((max-lisp-eval-depth (max max-lisp-eval-depth 1500)))
5073 (js2-print-ast ast)))
5074
5075 (defun js2-print-ast (node &optional indent)
5076 "Helper function for printing AST nodes.
5077 Requires `js2-ast-parent-nodes' to be non-nil.
5078 You should use `js2-print-tree' instead of this function."
5079 (let ((printer (get (aref node 0) 'js2-printer))
5080 (i (or indent 0)))
5081 ;; TODO: wedge comments in here somewhere
5082 (if printer
5083 (funcall printer node i))))
5084
5085 (defconst js2-side-effecting-tokens
5086 (let ((tokens (make-bool-vector js2-num-tokens nil)))
5087 (dolist (tt (list js2-ASSIGN
5088 js2-ASSIGN_ADD
5089 js2-ASSIGN_BITAND
5090 js2-ASSIGN_BITOR
5091 js2-ASSIGN_BITXOR
5092 js2-ASSIGN_DIV
5093 js2-ASSIGN_LSH
5094 js2-ASSIGN_MOD
5095 js2-ASSIGN_MUL
5096 js2-ASSIGN_RSH
5097 js2-ASSIGN_SUB
5098 js2-ASSIGN_URSH
5099 js2-BLOCK
5100 js2-BREAK
5101 js2-CALL
5102 js2-CATCH
5103 js2-CATCH_SCOPE
5104 js2-CLASS
5105 js2-CONST
5106 js2-CONTINUE
5107 js2-DEBUGGER
5108 js2-DEC
5109 js2-DELPROP
5110 js2-DEL_REF
5111 js2-DO
5112 js2-ELSE
5113 js2-EMPTY
5114 js2-ENTERWITH
5115 js2-EXPORT
5116 js2-EXPR_RESULT
5117 js2-FINALLY
5118 js2-FOR
5119 js2-FUNCTION
5120 js2-GOTO
5121 js2-IF
5122 js2-IFEQ
5123 js2-IFNE
5124 js2-IMPORT
5125 js2-INC
5126 js2-JSR
5127 js2-LABEL
5128 js2-LEAVEWITH
5129 js2-LET
5130 js2-LETEXPR
5131 js2-LOCAL_BLOCK
5132 js2-LOOP
5133 js2-NEW
5134 js2-REF_CALL
5135 js2-RETHROW
5136 js2-RETURN
5137 js2-RETURN_RESULT
5138 js2-SEMI
5139 js2-SETELEM
5140 js2-SETELEM_OP
5141 js2-SETNAME
5142 js2-SETPROP
5143 js2-SETPROP_OP
5144 js2-SETVAR
5145 js2-SET_REF
5146 js2-SET_REF_OP
5147 js2-SWITCH
5148 js2-TARGET
5149 js2-THROW
5150 js2-TRY
5151 js2-VAR
5152 js2-WHILE
5153 js2-WITH
5154 js2-WITHEXPR
5155 js2-YIELD))
5156 (aset tokens tt t))
5157 (if js2-instanceof-has-side-effects
5158 (aset tokens js2-INSTANCEOF t))
5159 tokens))
5160
5161 (defun js2-node-has-side-effects (node)
5162 "Return t if NODE has side effects."
5163 (when node ; makes it easier to handle malformed expressions
5164 (let ((tt (js2-node-type node)))
5165 (cond
5166 ;; This doubtless needs some work, since EXPR_VOID is used
5167 ;; in several ways in Rhino and I may not have caught them all.
5168 ;; I'll wait for people to notice incorrect warnings.
5169 ((and (= tt js2-EXPR_VOID)
5170 (js2-expr-stmt-node-p node)) ; but not if EXPR_RESULT
5171 (let ((expr (js2-expr-stmt-node-expr node)))
5172 (or (js2-node-has-side-effects expr)
5173 (when (js2-string-node-p expr)
5174 (member (js2-string-node-value expr) '("use strict" "use asm"))))))
5175 ((= tt js2-COMMA)
5176 (js2-node-has-side-effects (js2-infix-node-right node)))
5177 ((or (= tt js2-AND)
5178 (= tt js2-OR))
5179 (or (js2-node-has-side-effects (js2-infix-node-right node))
5180 (js2-node-has-side-effects (js2-infix-node-left node))))
5181 ((= tt js2-HOOK)
5182 (and (js2-node-has-side-effects (js2-cond-node-true-expr node))
5183 (js2-node-has-side-effects (js2-cond-node-false-expr node))))
5184 ((js2-paren-node-p node)
5185 (js2-node-has-side-effects (js2-paren-node-expr node)))
5186 ((= tt js2-ERROR) ; avoid cascaded error messages
5187 nil)
5188 (t
5189 (aref js2-side-effecting-tokens tt))))))
5190
5191 (defconst js2-stmt-node-types
5192 (list js2-BLOCK
5193 js2-BREAK
5194 js2-CONTINUE
5195 js2-DEFAULT ; e4x "default xml namespace" statement
5196 js2-DO
5197 js2-EXPORT
5198 js2-EXPR_RESULT
5199 js2-EXPR_VOID
5200 js2-FOR
5201 js2-IF
5202 js2-IMPORT
5203 js2-RETURN
5204 js2-SWITCH
5205 js2-THROW
5206 js2-TRY
5207 js2-WHILE
5208 js2-WITH)
5209 "Node types that only appear in statement contexts.
5210 The list does not include nodes that always appear as the child
5211 of another specific statement type, such as switch-cases,
5212 catch and finally blocks, and else-clauses. The list also excludes
5213 nodes like yield, let and var, which may appear in either expression
5214 or statement context, and in the latter context always have a
5215 `js2-expr-stmt-node' parent. Finally, the list does not include
5216 functions or scripts, which are treated separately from statements
5217 by the JavaScript parser and runtime.")
5218
5219 (defun js2-stmt-node-p (node)
5220 "Heuristic for figuring out if NODE is a statement.
5221 Some node types can appear in either an expression context or a
5222 statement context, e.g. let-nodes, yield-nodes, and var-decl nodes.
5223 For these node types in a statement context, the parent will be a
5224 `js2-expr-stmt-node'.
5225 Functions aren't included in the check."
5226 (memq (js2-node-type node) js2-stmt-node-types))
5227
5228 (defun js2-mode-find-first-stmt (node)
5229 "Search upward starting from NODE looking for a statement.
5230 For purposes of this function, a `js2-function-node' counts."
5231 (while (not (or (js2-stmt-node-p node)
5232 (js2-function-node-p node)))
5233 (setq node (js2-node-parent node)))
5234 node)
5235
5236 (defun js2-node-parent-stmt (node)
5237 "Return the node's first ancestor that is a statement.
5238 Returns nil if NODE is a `js2-ast-root'. Note that any expression
5239 appearing in a statement context will have a parent that is a
5240 `js2-expr-stmt-node' that will be returned by this function."
5241 (let ((parent (js2-node-parent node)))
5242 (if (or (null parent)
5243 (js2-stmt-node-p parent)
5244 (and (js2-function-node-p parent)
5245 (not (eq (js2-function-node-form parent)
5246 'FUNCTION_EXPRESSION))))
5247 parent
5248 (js2-node-parent-stmt parent))))
5249
5250 ;; In the Mozilla Rhino sources, Roshan James writes:
5251 ;; Does consistent-return analysis on the function body when strict mode is
5252 ;; enabled.
5253 ;;
5254 ;; function (x) { return (x+1) }
5255 ;;
5256 ;; is ok, but
5257 ;;
5258 ;; function (x) { if (x < 0) return (x+1); }
5259 ;;
5260 ;; is not because the function can potentially return a value when the
5261 ;; condition is satisfied and if not, the function does not explicitly
5262 ;; return a value.
5263 ;;
5264 ;; This extends to checking mismatches such as "return" and "return <value>"
5265 ;; used in the same function. Warnings are not emitted if inconsistent
5266 ;; returns exist in code that can be statically shown to be unreachable.
5267 ;; Ex.
5268 ;; function (x) { while (true) { ... if (..) { return value } ... } }
5269 ;;
5270 ;; emits no warning. However if the loop had a break statement, then a
5271 ;; warning would be emitted.
5272 ;;
5273 ;; The consistency analysis looks at control structures such as loops, ifs,
5274 ;; switch, try-catch-finally blocks, examines the reachable code paths and
5275 ;; warns the user about an inconsistent set of termination possibilities.
5276 ;;
5277 ;; These flags enumerate the possible ways a statement/function can
5278 ;; terminate. These flags are used by endCheck() and by the Parser to
5279 ;; detect inconsistent return usage.
5280 ;;
5281 ;; END_UNREACHED is reserved for code paths that are assumed to always be
5282 ;; able to execute (example: throw, continue)
5283 ;;
5284 ;; END_DROPS_OFF indicates if the statement can transfer control to the
5285 ;; next one. Statement such as return dont. A compound statement may have
5286 ;; some branch that drops off control to the next statement.
5287 ;;
5288 ;; END_RETURNS indicates that the statement can return with no value.
5289 ;; END_RETURNS_VALUE indicates that the statement can return a value.
5290 ;;
5291 ;; A compound statement such as
5292 ;; if (condition) {
5293 ;; return value;
5294 ;; }
5295 ;; Will be detected as (END_DROPS_OFF | END_RETURN_VALUE) by endCheck()
5296
5297 (defconst js2-END_UNREACHED 0)
5298 (defconst js2-END_DROPS_OFF 1)
5299 (defconst js2-END_RETURNS 2)
5300 (defconst js2-END_RETURNS_VALUE 4)
5301 (defconst js2-END_YIELDS 8)
5302
5303 (defun js2-has-consistent-return-usage (node)
5304 "Check that every return usage in a function body is consistent.
5305 Returns t if the function satisfies strict mode requirement."
5306 (let ((n (js2-end-check node)))
5307 ;; either it doesn't return a value in any branch...
5308 (or (js2-flag-not-set-p n js2-END_RETURNS_VALUE)
5309 ;; or it returns a value (or is unreached) at every branch
5310 (js2-flag-not-set-p n (logior js2-END_DROPS_OFF
5311 js2-END_RETURNS
5312 js2-END_YIELDS)))))
5313
5314 (defun js2-end-check-if (node)
5315 "Ensure that return usage in then/else blocks is consistent.
5316 If there is no else block, then the return statement can fall through.
5317 Returns logical OR of END_* flags"
5318 (let ((th (js2-if-node-then-part node))
5319 (el (js2-if-node-else-part node)))
5320 (if (null th)
5321 js2-END_UNREACHED
5322 (logior (js2-end-check th) (if el
5323 (js2-end-check el)
5324 js2-END_DROPS_OFF)))))
5325
5326 (defun js2-end-check-switch (node)
5327 "Consistency of return statements is checked between the case statements.
5328 If there is no default, then the switch can fall through. If there is a
5329 default, we check to see if all code paths in the default return or if
5330 there is a code path that can fall through.
5331 Returns logical OR of END_* flags."
5332 (let ((rv js2-END_UNREACHED)
5333 default-case)
5334 ;; examine the cases
5335 (catch 'break
5336 (dolist (c (js2-switch-node-cases node))
5337 (if (js2-case-node-expr c)
5338 (js2-set-flag rv (js2-end-check-block c))
5339 (setq default-case c)
5340 (throw 'break nil))))
5341 ;; we don't care how the cases drop into each other
5342 (js2-clear-flag rv js2-END_DROPS_OFF)
5343 ;; examine the default
5344 (js2-set-flag rv (if default-case
5345 (js2-end-check default-case)
5346 js2-END_DROPS_OFF))
5347 rv))
5348
5349 (defun js2-end-check-try (node)
5350 "If the block has a finally, return consistency is checked in the
5351 finally block. If all code paths in the finally return, then the
5352 returns in the try-catch blocks don't matter. If there is a code path
5353 that does not return or if there is no finally block, the returns
5354 of the try and catch blocks are checked for mismatch.
5355 Returns logical OR of END_* flags."
5356 (let ((finally (js2-try-node-finally-block node))
5357 rv)
5358 ;; check the finally if it exists
5359 (setq rv (if finally
5360 (js2-end-check (js2-finally-node-body finally))
5361 js2-END_DROPS_OFF))
5362 ;; If the finally block always returns, then none of the returns
5363 ;; in the try or catch blocks matter.
5364 (when (js2-flag-set-p rv js2-END_DROPS_OFF)
5365 (js2-clear-flag rv js2-END_DROPS_OFF)
5366 ;; examine the try block
5367 (js2-set-flag rv (js2-end-check (js2-try-node-try-block node)))
5368 ;; check each catch block
5369 (dolist (cb (js2-try-node-catch-clauses node))
5370 (js2-set-flag rv (js2-end-check cb))))
5371 rv))
5372
5373 (defun js2-end-check-loop (node)
5374 "Return statement in the loop body must be consistent.
5375 The default assumption for any kind of a loop is that it will eventually
5376 terminate. The only exception is a loop with a constant true condition.
5377 Code that follows such a loop is examined only if one can determine
5378 statically that there is a break out of the loop.
5379
5380 for(... ; ... ; ...) {}
5381 for(... in ... ) {}
5382 while(...) { }
5383 do { } while(...)
5384
5385 Returns logical OR of END_* flags."
5386 (let ((rv (js2-end-check (js2-loop-node-body node)))
5387 (condition (cond
5388 ((js2-while-node-p node)
5389 (js2-while-node-condition node))
5390 ((js2-do-node-p node)
5391 (js2-do-node-condition node))
5392 ((js2-for-node-p node)
5393 (js2-for-node-condition node)))))
5394
5395 ;; check to see if the loop condition is always true
5396 (if (and condition
5397 (eq (js2-always-defined-boolean-p condition) 'ALWAYS_TRUE))
5398 (js2-clear-flag rv js2-END_DROPS_OFF))
5399
5400 ;; look for effect of breaks
5401 (js2-set-flag rv (js2-node-get-prop node
5402 'CONTROL_BLOCK_PROP
5403 js2-END_UNREACHED))
5404 rv))
5405
5406 (defun js2-end-check-block (node)
5407 "A general block of code is examined statement by statement.
5408 If any statement (even a compound one) returns in all branches, then
5409 subsequent statements are not examined.
5410 Returns logical OR of END_* flags."
5411 (let* ((rv js2-END_DROPS_OFF)
5412 (kids (js2-block-node-kids node))
5413 (n (car kids)))
5414 ;; Check each statement. If the statement can continue onto the next
5415 ;; one (i.e. END_DROPS_OFF is set), then check the next statement.
5416 (while (and n (js2-flag-set-p rv js2-END_DROPS_OFF))
5417 (js2-clear-flag rv js2-END_DROPS_OFF)
5418 (js2-set-flag rv (js2-end-check n))
5419 (setq kids (cdr kids)
5420 n (car kids)))
5421 rv))
5422
5423 (defun js2-end-check-label (node)
5424 "A labeled statement implies that there may be a break to the label.
5425 The function processes the labeled statement and then checks the
5426 CONTROL_BLOCK_PROP property to see if there is ever a break to the
5427 particular label.
5428 Returns logical OR of END_* flags."
5429 (let ((rv (js2-end-check (js2-labeled-stmt-node-stmt node))))
5430 (logior rv (js2-node-get-prop node
5431 'CONTROL_BLOCK_PROP
5432 js2-END_UNREACHED))))
5433
5434 (defun js2-end-check-break (node)
5435 "When a break is encountered annotate the statement being broken
5436 out of by setting its CONTROL_BLOCK_PROP property.
5437 Returns logical OR of END_* flags."
5438 (and (js2-break-node-target node)
5439 (js2-node-set-prop (js2-break-node-target node)
5440 'CONTROL_BLOCK_PROP
5441 js2-END_DROPS_OFF))
5442 js2-END_UNREACHED)
5443
5444 (defun js2-end-check (node)
5445 "Examine the body of a function, doing a basic reachability analysis.
5446 Returns a combination of flags END_* flags that indicate
5447 how the function execution can terminate. These constitute only the
5448 pessimistic set of termination conditions. It is possible that at
5449 runtime certain code paths will never be actually taken. Hence this
5450 analysis will flag errors in cases where there may not be errors.
5451 Returns logical OR of END_* flags"
5452 (let (kid)
5453 (cond
5454 ((js2-break-node-p node)
5455 (js2-end-check-break node))
5456 ((js2-expr-stmt-node-p node)
5457 (if (setq kid (js2-expr-stmt-node-expr node))
5458 (js2-end-check kid)
5459 js2-END_DROPS_OFF))
5460 ((or (js2-continue-node-p node)
5461 (js2-throw-node-p node))
5462 js2-END_UNREACHED)
5463 ((js2-return-node-p node)
5464 (if (setq kid (js2-return-node-retval node))
5465 js2-END_RETURNS_VALUE
5466 js2-END_RETURNS))
5467 ((js2-loop-node-p node)
5468 (js2-end-check-loop node))
5469 ((js2-switch-node-p node)
5470 (js2-end-check-switch node))
5471 ((js2-labeled-stmt-node-p node)
5472 (js2-end-check-label node))
5473 ((js2-if-node-p node)
5474 (js2-end-check-if node))
5475 ((js2-try-node-p node)
5476 (js2-end-check-try node))
5477 ((js2-block-node-p node)
5478 (if (null (js2-block-node-kids node))
5479 js2-END_DROPS_OFF
5480 (js2-end-check-block node)))
5481 ((js2-yield-node-p node)
5482 js2-END_YIELDS)
5483 (t
5484 js2-END_DROPS_OFF))))
5485
5486 (defun js2-always-defined-boolean-p (node)
5487 "Check if NODE always evaluates to true or false in boolean context.
5488 Returns 'ALWAYS_TRUE, 'ALWAYS_FALSE, or nil if it's neither always true
5489 nor always false."
5490 (let ((tt (js2-node-type node))
5491 num)
5492 (cond
5493 ((or (= tt js2-FALSE) (= tt js2-NULL))
5494 'ALWAYS_FALSE)
5495 ((= tt js2-TRUE)
5496 'ALWAYS_TRUE)
5497 ((= tt js2-NUMBER)
5498 (setq num (js2-number-node-num-value node))
5499 (if (and (not (eq num 0.0e+NaN))
5500 (not (zerop num)))
5501 'ALWAYS_TRUE
5502 'ALWAYS_FALSE))
5503 (t
5504 nil))))
5505
5506 ;;; Scanner -- a port of Mozilla Rhino's lexer.
5507 ;; Corresponds to Rhino files Token.java and TokenStream.java.
5508
5509 (defvar js2-tokens nil
5510 "List of all defined token names.") ; initialized in `js2-token-names'
5511
5512 (defconst js2-token-names
5513 (let* ((names (make-vector js2-num-tokens -1))
5514 (case-fold-search nil) ; only match js2-UPPER_CASE
5515 (syms (apropos-internal "^js2-\\(?:[[:upper:]_]+\\)")))
5516 (cl-loop for sym in syms
5517 for i from 0
5518 do
5519 (unless (or (memq sym '(js2-EOF_CHAR js2-ERROR))
5520 (not (boundp sym)))
5521 (aset names (symbol-value sym) ; code, e.g. 152
5522 (downcase
5523 (substring (symbol-name sym) 4))) ; name, e.g. "let"
5524 (push sym js2-tokens)))
5525 names)
5526 "Vector mapping int values to token string names, sans `js2-' prefix.")
5527
5528 (defun js2-tt-name (tok)
5529 "Return a string name for TOK, a token symbol or code.
5530 Signals an error if it's not a recognized token."
5531 (let ((code tok))
5532 (if (symbolp tok)
5533 (setq code (symbol-value tok)))
5534 (if (eq code -1)
5535 "ERROR"
5536 (if (and (numberp code)
5537 (not (cl-minusp code))
5538 (< code js2-num-tokens))
5539 (aref js2-token-names code)
5540 (error "Invalid token: %s" code)))))
5541
5542 (defsubst js2-tt-sym (tok)
5543 "Return symbol for TOK given its code, e.g. 'js2-LP for code 86."
5544 (intern (js2-tt-name tok)))
5545
5546 (defconst js2-token-codes
5547 (let ((table (make-hash-table :test 'eq :size 256)))
5548 (cl-loop for name across js2-token-names
5549 for sym = (intern (concat "js2-" (upcase name)))
5550 do
5551 (puthash sym (symbol-value sym) table))
5552 ;; clean up a few that are "wrong" in Rhino's token codes
5553 (puthash 'js2-DELETE js2-DELPROP table)
5554 table)
5555 "Hashtable mapping token type symbols to their bytecodes.")
5556
5557 (defsubst js2-tt-code (sym)
5558 "Return code for token symbol SYM, e.g. 86 for 'js2-LP."
5559 (or (gethash sym js2-token-codes)
5560 (error "Invalid token symbol: %s " sym))) ; signal code bug
5561
5562 (defun js2-report-scan-error (msg &optional no-throw beg len)
5563 (setf (js2-token-end (js2-current-token)) js2-ts-cursor)
5564 (js2-report-error msg nil
5565 (or beg (js2-current-token-beg))
5566 (or len (js2-current-token-len)))
5567 (unless no-throw
5568 (throw 'return js2-ERROR)))
5569
5570 (defun js2-set-string-from-buffer (token)
5571 "Set `string' and `end' slots for TOKEN, return the string."
5572 (setf (js2-token-end token) js2-ts-cursor
5573 (js2-token-string token) (js2-collect-string js2-ts-string-buffer)))
5574
5575 ;; TODO: could potentially avoid a lot of consing by allocating a
5576 ;; char buffer the way Rhino does.
5577 (defsubst js2-add-to-string (c)
5578 (push c js2-ts-string-buffer))
5579
5580 ;; Note that when we "read" the end-of-file, we advance js2-ts-cursor
5581 ;; to (1+ (point-max)), which lets the scanner treat end-of-file like
5582 ;; any other character: when it's not part of the current token, we
5583 ;; unget it, allowing it to be read again by the following call.
5584 (defsubst js2-unget-char ()
5585 (cl-decf js2-ts-cursor))
5586
5587 ;; Rhino distinguishes \r and \n line endings. We don't need to
5588 ;; because we only scan from Emacs buffers, which always use \n.
5589 (defun js2-get-char ()
5590 "Read and return the next character from the input buffer.
5591 Increments `js2-ts-lineno' if the return value is a newline char.
5592 Updates `js2-ts-cursor' to the point after the returned char.
5593 Returns `js2-EOF_CHAR' if we hit the end of the buffer.
5594 Also updates `js2-ts-hit-eof' and `js2-ts-line-start' as needed."
5595 (let (c)
5596 ;; check for end of buffer
5597 (if (>= js2-ts-cursor (point-max))
5598 (setq js2-ts-hit-eof t
5599 js2-ts-cursor (1+ js2-ts-cursor)
5600 c js2-EOF_CHAR) ; return value
5601 ;; otherwise read next char
5602 (setq c (char-before (cl-incf js2-ts-cursor)))
5603 ;; if we read a newline, update counters
5604 (if (= c ?\n)
5605 (setq js2-ts-line-start js2-ts-cursor
5606 js2-ts-lineno (1+ js2-ts-lineno)))
5607 ;; TODO: skip over format characters
5608 c)))
5609
5610 (defun js2-read-unicode-escape ()
5611 "Read a \\uNNNN sequence from the input.
5612 Assumes the ?\ and ?u have already been read.
5613 Returns the unicode character, or nil if it wasn't a valid character.
5614 Doesn't change the values of any scanner variables."
5615 ;; I really wish I knew a better way to do this, but I can't
5616 ;; find the Emacs function that takes a 16-bit int and converts
5617 ;; it to a Unicode/utf-8 character. So I basically eval it with (read).
5618 ;; Have to first check that it's 4 hex characters or it may stop
5619 ;; the read early.
5620 (ignore-errors
5621 (let ((s (buffer-substring-no-properties js2-ts-cursor
5622 (+ 4 js2-ts-cursor))))
5623 (if (string-match "[0-9a-fA-F]\\{4\\}" s)
5624 (read (concat "?\\u" s))))))
5625
5626 (defun js2-match-char (test)
5627 "Consume and return next character if it matches TEST, a character.
5628 Returns nil and consumes nothing if TEST is not the next character."
5629 (let ((c (js2-get-char)))
5630 (if (eq c test)
5631 t
5632 (js2-unget-char)
5633 nil)))
5634
5635 (defun js2-peek-char ()
5636 (prog1
5637 (js2-get-char)
5638 (js2-unget-char)))
5639
5640 (defun js2-identifier-start-p (c)
5641 "Is C a valid start to an ES5 Identifier?
5642 See http://es5.github.io/#x7.6"
5643 (or
5644 (memq c '(?$ ?_))
5645 (memq (get-char-code-property c 'general-category)
5646 ;; Letters
5647 '(Lu Ll Lt Lm Lo Nl))))
5648
5649 (defun js2-identifier-part-p (c)
5650 "Is C a valid part of an ES5 Identifier?
5651 See http://es5.github.io/#x7.6"
5652 (or
5653 (memq c '(?$ ?_ ?\u200c ?\u200d))
5654 (memq (get-char-code-property c 'general-category)
5655 '(;; Letters
5656 Lu Ll Lt Lm Lo Nl
5657 ;; Combining Marks
5658 Mn Mc
5659 ;; Digits
5660 Nd
5661 ;; Connector Punctuation
5662 Pc))))
5663
5664 (defun js2-alpha-p (c)
5665 (cond ((and (<= ?A c) (<= c ?Z)) t)
5666 ((and (<= ?a c) (<= c ?z)) t)
5667 (t nil)))
5668
5669 (defsubst js2-digit-p (c)
5670 (and (<= ?0 c) (<= c ?9)))
5671
5672 (defun js2-js-space-p (c)
5673 (if (<= c 127)
5674 (memq c '(#x20 #x9 #xB #xC #xD))
5675 (or
5676 (eq c #xA0)
5677 ;; TODO: change this nil to check for Unicode space character
5678 nil)))
5679
5680 (defconst js2-eol-chars (list js2-EOF_CHAR ?\n ?\r))
5681
5682 (defun js2-skip-line ()
5683 "Skip to end of line."
5684 (while (not (memq (js2-get-char) js2-eol-chars)))
5685 (js2-unget-char)
5686 (setf (js2-token-end (js2-current-token)) js2-ts-cursor))
5687
5688 (defun js2-init-scanner (&optional buf line)
5689 "Create token stream for BUF starting on LINE.
5690 BUF defaults to `current-buffer' and LINE defaults to 1.
5691
5692 A buffer can only have one scanner active at a time, which yields
5693 dramatically simpler code than using a defstruct. If you need to
5694 have simultaneous scanners in a buffer, copy the regions to scan
5695 into temp buffers."
5696 (with-current-buffer (or buf (current-buffer))
5697 (setq js2-ts-dirty-line nil
5698 js2-ts-hit-eof nil
5699 js2-ts-line-start 0
5700 js2-ts-lineno (or line 1)
5701 js2-ts-line-end-char -1
5702 js2-ts-cursor (point-min)
5703 js2-ti-tokens (make-vector js2-ti-ntokens nil)
5704 js2-ti-tokens-cursor 0
5705 js2-ti-lookahead 0
5706 js2-ts-is-xml-attribute nil
5707 js2-ts-xml-is-tag-content nil
5708 js2-ts-xml-open-tags-count 0
5709 js2-ts-string-buffer nil)))
5710
5711 ;; This function uses the cached op, string and number fields in
5712 ;; TokenStream; if getToken has been called since the passed token
5713 ;; was scanned, the op or string printed may be incorrect.
5714 (defun js2-token-to-string (token)
5715 ;; Not sure where this function is used in Rhino. Not tested.
5716 (if (not js2-debug-print-trees)
5717 ""
5718 (let ((name (js2-tt-name token)))
5719 (cond
5720 ((memq token '(js2-STRING js2-REGEXP js2-NAME
5721 js2-TEMPLATE_HEAD js2-NO_SUBS_TEMPLATE))
5722 (concat name " `" (js2-current-token-string) "'"))
5723 ((eq token js2-NUMBER)
5724 (format "NUMBER %g" (js2-token-number (js2-current-token))))
5725 (t
5726 name)))))
5727
5728 (defconst js2-keywords
5729 '(break
5730 case catch class const continue
5731 debugger default delete do
5732 else extends export
5733 false finally for function
5734 if in instanceof import
5735 let
5736 new null
5737 return
5738 super switch
5739 this throw true try typeof
5740 var void
5741 while with
5742 yield))
5743
5744 ;; Token names aren't exactly the same as the keywords, unfortunately.
5745 ;; E.g. delete is js2-DELPROP.
5746 (defconst js2-kwd-tokens
5747 (let ((table (make-vector js2-num-tokens nil))
5748 (tokens
5749 (list js2-BREAK
5750 js2-CASE js2-CATCH js2-CLASS js2-CONST js2-CONTINUE
5751 js2-DEBUGGER js2-DEFAULT js2-DELPROP js2-DO
5752 js2-ELSE js2-EXPORT
5753 js2-ELSE js2-EXTENDS js2-EXPORT
5754 js2-FALSE js2-FINALLY js2-FOR js2-FUNCTION
5755 js2-IF js2-IN js2-INSTANCEOF js2-IMPORT
5756 js2-LET
5757 js2-NEW js2-NULL
5758 js2-RETURN
5759 js2-SUPER js2-SWITCH
5760 js2-THIS js2-THROW js2-TRUE js2-TRY js2-TYPEOF
5761 js2-VAR
5762 js2-WHILE js2-WITH
5763 js2-YIELD)))
5764 (dolist (i tokens)
5765 (aset table i 'font-lock-keyword-face))
5766 (aset table js2-STRING 'font-lock-string-face)
5767 (aset table js2-REGEXP 'font-lock-string-face)
5768 (aset table js2-NO_SUBS_TEMPLATE 'font-lock-string-face)
5769 (aset table js2-TEMPLATE_HEAD 'font-lock-string-face)
5770 (aset table js2-COMMENT 'font-lock-comment-face)
5771 (aset table js2-THIS 'font-lock-builtin-face)
5772 (aset table js2-SUPER 'font-lock-builtin-face)
5773 (aset table js2-VOID 'font-lock-constant-face)
5774 (aset table js2-NULL 'font-lock-constant-face)
5775 (aset table js2-TRUE 'font-lock-constant-face)
5776 (aset table js2-FALSE 'font-lock-constant-face)
5777 (aset table js2-NOT 'font-lock-negation-char-face)
5778 table)
5779 "Vector whose values are non-nil for tokens that are keywords.
5780 The values are default faces to use for highlighting the keywords.")
5781
5782 ;; FIXME: Support strict mode-only future reserved words, after we know
5783 ;; which parts scopes are in strict mode, and which are not.
5784 (defconst js2-reserved-words '(class enum export extends import static super)
5785 "Future reserved keywords in ECMAScript 5.1.")
5786
5787 (defconst js2-keyword-names
5788 (let ((table (make-hash-table :test 'equal)))
5789 (cl-loop for k in js2-keywords
5790 do (puthash
5791 (symbol-name k) ; instanceof
5792 (intern (concat "js2-"
5793 (upcase (symbol-name k)))) ; js2-INSTANCEOF
5794 table))
5795 table)
5796 "JavaScript keywords by name, mapped to their symbols.")
5797
5798 (defconst js2-reserved-word-names
5799 (let ((table (make-hash-table :test 'equal)))
5800 (cl-loop for k in js2-reserved-words
5801 do
5802 (puthash (symbol-name k) 'js2-RESERVED table))
5803 table)
5804 "JavaScript reserved words by name, mapped to 'js2-RESERVED.")
5805
5806 (defun js2-collect-string (buf)
5807 "Convert BUF, a list of chars, to a string.
5808 Reverses BUF before converting."
5809 (if buf
5810 (apply #'string (nreverse buf))
5811 ""))
5812
5813 (defun js2-string-to-keyword (s)
5814 "Return token for S, a string, if S is a keyword or reserved word.
5815 Returns a symbol such as 'js2-BREAK, or nil if not keyword/reserved."
5816 (or (gethash s js2-keyword-names)
5817 (gethash s js2-reserved-word-names)))
5818
5819 (defsubst js2-ts-set-char-token-bounds (token)
5820 "Used when next token is one character."
5821 (setf (js2-token-beg token) (1- js2-ts-cursor)
5822 (js2-token-end token) js2-ts-cursor))
5823
5824 (defsubst js2-ts-return (token type)
5825 "Update the `end' and `type' slots of TOKEN,
5826 then throw `return' with value TYPE."
5827 (setf (js2-token-end token) js2-ts-cursor
5828 (js2-token-type token) type)
5829 (throw 'return type))
5830
5831 (defun js2-x-digit-to-int (c accumulator)
5832 "Build up a hex number.
5833 If C is a hexadecimal digit, return ACCUMULATOR * 16 plus
5834 corresponding number. Otherwise return -1."
5835 (catch 'return
5836 (catch 'check
5837 ;; Use 0..9 < A..Z < a..z
5838 (cond
5839 ((<= c ?9)
5840 (cl-decf c ?0)
5841 (if (<= 0 c)
5842 (throw 'check nil)))
5843 ((<= c ?F)
5844 (when (<= ?A c)
5845 (cl-decf c (- ?A 10))
5846 (throw 'check nil)))
5847 ((<= c ?f)
5848 (when (<= ?a c)
5849 (cl-decf c (- ?a 10))
5850 (throw 'check nil))))
5851 (throw 'return -1))
5852 (logior c (lsh accumulator 4))))
5853
5854 (defun js2-get-token (&optional modifier)
5855 "If `js2-ti-lookahead' is zero, call scanner to get new token.
5856 Otherwise, move `js2-ti-tokens-cursor' and return the type of
5857 next saved token.
5858
5859 This function will not return a newline (js2-EOL) - instead, it
5860 gobbles newlines until it finds a non-newline token. Call
5861 `js2-peek-token-or-eol' when you care about newlines.
5862
5863 This function will also not return a js2-COMMENT. Instead, it
5864 records comments found in `js2-scanned-comments'. If the token
5865 returned by this function immediately follows a jsdoc comment,
5866 the token is flagged as such."
5867 (if (zerop js2-ti-lookahead)
5868 (js2-get-token-internal modifier)
5869 (cl-decf js2-ti-lookahead)
5870 (setq js2-ti-tokens-cursor (mod (1+ js2-ti-tokens-cursor) js2-ti-ntokens))
5871 (let ((tt (js2-current-token-type)))
5872 (cl-assert (not (= tt js2-EOL)))
5873 tt)))
5874
5875 (defun js2-unget-token ()
5876 (cl-assert (< js2-ti-lookahead js2-ti-max-lookahead))
5877 (cl-incf js2-ti-lookahead)
5878 (setq js2-ti-tokens-cursor (mod (1- js2-ti-tokens-cursor) js2-ti-ntokens)))
5879
5880 (defun js2-get-token-internal (modifier)
5881 (let* ((token (js2-get-token-internal-1 modifier)) ; call scanner
5882 (tt (js2-token-type token))
5883 saw-eol
5884 face)
5885 ;; process comments
5886 (while (or (= tt js2-EOL) (= tt js2-COMMENT))
5887 (if (= tt js2-EOL)
5888 (setq saw-eol t)
5889 (setq saw-eol nil)
5890 (when js2-record-comments
5891 (js2-record-comment token)))
5892 (setq js2-ti-tokens-cursor (mod (1- js2-ti-tokens-cursor) js2-ti-ntokens))
5893 (setq token (js2-get-token-internal-1 modifier) ; call scanner again
5894 tt (js2-token-type token)))
5895
5896 (when saw-eol
5897 (setf (js2-token-follows-eol-p token) t))
5898
5899 ;; perform lexical fontification as soon as token is scanned
5900 (when js2-parse-ide-mode
5901 (cond
5902 ((cl-minusp tt)
5903 (js2-record-face 'js2-error token))
5904 ((setq face (aref js2-kwd-tokens tt))
5905 (js2-record-face face token))
5906 ((and (= tt js2-NAME)
5907 (equal (js2-token-string token) "undefined"))
5908 (js2-record-face 'font-lock-constant-face token))))
5909 tt))
5910
5911 (defsubst js2-string-to-number (str base)
5912 ;; TODO: Maybe port ScriptRuntime.stringToNumber.
5913 (condition-case nil
5914 (string-to-number str base)
5915 (overflow-error -1)))
5916
5917 (defun js2-get-token-internal-1 (modifier)
5918 "Return next JavaScript token type, an int such as js2-RETURN.
5919 During operation, creates an instance of `js2-token' struct, sets
5920 its relevant fields and puts it into `js2-ti-tokens'."
5921 (let (identifier-start
5922 is-unicode-escape-start c
5923 contains-escape escape-val str result base
5924 look-for-slash continue tt
5925 (token (js2-new-token 0)))
5926 (setq
5927 tt
5928 (catch 'return
5929 (when (eq modifier 'TEMPLATE_TAIL)
5930 (setf (js2-token-beg token) (1- js2-ts-cursor))
5931 (throw 'return (js2-get-string-or-template-token ?` token)))
5932 (while t
5933 ;; Eat whitespace, possibly sensitive to newlines.
5934 (setq continue t)
5935 (while continue
5936 (setq c (js2-get-char))
5937 (cond
5938 ((eq c js2-EOF_CHAR)
5939 (js2-unget-char)
5940 (js2-ts-set-char-token-bounds token)
5941 (throw 'return js2-EOF))
5942 ((eq c ?\n)
5943 (js2-ts-set-char-token-bounds token)
5944 (setq js2-ts-dirty-line nil)
5945 (throw 'return js2-EOL))
5946 ((not (js2-js-space-p c))
5947 (if (/= c ?-) ; in case end of HTML comment
5948 (setq js2-ts-dirty-line t))
5949 (setq continue nil))))
5950 ;; Assume the token will be 1 char - fixed up below.
5951 (js2-ts-set-char-token-bounds token)
5952 (when (eq c ?@)
5953 (throw 'return js2-XMLATTR))
5954 ;; identifier/keyword/instanceof?
5955 ;; watch out for starting with a <backslash>
5956 (cond
5957 ((eq c ?\\)
5958 (setq c (js2-get-char))
5959 (if (eq c ?u)
5960 (setq identifier-start t
5961 is-unicode-escape-start t
5962 js2-ts-string-buffer nil)
5963 (setq identifier-start nil)
5964 (js2-unget-char)
5965 (setq c ?\\)))
5966 (t
5967 (when (setq identifier-start (js2-identifier-start-p c))
5968 (setq js2-ts-string-buffer nil)
5969 (js2-add-to-string c))))
5970 (when identifier-start
5971 (setq contains-escape is-unicode-escape-start)
5972 (catch 'break
5973 (while t
5974 (if is-unicode-escape-start
5975 ;; strictly speaking we should probably push-back
5976 ;; all the bad characters if the <backslash>uXXXX
5977 ;; sequence is malformed. But since there isn't a
5978 ;; correct context(is there?) for a bad Unicode
5979 ;; escape sequence in an identifier, we can report
5980 ;; an error here.
5981 (progn
5982 (setq escape-val 0)
5983 (dotimes (_ 4)
5984 (setq c (js2-get-char)
5985 escape-val (js2-x-digit-to-int c escape-val))
5986 ;; Next check takes care of c < 0 and bad escape
5987 (if (cl-minusp escape-val)
5988 (throw 'break nil)))
5989 (if (cl-minusp escape-val)
5990 (js2-report-scan-error "msg.invalid.escape" t))
5991 (js2-add-to-string escape-val)
5992 (setq is-unicode-escape-start nil))
5993 (setq c (js2-get-char))
5994 (cond
5995 ((eq c ?\\)
5996 (setq c (js2-get-char))
5997 (if (eq c ?u)
5998 (setq is-unicode-escape-start t
5999 contains-escape t)
6000 (js2-report-scan-error "msg.illegal.character" t)))
6001 (t
6002 (if (or (eq c js2-EOF_CHAR)
6003 (not (js2-identifier-part-p c)))
6004 (throw 'break nil))
6005 (js2-add-to-string c))))))
6006 (js2-unget-char)
6007 (setf str (js2-collect-string js2-ts-string-buffer)
6008 (js2-token-end token) js2-ts-cursor)
6009 ;; FIXME: Invalid in ES5 and ES6, see
6010 ;; https://bugzilla.mozilla.org/show_bug.cgi?id=694360
6011 ;; Probably should just drop this conditional.
6012 (unless contains-escape
6013 ;; OPT we shouldn't have to make a string (object!) to
6014 ;; check if it's a keyword.
6015 ;; Return the corresponding token if it's a keyword
6016 (when (and (not (eq modifier 'KEYWORD_IS_NAME))
6017 (setq result (js2-string-to-keyword str)))
6018 (if (and (< js2-language-version 170)
6019 (memq result '(js2-LET js2-YIELD)))
6020 ;; LET and YIELD are tokens only in 1.7 and later
6021 (setq result 'js2-NAME))
6022 (when (eq result 'js2-RESERVED)
6023 (setf (js2-token-string token) str))
6024 (throw 'return (js2-tt-code result))))
6025 ;; If we want to intern these as Rhino does, just use (intern str)
6026 (setf (js2-token-string token) str)
6027 (throw 'return js2-NAME)) ; end identifier/kwd check
6028 ;; is it a number?
6029 (when (or (js2-digit-p c)
6030 (and (eq c ?.) (js2-digit-p (js2-peek-char))))
6031 (setq js2-ts-string-buffer nil
6032 base 10)
6033 (when (eq c ?0)
6034 (setq c (js2-get-char))
6035 (cond
6036 ((or (eq c ?x) (eq c ?X))
6037 (setq base 16)
6038 (setq c (js2-get-char)))
6039 ((and (or (eq c ?b) (eq c ?B))
6040 (>= js2-language-version 200))
6041 (setq base 2)
6042 (setq c (js2-get-char)))
6043 ((and (or (eq c ?o) (eq c ?O))
6044 (>= js2-language-version 200))
6045 (setq base 8)
6046 (setq c (js2-get-char)))
6047 ((js2-digit-p c)
6048 (setq base 'maybe-8))
6049 (t
6050 (js2-add-to-string ?0))))
6051 (cond
6052 ((eq base 16)
6053 (if (> 0 (js2-x-digit-to-int c 0))
6054 (js2-report-scan-error "msg.missing.hex.digits")
6055 (while (<= 0 (js2-x-digit-to-int c 0))
6056 (js2-add-to-string c)
6057 (setq c (js2-get-char)))))
6058 ((eq base 2)
6059 (if (not (memq c '(?0 ?1)))
6060 (js2-report-scan-error "msg.missing.binary.digits")
6061 (while (memq c '(?0 ?1))
6062 (js2-add-to-string c)
6063 (setq c (js2-get-char)))))
6064 ((eq base 8)
6065 (if (or (> ?0 c) (< ?7 c))
6066 (js2-report-scan-error "msg.missing.octal.digits")
6067 (while (and (<= ?0 c) (>= ?7 c))
6068 (js2-add-to-string c)
6069 (setq c (js2-get-char)))))
6070 (t
6071 (while (and (<= ?0 c) (<= c ?9))
6072 ;; We permit 08 and 09 as decimal numbers, which
6073 ;; makes our behavior a superset of the ECMA
6074 ;; numeric grammar. We might not always be so
6075 ;; permissive, so we warn about it.
6076 (when (and (eq base 'maybe-8) (>= c ?8))
6077 (js2-report-warning "msg.bad.octal.literal"
6078 (if (eq c ?8) "8" "9"))
6079 (setq base 10))
6080 (js2-add-to-string c)
6081 (setq c (js2-get-char)))
6082 (when (eq base 'maybe-8)
6083 (setq base 8))))
6084 (when (and (eq base 10) (memq c '(?. ?e ?E)))
6085 (when (eq c ?.)
6086 (cl-loop do
6087 (js2-add-to-string c)
6088 (setq c (js2-get-char))
6089 while (js2-digit-p c)))
6090 (when (memq c '(?e ?E))
6091 (js2-add-to-string c)
6092 (setq c (js2-get-char))
6093 (when (memq c '(?+ ?-))
6094 (js2-add-to-string c)
6095 (setq c (js2-get-char)))
6096 (unless (js2-digit-p c)
6097 (js2-report-scan-error "msg.missing.exponent" t))
6098 (cl-loop do
6099 (js2-add-to-string c)
6100 (setq c (js2-get-char))
6101 while (js2-digit-p c))))
6102 (js2-unget-char)
6103 (let ((str (js2-set-string-from-buffer token)))
6104 (setf (js2-token-number token)
6105 (js2-string-to-number str base)))
6106 (throw 'return js2-NUMBER))
6107 ;; is it a string?
6108 (when (or (memq c '(?\" ?\'))
6109 (and (>= js2-language-version 200)
6110 (= c ?`)))
6111 (throw 'return
6112 (js2-get-string-or-template-token c token)))
6113 (js2-ts-return token
6114 (cl-case c
6115 (?\;
6116 (throw 'return js2-SEMI))
6117 (?\[
6118 (throw 'return js2-LB))
6119 (?\]
6120 (throw 'return js2-RB))
6121 (?{
6122 (throw 'return js2-LC))
6123 (?}
6124 (throw 'return js2-RC))
6125 (?\(
6126 (throw 'return js2-LP))
6127 (?\)
6128 (throw 'return js2-RP))
6129 (?,
6130 (throw 'return js2-COMMA))
6131 (??
6132 (throw 'return js2-HOOK))
6133 (?:
6134 (if (js2-match-char ?:)
6135 js2-COLONCOLON
6136 (throw 'return js2-COLON)))
6137 (?.
6138 (if (js2-match-char ?.)
6139 (if (js2-match-char ?.)
6140 js2-TRIPLEDOT js2-DOTDOT)
6141 (if (js2-match-char ?\()
6142 js2-DOTQUERY
6143 (throw 'return js2-DOT))))
6144 (?|
6145 (if (js2-match-char ?|)
6146 (throw 'return js2-OR)
6147 (if (js2-match-char ?=)
6148 js2-ASSIGN_BITOR
6149 (throw 'return js2-BITOR))))
6150 (?^
6151 (if (js2-match-char ?=)
6152 js2-ASSIGN_BITOR
6153 (throw 'return js2-BITXOR)))
6154 (?&
6155 (if (js2-match-char ?&)
6156 (throw 'return js2-AND)
6157 (if (js2-match-char ?=)
6158 js2-ASSIGN_BITAND
6159 (throw 'return js2-BITAND))))
6160 (?=
6161 (if (js2-match-char ?=)
6162 (if (js2-match-char ?=)
6163 js2-SHEQ
6164 (throw 'return js2-EQ))
6165 (if (js2-match-char ?>)
6166 (js2-ts-return token js2-ARROW)
6167 (throw 'return js2-ASSIGN))))
6168 (?!
6169 (if (js2-match-char ?=)
6170 (if (js2-match-char ?=)
6171 js2-SHNE
6172 js2-NE)
6173 (throw 'return js2-NOT)))
6174 (?<
6175 ;; NB:treat HTML begin-comment as comment-till-eol
6176 (when (js2-match-char ?!)
6177 (when (js2-match-char ?-)
6178 (when (js2-match-char ?-)
6179 (js2-skip-line)
6180 (setf (js2-token-comment-type (js2-current-token)) 'html)
6181 (throw 'return js2-COMMENT)))
6182 (js2-unget-char))
6183 (if (js2-match-char ?<)
6184 (if (js2-match-char ?=)
6185 js2-ASSIGN_LSH
6186 js2-LSH)
6187 (if (js2-match-char ?=)
6188 js2-LE
6189 (throw 'return js2-LT))))
6190 (?>
6191 (if (js2-match-char ?>)
6192 (if (js2-match-char ?>)
6193 (if (js2-match-char ?=)
6194 js2-ASSIGN_URSH
6195 js2-URSH)
6196 (if (js2-match-char ?=)
6197 js2-ASSIGN_RSH
6198 js2-RSH))
6199 (if (js2-match-char ?=)
6200 js2-GE
6201 (throw 'return js2-GT))))
6202 (?*
6203 (if (js2-match-char ?=)
6204 js2-ASSIGN_MUL
6205 (throw 'return js2-MUL)))
6206 (?/
6207 ;; is it a // comment?
6208 (when (js2-match-char ?/)
6209 (setf (js2-token-beg token) (- js2-ts-cursor 2))
6210 (js2-skip-line)
6211 (setf (js2-token-comment-type token) 'line)
6212 ;; include newline so highlighting goes to end of
6213 ;; window, if there actually is a newline; if we
6214 ;; hit eof, then implicitly there isn't
6215 (unless js2-ts-hit-eof
6216 (cl-incf (js2-token-end token)))
6217 (throw 'return js2-COMMENT))
6218 ;; is it a /* comment?
6219 (when (js2-match-char ?*)
6220 (setf look-for-slash nil
6221 (js2-token-beg token) (- js2-ts-cursor 2)
6222 (js2-token-comment-type token)
6223 (if (js2-match-char ?*)
6224 (progn
6225 (setq look-for-slash t)
6226 'jsdoc)
6227 'block))
6228 (while t
6229 (setq c (js2-get-char))
6230 (cond
6231 ((eq c js2-EOF_CHAR)
6232 (setf (js2-token-end token) (1- js2-ts-cursor))
6233 (js2-report-error "msg.unterminated.comment")
6234 (throw 'return js2-COMMENT))
6235 ((eq c ?*)
6236 (setq look-for-slash t))
6237 ((eq c ?/)
6238 (if look-for-slash
6239 (js2-ts-return token js2-COMMENT)))
6240 (t
6241 (setf look-for-slash nil
6242 (js2-token-end token) js2-ts-cursor)))))
6243 (if (js2-match-char ?=)
6244 js2-ASSIGN_DIV
6245 (throw 'return js2-DIV)))
6246 (?#
6247 (when js2-skip-preprocessor-directives
6248 (js2-skip-line)
6249 (setf (js2-token-comment-type token) 'preprocessor
6250 (js2-token-end token) js2-ts-cursor)
6251 (throw 'return js2-COMMENT))
6252 (throw 'return js2-ERROR))
6253 (?%
6254 (if (js2-match-char ?=)
6255 js2-ASSIGN_MOD
6256 (throw 'return js2-MOD)))
6257 (?~
6258 (throw 'return js2-BITNOT))
6259 (?+
6260 (if (js2-match-char ?=)
6261 js2-ASSIGN_ADD
6262 (if (js2-match-char ?+)
6263 js2-INC
6264 (throw 'return js2-ADD))))
6265 (?-
6266 (cond
6267 ((js2-match-char ?=)
6268 (setq c js2-ASSIGN_SUB))
6269 ((js2-match-char ?-)
6270 (unless js2-ts-dirty-line
6271 ;; treat HTML end-comment after possible whitespace
6272 ;; after line start as comment-until-eol
6273 (when (js2-match-char ?>)
6274 (js2-skip-line)
6275 (setf (js2-token-comment-type (js2-current-token)) 'html)
6276 (throw 'return js2-COMMENT)))
6277 (setq c js2-DEC))
6278 (t
6279 (setq c js2-SUB)))
6280 (setq js2-ts-dirty-line t)
6281 c)
6282 (otherwise
6283 (js2-report-scan-error "msg.illegal.character")))))))
6284 (setf (js2-token-type token) tt)
6285 token))
6286
6287 (defun js2-get-string-or-template-token (quote-char token)
6288 ;; We attempt to accumulate a string the fast way, by
6289 ;; building it directly out of the reader. But if there
6290 ;; are any escaped characters in the string, we revert to
6291 ;; building it out of a string buffer.
6292 (let ((c (js2-get-char))
6293 js2-ts-string-buffer
6294 nc c1 val escape-val)
6295 (catch 'break
6296 (while (/= c quote-char)
6297 (catch 'continue
6298 (when (eq c js2-EOF_CHAR)
6299 (js2-unget-char)
6300 (js2-report-error "msg.unterminated.string.lit")
6301 (throw 'break nil))
6302 (when (and (eq c ?\n) (not (eq quote-char ?`)))
6303 (js2-unget-char)
6304 (js2-report-error "msg.unterminated.string.lit")
6305 (throw 'break nil))
6306 (when (eq c ?\\)
6307 ;; We've hit an escaped character
6308 (setq c (js2-get-char))
6309 (cl-case c
6310 (?b (setq c ?\b))
6311 (?f (setq c ?\f))
6312 (?n (setq c ?\n))
6313 (?r (setq c ?\r))
6314 (?t (setq c ?\t))
6315 (?v (setq c ?\v))
6316 (?u
6317 (setq c1 (js2-read-unicode-escape))
6318 (if js2-parse-ide-mode
6319 (if c1
6320 (progn
6321 ;; just copy the string in IDE-mode
6322 (js2-add-to-string ?\\)
6323 (js2-add-to-string ?u)
6324 (dotimes (_ 3)
6325 (js2-add-to-string (js2-get-char)))
6326 (setq c (js2-get-char))) ; added at end of loop
6327 ;; flag it as an invalid escape
6328 (js2-report-warning "msg.invalid.escape"
6329 nil (- js2-ts-cursor 2) 6))
6330 ;; Get 4 hex digits; if the u escape is not
6331 ;; followed by 4 hex digits, use 'u' + the
6332 ;; literal character sequence that follows.
6333 (js2-add-to-string ?u)
6334 (setq escape-val 0)
6335 (dotimes (_ 4)
6336 (setq c (js2-get-char)
6337 escape-val (js2-x-digit-to-int c escape-val))
6338 (if (cl-minusp escape-val)
6339 (throw 'continue nil))
6340 (js2-add-to-string c))
6341 ;; prepare for replace of stored 'u' sequence by escape value
6342 (setq js2-ts-string-buffer (nthcdr 5 js2-ts-string-buffer)
6343 c escape-val)))
6344 (?x
6345 ;; Get 2 hex digits, defaulting to 'x'+literal
6346 ;; sequence, as above.
6347 (setq c (js2-get-char)
6348 escape-val (js2-x-digit-to-int c 0))
6349 (if (cl-minusp escape-val)
6350 (progn
6351 (js2-add-to-string ?x)
6352 (throw 'continue nil))
6353 (setq c1 c
6354 c (js2-get-char)
6355 escape-val (js2-x-digit-to-int c escape-val))
6356 (if (cl-minusp escape-val)
6357 (progn
6358 (js2-add-to-string ?x)
6359 (js2-add-to-string c1)
6360 (throw 'continue nil))
6361 ;; got 2 hex digits
6362 (setq c escape-val))))
6363 (?\n
6364 ;; Remove line terminator after escape to follow
6365 ;; SpiderMonkey and C/C++
6366 (setq c (js2-get-char))
6367 (throw 'continue nil))
6368 (t
6369 (when (and (<= ?0 c) (< c ?8))
6370 (setq val (- c ?0)
6371 c (js2-get-char))
6372 (when (and (<= ?0 c) (< c ?8))
6373 (setq val (- (+ (* 8 val) c) ?0)
6374 c (js2-get-char))
6375 (when (and (<= ?0 c)
6376 (< c ?8)
6377 (< val #o37))
6378 ;; c is 3rd char of octal sequence only
6379 ;; if the resulting val <= 0377
6380 (setq val (- (+ (* 8 val) c) ?0)
6381 c (js2-get-char))))
6382 (js2-unget-char)
6383 (setq c val)))))
6384 (when (and (eq quote-char ?`) (eq c ?$))
6385 (when (eq (setq nc (js2-get-char)) ?\{)
6386 (throw 'break nil))
6387 (js2-unget-char))
6388 (js2-add-to-string c)
6389 (setq c (js2-get-char)))))
6390 (js2-set-string-from-buffer token)
6391 (if (not (eq quote-char ?`))
6392 js2-STRING
6393 (if (and (eq c ?$) (eq nc ?\{))
6394 js2-TEMPLATE_HEAD
6395 js2-NO_SUBS_TEMPLATE))))
6396
6397 (defun js2-read-regexp (start-tt)
6398 "Called by parser when it gets / or /= in literal context."
6399 (let (c err
6400 in-class ; inside a '[' .. ']' character-class
6401 flags
6402 (continue t)
6403 (token (js2-new-token 0)))
6404 (setq js2-ts-string-buffer nil)
6405 (if (eq start-tt js2-ASSIGN_DIV)
6406 ;; mis-scanned /=
6407 (js2-add-to-string ?=)
6408 (if (not (eq start-tt js2-DIV))
6409 (error "failed assertion")))
6410 (while (and (not err)
6411 (or (/= (setq c (js2-get-char)) ?/)
6412 in-class))
6413 (cond
6414 ((or (= c ?\n)
6415 (= c js2-EOF_CHAR))
6416 (setf (js2-token-end token) (1- js2-ts-cursor)
6417 err t
6418 (js2-token-string token) (js2-collect-string js2-ts-string-buffer))
6419 (js2-report-error "msg.unterminated.re.lit"))
6420 (t (cond
6421 ((= c ?\\)
6422 (js2-add-to-string c)
6423 (setq c (js2-get-char)))
6424 ((= c ?\[)
6425 (setq in-class t))
6426 ((= c ?\])
6427 (setq in-class nil)))
6428 (js2-add-to-string c))))
6429 (unless err
6430 (while continue
6431 (cond
6432 ((js2-match-char ?g)
6433 (push ?g flags))
6434 ((js2-match-char ?i)
6435 (push ?i flags))
6436 ((js2-match-char ?m)
6437 (push ?m flags))
6438 ((and (js2-match-char ?u)
6439 (>= js2-language-version 200))
6440 (push ?u flags))
6441 ((and (js2-match-char ?y)
6442 (>= js2-language-version 200))
6443 (push ?y flags))
6444 (t
6445 (setq continue nil))))
6446 (if (js2-alpha-p (js2-peek-char))
6447 (js2-report-scan-error "msg.invalid.re.flag" t
6448 js2-ts-cursor 1))
6449 (js2-set-string-from-buffer token))
6450 (js2-collect-string flags)))
6451
6452 (defun js2-get-first-xml-token ()
6453 (setq js2-ts-xml-open-tags-count 0
6454 js2-ts-is-xml-attribute nil
6455 js2-ts-xml-is-tag-content nil)
6456 (js2-unget-char)
6457 (js2-get-next-xml-token))
6458
6459 (defun js2-xml-discard-string (token)
6460 "Throw away the string in progress and flag an XML parse error."
6461 (setf js2-ts-string-buffer nil
6462 (js2-token-string token) nil)
6463 (js2-report-scan-error "msg.XML.bad.form" t))
6464
6465 (defun js2-get-next-xml-token ()
6466 (setq js2-ts-string-buffer nil) ; for recording the XML
6467 (let ((token (js2-new-token 0))
6468 c result)
6469 (setq result
6470 (catch 'return
6471 (while t
6472 (setq c (js2-get-char))
6473 (cond
6474 ((= c js2-EOF_CHAR)
6475 (throw 'return js2-ERROR))
6476 (js2-ts-xml-is-tag-content
6477 (cl-case c
6478 (?>
6479 (js2-add-to-string c)
6480 (setq js2-ts-xml-is-tag-content nil
6481 js2-ts-is-xml-attribute nil))
6482 (?/
6483 (js2-add-to-string c)
6484 (when (eq ?> (js2-peek-char))
6485 (setq c (js2-get-char))
6486 (js2-add-to-string c)
6487 (setq js2-ts-xml-is-tag-content nil)
6488 (cl-decf js2-ts-xml-open-tags-count)))
6489 (?{
6490 (js2-unget-char)
6491 (js2-set-string-from-buffer token)
6492 (throw 'return js2-XML))
6493 ((?\' ?\")
6494 (js2-add-to-string c)
6495 (unless (js2-read-quoted-string c token)
6496 (throw 'return js2-ERROR)))
6497 (?=
6498 (js2-add-to-string c)
6499 (setq js2-ts-is-xml-attribute t))
6500 ((? ?\t ?\r ?\n)
6501 (js2-add-to-string c))
6502 (t
6503 (js2-add-to-string c)
6504 (setq js2-ts-is-xml-attribute nil)))
6505 (when (and (not js2-ts-xml-is-tag-content)
6506 (zerop js2-ts-xml-open-tags-count))
6507 (js2-set-string-from-buffer token)
6508 (throw 'return js2-XMLEND)))
6509 (t
6510 ;; else not tag content
6511 (cl-case c
6512 (?<
6513 (js2-add-to-string c)
6514 (setq c (js2-peek-char))
6515 (cl-case c
6516 (?!
6517 (setq c (js2-get-char)) ;; skip !
6518 (js2-add-to-string c)
6519 (setq c (js2-peek-char))
6520 (cl-case c
6521 (?-
6522 (setq c (js2-get-char)) ;; skip -
6523 (js2-add-to-string c)
6524 (if (eq c ?-)
6525 (progn
6526 (js2-add-to-string c)
6527 (unless (js2-read-xml-comment token)
6528 (throw 'return js2-ERROR)))
6529 (js2-xml-discard-string token)
6530 (throw 'return js2-ERROR)))
6531 (?\[
6532 (setq c (js2-get-char)) ;; skip [
6533 (js2-add-to-string c)
6534 (if (and (= (js2-get-char) ?C)
6535 (= (js2-get-char) ?D)
6536 (= (js2-get-char) ?A)
6537 (= (js2-get-char) ?T)
6538 (= (js2-get-char) ?A)
6539 (= (js2-get-char) ?\[))
6540 (progn
6541 (js2-add-to-string ?C)
6542 (js2-add-to-string ?D)
6543 (js2-add-to-string ?A)
6544 (js2-add-to-string ?T)
6545 (js2-add-to-string ?A)
6546 (js2-add-to-string ?\[)
6547 (unless (js2-read-cdata token)
6548 (throw 'return js2-ERROR)))
6549 (js2-xml-discard-string token)
6550 (throw 'return js2-ERROR)))
6551 (t
6552 (unless (js2-read-entity token)
6553 (throw 'return js2-ERROR))))
6554 ;; Allow bare CDATA section, e.g.:
6555 ;; let xml = <![CDATA[ foo bar baz ]]>;
6556 (when (zerop js2-ts-xml-open-tags-count)
6557 (throw 'return js2-XMLEND)))
6558 (??
6559 (setq c (js2-get-char)) ;; skip ?
6560 (js2-add-to-string c)
6561 (unless (js2-read-PI token)
6562 (throw 'return js2-ERROR)))
6563 (?/
6564 ;; end tag
6565 (setq c (js2-get-char)) ;; skip /
6566 (js2-add-to-string c)
6567 (when (zerop js2-ts-xml-open-tags-count)
6568 (js2-xml-discard-string token)
6569 (throw 'return js2-ERROR))
6570 (setq js2-ts-xml-is-tag-content t)
6571 (cl-decf js2-ts-xml-open-tags-count))
6572 (t
6573 ;; start tag
6574 (setq js2-ts-xml-is-tag-content t)
6575 (cl-incf js2-ts-xml-open-tags-count))))
6576 (?{
6577 (js2-unget-char)
6578 (js2-set-string-from-buffer token)
6579 (throw 'return js2-XML))
6580 (t
6581 (js2-add-to-string c))))))))
6582 (setf (js2-token-end token) js2-ts-cursor)
6583 (setf (js2-token-type token) result)
6584 result))
6585
6586 (defun js2-read-quoted-string (quote token)
6587 (let (c)
6588 (catch 'return
6589 (while (/= (setq c (js2-get-char)) js2-EOF_CHAR)
6590 (js2-add-to-string c)
6591 (if (eq c quote)
6592 (throw 'return t)))
6593 (js2-xml-discard-string token) ;; throw away string in progress
6594 nil)))
6595
6596 (defun js2-read-xml-comment (token)
6597 (let ((c (js2-get-char)))
6598 (catch 'return
6599 (while (/= c js2-EOF_CHAR)
6600 (catch 'continue
6601 (js2-add-to-string c)
6602 (when (and (eq c ?-) (eq ?- (js2-peek-char)))
6603 (setq c (js2-get-char))
6604 (js2-add-to-string c)
6605 (if (eq (js2-peek-char) ?>)
6606 (progn
6607 (setq c (js2-get-char)) ;; skip >
6608 (js2-add-to-string c)
6609 (throw 'return t))
6610 (throw 'continue nil)))
6611 (setq c (js2-get-char))))
6612 (js2-xml-discard-string token)
6613 nil)))
6614
6615 (defun js2-read-cdata (token)
6616 (let ((c (js2-get-char)))
6617 (catch 'return
6618 (while (/= c js2-EOF_CHAR)
6619 (catch 'continue
6620 (js2-add-to-string c)
6621 (when (and (eq c ?\]) (eq (js2-peek-char) ?\]))
6622 (setq c (js2-get-char))
6623 (js2-add-to-string c)
6624 (if (eq (js2-peek-char) ?>)
6625 (progn
6626 (setq c (js2-get-char)) ;; Skip >
6627 (js2-add-to-string c)
6628 (throw 'return t))
6629 (throw 'continue nil)))
6630 (setq c (js2-get-char))))
6631 (js2-xml-discard-string token)
6632 nil)))
6633
6634 (defun js2-read-entity (token)
6635 (let ((decl-tags 1)
6636 c)
6637 (catch 'return
6638 (while (/= js2-EOF_CHAR (setq c (js2-get-char)))
6639 (js2-add-to-string c)
6640 (cl-case c
6641 (?<
6642 (cl-incf decl-tags))
6643 (?>
6644 (cl-decf decl-tags)
6645 (if (zerop decl-tags)
6646 (throw 'return t)))))
6647 (js2-xml-discard-string token)
6648 nil)))
6649
6650 (defun js2-read-PI (token)
6651 "Scan an XML processing instruction."
6652 (let (c)
6653 (catch 'return
6654 (while (/= js2-EOF_CHAR (setq c (js2-get-char)))
6655 (js2-add-to-string c)
6656 (when (and (eq c ??) (eq (js2-peek-char) ?>))
6657 (setq c (js2-get-char)) ;; Skip >
6658 (js2-add-to-string c)
6659 (throw 'return t)))
6660 (js2-xml-discard-string token)
6661 nil)))
6662
6663 ;;; Highlighting
6664
6665 (defun js2-set-face (beg end face &optional record)
6666 "Fontify a region. If RECORD is non-nil, record for later."
6667 (when (cl-plusp js2-highlight-level)
6668 (setq beg (min (point-max) beg)
6669 beg (max (point-min) beg)
6670 end (min (point-max) end)
6671 end (max (point-min) end))
6672 (if record
6673 (push (list beg end face) js2-mode-fontifications)
6674 (put-text-property beg end 'font-lock-face face))))
6675
6676 (defsubst js2-clear-face (beg end)
6677 (remove-text-properties beg end '(font-lock-face nil
6678 help-echo nil
6679 point-entered nil
6680 cursor-sensor-functions nil
6681 c-in-sws nil)))
6682
6683 (defconst js2-ecma-global-props
6684 (concat "^"
6685 (regexp-opt
6686 '("Infinity" "NaN" "undefined" "arguments") t)
6687 "$")
6688 "Value properties of the Ecma-262 Global Object.
6689 Shown at or above `js2-highlight-level' 2.")
6690
6691 ;; might want to add the name "arguments" to this list?
6692 (defconst js2-ecma-object-props
6693 (concat "^"
6694 (regexp-opt
6695 '("prototype" "__proto__" "__parent__") t)
6696 "$")
6697 "Value properties of the Ecma-262 Object constructor.
6698 Shown at or above `js2-highlight-level' 2.")
6699
6700 (defconst js2-ecma-global-funcs
6701 (concat
6702 "^"
6703 (regexp-opt
6704 '("decodeURI" "decodeURIComponent" "encodeURI" "encodeURIComponent"
6705 "eval" "isFinite" "isNaN" "parseFloat" "parseInt") t)
6706 "$")
6707 "Function properties of the Ecma-262 Global object.
6708 Shown at or above `js2-highlight-level' 2.")
6709
6710 (defconst js2-ecma-number-props
6711 (concat "^"
6712 (regexp-opt '("MAX_VALUE" "MIN_VALUE" "NaN"
6713 "NEGATIVE_INFINITY"
6714 "POSITIVE_INFINITY") t)
6715 "$")
6716 "Properties of the Ecma-262 Number constructor.
6717 Shown at or above `js2-highlight-level' 2.")
6718
6719 (defconst js2-ecma-date-props "^\\(parse\\|UTC\\)$"
6720 "Properties of the Ecma-262 Date constructor.
6721 Shown at or above `js2-highlight-level' 2.")
6722
6723 (defconst js2-ecma-math-props
6724 (concat "^"
6725 (regexp-opt
6726 '("E" "LN10" "LN2" "LOG2E" "LOG10E" "PI" "SQRT1_2" "SQRT2")
6727 t)
6728 "$")
6729 "Properties of the Ecma-262 Math object.
6730 Shown at or above `js2-highlight-level' 2.")
6731
6732 (defconst js2-ecma-math-funcs
6733 (concat "^"
6734 (regexp-opt
6735 '("abs" "acos" "asin" "atan" "atan2" "ceil" "cos" "exp" "floor"
6736 "log" "max" "min" "pow" "random" "round" "sin" "sqrt" "tan") t)
6737 "$")
6738 "Function properties of the Ecma-262 Math object.
6739 Shown at or above `js2-highlight-level' 2.")
6740
6741 (defconst js2-ecma-function-props
6742 (concat
6743 "^"
6744 (regexp-opt
6745 '(;; properties of the Object prototype object
6746 "hasOwnProperty" "isPrototypeOf" "propertyIsEnumerable"
6747 "toLocaleString" "toString" "valueOf"
6748 ;; properties of the Function prototype object
6749 "apply" "call"
6750 ;; properties of the Array prototype object
6751 "concat" "join" "pop" "push" "reverse" "shift" "slice" "sort"
6752 "splice" "unshift"
6753 ;; properties of the String prototype object
6754 "charAt" "charCodeAt" "fromCharCode" "indexOf" "lastIndexOf"
6755 "localeCompare" "match" "replace" "search" "split" "substring"
6756 "toLocaleLowerCase" "toLocaleUpperCase" "toLowerCase"
6757 "toUpperCase"
6758 ;; properties of the Number prototype object
6759 "toExponential" "toFixed" "toPrecision"
6760 ;; properties of the Date prototype object
6761 "getDate" "getDay" "getFullYear" "getHours" "getMilliseconds"
6762 "getMinutes" "getMonth" "getSeconds" "getTime"
6763 "getTimezoneOffset" "getUTCDate" "getUTCDay" "getUTCFullYear"
6764 "getUTCHours" "getUTCMilliseconds" "getUTCMinutes" "getUTCMonth"
6765 "getUTCSeconds" "setDate" "setFullYear" "setHours"
6766 "setMilliseconds" "setMinutes" "setMonth" "setSeconds" "setTime"
6767 "setUTCDate" "setUTCFullYear" "setUTCHours" "setUTCMilliseconds"
6768 "setUTCMinutes" "setUTCMonth" "setUTCSeconds" "toDateString"
6769 "toLocaleDateString" "toLocaleString" "toLocaleTimeString"
6770 "toTimeString" "toUTCString"
6771 ;; properties of the RegExp prototype object
6772 "exec" "test"
6773 ;; properties of the JSON prototype object
6774 "parse" "stringify"
6775 ;; SpiderMonkey/Rhino extensions, versions 1.5+
6776 "toSource" "__defineGetter__" "__defineSetter__"
6777 "__lookupGetter__" "__lookupSetter__" "__noSuchMethod__"
6778 "every" "filter" "forEach" "lastIndexOf" "map" "some")
6779 t)
6780 "$")
6781 "Built-in functions defined by Ecma-262 and SpiderMonkey extensions.
6782 Shown at or above `js2-highlight-level' 3.")
6783
6784 (defun js2-parse-highlight-prop-get (parent target prop call-p)
6785 (let ((target-name (and target
6786 (js2-name-node-p target)
6787 (js2-name-node-name target)))
6788 (prop-name (if prop (js2-name-node-name prop)))
6789 (level2 (>= js2-highlight-level 2))
6790 (level3 (>= js2-highlight-level 3)))
6791 (when level2
6792 (let ((face
6793 (if call-p
6794 (cond
6795 ((and target prop)
6796 (cond
6797 ((and level3 (string-match js2-ecma-function-props prop-name))
6798 'font-lock-builtin-face)
6799 ((and target-name prop)
6800 (cond
6801 ((string= target-name "Date")
6802 (if (string-match js2-ecma-date-props prop-name)
6803 'font-lock-builtin-face))
6804 ((string= target-name "Math")
6805 (if (string-match js2-ecma-math-funcs prop-name)
6806 'font-lock-builtin-face))))))
6807 (prop
6808 (if (string-match js2-ecma-global-funcs prop-name)
6809 'font-lock-builtin-face)))
6810 (cond
6811 ((and target prop)
6812 (cond
6813 ((string= target-name "Number")
6814 (if (string-match js2-ecma-number-props prop-name)
6815 'font-lock-constant-face))
6816 ((string= target-name "Math")
6817 (if (string-match js2-ecma-math-props prop-name)
6818 'font-lock-constant-face))))
6819 (prop
6820 (if (string-match js2-ecma-object-props prop-name)
6821 'font-lock-constant-face))))))
6822 (when face
6823 (let ((pos (+ (js2-node-pos parent) ; absolute
6824 (js2-node-pos prop)))) ; relative
6825 (js2-set-face pos
6826 (+ pos (js2-node-len prop))
6827 face 'record)))))))
6828
6829 (defun js2-parse-highlight-member-expr-node (node)
6830 "Perform syntax highlighting of EcmaScript built-in properties.
6831 The variable `js2-highlight-level' governs this highlighting."
6832 (let (face target prop name pos end parent call-p callee)
6833 (cond
6834 ;; case 1: simple name, e.g. foo
6835 ((js2-name-node-p node)
6836 (setq name (js2-name-node-name node))
6837 ;; possible for name to be nil in rare cases - saw it when
6838 ;; running js2-mode on an elisp buffer. Might as well try to
6839 ;; make it so js2-mode never barfs.
6840 (when name
6841 (setq face (if (string-match js2-ecma-global-props name)
6842 'font-lock-constant-face))
6843 (when face
6844 (setq pos (js2-node-pos node)
6845 end (+ pos (js2-node-len node)))
6846 (js2-set-face pos end face 'record))))
6847 ;; case 2: property access or function call
6848 ((or (js2-prop-get-node-p node)
6849 ;; highlight function call if expr is a prop-get node
6850 ;; or a plain name (i.e. unqualified function call)
6851 (and (setq call-p (js2-call-node-p node))
6852 (setq callee (js2-call-node-target node)) ; separate setq!
6853 (or (js2-prop-get-node-p callee)
6854 (js2-name-node-p callee))))
6855 (setq parent node
6856 node (if call-p callee node))
6857 (if (and call-p (js2-name-node-p callee))
6858 (setq prop callee)
6859 (setq target (js2-prop-get-node-left node)
6860 prop (js2-prop-get-node-right node)))
6861 (cond
6862 ((js2-name-node-p prop)
6863 ;; case 2(a&c): simple or complex target, simple name, e.g. x[y].bar
6864 (js2-parse-highlight-prop-get parent target prop call-p))
6865 ((js2-name-node-p target)
6866 ;; case 2b: simple target, complex name, e.g. foo.x[y]
6867 (js2-parse-highlight-prop-get parent target nil call-p)))))))
6868
6869 (defun js2-parse-highlight-member-expr-fn-name (expr)
6870 "Highlight the `baz' in function foo.bar.baz(args) {...}.
6871 This is experimental Rhino syntax. EXPR is the foo.bar.baz member expr.
6872 We currently only handle the case where the last component is a prop-get
6873 of a simple name. Called before EXPR has a parent node."
6874 (let (pos
6875 (name (and (js2-prop-get-node-p expr)
6876 (js2-prop-get-node-right expr))))
6877 (when (js2-name-node-p name)
6878 (js2-set-face (setq pos (+ (js2-node-pos expr) ; parent is absolute
6879 (js2-node-pos name)))
6880 (+ pos (js2-node-len name))
6881 'font-lock-function-name-face
6882 'record))))
6883
6884 ;; source: http://jsdoc.sourceforge.net/
6885 ;; Note - this syntax is for Google's enhanced jsdoc parser that
6886 ;; allows type specifications, and needs work before entering the wild.
6887
6888 (defconst js2-jsdoc-param-tag-regexp
6889 (concat "^\\s-*\\*+\\s-*\\(@"
6890 "\\(?:param\\|argument\\)"
6891 "\\)"
6892 "\\s-*\\({[^}]+}\\)?" ; optional type
6893 "\\s-*\\[?\\([[:alnum:]_$\.]+\\)?\\]?" ; name
6894 "\\>")
6895 "Matches jsdoc tags with optional type and optional param name.")
6896
6897 (defconst js2-jsdoc-typed-tag-regexp
6898 (concat "^\\s-*\\*+\\s-*\\(@\\(?:"
6899 (regexp-opt
6900 '("enum"
6901 "extends"
6902 "field"
6903 "id"
6904 "implements"
6905 "lends"
6906 "mods"
6907 "requires"
6908 "return"
6909 "returns"
6910 "throw"
6911 "throws"))
6912 "\\)\\)\\s-*\\({[^}]+}\\)?")
6913 "Matches jsdoc tags with optional type.")
6914
6915 (defconst js2-jsdoc-arg-tag-regexp
6916 (concat "^\\s-*\\*+\\s-*\\(@\\(?:"
6917 (regexp-opt
6918 '("alias"
6919 "augments"
6920 "borrows"
6921 "bug"
6922 "base"
6923 "config"
6924 "default"
6925 "define"
6926 "exception"
6927 "function"
6928 "member"
6929 "memberOf"
6930 "name"
6931 "namespace"
6932 "property"
6933 "since"
6934 "suppress"
6935 "this"
6936 "throws"
6937 "type"
6938 "version"))
6939 "\\)\\)\\s-+\\([^ \t]+\\)")
6940 "Matches jsdoc tags with a single argument.")
6941
6942 (defconst js2-jsdoc-empty-tag-regexp
6943 (concat "^\\s-*\\*+\\s-*\\(@\\(?:"
6944 (regexp-opt
6945 '("addon"
6946 "author"
6947 "class"
6948 "const"
6949 "constant"
6950 "constructor"
6951 "constructs"
6952 "deprecated"
6953 "desc"
6954 "description"
6955 "event"
6956 "example"
6957 "exec"
6958 "export"
6959 "fileoverview"
6960 "final"
6961 "function"
6962 "hidden"
6963 "ignore"
6964 "implicitCast"
6965 "inheritDoc"
6966 "inner"
6967 "interface"
6968 "license"
6969 "noalias"
6970 "noshadow"
6971 "notypecheck"
6972 "override"
6973 "owner"
6974 "preserve"
6975 "preserveTry"
6976 "private"
6977 "protected"
6978 "public"
6979 "static"
6980 "supported"
6981 ))
6982 "\\)\\)\\s-*")
6983 "Matches empty jsdoc tags.")
6984
6985 (defconst js2-jsdoc-link-tag-regexp
6986 "{\\(@\\(?:link\\|code\\)\\)\\s-+\\([^#}\n]+\\)\\(#.+\\)?}"
6987 "Matches a jsdoc link or code tag.")
6988
6989 (defconst js2-jsdoc-see-tag-regexp
6990 "^\\s-*\\*+\\s-*\\(@see\\)\\s-+\\([^#}\n]+\\)\\(#.+\\)?"
6991 "Matches a jsdoc @see tag.")
6992
6993 (defconst js2-jsdoc-html-tag-regexp
6994 "\\(</?\\)\\([[:alpha:]]+\\)\\s-*\\(/?>\\)"
6995 "Matches a simple (no attributes) html start- or end-tag.")
6996
6997 (defun js2-jsdoc-highlight-helper ()
6998 (js2-set-face (match-beginning 1)
6999 (match-end 1)
7000 'js2-jsdoc-tag)
7001 (if (match-beginning 2)
7002 (if (save-excursion
7003 (goto-char (match-beginning 2))
7004 (= (char-after) ?{))
7005 (js2-set-face (1+ (match-beginning 2))
7006 (1- (match-end 2))
7007 'js2-jsdoc-type)
7008 (js2-set-face (match-beginning 2)
7009 (match-end 2)
7010 'js2-jsdoc-value)))
7011 (if (match-beginning 3)
7012 (js2-set-face (match-beginning 3)
7013 (match-end 3)
7014 'js2-jsdoc-value)))
7015
7016 (defun js2-highlight-jsdoc (ast)
7017 "Highlight doc comment tags."
7018 (let ((comments (js2-ast-root-comments ast))
7019 beg end)
7020 (save-excursion
7021 (dolist (node comments)
7022 (when (eq (js2-comment-node-format node) 'jsdoc)
7023 (setq beg (js2-node-abs-pos node)
7024 end (+ beg (js2-node-len node)))
7025 (save-restriction
7026 (narrow-to-region beg end)
7027 (dolist (re (list js2-jsdoc-param-tag-regexp
7028 js2-jsdoc-typed-tag-regexp
7029 js2-jsdoc-arg-tag-regexp
7030 js2-jsdoc-link-tag-regexp
7031 js2-jsdoc-see-tag-regexp
7032 js2-jsdoc-empty-tag-regexp))
7033 (goto-char beg)
7034 (while (re-search-forward re nil t)
7035 (js2-jsdoc-highlight-helper)))
7036 ;; simple highlighting for html tags
7037 (goto-char beg)
7038 (while (re-search-forward js2-jsdoc-html-tag-regexp nil t)
7039 (js2-set-face (match-beginning 1)
7040 (match-end 1)
7041 'js2-jsdoc-html-tag-delimiter)
7042 (js2-set-face (match-beginning 2)
7043 (match-end 2)
7044 'js2-jsdoc-html-tag-name)
7045 (js2-set-face (match-beginning 3)
7046 (match-end 3)
7047 'js2-jsdoc-html-tag-delimiter))))))))
7048
7049 (defun js2-highlight-assign-targets (_node left right)
7050 "Highlight function properties and external variables."
7051 (let (leftpos name)
7052 ;; highlight vars and props assigned function values
7053 (when (or (js2-function-node-p right)
7054 (js2-class-node-p right))
7055 (cond
7056 ;; var foo = function() {...}
7057 ((js2-name-node-p left)
7058 (setq name left))
7059 ;; foo.bar.baz = function() {...}
7060 ((and (js2-prop-get-node-p left)
7061 (js2-name-node-p (js2-prop-get-node-right left)))
7062 (setq name (js2-prop-get-node-right left))))
7063 (when name
7064 (js2-set-face (setq leftpos (js2-node-abs-pos name))
7065 (+ leftpos (js2-node-len name))
7066 'font-lock-function-name-face
7067 'record)))))
7068
7069 (defun js2-record-name-node (node)
7070 "Saves NODE to `js2-recorded-identifiers' to check for undeclared variables
7071 later. NODE must be a name node."
7072 (let ((leftpos (js2-node-abs-pos node)))
7073 (push (list node js2-current-scope
7074 leftpos
7075 (+ leftpos (js2-node-len node)))
7076 js2-recorded-identifiers)))
7077
7078 (defun js2-highlight-undeclared-vars ()
7079 "After entire parse is finished, look for undeclared variable references.
7080 We have to wait until entire buffer is parsed, since JavaScript permits var
7081 decls to occur after they're used.
7082
7083 If any undeclared var name is in `js2-externs' or `js2-additional-externs',
7084 it is considered declared."
7085 (let (name)
7086 (dolist (entry js2-recorded-identifiers)
7087 (cl-destructuring-bind (name-node scope pos end) entry
7088 (setq name (js2-name-node-name name-node))
7089 (unless (or (member name js2-global-externs)
7090 (member name js2-default-externs)
7091 (member name js2-additional-externs)
7092 (js2-get-defining-scope scope name pos))
7093 (js2-report-warning "msg.undeclared.variable" name pos (- end pos)
7094 'js2-external-variable))))))
7095
7096 (defun js2--add-or-update-symbol (symbol inition used vars)
7097 "Add or update SYMBOL entry in VARS, an hash table.
7098 SYMBOL is a js2-name-node, INITION either nil, t, or ?P,
7099 respectively meaning that SYMBOL is a mere declaration, an
7100 assignment or a function parameter; when USED is t, the symbol
7101 node is assumed to be an usage and thus added to the list stored
7102 in the cdr of the entry.
7103 "
7104 (let* ((nm (js2-name-node-name symbol))
7105 (es (js2-node-get-enclosing-scope symbol))
7106 (ds (js2-get-defining-scope es nm)))
7107 (when (and ds (not (equal nm "arguments")))
7108 (let* ((sym (js2-scope-get-symbol ds nm))
7109 (var (gethash sym vars))
7110 (err-var-p (js2-catch-node-p ds)))
7111 (unless inition
7112 (setq inition err-var-p))
7113 (if var
7114 (progn
7115 (when (and inition (not (equal (car var) ?P)))
7116 (setcar var inition))
7117 (when used
7118 (push symbol (cdr var))))
7119 ;; do not consider the declaration of catch parameter as an usage
7120 (when (and err-var-p used)
7121 (setq used nil))
7122 (puthash sym (cons inition (if used (list symbol))) vars))))))
7123
7124 (defun js2--classify-variables ()
7125 "Collect and classify variables declared or used within js2-mode-ast.
7126 Traverse the whole ast tree returning a summary of the variables
7127 usage as an hash-table, keyed by their corresponding symbol table
7128 entry.
7129 Each variable is described by a tuple where the car is a flag
7130 indicating whether the variable has been initialized and the cdr
7131 is a possibly empty list of name nodes where it is used. External
7132 symbols, i.e. those not present in the whole scopes hierarchy,
7133 are ignored."
7134 (let ((vars (make-hash-table :test #'eq :size 100)))
7135 (js2-visit-ast
7136 js2-mode-ast
7137 (lambda (node end-p)
7138 (when (null end-p)
7139 (cond
7140 ((js2-var-init-node-p node)
7141 ;; take note about possibly initialized declarations
7142 (let ((target (js2-var-init-node-target node))
7143 (initializer (js2-var-init-node-initializer node)))
7144 (when target
7145 (let* ((parent (js2-node-parent node))
7146 (grandparent (if parent (js2-node-parent parent)))
7147 (inited (not (null initializer))))
7148 (unless inited
7149 (setq inited
7150 (and grandparent
7151 (js2-for-in-node-p grandparent)
7152 (memq target
7153 (mapcar #'js2-var-init-node-target
7154 (js2-var-decl-node-kids
7155 (js2-for-in-node-iterator grandparent)))))))
7156 (js2--add-or-update-symbol target inited nil vars)))))
7157
7158 ((js2-assign-node-p node)
7159 ;; take note about assignments
7160 (let ((left (js2-assign-node-left node)))
7161 (when (js2-name-node-p left)
7162 (js2--add-or-update-symbol left t nil vars))))
7163
7164 ((js2-prop-get-node-p node)
7165 ;; handle x.y.z nodes, considering only x
7166 (let ((left (js2-prop-get-node-left node)))
7167 (when (js2-name-node-p left)
7168 (js2--add-or-update-symbol left nil t vars))))
7169
7170 ((js2-name-node-p node)
7171 ;; take note about used variables
7172 (let ((parent (js2-node-parent node)))
7173 (when parent
7174 (unless (or (and (js2-var-init-node-p parent) ; handled above
7175 (eq node (js2-var-init-node-target parent)))
7176 (and (js2-assign-node-p parent)
7177 (eq node (js2-assign-node-left parent)))
7178 (js2-prop-get-node-p parent))
7179 (let ((used t) inited)
7180 (cond
7181 ((and (js2-function-node-p parent)
7182 (js2-wrapper-function-p parent))
7183 (setq inited (if (memq node (js2-function-node-params parent)) ?P t)))
7184
7185 ((js2-for-in-node-p parent)
7186 (if (eq node (js2-for-in-node-iterator parent))
7187 (setq inited t used nil)))
7188
7189 ((js2-function-node-p parent)
7190 (setq inited (if (memq node (js2-function-node-params parent)) ?P t)
7191 used nil)))
7192
7193 (unless used
7194 (let ((grandparent (js2-node-parent parent)))
7195 (when grandparent
7196 (setq used (js2-return-node-p grandparent)))))
7197
7198 (js2--add-or-update-symbol node inited used vars))))))))
7199 t))
7200 vars))
7201
7202 (defun js2--get-name-node (node)
7203 (cond
7204 ((js2-name-node-p node) node)
7205 ((js2-function-node-p node)
7206 (js2-function-node-name node))
7207 ((js2-class-node-p node)
7208 (js2-class-node-name node))
7209 ((js2-comp-loop-node-p node)
7210 (js2-comp-loop-node-iterator node))
7211 (t node)))
7212
7213 (defun js2--highlight-unused-variable (symbol info)
7214 (let ((name (js2-symbol-name symbol))
7215 (inited (car info))
7216 (refs (cdr info))
7217 pos len)
7218 (unless (and inited refs)
7219 (if refs
7220 (dolist (ref refs)
7221 (setq pos (js2-node-abs-pos ref))
7222 (setq len (js2-name-node-len ref))
7223 (js2-report-warning "msg.uninitialized.variable" name pos len
7224 'js2-warning))
7225 (when (or js2-warn-about-unused-function-arguments
7226 (not (eq inited ?P)))
7227 (let* ((symn (js2-symbol-ast-node symbol))
7228 (namen (js2--get-name-node symn)))
7229 (unless (js2-node-top-level-decl-p namen)
7230 (setq pos (js2-node-abs-pos namen))
7231 (setq len (js2-name-node-len namen))
7232 (js2-report-warning "msg.unused.variable" name pos len
7233 'js2-warning))))))))
7234
7235 (defun js2-highlight-unused-variables ()
7236 "Highlight unused variables."
7237 (let ((vars (js2--classify-variables)))
7238 (maphash #'js2--highlight-unused-variable vars)))
7239
7240 ;;;###autoload
7241 (define-minor-mode js2-highlight-unused-variables-mode
7242 "Toggle highlight of unused variables."
7243 :lighter ""
7244 (if js2-highlight-unused-variables-mode
7245 (add-hook 'js2-post-parse-callbacks
7246 #'js2-highlight-unused-variables nil t)
7247 (remove-hook 'js2-post-parse-callbacks
7248 #'js2-highlight-unused-variables t)))
7249
7250 (defun js2-set-default-externs ()
7251 "Set the value of `js2-default-externs' based on the various
7252 `js2-include-?-externs' variables."
7253 (setq js2-default-externs
7254 (append js2-ecma-262-externs
7255 (if js2-include-browser-externs js2-browser-externs)
7256 (if (and js2-include-browser-externs
7257 (>= js2-language-version 200)) js2-harmony-externs)
7258 (if js2-include-rhino-externs js2-rhino-externs)
7259 (if js2-include-node-externs js2-node-externs)
7260 (if (or js2-include-browser-externs js2-include-node-externs)
7261 js2-typed-array-externs))))
7262
7263 (defun js2-apply-jslint-globals ()
7264 (setq js2-additional-externs
7265 (nconc (js2-get-jslint-globals)
7266 js2-additional-externs)))
7267
7268 (defun js2-get-jslint-globals ()
7269 (cl-loop for node in (js2-ast-root-comments js2-mode-ast)
7270 when (and (eq 'block (js2-comment-node-format node))
7271 (save-excursion
7272 (goto-char (js2-node-abs-pos node))
7273 (looking-at "/\\*global ")))
7274 append (js2-get-jslint-globals-in
7275 (match-end 0)
7276 (js2-node-abs-end node))))
7277
7278 (defun js2-get-jslint-globals-in (beg end)
7279 (let (res)
7280 (save-excursion
7281 (goto-char beg)
7282 (while (re-search-forward js2-mode-identifier-re end t)
7283 (let ((match (match-string 0)))
7284 (unless (member match '("true" "false"))
7285 (push match res)))))
7286 (nreverse res)))
7287
7288 ;;; IMenu support
7289
7290 ;; We currently only support imenu, but eventually should support speedbar and
7291 ;; possibly other browsing mechanisms.
7292
7293 ;; The basic strategy is to identify function assignment targets of the form
7294 ;; `foo.bar.baz', convert them to (list fn foo bar baz <position>), and push the
7295 ;; list into `js2-imenu-recorder'. The lists are merged into a trie-like tree
7296 ;; for imenu after parsing is finished.
7297
7298 ;; A `foo.bar.baz' assignment target may be expressed in many ways in
7299 ;; JavaScript, and the general problem is undecidable. However, several forms
7300 ;; are readily recognizable at parse-time; the forms we attempt to recognize
7301 ;; include:
7302
7303 ;; function foo() -- function declaration
7304 ;; foo = function() -- function expression assigned to variable
7305 ;; foo.bar.baz = function() -- function expr assigned to nested property-get
7306 ;; foo = {bar: function()} -- fun prop in object literal assigned to var
7307 ;; foo = {bar: {baz: function()}} -- inside nested object literal
7308 ;; foo.bar = {baz: function()}} -- obj lit assigned to nested prop get
7309 ;; a.b = {c: {d: function()}} -- nested obj lit assigned to nested prop get
7310 ;; foo = {get bar() {...}} -- getter/setter in obj literal
7311 ;; function foo() {function bar() {...}} -- nested function
7312 ;; foo['a'] = function() -- fun expr assigned to deterministic element-get
7313
7314 ;; This list boils down to a few forms that can be combined recursively.
7315 ;; Top-level named function declarations include both the left-hand (name)
7316 ;; and the right-hand (function value) expressions needed to produce an imenu
7317 ;; entry. The other "right-hand" forms we need to look for are:
7318 ;; - functions declared as props/getters/setters in object literals
7319 ;; - nested named function declarations
7320 ;; The "left-hand" expressions that functions can be assigned to include:
7321 ;; - local/global variables
7322 ;; - nested property-get expressions like a.b.c.d
7323 ;; - element gets like foo[10] or foo['bar'] where the index
7324 ;; expression can be trivially converted to a property name. They
7325 ;; effectively then become property gets.
7326
7327 ;; All the different definition types are canonicalized into the form
7328 ;; foo.bar.baz = position-of-function-keyword
7329
7330 ;; We need to build a trie-like structure for imenu. As an example,
7331 ;; consider the following JavaScript code:
7332
7333 ;; a = function() {...} // function at position 5
7334 ;; b = function() {...} // function at position 25
7335 ;; foo = function() {...} // function at position 100
7336 ;; foo.bar = function() {...} // function at position 200
7337 ;; foo.bar.baz = function() {...} // function at position 300
7338 ;; foo.bar.zab = function() {...} // function at position 400
7339
7340 ;; During parsing we accumulate an entry for each definition in
7341 ;; the variable `js2-imenu-recorder', like so:
7342
7343 ;; '((fn a 5)
7344 ;; (fn b 25)
7345 ;; (fn foo 100)
7346 ;; (fn foo bar 200)
7347 ;; (fn foo bar baz 300)
7348 ;; (fn foo bar zab 400))
7349
7350 ;; Where 'fn' is the respective function node.
7351 ;; After parsing these entries are merged into this alist-trie:
7352
7353 ;; '((a . 1)
7354 ;; (b . 2)
7355 ;; (foo (<definition> . 3)
7356 ;; (bar (<definition> . 6)
7357 ;; (baz . 100)
7358 ;; (zab . 200))))
7359
7360 ;; Note the wacky need for a <definition> name. The token can be anything
7361 ;; that isn't a valid JavaScript identifier, because you might make foo
7362 ;; a function and then start setting properties on it that are also functions.
7363
7364 (defun js2-prop-node-name (node)
7365 "Return the name of a node that may be a property-get/property-name.
7366 If NODE is not a valid name-node, string-node or integral number-node,
7367 returns nil. Otherwise returns the string name/value of the node."
7368 (cond
7369 ((js2-name-node-p node)
7370 (js2-name-node-name node))
7371 ((js2-string-node-p node)
7372 (js2-string-node-value node))
7373 ((and (js2-number-node-p node)
7374 (string-match "^[0-9]+$" (js2-number-node-value node)))
7375 (js2-number-node-value node))
7376 ((eq (js2-node-type node) js2-THIS)
7377 "this")
7378 ((eq (js2-node-type node) js2-SUPER)
7379 "super")))
7380
7381 (defun js2-node-qname-component (node)
7382 "Return the name of this node, if it contributes to a qname.
7383 Returns nil if the node doesn't contribute."
7384 (copy-sequence
7385 (or (js2-prop-node-name node)
7386 (if (and (js2-function-node-p node)
7387 (js2-function-node-name node))
7388 (js2-name-node-name (js2-function-node-name node))))))
7389
7390 (defun js2-record-imenu-entry (fn-node qname pos)
7391 "Add an entry to `js2-imenu-recorder'.
7392 FN-NODE should be the current item's function node.
7393
7394 Associate FN-NODE with its QNAME for later lookup.
7395 This is used in postprocessing the chain list. For each chain, we find
7396 the parent function, look up its qname, then prepend a copy of it to the chain."
7397 (push (cons fn-node (append qname (list pos))) js2-imenu-recorder)
7398 (unless js2-imenu-function-map
7399 (setq js2-imenu-function-map (make-hash-table :test 'eq)))
7400 (puthash fn-node qname js2-imenu-function-map))
7401
7402 (defun js2-record-imenu-functions (node &optional var)
7403 "Record function definitions for imenu.
7404 NODE is a function node or an object literal.
7405 VAR, if non-nil, is the expression that NODE is being assigned to.
7406 When passed arguments of wrong type, does nothing."
7407 (when js2-parse-ide-mode
7408 (let ((fun-p (js2-function-node-p node))
7409 qname fname-node)
7410 (cond
7411 ;; non-anonymous function declaration?
7412 ((and fun-p
7413 (not var)
7414 (setq fname-node (js2-function-node-name node)))
7415 (js2-record-imenu-entry node (list fname-node) (js2-node-pos node)))
7416 ;; for remaining forms, compute left-side tree branch first
7417 ((and var (setq qname (js2-compute-nested-prop-get var)))
7418 (cond
7419 ;; foo.bar.baz = function
7420 (fun-p
7421 (js2-record-imenu-entry node qname (js2-node-pos node)))
7422 ;; foo.bar.baz = object-literal
7423 ;; look for nested functions: {a: {b: function() {...} }}
7424 ((js2-object-node-p node)
7425 ;; Node position here is still absolute, since the parser
7426 ;; passes the assignment target and value expressions
7427 ;; to us before they are added as children of the assignment node.
7428 (js2-record-object-literal node qname (js2-node-pos node)))))))))
7429
7430 (defun js2-compute-nested-prop-get (node)
7431 "If NODE is of form foo.bar, foo['bar'], or any nested combination, return
7432 component nodes as a list. Otherwise return nil. Element-gets are treated
7433 as property-gets if the index expression is a string, or a positive integer."
7434 (let (left right head)
7435 (cond
7436 ((or (js2-name-node-p node)
7437 (js2-this-or-super-node-p node))
7438 (list node))
7439 ;; foo.bar.baz is parenthesized as (foo.bar).baz => right operand is a leaf
7440 ((js2-prop-get-node-p node) ; foo.bar
7441 (setq left (js2-prop-get-node-left node)
7442 right (js2-prop-get-node-right node))
7443 (if (setq head (js2-compute-nested-prop-get left))
7444 (nconc head (list right))))
7445 ((js2-elem-get-node-p node) ; foo['bar'] or foo[101]
7446 (setq left (js2-elem-get-node-target node)
7447 right (js2-elem-get-node-element node))
7448 (if (or (js2-string-node-p right) ; ['bar']
7449 (and (js2-number-node-p right) ; [10]
7450 (string-match "^[0-9]+$"
7451 (js2-number-node-value right))))
7452 (if (setq head (js2-compute-nested-prop-get left))
7453 (nconc head (list right))))))))
7454
7455 (defun js2-record-object-literal (node qname pos)
7456 "Recursively process an object literal looking for functions.
7457 NODE is an object literal that is the right-hand child of an assignment
7458 expression. QNAME is a list of nodes representing the assignment target,
7459 e.g. for foo.bar.baz = {...}, QNAME is (foo-node bar-node baz-node).
7460 POS is the absolute position of the node.
7461 We do a depth-first traversal of NODE. For any functions we find,
7462 we append the property name to QNAME, then call `js2-record-imenu-entry'."
7463 (let (right)
7464 (dolist (e (js2-object-node-elems node)) ; e is a `js2-object-prop-node'
7465 (let ((left (js2-infix-node-left e))
7466 ;; Element positions are relative to the parent position.
7467 (pos (+ pos (js2-node-pos e))))
7468 (cond
7469 ;; foo: function() {...}
7470 ((js2-function-node-p (setq right (js2-infix-node-right e)))
7471 (when (js2-prop-node-name left)
7472 ;; As a policy decision, we record the position of the property,
7473 ;; not the position of the `function' keyword, since the property
7474 ;; is effectively the name of the function.
7475 (js2-record-imenu-entry right (append qname (list left)) pos)))
7476 ;; foo: {object-literal} -- add foo to qname, offset position, and recurse
7477 ((js2-object-node-p right)
7478 (js2-record-object-literal right
7479 (append qname (list (js2-infix-node-left e)))
7480 (+ pos (js2-node-pos right)))))))))
7481
7482 (defun js2-node-top-level-decl-p (node)
7483 "Return t if NODE's name is defined in the top-level scope.
7484 Also returns t if NODE's name is not defined in any scope, since it implies
7485 that it's an external variable, which must also be in the top-level scope."
7486 (let* ((name (js2-prop-node-name node))
7487 (this-scope (js2-node-get-enclosing-scope node))
7488 defining-scope)
7489 (cond
7490 ((js2-this-or-super-node-p node)
7491 nil)
7492 ((null this-scope)
7493 t)
7494 ((setq defining-scope (js2-get-defining-scope this-scope name))
7495 (js2-ast-root-p defining-scope))
7496 (t t))))
7497
7498 (defun js2-wrapper-function-p (node)
7499 "Return t if NODE is a function expression that's immediately invoked.
7500 NODE must be `js2-function-node'."
7501 (let ((parent (js2-node-parent node)))
7502 (or
7503 ;; function(){...}();
7504 (and (js2-call-node-p parent)
7505 (eq node (js2-call-node-target parent)))
7506 (and (js2-paren-node-p parent)
7507 ;; (function(){...})();
7508 (or (js2-call-node-p (setq parent (js2-node-parent parent)))
7509 ;; (function(){...}).call(this);
7510 (and (js2-prop-get-node-p parent)
7511 (member (js2-name-node-name (js2-prop-get-node-right parent))
7512 '("call" "apply"))
7513 (js2-call-node-p (js2-node-parent parent))))))))
7514
7515 (defun js2-browse-postprocess-chains ()
7516 "Modify function-declaration name chains after parsing finishes.
7517 Some of the information is only available after the parse tree is complete.
7518 For instance, processing a nested scope requires a parent function node."
7519 (let (result fn parent-qname p elem)
7520 (dolist (entry js2-imenu-recorder)
7521 ;; function node goes first
7522 (cl-destructuring-bind (current-fn &rest (&whole chain head &rest)) entry
7523 ;; Examine head's defining scope:
7524 ;; Pre-processed chain, or top-level/external, keep as-is.
7525 (if (or (stringp head) (js2-node-top-level-decl-p head))
7526 (push chain result)
7527 (when (js2-this-or-super-node-p head)
7528 (setq chain (cdr chain))) ; discard this-node
7529 (when (setq fn (js2-node-parent-script-or-fn current-fn))
7530 (setq parent-qname (gethash fn js2-imenu-function-map 'not-found))
7531 (when (eq parent-qname 'not-found)
7532 ;; anonymous function expressions are not recorded
7533 ;; during the parse, so we need to handle this case here
7534 (setq parent-qname
7535 (if (js2-wrapper-function-p fn)
7536 (let ((grandparent (js2-node-parent-script-or-fn fn)))
7537 (if (js2-ast-root-p grandparent)
7538 nil
7539 (gethash grandparent js2-imenu-function-map 'skip)))
7540 'skip))
7541 (puthash fn parent-qname js2-imenu-function-map))
7542 (if (eq parent-qname 'skip)
7543 ;; We don't show it, let's record that fact.
7544 (remhash current-fn js2-imenu-function-map)
7545 ;; Prepend parent fn qname to this chain.
7546 (let ((qname (append parent-qname chain)))
7547 (puthash current-fn (butlast qname) js2-imenu-function-map)
7548 (push qname result)))))))
7549 ;; Collect chains obtained by third-party code.
7550 (let (js2-imenu-recorder)
7551 (run-hooks 'js2-build-imenu-callbacks)
7552 (dolist (entry js2-imenu-recorder)
7553 (push (cdr entry) result)))
7554 ;; Finally replace each node in each chain with its name.
7555 (dolist (chain result)
7556 (setq p chain)
7557 (while p
7558 (if (js2-node-p (setq elem (car p)))
7559 (setcar p (js2-node-qname-component elem)))
7560 (setq p (cdr p))))
7561 result))
7562
7563 ;; Merge name chains into a trie-like tree structure of nested lists.
7564 ;; To simplify construction of the trie, we first build it out using the rule
7565 ;; that the trie consists of lists of pairs. Each pair is a 2-element array:
7566 ;; [key, num-or-list]. The second element can be a number; if so, this key
7567 ;; is a leaf-node with only one value. (I.e. there is only one declaration
7568 ;; associated with the key at this level.) Otherwise the second element is
7569 ;; a list of pairs, with the rule applied recursively. This symmetry permits
7570 ;; a simple recursive formulation.
7571 ;;
7572 ;; js2-mode is building the data structure for imenu. The imenu documentation
7573 ;; claims that it's the structure above, but in practice it wants the children
7574 ;; at the same list level as the key for that level, which is how I've drawn
7575 ;; the "Expected final result" above. We'll postprocess the trie to remove the
7576 ;; list wrapper around the children at each level.
7577 ;;
7578 ;; A completed nested imenu-alist entry looks like this:
7579 ;; '(("foo"
7580 ;; ("<definition>" . 7)
7581 ;; ("bar"
7582 ;; ("a" . 40)
7583 ;; ("b" . 60))))
7584 ;;
7585 ;; In particular, the documentation for `imenu--index-alist' says that
7586 ;; a nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
7587 ;; The sub-alist entries immediately follow INDEX-NAME, the head of the list.
7588
7589 (defun js2-treeify (lst)
7590 "Convert (a b c d) to (a ((b ((c d)))))."
7591 (if (null (cddr lst)) ; list length <= 2
7592 lst
7593 (list (car lst) (list (js2-treeify (cdr lst))))))
7594
7595 (defun js2-build-alist-trie (chains trie)
7596 "Merge declaration name chains into a trie-like alist structure for imenu.
7597 CHAINS is the qname chain list produced during parsing. TRIE is a
7598 list of elements built up so far."
7599 (let (head tail pos branch kids)
7600 (dolist (chain chains)
7601 (setq head (car chain)
7602 tail (cdr chain)
7603 pos (if (numberp (car tail)) (car tail))
7604 branch (js2-find-if (lambda (n)
7605 (string= (car n) head))
7606 trie)
7607 kids (cl-second branch))
7608 (cond
7609 ;; case 1: this key isn't in the trie yet
7610 ((null branch)
7611 (if trie
7612 (setcdr (last trie) (list (js2-treeify chain)))
7613 (setq trie (list (js2-treeify chain)))))
7614 ;; case 2: key is present with a single number entry: replace w/ list
7615 ;; ("a1" 10) + ("a1" 20) => ("a1" (("<definition>" 10)
7616 ;; ("<definition>" 20)))
7617 ((numberp kids)
7618 (setcar (cdr branch)
7619 (list (list "<definition-1>" kids)
7620 (if pos
7621 (list "<definition-2>" pos)
7622 (js2-treeify tail)))))
7623 ;; case 3: key is there (with kids), and we're a number entry
7624 (pos
7625 (setcdr (last kids)
7626 (list
7627 (list (format "<definition-%d>"
7628 (1+ (cl-loop for kid in kids
7629 count (eq ?< (aref (car kid) 0)))))
7630 pos))))
7631 ;; case 4: key is there with kids, need to merge in our chain
7632 (t
7633 (js2-build-alist-trie (list tail) kids))))
7634 trie))
7635
7636 (defun js2-flatten-trie (trie)
7637 "Convert TRIE to imenu-format.
7638 Recurses through nodes, and for each one whose second element is a list,
7639 appends the list's flattened elements to the current element. Also
7640 changes the tails into conses. For instance, this pre-flattened trie
7641
7642 '(a ((b 20)
7643 (c ((d 30)
7644 (e 40)))))
7645
7646 becomes
7647
7648 '(a (b . 20)
7649 (c (d . 30)
7650 (e . 40)))
7651
7652 Note that the root of the trie has no key, just a list of chains.
7653 This is also true for the value of any key with multiple children,
7654 e.g. key 'c' in the example above."
7655 (cond
7656 ((listp (car trie))
7657 (mapcar #'js2-flatten-trie trie))
7658 (t
7659 (if (numberp (cl-second trie))
7660 (cons (car trie) (cl-second trie))
7661 ;; else pop list and append its kids
7662 (apply #'append (list (car trie)) (js2-flatten-trie (cdr trie)))))))
7663
7664 (defun js2-build-imenu-index ()
7665 "Turn `js2-imenu-recorder' into an imenu data structure."
7666 (when (eq js2-imenu-recorder 'empty)
7667 (setq js2-imenu-recorder nil))
7668 (let* ((chains (js2-browse-postprocess-chains))
7669 (result (js2-build-alist-trie chains nil)))
7670 (js2-flatten-trie result)))
7671
7672 (defun js2-test-print-chains (chains)
7673 "Print a list of qname chains.
7674 Each element of CHAINS is a list of the form (NODE [NODE *] pos);
7675 i.e. one or more nodes, and an integer position as the list tail."
7676 (mapconcat (lambda (chain)
7677 (concat "("
7678 (mapconcat (lambda (elem)
7679 (if (js2-node-p elem)
7680 (or (js2-node-qname-component elem)
7681 "nil")
7682 (number-to-string elem)))
7683 chain
7684 " ")
7685 ")"))
7686 chains
7687 "\n"))
7688
7689 ;;; Parser
7690
7691 (defconst js2-version "1.8.5"
7692 "Version of JavaScript supported.")
7693
7694 (defun js2-record-face (face &optional token)
7695 "Record a style run of FACE for TOKEN or the current token."
7696 (unless token (setq token (js2-current-token)))
7697 (js2-set-face (js2-token-beg token) (js2-token-end token) face 'record))
7698
7699 (defsubst js2-node-end (n)
7700 "Computes the absolute end of node N.
7701 Use with caution! Assumes `js2-node-pos' is -absolute-, which
7702 is only true until the node is added to its parent; i.e., while parsing."
7703 (+ (js2-node-pos n)
7704 (js2-node-len n)))
7705
7706 (defun js2-record-comment (token)
7707 "Record a comment in `js2-scanned-comments'."
7708 (let ((ct (js2-token-comment-type token))
7709 (beg (js2-token-beg token))
7710 (end (js2-token-end token)))
7711 (push (make-js2-comment-node :len (- end beg)
7712 :format ct)
7713 js2-scanned-comments)
7714 (when js2-parse-ide-mode
7715 (js2-record-face (if (eq ct 'jsdoc)
7716 'font-lock-doc-face
7717 'font-lock-comment-face)
7718 token)
7719 (when (memq ct '(html preprocessor))
7720 ;; Tell cc-engine the bounds of the comment.
7721 (js2-record-text-property beg (1- end) 'c-in-sws t)))))
7722
7723 (defun js2-peek-token ()
7724 "Return the next token type without consuming it.
7725 If `js2-ti-lookahead' is positive, return the type of next token
7726 from `js2-ti-tokens'. Otherwise, call `js2-get-token'."
7727 (if (not (zerop js2-ti-lookahead))
7728 (js2-token-type
7729 (aref js2-ti-tokens (mod (1+ js2-ti-tokens-cursor) js2-ti-ntokens)))
7730 (let ((tt (js2-get-token-internal nil)))
7731 (js2-unget-token)
7732 tt)))
7733
7734 (defalias 'js2-next-token 'js2-get-token)
7735
7736 (defun js2-match-token (match &optional dont-unget)
7737 "Get next token and return t if it matches MATCH, a bytecode.
7738 Returns nil and consumes nothing if MATCH is not the next token."
7739 (if (/= (js2-get-token) match)
7740 (ignore (unless dont-unget (js2-unget-token)))
7741 t))
7742
7743 (defun js2-match-contextual-kwd (name)
7744 "Consume and return t if next token is `js2-NAME', and its
7745 string is NAME. Returns nil and keeps current token otherwise."
7746 (if (or (/= (js2-get-token) js2-NAME)
7747 (not (string= (js2-current-token-string) name)))
7748 (progn
7749 (js2-unget-token)
7750 nil)
7751 (js2-record-face 'font-lock-keyword-face)
7752 t))
7753
7754 (defun js2-get-prop-name-token ()
7755 (js2-get-token (and (>= js2-language-version 170) 'KEYWORD_IS_NAME)))
7756
7757 (defun js2-match-prop-name ()
7758 "Consume token and return t if next token is a valid property name.
7759 If `js2-language-version' is >= 180, a keyword or reserved word
7760 is considered valid name as well."
7761 (if (eq js2-NAME (js2-get-prop-name-token))
7762 t
7763 (js2-unget-token)
7764 nil))
7765
7766 (defun js2-must-match-prop-name (msg-id &optional pos len)
7767 (if (js2-match-prop-name)
7768 t
7769 (js2-report-error msg-id nil pos len)
7770 nil))
7771
7772 (defun js2-peek-token-or-eol ()
7773 "Return js2-EOL if the next token immediately follows a newline.
7774 Else returns the next token. Used in situations where we don't
7775 consider certain token types valid if they are preceded by a newline.
7776 One example is the postfix ++ or -- operator, which has to be on the
7777 same line as its operand."
7778 (let ((tt (js2-get-token))
7779 (follows-eol (js2-token-follows-eol-p (js2-current-token))))
7780 (js2-unget-token)
7781 (if follows-eol
7782 js2-EOL
7783 tt)))
7784
7785 (defun js2-must-match (token msg-id &optional pos len)
7786 "Match next token to token code TOKEN, or record a syntax error.
7787 MSG-ID is the error message to report if the match fails.
7788 Returns t on match, nil if no match."
7789 (if (js2-match-token token t)
7790 t
7791 (js2-report-error msg-id nil pos len)
7792 (js2-unget-token)
7793 nil))
7794
7795 (defun js2-must-match-name (msg-id)
7796 (if (js2-match-token js2-NAME t)
7797 t
7798 (if (eq (js2-current-token-type) js2-RESERVED)
7799 (js2-report-error "msg.reserved.id" (js2-current-token-string))
7800 (js2-report-error msg-id)
7801 (js2-unget-token))
7802 nil))
7803
7804 (defsubst js2-inside-function ()
7805 (cl-plusp js2-nesting-of-function))
7806
7807 (defun js2-set-requires-activation ()
7808 (if (js2-function-node-p js2-current-script-or-fn)
7809 (setf (js2-function-node-needs-activation js2-current-script-or-fn) t)))
7810
7811 (defun js2-check-activation-name (name _token)
7812 (when (js2-inside-function)
7813 ;; skip language-version 1.2 check from Rhino
7814 (if (or (string= "arguments" name)
7815 (and js2-compiler-activation-names ; only used in codegen
7816 (gethash name js2-compiler-activation-names)))
7817 (js2-set-requires-activation))))
7818
7819 (defun js2-set-is-generator ()
7820 (let ((fn-node js2-current-script-or-fn))
7821 (when (and (js2-function-node-p fn-node)
7822 (not (js2-function-node-generator-type fn-node)))
7823 (setf (js2-function-node-generator-type js2-current-script-or-fn) 'LEGACY))))
7824
7825 (defun js2-must-have-xml ()
7826 (unless js2-compiler-xml-available
7827 (js2-report-error "msg.XML.not.available")))
7828
7829 (defun js2-push-scope (scope)
7830 "Push SCOPE, a `js2-scope', onto the lexical scope chain."
7831 (cl-assert (js2-scope-p scope))
7832 (cl-assert (null (js2-scope-parent-scope scope)))
7833 (cl-assert (not (eq js2-current-scope scope)))
7834 (setf (js2-scope-parent-scope scope) js2-current-scope
7835 js2-current-scope scope))
7836
7837 (defsubst js2-pop-scope ()
7838 (setq js2-current-scope
7839 (js2-scope-parent-scope js2-current-scope)))
7840
7841 (defun js2-enter-loop (loop-node)
7842 (push loop-node js2-loop-set)
7843 (push loop-node js2-loop-and-switch-set)
7844 (js2-push-scope loop-node)
7845 ;; Tell the current labeled statement (if any) its statement,
7846 ;; and set the jump target of the first label to the loop.
7847 ;; These are used in `js2-parse-continue' to verify that the
7848 ;; continue target is an actual labeled loop. (And for codegen.)
7849 (when js2-labeled-stmt
7850 (setf (js2-labeled-stmt-node-stmt js2-labeled-stmt) loop-node
7851 (js2-label-node-loop (car (js2-labeled-stmt-node-labels
7852 js2-labeled-stmt))) loop-node)))
7853
7854 (defun js2-exit-loop ()
7855 (pop js2-loop-set)
7856 (pop js2-loop-and-switch-set)
7857 (js2-pop-scope))
7858
7859 (defsubst js2-enter-switch (switch-node)
7860 (push switch-node js2-loop-and-switch-set))
7861
7862 (defsubst js2-exit-switch ()
7863 (pop js2-loop-and-switch-set))
7864
7865 (defun js2-parse (&optional buf cb)
7866 "Tell the js2 parser to parse a region of JavaScript.
7867
7868 BUF is a buffer or buffer name containing the code to parse.
7869 Call `narrow-to-region' first to parse only part of the buffer.
7870
7871 The returned AST root node is given some additional properties:
7872 `node-count' - total number of nodes in the AST
7873 `buffer' - BUF. The buffer it refers to may change or be killed,
7874 so the value is not necessarily reliable.
7875
7876 An optional callback CB can be specified to report parsing
7877 progress. If `(functionp CB)' returns t, it will be called with
7878 the current line number once before parsing begins, then again
7879 each time the lexer reaches a new line number.
7880
7881 CB can also be a list of the form `(symbol cb ...)' to specify
7882 multiple callbacks with different criteria. Each symbol is a
7883 criterion keyword, and the following element is the callback to
7884 call
7885
7886 :line - called whenever the line number changes
7887 :token - called for each new token consumed
7888
7889 The list of criteria could be extended to include entering or
7890 leaving a statement, an expression, or a function definition."
7891 (if (and cb (not (functionp cb)))
7892 (error "criteria callbacks not yet implemented"))
7893 (let ((inhibit-point-motion-hooks t)
7894 (js2-compiler-xml-available (>= js2-language-version 160))
7895 ;; This is a recursive-descent parser, so give it a big stack.
7896 (max-lisp-eval-depth (max max-lisp-eval-depth 3000))
7897 (max-specpdl-size (max max-specpdl-size 3000))
7898 (case-fold-search nil)
7899 ast)
7900 (with-current-buffer (or buf (current-buffer))
7901 (setq js2-scanned-comments nil
7902 js2-parsed-errors nil
7903 js2-parsed-warnings nil
7904 js2-imenu-recorder nil
7905 js2-imenu-function-map nil
7906 js2-label-set nil)
7907 (js2-init-scanner)
7908 (setq ast (js2-do-parse))
7909 (unless js2-ts-hit-eof
7910 (js2-report-error "msg.got.syntax.errors" (length js2-parsed-errors)))
7911 (setf (js2-ast-root-errors ast) js2-parsed-errors
7912 (js2-ast-root-warnings ast) js2-parsed-warnings)
7913 ;; if we didn't find any declarations, put a dummy in this list so we
7914 ;; don't end up re-parsing the buffer in `js2-mode-create-imenu-index'
7915 (unless js2-imenu-recorder
7916 (setq js2-imenu-recorder 'empty))
7917 (run-hooks 'js2-parse-finished-hook)
7918 ast)))
7919
7920 ;; Corresponds to Rhino's Parser.parse() method.
7921 (defun js2-do-parse ()
7922 "Parse current buffer starting from current point.
7923 Scanner should be initialized."
7924 (let ((pos js2-ts-cursor)
7925 (end js2-ts-cursor) ; in case file is empty
7926 root n tt)
7927 ;; initialize buffer-local parsing vars
7928 (setf root (make-js2-ast-root :buffer (buffer-name) :pos pos)
7929 js2-current-script-or-fn root
7930 js2-current-scope root
7931 js2-nesting-of-function 0
7932 js2-labeled-stmt nil
7933 js2-recorded-identifiers nil) ; for js2-highlight
7934 (while (/= (setq tt (js2-get-token)) js2-EOF)
7935 (if (= tt js2-FUNCTION)
7936 (progn
7937 (setq n (if js2-called-by-compile-function
7938 (js2-parse-function-expr)
7939 (js2-parse-function-stmt))))
7940 ;; not a function - parse a statement
7941 (js2-unget-token)
7942 (setq n (js2-parse-statement)))
7943 ;; add function or statement to script
7944 (setq end (js2-node-end n))
7945 (js2-block-node-push root n))
7946 ;; add comments to root in lexical order
7947 (when js2-scanned-comments
7948 ;; if we find a comment beyond end of normal kids, use its end
7949 (setq end (max end (js2-node-end (cl-first js2-scanned-comments))))
7950 (dolist (comment js2-scanned-comments)
7951 (push comment (js2-ast-root-comments root))
7952 (js2-node-add-children root comment)))
7953 (setf (js2-node-len root) (- end pos))
7954 (setq js2-mode-ast root) ; Make sure this is available for callbacks.
7955 ;; Give extensions a chance to muck with things before highlighting starts.
7956 (let ((js2-additional-externs js2-additional-externs))
7957 (save-excursion
7958 (run-hooks 'js2-post-parse-callbacks))
7959 (js2-highlight-undeclared-vars))
7960 root))
7961
7962 (defun js2-parse-function-closure-body (fn-node)
7963 "Parse a JavaScript 1.8 function closure body."
7964 (let ((js2-nesting-of-function (1+ js2-nesting-of-function)))
7965 (if js2-ts-hit-eof
7966 (js2-report-error "msg.no.brace.body" nil
7967 (js2-node-pos fn-node)
7968 (- js2-ts-cursor (js2-node-pos fn-node)))
7969 (js2-node-add-children fn-node
7970 (setf (js2-function-node-body fn-node)
7971 (js2-parse-expr t))))))
7972
7973 (defun js2-parse-function-body (fn-node)
7974 (js2-must-match js2-LC "msg.no.brace.body"
7975 (js2-node-pos fn-node)
7976 (- js2-ts-cursor (js2-node-pos fn-node)))
7977 (let ((pos (js2-current-token-beg)) ; LC position
7978 (pn (make-js2-block-node)) ; starts at LC position
7979 tt
7980 end)
7981 (cl-incf js2-nesting-of-function)
7982 (unwind-protect
7983 (while (not (or (= (setq tt (js2-peek-token)) js2-ERROR)
7984 (= tt js2-EOF)
7985 (= tt js2-RC)))
7986 (js2-block-node-push pn (if (/= tt js2-FUNCTION)
7987 (js2-parse-statement)
7988 (js2-get-token)
7989 (js2-parse-function-stmt))))
7990 (cl-decf js2-nesting-of-function))
7991 (setq end (js2-current-token-end)) ; assume no curly and leave at current token
7992 (if (js2-must-match js2-RC "msg.no.brace.after.body" pos)
7993 (setq end (js2-current-token-end)))
7994 (setf (js2-node-pos pn) pos
7995 (js2-node-len pn) (- end pos))
7996 (setf (js2-function-node-body fn-node) pn)
7997 (js2-node-add-children fn-node pn)
7998 pn))
7999
8000 (defun js2-define-destruct-symbols (node decl-type face &optional ignore-not-in-block)
8001 "Declare and fontify destructuring parameters inside NODE.
8002 NODE is either `js2-array-node', `js2-object-node', or `js2-name-node'."
8003 (cond
8004 ((js2-name-node-p node)
8005 (let (leftpos)
8006 (js2-define-symbol decl-type (js2-name-node-name node)
8007 node ignore-not-in-block)
8008 (when face
8009 (js2-set-face (setq leftpos (js2-node-abs-pos node))
8010 (+ leftpos (js2-node-len node))
8011 face 'record))))
8012 ((js2-object-node-p node)
8013 (dolist (elem (js2-object-node-elems node))
8014 (js2-define-destruct-symbols
8015 ;; In abbreviated destructuring {a, b}, right == left.
8016 (js2-object-prop-node-right elem)
8017 decl-type face ignore-not-in-block)))
8018 ((js2-array-node-p node)
8019 (dolist (elem (js2-array-node-elems node))
8020 (when elem
8021 (js2-define-destruct-symbols elem decl-type face ignore-not-in-block))))
8022 (t (js2-report-error "msg.no.parm" nil (js2-node-abs-pos node)
8023 (js2-node-len node)))))
8024
8025 (defun js2-parse-function-params (function-type fn-node pos)
8026 (if (js2-match-token js2-RP)
8027 (setf (js2-function-node-rp fn-node) (- (js2-current-token-beg) pos))
8028 (let ((paren-free-arrow (and (eq function-type 'FUNCTION_ARROW)
8029 (eq (js2-current-token-type) js2-NAME)))
8030 params param default-found rest-param-at)
8031 (when paren-free-arrow
8032 (js2-unget-token))
8033 (cl-loop for tt = (js2-peek-token)
8034 do
8035 (cond
8036 ;; destructuring param
8037 ((and (not paren-free-arrow)
8038 (or (= tt js2-LB) (= tt js2-LC)))
8039 (js2-get-token)
8040 (when default-found
8041 (js2-report-error "msg.no.default.after.default.param"))
8042 (setq param (js2-parse-destruct-primary-expr))
8043 (js2-define-destruct-symbols param
8044 js2-LP
8045 'js2-function-param)
8046 (push param params))
8047 ;; variable name
8048 (t
8049 (when (and (>= js2-language-version 200)
8050 (not paren-free-arrow)
8051 (js2-match-token js2-TRIPLEDOT)
8052 (not rest-param-at))
8053 ;; to report errors if there are more parameters
8054 (setq rest-param-at (length params)))
8055 (js2-must-match-name "msg.no.parm")
8056 (js2-record-face 'js2-function-param)
8057 (setq param (js2-create-name-node))
8058 (js2-define-symbol js2-LP (js2-current-token-string) param)
8059 ;; default parameter value
8060 (when (or (and default-found
8061 (not rest-param-at)
8062 (js2-must-match js2-ASSIGN
8063 "msg.no.default.after.default.param"
8064 (js2-node-pos param)
8065 (js2-node-len param)))
8066 (and (>= js2-language-version 200)
8067 (js2-match-token js2-ASSIGN)))
8068 (cl-assert (not paren-free-arrow))
8069 (let* ((pos (js2-node-pos param))
8070 (tt (js2-current-token-type))
8071 (op-pos (- (js2-current-token-beg) pos))
8072 (left param)
8073 (right (js2-parse-assign-expr))
8074 (len (- (js2-node-end right) pos)))
8075 (setq param (make-js2-assign-node
8076 :type tt :pos pos :len len :op-pos op-pos
8077 :left left :right right)
8078 default-found t)
8079 (js2-node-add-children param left right)))
8080 (push param params)))
8081 (when (and rest-param-at (> (length params) (1+ rest-param-at)))
8082 (js2-report-error "msg.param.after.rest" nil
8083 (js2-node-pos param) (js2-node-len param)))
8084 while
8085 (js2-match-token js2-COMMA))
8086 (when (and (not paren-free-arrow)
8087 (js2-must-match js2-RP "msg.no.paren.after.parms"))
8088 (setf (js2-function-node-rp fn-node) (- (js2-current-token-beg) pos)))
8089 (when rest-param-at
8090 (setf (js2-function-node-rest-p fn-node) t))
8091 (dolist (p params)
8092 (js2-node-add-children fn-node p)
8093 (push p (js2-function-node-params fn-node))))))
8094
8095 (defun js2-check-inconsistent-return-warning (fn-node name)
8096 "Possibly show inconsistent-return warning.
8097 Last token scanned is the close-curly for the function body."
8098 (when (and js2-mode-show-strict-warnings
8099 js2-strict-inconsistent-return-warning
8100 (not (js2-has-consistent-return-usage
8101 (js2-function-node-body fn-node))))
8102 ;; Have it extend from close-curly to bol or beginning of block.
8103 (let ((pos (save-excursion
8104 (goto-char (js2-current-token-end))
8105 (max (js2-node-abs-pos (js2-function-node-body fn-node))
8106 (point-at-bol))))
8107 (end (js2-current-token-end)))
8108 (if (cl-plusp (js2-name-node-length name))
8109 (js2-add-strict-warning "msg.no.return.value"
8110 (js2-name-node-name name) pos end)
8111 (js2-add-strict-warning "msg.anon.no.return.value" nil pos end)))))
8112
8113 (defun js2-parse-function-stmt ()
8114 (let ((pos (js2-current-token-beg))
8115 (star-p (js2-match-token js2-MUL)))
8116 (js2-must-match-name "msg.unnamed.function.stmt")
8117 (let ((name (js2-create-name-node t))
8118 pn member-expr)
8119 (cond
8120 ((js2-match-token js2-LP)
8121 (js2-parse-function 'FUNCTION_STATEMENT pos star-p name))
8122 (js2-allow-member-expr-as-function-name
8123 (setq member-expr (js2-parse-member-expr-tail nil name))
8124 (js2-parse-highlight-member-expr-fn-name member-expr)
8125 (js2-must-match js2-LP "msg.no.paren.parms")
8126 (setf pn (js2-parse-function 'FUNCTION_STATEMENT pos star-p)
8127 (js2-function-node-member-expr pn) member-expr)
8128 pn)
8129 (t
8130 (js2-report-error "msg.no.paren.parms")
8131 (make-js2-error-node))))))
8132
8133 (defun js2-parse-function-expr ()
8134 (let ((pos (js2-current-token-beg))
8135 (star-p (js2-match-token js2-MUL))
8136 name)
8137 (when (js2-match-token js2-NAME)
8138 (setq name (js2-create-name-node t)))
8139 (js2-must-match js2-LP "msg.no.paren.parms")
8140 (js2-parse-function 'FUNCTION_EXPRESSION pos star-p name)))
8141
8142 (defun js2-parse-function (function-type pos star-p &optional name)
8143 "Function parser. FUNCTION-TYPE is a symbol, POS is the
8144 beginning of the first token (function keyword, unless it's an
8145 arrow function), NAME is js2-name-node."
8146 (let (fn-node lp)
8147 (if (= (js2-current-token-type) js2-LP) ; eventually matched LP?
8148 (setq lp (js2-current-token-beg)))
8149 (setf fn-node (make-js2-function-node :pos pos
8150 :name name
8151 :form function-type
8152 :lp (if lp (- lp pos))
8153 :generator-type (and star-p 'STAR)))
8154 (when name
8155 (js2-set-face (js2-node-pos name) (js2-node-end name)
8156 'font-lock-function-name-face 'record)
8157 (when (and (eq function-type 'FUNCTION_STATEMENT)
8158 (cl-plusp (js2-name-node-length name)))
8159 ;; Function statements define a symbol in the enclosing scope
8160 (js2-define-symbol js2-FUNCTION (js2-name-node-name name) fn-node)))
8161 (if (or (js2-inside-function) (cl-plusp js2-nesting-of-with))
8162 ;; 1. Nested functions are not affected by the dynamic scope flag
8163 ;; as dynamic scope is already a parent of their scope.
8164 ;; 2. Functions defined under the with statement also immune to
8165 ;; this setup, in which case dynamic scope is ignored in favor
8166 ;; of the with object.
8167 (setf (js2-function-node-ignore-dynamic fn-node) t))
8168 ;; dynamically bind all the per-function variables
8169 (let ((js2-current-script-or-fn fn-node)
8170 (js2-current-scope fn-node)
8171 (js2-nesting-of-with 0)
8172 (js2-end-flags 0)
8173 js2-label-set
8174 js2-loop-set
8175 js2-loop-and-switch-set)
8176 (js2-parse-function-params function-type fn-node pos)
8177 (when (eq function-type 'FUNCTION_ARROW)
8178 (js2-must-match js2-ARROW "msg.bad.arrow.args"))
8179 (if (and (>= js2-language-version 180)
8180 (/= (js2-peek-token) js2-LC))
8181 (js2-parse-function-closure-body fn-node)
8182 (js2-parse-function-body fn-node))
8183 (js2-check-inconsistent-return-warning fn-node name)
8184
8185 (when name
8186 (js2-node-add-children fn-node name)
8187 ;; Function expressions define a name only in the body of the
8188 ;; function, and only if not hidden by a parameter name
8189 (when (and (eq function-type 'FUNCTION_EXPRESSION)
8190 (null (js2-scope-get-symbol js2-current-scope
8191 (js2-name-node-name name))))
8192 (js2-define-symbol js2-FUNCTION
8193 (js2-name-node-name name)
8194 fn-node))
8195 (when (eq function-type 'FUNCTION_STATEMENT)
8196 (js2-record-imenu-functions fn-node))))
8197
8198 (setf (js2-node-len fn-node) (- js2-ts-cursor pos))
8199 ;; Rhino doesn't do this, but we need it for finding undeclared vars.
8200 ;; We wait until after parsing the function to set its parent scope,
8201 ;; since `js2-define-symbol' needs the defining-scope check to stop
8202 ;; at the function boundary when checking for redeclarations.
8203 (setf (js2-scope-parent-scope fn-node) js2-current-scope)
8204 fn-node))
8205
8206 (defun js2-parse-statements (&optional parent)
8207 "Parse a statement list. Last token consumed must be js2-LC.
8208
8209 PARENT can be a `js2-block-node', in which case the statements are
8210 appended to PARENT. Otherwise a new `js2-block-node' is created
8211 and returned.
8212
8213 This function does not match the closing js2-RC: the caller
8214 matches the RC so it can provide a suitable error message if not
8215 matched. This means it's up to the caller to set the length of
8216 the node to include the closing RC. The node start pos is set to
8217 the absolute buffer start position, and the caller should fix it
8218 up to be relative to the parent node. All children of this block
8219 node are given relative start positions and correct lengths."
8220 (let ((pn (or parent (make-js2-block-node)))
8221 tt)
8222 (while (and (> (setq tt (js2-peek-token)) js2-EOF)
8223 (/= tt js2-RC))
8224 (js2-block-node-push pn (js2-parse-statement)))
8225 pn))
8226
8227 (defun js2-parse-statement ()
8228 (let (pn beg end)
8229 ;; coarse-grained user-interrupt check - needs work
8230 (and js2-parse-interruptable-p
8231 (zerop (% (cl-incf js2-parse-stmt-count)
8232 js2-statements-per-pause))
8233 (input-pending-p)
8234 (throw 'interrupted t))
8235 (setq pn (js2-statement-helper))
8236 ;; no-side-effects warning check
8237 (unless (js2-node-has-side-effects pn)
8238 (setq end (js2-node-end pn))
8239 (save-excursion
8240 (goto-char end)
8241 (setq beg (max (js2-node-pos pn) (point-at-bol))))
8242 (js2-add-strict-warning "msg.no.side.effects" nil beg end))
8243 pn))
8244
8245 ;; These correspond to the switch cases in Parser.statementHelper
8246 (defconst js2-parsers
8247 (let ((parsers (make-vector js2-num-tokens
8248 #'js2-parse-expr-stmt)))
8249 (aset parsers js2-BREAK #'js2-parse-break)
8250 (aset parsers js2-CLASS #'js2-parse-class-stmt)
8251 (aset parsers js2-CONST #'js2-parse-const-var)
8252 (aset parsers js2-CONTINUE #'js2-parse-continue)
8253 (aset parsers js2-DEBUGGER #'js2-parse-debugger)
8254 (aset parsers js2-DEFAULT #'js2-parse-default-xml-namespace)
8255 (aset parsers js2-DO #'js2-parse-do)
8256 (aset parsers js2-EXPORT #'js2-parse-export)
8257 (aset parsers js2-FOR #'js2-parse-for)
8258 (aset parsers js2-FUNCTION #'js2-parse-function-stmt)
8259 (aset parsers js2-IF #'js2-parse-if)
8260 (aset parsers js2-IMPORT #'js2-parse-import)
8261 (aset parsers js2-LC #'js2-parse-block)
8262 (aset parsers js2-LET #'js2-parse-let-stmt)
8263 (aset parsers js2-NAME #'js2-parse-name-or-label)
8264 (aset parsers js2-RETURN #'js2-parse-ret-yield)
8265 (aset parsers js2-SEMI #'js2-parse-semi)
8266 (aset parsers js2-SWITCH #'js2-parse-switch)
8267 (aset parsers js2-THROW #'js2-parse-throw)
8268 (aset parsers js2-TRY #'js2-parse-try)
8269 (aset parsers js2-VAR #'js2-parse-const-var)
8270 (aset parsers js2-WHILE #'js2-parse-while)
8271 (aset parsers js2-WITH #'js2-parse-with)
8272 (aset parsers js2-YIELD #'js2-parse-ret-yield)
8273 parsers)
8274 "A vector mapping token types to parser functions.")
8275
8276 (defun js2-parse-warn-missing-semi (beg end)
8277 (and js2-mode-show-strict-warnings
8278 js2-strict-missing-semi-warning
8279 (js2-add-strict-warning
8280 "msg.missing.semi" nil
8281 ;; back up to beginning of statement or line
8282 (max beg (save-excursion
8283 (goto-char end)
8284 (point-at-bol)))
8285 end)))
8286
8287 (defconst js2-no-semi-insertion
8288 (list js2-IF
8289 js2-SWITCH
8290 js2-WHILE
8291 js2-DO
8292 js2-FOR
8293 js2-TRY
8294 js2-WITH
8295 js2-LC
8296 js2-ERROR
8297 js2-SEMI
8298 js2-CLASS
8299 js2-FUNCTION
8300 js2-EXPORT)
8301 "List of tokens that don't do automatic semicolon insertion.")
8302
8303 (defconst js2-autoinsert-semi-and-warn
8304 (list js2-ERROR js2-EOF js2-RC))
8305
8306 (defun js2-statement-helper ()
8307 (let* ((tt (js2-get-token))
8308 (first-tt tt)
8309 (parser (if (= tt js2-ERROR)
8310 #'js2-parse-semi
8311 (aref js2-parsers tt)))
8312 pn)
8313 ;; If the statement is set, then it's been told its label by now.
8314 (and js2-labeled-stmt
8315 (js2-labeled-stmt-node-stmt js2-labeled-stmt)
8316 (setq js2-labeled-stmt nil))
8317 (setq pn (funcall parser))
8318 ;; Don't do auto semi insertion for certain statement types.
8319 (unless (or (memq first-tt js2-no-semi-insertion)
8320 (js2-labeled-stmt-node-p pn))
8321 (js2-auto-insert-semicolon pn))
8322 pn))
8323
8324 (defun js2-auto-insert-semicolon (pn)
8325 (let* ((tt (js2-get-token))
8326 (pos (js2-node-pos pn)))
8327 (cond
8328 ((= tt js2-SEMI)
8329 ;; extend the node bounds to include the semicolon.
8330 (setf (js2-node-len pn) (- (js2-current-token-end) pos)))
8331 ((memq tt js2-autoinsert-semi-and-warn)
8332 (js2-unget-token) ; Not ';', do not consume.
8333 ;; Autoinsert ;
8334 (js2-parse-warn-missing-semi pos (js2-node-end pn)))
8335 (t
8336 (if (not (js2-token-follows-eol-p (js2-current-token)))
8337 ;; Report error if no EOL or autoinsert ';' otherwise
8338 (js2-report-error "msg.no.semi.stmt")
8339 (js2-parse-warn-missing-semi pos (js2-node-end pn)))
8340 (js2-unget-token) ; Not ';', do not consume.
8341 ))))
8342
8343 (defun js2-parse-condition ()
8344 "Parse a parenthesized boolean expression, e.g. in an if- or while-stmt.
8345 The parens are discarded and the expression node is returned.
8346 The `pos' field of the return value is set to an absolute position
8347 that must be fixed up by the caller.
8348 Return value is a list (EXPR LP RP), with absolute paren positions."
8349 (let (pn lp rp)
8350 (if (js2-must-match js2-LP "msg.no.paren.cond")
8351 (setq lp (js2-current-token-beg)))
8352 (setq pn (js2-parse-expr))
8353 (if (js2-must-match js2-RP "msg.no.paren.after.cond")
8354 (setq rp (js2-current-token-beg)))
8355 ;; Report strict warning on code like "if (a = 7) ..."
8356 (if (and js2-strict-cond-assign-warning
8357 (js2-assign-node-p pn))
8358 (js2-add-strict-warning "msg.equal.as.assign" nil
8359 (js2-node-pos pn)
8360 (+ (js2-node-pos pn)
8361 (js2-node-len pn))))
8362 (list pn lp rp)))
8363
8364 (defun js2-parse-if ()
8365 "Parser for if-statement. Last matched token must be js2-IF."
8366 (let ((pos (js2-current-token-beg))
8367 cond if-true if-false else-pos end pn)
8368 (setq cond (js2-parse-condition)
8369 if-true (js2-parse-statement)
8370 if-false (if (js2-match-token js2-ELSE)
8371 (progn
8372 (setq else-pos (- (js2-current-token-beg) pos))
8373 (js2-parse-statement)))
8374 end (js2-node-end (or if-false if-true))
8375 pn (make-js2-if-node :pos pos
8376 :len (- end pos)
8377 :condition (car cond)
8378 :then-part if-true
8379 :else-part if-false
8380 :else-pos else-pos
8381 :lp (js2-relpos (cl-second cond) pos)
8382 :rp (js2-relpos (cl-third cond) pos)))
8383 (js2-node-add-children pn (car cond) if-true if-false)
8384 pn))
8385
8386 (defun js2-parse-import ()
8387 "Parse import statement. The current token must be js2-IMPORT."
8388 (unless (js2-ast-root-p js2-current-scope)
8389 (js2-report-error "msg.mod.import.decl.at.top.level"))
8390 (let ((beg (js2-current-token-beg)))
8391 (cond ((js2-match-token js2-STRING)
8392 (make-js2-import-node
8393 :pos beg
8394 :len (- (js2-current-token-end) beg)
8395 :module-id (js2-current-token-string)))
8396 (t
8397 (let* ((import-clause (js2-parse-import-clause))
8398 (from-clause (and import-clause (js2-parse-from-clause)))
8399 (module-id (when from-clause (js2-from-clause-node-module-id from-clause)))
8400 (node (make-js2-import-node
8401 :pos beg
8402 :len (- (js2-current-token-end) beg)
8403 :import import-clause
8404 :from from-clause
8405 :module-id module-id)))
8406 (when import-clause
8407 (js2-node-add-children node import-clause))
8408 (when from-clause
8409 (js2-node-add-children node from-clause))
8410 node)))))
8411
8412 (defun js2-parse-import-clause ()
8413 "Parse the bindings in an import statement.
8414 This can take many forms:
8415
8416 ImportedDefaultBinding -> 'foo'
8417 NameSpaceImport -> '* as lib'
8418 NamedImports -> '{foo as bar, bang}'
8419 ImportedDefaultBinding , NameSpaceImport -> 'foo, * as lib'
8420 ImportedDefaultBinding , NamedImports -> 'foo, {bar, baz as bif}'
8421
8422 Try to match namespace imports and named imports first because nothing can
8423 come after them. If it is an imported default binding, then it could have named
8424 imports or a namespace import that follows it.
8425 "
8426 (let* ((beg (js2-current-token-beg))
8427 (clause (make-js2-import-clause-node
8428 :pos beg))
8429 (children (list)))
8430 (cond
8431 ((js2-match-token js2-MUL)
8432 (let ((ns-import (js2-parse-namespace-import)))
8433 (when ns-import
8434 (let ((name-node (js2-namespace-import-node-name ns-import)))
8435 (js2-define-symbol
8436 js2-LET (js2-name-node-name name-node) name-node t)))
8437 (setf (js2-import-clause-node-namespace-import clause) ns-import)
8438 (push ns-import children)))
8439 ((js2-match-token js2-LC)
8440 (let ((imports (js2-parse-export-bindings t)))
8441 (setf (js2-import-clause-node-named-imports clause) imports)
8442 (dolist (import imports)
8443 (push import children)
8444 (let ((name-node (js2-export-binding-node-local-name import)))
8445 (when name-node
8446 (js2-define-symbol
8447 js2-LET (js2-name-node-name name-node) name-node t))))))
8448 ((= (js2-peek-token) js2-NAME)
8449 (let ((binding (js2-maybe-parse-export-binding)))
8450 (let ((node-name (js2-export-binding-node-local-name binding)))
8451 (js2-define-symbol js2-LET (js2-name-node-name node-name) node-name t))
8452 (setf (js2-import-clause-node-default-binding clause) binding)
8453 (push binding children))
8454 (when (js2-match-token js2-COMMA)
8455 (cond
8456 ((js2-match-token js2-MUL)
8457 (let ((ns-import (js2-parse-namespace-import)))
8458 (let ((name-node (js2-namespace-import-node-name ns-import)))
8459 (js2-define-symbol
8460 js2-LET (js2-name-node-name name-node) name-node t))
8461 (setf (js2-import-clause-node-namespace-import clause) ns-import)
8462 (push ns-import children)))
8463 ((js2-match-token js2-LC)
8464 (let ((imports (js2-parse-export-bindings t)))
8465 (setf (js2-import-clause-node-named-imports clause) imports)
8466 (dolist (import imports)
8467 (push import children)
8468 (let ((name-node (js2-export-binding-node-local-name import)))
8469 (when name-node
8470 (js2-define-symbol
8471 js2-LET (js2-name-node-name name-node) name-node t))))))
8472 (t (js2-report-error "msg.syntax")))))
8473 (t (js2-report-error "msg.mod.declaration.after.import")))
8474 (setf (js2-node-len clause) (- (js2-current-token-end) beg))
8475 (apply #'js2-node-add-children clause children)
8476 clause))
8477
8478 (defun js2-parse-namespace-import ()
8479 "Parse a namespace import expression such as '* as bar'.
8480 The current token must be js2-MUL."
8481 (let ((beg (js2-current-token-beg)))
8482 (when (js2-must-match js2-NAME "msg.syntax")
8483 (if (equal "as" (js2-current-token-string))
8484 (when (js2-must-match-prop-name "msg.syntax")
8485 (let ((node (make-js2-namespace-import-node
8486 :pos beg
8487 :len (- (js2-current-token-end) beg)
8488 :name (make-js2-name-node
8489 :pos (js2-current-token-beg)
8490 :len (js2-current-token-end)
8491 :name (js2-current-token-string)))))
8492 (js2-node-add-children node (js2-namespace-import-node-name node))
8493 node))
8494 (js2-unget-token)
8495 (js2-report-error "msg.syntax")))))
8496
8497
8498 (defun js2-parse-from-clause ()
8499 "Parse the from clause in an import or export statement. E.g. from 'src/lib'"
8500 (when (js2-must-match-name "msg.mod.from.after.import.spec.set")
8501 (let ((beg (js2-current-token-beg)))
8502 (if (equal "from" (js2-current-token-string))
8503 (cond
8504 ((js2-match-token js2-STRING)
8505 (make-js2-from-clause-node
8506 :pos beg
8507 :len (- (js2-current-token-end) beg)
8508 :module-id (js2-current-token-string)
8509 :metadata-p nil))
8510 ((js2-match-token js2-THIS)
8511 (when (js2-must-match-name "msg.mod.spec.after.from")
8512 (if (equal "module" (js2-current-token-string))
8513 (make-js2-from-clause-node
8514 :pos beg
8515 :len (- (js2-current-token-end) beg)
8516 :module-id "this"
8517 :metadata-p t)
8518 (js2-unget-token)
8519 (js2-unget-token)
8520 (js2-report-error "msg.mod.spec.after.from")
8521 nil)))
8522 (t (js2-report-error "msg.mod.spec.after.from") nil))
8523 (js2-unget-token)
8524 (js2-report-error "msg.mod.from.after.import.spec.set")
8525 nil))))
8526
8527 (defun js2-parse-export-bindings (&optional import-p)
8528 "Parse a list of export binding expressions such as {}, {foo, bar}, and
8529 {foo as bar, baz as bang}. The current token must be
8530 js2-LC. Return a lisp list of js2-export-binding-node"
8531 (let ((bindings (list)))
8532 (while
8533 (let ((binding (js2-maybe-parse-export-binding)))
8534 (when binding
8535 (push binding bindings))
8536 (js2-match-token js2-COMMA)))
8537 (when (js2-must-match js2-RC (if import-p
8538 "msg.mod.rc.after.import.spec.list"
8539 "msg.mod.rc.after.export.spec.list"))
8540 (reverse bindings))))
8541
8542 (defun js2-maybe-parse-export-binding ()
8543 "Attempt to parse a binding expression found inside an import/export statement.
8544 This can take the form of either as single js2-NAME token as in 'foo' or as in a
8545 rebinding expression 'bar as foo'. If it matches, it will return an instance of
8546 js2-export-binding-node and consume all the tokens. If it does not match, it
8547 consumes no tokens."
8548 (let ((extern-name (when (js2-match-prop-name) (js2-current-token-string)))
8549 (beg (js2-current-token-beg))
8550 (extern-name-len (js2-current-token-len))
8551 (is-reserved-name (or (= (js2-current-token-type) js2-RESERVED)
8552 (aref js2-kwd-tokens (js2-current-token-type)))))
8553 (if extern-name
8554 (let ((as (and (js2-match-token js2-NAME) (js2-current-token-string))))
8555 (if (and as (equal "as" (js2-current-token-string)))
8556 (let ((name
8557 (or
8558 (and (js2-match-token js2-DEFAULT) "default")
8559 (and (js2-match-token js2-NAME) (js2-current-token-string)))))
8560 (if name
8561 (let ((node (make-js2-export-binding-node
8562 :pos beg
8563 :len (- (js2-current-token-end) beg)
8564 :local-name (make-js2-name-node
8565 :name name
8566 :pos (js2-current-token-beg)
8567 :len (js2-current-token-len))
8568 :extern-name (make-js2-name-node
8569 :name extern-name
8570 :pos beg
8571 :len extern-name-len))))
8572 (js2-node-add-children
8573 node
8574 (js2-export-binding-node-local-name node)
8575 (js2-export-binding-node-extern-name node))
8576 node)
8577 (js2-unget-token)
8578 nil))
8579 (when as (js2-unget-token))
8580 (let* ((name-node (make-js2-name-node
8581 :name (js2-current-token-string)
8582 :pos (js2-current-token-beg)
8583 :len (js2-current-token-len)))
8584 (node (make-js2-export-binding-node
8585 :pos (js2-current-token-beg)
8586 :len (js2-current-token-len)
8587 :local-name name-node
8588 :extern-name name-node)))
8589 (when is-reserved-name
8590 (js2-report-error "msg.mod.as.after.reserved.word" extern-name))
8591 (js2-node-add-children node name-node)
8592 node)))
8593 nil)))
8594
8595 (defun js2-parse-switch ()
8596 "Parser for switch-statement. Last matched token must be js2-SWITCH."
8597 (let ((pos (js2-current-token-beg))
8598 tt pn discriminant has-default case-expr case-node
8599 case-pos cases stmt lp)
8600 (if (js2-must-match js2-LP "msg.no.paren.switch")
8601 (setq lp (js2-current-token-beg)))
8602 (setq discriminant (js2-parse-expr)
8603 pn (make-js2-switch-node :discriminant discriminant
8604 :pos pos
8605 :lp (js2-relpos lp pos)))
8606 (js2-node-add-children pn discriminant)
8607 (js2-enter-switch pn)
8608 (unwind-protect
8609 (progn
8610 (if (js2-must-match js2-RP "msg.no.paren.after.switch")
8611 (setf (js2-switch-node-rp pn) (- (js2-current-token-beg) pos)))
8612 (js2-must-match js2-LC "msg.no.brace.switch")
8613 (catch 'break
8614 (while t
8615 (setq tt (js2-next-token)
8616 case-pos (js2-current-token-beg))
8617 (cond
8618 ((= tt js2-RC)
8619 (setf (js2-node-len pn) (- (js2-current-token-end) pos))
8620 (throw 'break nil)) ; done
8621 ((= tt js2-CASE)
8622 (setq case-expr (js2-parse-expr))
8623 (js2-must-match js2-COLON "msg.no.colon.case"))
8624 ((= tt js2-DEFAULT)
8625 (if has-default
8626 (js2-report-error "msg.double.switch.default"))
8627 (setq has-default t
8628 case-expr nil)
8629 (js2-must-match js2-COLON "msg.no.colon.case"))
8630 (t
8631 (js2-report-error "msg.bad.switch")
8632 (throw 'break nil)))
8633 (setq case-node (make-js2-case-node :pos case-pos
8634 :len (- (js2-current-token-end) case-pos)
8635 :expr case-expr))
8636 (js2-node-add-children case-node case-expr)
8637 (while (and (/= (setq tt (js2-peek-token)) js2-RC)
8638 (/= tt js2-CASE)
8639 (/= tt js2-DEFAULT)
8640 (/= tt js2-EOF))
8641 (setf stmt (js2-parse-statement)
8642 (js2-node-len case-node) (- (js2-node-end stmt) case-pos))
8643 (js2-block-node-push case-node stmt))
8644 (push case-node cases)))
8645 ;; add cases last, as pushing reverses the order to be correct
8646 (dolist (kid cases)
8647 (js2-node-add-children pn kid)
8648 (push kid (js2-switch-node-cases pn)))
8649 pn) ; return value
8650 (js2-exit-switch))))
8651
8652 (defun js2-parse-while ()
8653 "Parser for while-statement. Last matched token must be js2-WHILE."
8654 (let ((pos (js2-current-token-beg))
8655 (pn (make-js2-while-node))
8656 cond body)
8657 (js2-enter-loop pn)
8658 (unwind-protect
8659 (progn
8660 (setf cond (js2-parse-condition)
8661 (js2-while-node-condition pn) (car cond)
8662 body (js2-parse-statement)
8663 (js2-while-node-body pn) body
8664 (js2-node-len pn) (- (js2-node-end body) pos)
8665 (js2-while-node-lp pn) (js2-relpos (cl-second cond) pos)
8666 (js2-while-node-rp pn) (js2-relpos (cl-third cond) pos))
8667 (js2-node-add-children pn body (car cond)))
8668 (js2-exit-loop))
8669 pn))
8670
8671 (defun js2-parse-do ()
8672 "Parser for do-statement. Last matched token must be js2-DO."
8673 (let ((pos (js2-current-token-beg))
8674 (pn (make-js2-do-node))
8675 cond body end)
8676 (js2-enter-loop pn)
8677 (unwind-protect
8678 (progn
8679 (setq body (js2-parse-statement))
8680 (js2-must-match js2-WHILE "msg.no.while.do")
8681 (setf (js2-do-node-while-pos pn) (- (js2-current-token-beg) pos)
8682 cond (js2-parse-condition)
8683 (js2-do-node-condition pn) (car cond)
8684 (js2-do-node-body pn) body
8685 end js2-ts-cursor
8686 (js2-do-node-lp pn) (js2-relpos (cl-second cond) pos)
8687 (js2-do-node-rp pn) (js2-relpos (cl-third cond) pos))
8688 (js2-node-add-children pn (car cond) body))
8689 (js2-exit-loop))
8690 ;; Always auto-insert semicolon to follow SpiderMonkey:
8691 ;; It is required by ECMAScript but is ignored by the rest of
8692 ;; world; see bug 238945
8693 (if (js2-match-token js2-SEMI)
8694 (setq end js2-ts-cursor))
8695 (setf (js2-node-len pn) (- end pos))
8696 pn))
8697
8698 (defun js2-parse-export ()
8699 "Parse an export statement.
8700 The Last matched token must be js2-EXPORT. Currently, the 'default' and 'expr'
8701 expressions should only be either hoistable expressions (function or generator)
8702 or assignment expressions, but there is no checking to enforce that and so it
8703 will parse without error a small subset of
8704 invalid export statements."
8705 (unless (js2-ast-root-p js2-current-scope)
8706 (js2-report-error "msg.mod.export.decl.at.top.level"))
8707 (let ((beg (js2-current-token-beg))
8708 (children (list))
8709 exports-list from-clause declaration default)
8710 (cond
8711 ((js2-match-token js2-MUL)
8712 (setq from-clause (js2-parse-from-clause))
8713 (when from-clause
8714 (push from-clause children)))
8715 ((js2-match-token js2-LC)
8716 (setq exports-list (js2-parse-export-bindings))
8717 (when exports-list
8718 (dolist (export exports-list)
8719 (push export children)))
8720 (when (js2-match-token js2-NAME)
8721 (if (equal "from" (js2-current-token-string))
8722 (progn
8723 (js2-unget-token)
8724 (setq from-clause (js2-parse-from-clause)))
8725 (js2-unget-token))))
8726 ((js2-match-token js2-DEFAULT)
8727 (setq default (js2-parse-expr)))
8728 ((or (js2-match-token js2-VAR) (js2-match-token js2-CONST) (js2-match-token js2-LET))
8729 (setq declaration (js2-parse-variables (js2-current-token-type) (js2-current-token-beg))))
8730 (t
8731 (setq declaration (js2-parse-expr))))
8732 (when from-clause
8733 (push from-clause children))
8734 (when declaration
8735 (push declaration children)
8736 (when (not (js2-function-node-p declaration))
8737 (js2-auto-insert-semicolon declaration)))
8738 (when default
8739 (push default children)
8740 (when (not (js2-function-node-p default))
8741 (js2-auto-insert-semicolon default)))
8742 (let ((node (make-js2-export-node
8743 :pos beg
8744 :len (- (js2-current-token-end) beg)
8745 :exports-list exports-list
8746 :from-clause from-clause
8747 :declaration declaration
8748 :default default)))
8749 (apply #'js2-node-add-children node children)
8750 node)))
8751
8752 (defun js2-parse-for ()
8753 "Parse a for, for-in or for each-in statement.
8754 Last matched token must be js2-FOR."
8755 (let ((for-pos (js2-current-token-beg))
8756 (tmp-scope (make-js2-scope))
8757 pn is-for-each is-for-in-or-of is-for-of
8758 in-pos each-pos tmp-pos
8759 init ; Node init is also foo in 'foo in object'.
8760 cond ; Node cond is also object in 'foo in object'.
8761 incr ; 3rd section of for-loop initializer.
8762 body tt lp rp)
8763 ;; See if this is a for each () instead of just a for ()
8764 (when (js2-match-token js2-NAME)
8765 (if (string= "each" (js2-current-token-string))
8766 (progn
8767 (setq is-for-each t
8768 each-pos (- (js2-current-token-beg) for-pos)) ; relative
8769 (js2-record-face 'font-lock-keyword-face))
8770 (js2-report-error "msg.no.paren.for")))
8771 (if (js2-must-match js2-LP "msg.no.paren.for")
8772 (setq lp (- (js2-current-token-beg) for-pos)))
8773 (setq tt (js2-get-token))
8774 ;; Capture identifiers inside parens. We can't create the node
8775 ;; (and use it as the current scope) until we know its type.
8776 (js2-push-scope tmp-scope)
8777 (unwind-protect
8778 (progn
8779 ;; parse init clause
8780 (let ((js2-in-for-init t)) ; set as dynamic variable
8781 (cond
8782 ((= tt js2-SEMI)
8783 (js2-unget-token)
8784 (setq init (make-js2-empty-expr-node)))
8785 ((or (= tt js2-VAR) (= tt js2-LET))
8786 (setq init (js2-parse-variables tt (js2-current-token-beg))))
8787 (t
8788 (js2-unget-token)
8789 (setq init (js2-parse-expr)))))
8790 (if (or (js2-match-token js2-IN)
8791 (and (>= js2-language-version 200)
8792 (js2-match-contextual-kwd "of")
8793 (setq is-for-of t)))
8794 (setq is-for-in-or-of t
8795 in-pos (- (js2-current-token-beg) for-pos)
8796 ;; scope of iteration target object is not the scope we've created above.
8797 ;; stash current scope temporary.
8798 cond (let ((js2-current-scope (js2-scope-parent-scope js2-current-scope)))
8799 (js2-parse-expr))) ; object over which we're iterating
8800 ;; else ordinary for loop - parse cond and incr
8801 (js2-must-match js2-SEMI "msg.no.semi.for")
8802 (setq cond (if (= (js2-peek-token) js2-SEMI)
8803 (make-js2-empty-expr-node) ; no loop condition
8804 (js2-parse-expr)))
8805 (js2-must-match js2-SEMI "msg.no.semi.for.cond")
8806 (setq tmp-pos (js2-current-token-end)
8807 incr (if (= (js2-peek-token) js2-RP)
8808 (make-js2-empty-expr-node :pos tmp-pos)
8809 (js2-parse-expr)))))
8810 (js2-pop-scope))
8811 (if (js2-must-match js2-RP "msg.no.paren.for.ctrl")
8812 (setq rp (- (js2-current-token-beg) for-pos)))
8813 (if (not is-for-in-or-of)
8814 (setq pn (make-js2-for-node :init init
8815 :condition cond
8816 :update incr
8817 :lp lp
8818 :rp rp))
8819 ;; cond could be null if 'in obj' got eaten by the init node.
8820 (if (js2-infix-node-p init)
8821 ;; it was (foo in bar) instead of (var foo in bar)
8822 (setq cond (js2-infix-node-right init)
8823 init (js2-infix-node-left init))
8824 (if (and (js2-var-decl-node-p init)
8825 (> (length (js2-var-decl-node-kids init)) 1))
8826 (js2-report-error "msg.mult.index")))
8827 (setq pn (make-js2-for-in-node :iterator init
8828 :object cond
8829 :in-pos in-pos
8830 :foreach-p is-for-each
8831 :each-pos each-pos
8832 :forof-p is-for-of
8833 :lp lp
8834 :rp rp)))
8835 ;; Transplant the declarations.
8836 (setf (js2-scope-symbol-table pn)
8837 (js2-scope-symbol-table tmp-scope))
8838 (unwind-protect
8839 (progn
8840 (js2-enter-loop pn)
8841 ;; We have to parse the body -after- creating the loop node,
8842 ;; so that the loop node appears in the js2-loop-set, allowing
8843 ;; break/continue statements to find the enclosing loop.
8844 (setf body (js2-parse-statement)
8845 (js2-loop-node-body pn) body
8846 (js2-node-pos pn) for-pos
8847 (js2-node-len pn) (- (js2-node-end body) for-pos))
8848 (js2-node-add-children pn init cond incr body))
8849 ;; finally
8850 (js2-exit-loop))
8851 pn))
8852
8853 (defun js2-parse-try ()
8854 "Parse a try statement. Last matched token must be js2-TRY."
8855 (let ((try-pos (js2-current-token-beg))
8856 try-end
8857 try-block
8858 catch-blocks
8859 finally-block
8860 saw-default-catch
8861 peek)
8862 (if (/= (js2-peek-token) js2-LC)
8863 (js2-report-error "msg.no.brace.try"))
8864 (setq try-block (js2-parse-statement)
8865 try-end (js2-node-end try-block)
8866 peek (js2-peek-token))
8867 (cond
8868 ((= peek js2-CATCH)
8869 (while (js2-match-token js2-CATCH)
8870 (let* ((catch-pos (js2-current-token-beg))
8871 (catch-node (make-js2-catch-node :pos catch-pos))
8872 param
8873 guard-kwd
8874 catch-cond
8875 lp rp)
8876 (if saw-default-catch
8877 (js2-report-error "msg.catch.unreachable"))
8878 (if (js2-must-match js2-LP "msg.no.paren.catch")
8879 (setq lp (- (js2-current-token-beg) catch-pos)))
8880 (js2-push-scope catch-node)
8881 (let ((tt (js2-peek-token)))
8882 (cond
8883 ;; Destructuring pattern:
8884 ;; catch ({ message, file }) { ... }
8885 ((or (= tt js2-LB) (= tt js2-LC))
8886 (js2-get-token)
8887 (setq param (js2-parse-destruct-primary-expr))
8888 (js2-define-destruct-symbols param js2-LET nil))
8889 ;; Simple name.
8890 (t
8891 (js2-must-match-name "msg.bad.catchcond")
8892 (setq param (js2-create-name-node))
8893 (js2-define-symbol js2-LET (js2-current-token-string) param))))
8894 ;; Catch condition.
8895 (if (js2-match-token js2-IF)
8896 (setq guard-kwd (- (js2-current-token-beg) catch-pos)
8897 catch-cond (js2-parse-expr))
8898 (setq saw-default-catch t))
8899 (if (js2-must-match js2-RP "msg.bad.catchcond")
8900 (setq rp (- (js2-current-token-beg) catch-pos)))
8901 (js2-must-match js2-LC "msg.no.brace.catchblock")
8902 (js2-parse-statements catch-node)
8903 (if (js2-must-match js2-RC "msg.no.brace.after.body")
8904 (setq try-end (js2-current-token-end)))
8905 (js2-pop-scope)
8906 (setf (js2-node-len catch-node) (- try-end catch-pos)
8907 (js2-catch-node-param catch-node) param
8908 (js2-catch-node-guard-expr catch-node) catch-cond
8909 (js2-catch-node-guard-kwd catch-node) guard-kwd
8910 (js2-catch-node-lp catch-node) lp
8911 (js2-catch-node-rp catch-node) rp)
8912 (js2-node-add-children catch-node param catch-cond)
8913 (push catch-node catch-blocks))))
8914 ((/= peek js2-FINALLY)
8915 (js2-must-match js2-FINALLY "msg.try.no.catchfinally"
8916 (js2-node-pos try-block)
8917 (- (setq try-end (js2-node-end try-block))
8918 (js2-node-pos try-block)))))
8919 (when (js2-match-token js2-FINALLY)
8920 (let ((finally-pos (js2-current-token-beg))
8921 (block (js2-parse-statement)))
8922 (setq try-end (js2-node-end block)
8923 finally-block (make-js2-finally-node :pos finally-pos
8924 :len (- try-end finally-pos)
8925 :body block))
8926 (js2-node-add-children finally-block block)))
8927 (let ((pn (make-js2-try-node :pos try-pos
8928 :len (- try-end try-pos)
8929 :try-block try-block
8930 :finally-block finally-block)))
8931 (js2-node-add-children pn try-block finally-block)
8932 ;; Push them onto the try-node, which reverses and corrects their order.
8933 (dolist (cb catch-blocks)
8934 (js2-node-add-children pn cb)
8935 (push cb (js2-try-node-catch-clauses pn)))
8936 pn)))
8937
8938 (defun js2-parse-throw ()
8939 "Parser for throw-statement. Last matched token must be js2-THROW."
8940 (let ((pos (js2-current-token-beg))
8941 expr pn)
8942 (if (= (js2-peek-token-or-eol) js2-EOL)
8943 ;; ECMAScript does not allow new lines before throw expression,
8944 ;; see bug 256617
8945 (js2-report-error "msg.bad.throw.eol"))
8946 (setq expr (js2-parse-expr)
8947 pn (make-js2-throw-node :pos pos
8948 :len (- (js2-node-end expr) pos)
8949 :expr expr))
8950 (js2-node-add-children pn expr)
8951 pn))
8952
8953 (defun js2-match-jump-label-name (label-name)
8954 "If break/continue specified a label, return that label's labeled stmt.
8955 Returns the corresponding `js2-labeled-stmt-node', or if LABEL-NAME
8956 does not match an existing label, reports an error and returns nil."
8957 (let ((bundle (cdr (assoc label-name js2-label-set))))
8958 (if (null bundle)
8959 (js2-report-error "msg.undef.label"))
8960 bundle))
8961
8962 (defun js2-parse-break ()
8963 "Parser for break-statement. Last matched token must be js2-BREAK."
8964 (let ((pos (js2-current-token-beg))
8965 (end (js2-current-token-end))
8966 break-target ; statement to break from
8967 break-label ; in "break foo", name-node representing the foo
8968 labels ; matching labeled statement to break to
8969 pn)
8970 (when (eq (js2-peek-token-or-eol) js2-NAME)
8971 (js2-get-token)
8972 (setq break-label (js2-create-name-node)
8973 end (js2-node-end break-label)
8974 ;; matchJumpLabelName only matches if there is one
8975 labels (js2-match-jump-label-name (js2-current-token-string))
8976 break-target (if labels (car (js2-labeled-stmt-node-labels labels)))))
8977 (unless (or break-target break-label)
8978 ;; no break target specified - try for innermost enclosing loop/switch
8979 (if (null js2-loop-and-switch-set)
8980 (unless break-label
8981 (js2-report-error "msg.bad.break" nil pos (length "break")))
8982 (setq break-target (car js2-loop-and-switch-set))))
8983 (setq pn (make-js2-break-node :pos pos
8984 :len (- end pos)
8985 :label break-label
8986 :target break-target))
8987 (js2-node-add-children pn break-label) ; but not break-target
8988 pn))
8989
8990 (defun js2-parse-continue ()
8991 "Parser for continue-statement. Last matched token must be js2-CONTINUE."
8992 (let ((pos (js2-current-token-beg))
8993 (end (js2-current-token-end))
8994 label ; optional user-specified label, a `js2-name-node'
8995 labels ; current matching labeled stmt, if any
8996 target ; the `js2-loop-node' target of this continue stmt
8997 pn)
8998 (when (= (js2-peek-token-or-eol) js2-NAME)
8999 (js2-get-token)
9000 (setq label (js2-create-name-node)
9001 end (js2-node-end label)
9002 ;; matchJumpLabelName only matches if there is one
9003 labels (js2-match-jump-label-name (js2-current-token-string))))
9004 (cond
9005 ((null labels) ; no current label to go to
9006 (if (null js2-loop-set) ; no loop to continue to
9007 (js2-report-error "msg.continue.outside" nil pos
9008 (length "continue"))
9009 (setq target (car js2-loop-set)))) ; innermost enclosing loop
9010 (t
9011 (if (js2-loop-node-p (js2-labeled-stmt-node-stmt labels))
9012 (setq target (js2-labeled-stmt-node-stmt labels))
9013 (js2-report-error "msg.continue.nonloop" nil pos (- end pos)))))
9014 (setq pn (make-js2-continue-node :pos pos
9015 :len (- end pos)
9016 :label label
9017 :target target))
9018 (js2-node-add-children pn label) ; but not target - it's not our child
9019 pn))
9020
9021 (defun js2-parse-with ()
9022 "Parser for with-statement. Last matched token must be js2-WITH."
9023 (let ((pos (js2-current-token-beg))
9024 obj body pn lp rp)
9025 (if (js2-must-match js2-LP "msg.no.paren.with")
9026 (setq lp (js2-current-token-beg)))
9027 (setq obj (js2-parse-expr))
9028 (if (js2-must-match js2-RP "msg.no.paren.after.with")
9029 (setq rp (js2-current-token-beg)))
9030 (let ((js2-nesting-of-with (1+ js2-nesting-of-with)))
9031 (setq body (js2-parse-statement)))
9032 (setq pn (make-js2-with-node :pos pos
9033 :len (- (js2-node-end body) pos)
9034 :object obj
9035 :body body
9036 :lp (js2-relpos lp pos)
9037 :rp (js2-relpos rp pos)))
9038 (js2-node-add-children pn obj body)
9039 pn))
9040
9041 (defun js2-parse-const-var ()
9042 "Parser for var- or const-statement.
9043 Last matched token must be js2-CONST or js2-VAR."
9044 (let ((tt (js2-current-token-type))
9045 (pos (js2-current-token-beg))
9046 expr pn)
9047 (setq expr (js2-parse-variables tt (js2-current-token-beg))
9048 pn (make-js2-expr-stmt-node :pos pos
9049 :len (- (js2-node-end expr) pos)
9050 :expr expr))
9051 (js2-node-add-children pn expr)
9052 pn))
9053
9054 (defun js2-wrap-with-expr-stmt (pos expr &optional add-child)
9055 (let ((pn (make-js2-expr-stmt-node :pos pos
9056 :len (js2-node-len expr)
9057 :type (if (js2-inside-function)
9058 js2-EXPR_VOID
9059 js2-EXPR_RESULT)
9060 :expr expr)))
9061 (if add-child
9062 (js2-node-add-children pn expr))
9063 pn))
9064
9065 (defun js2-parse-let-stmt ()
9066 "Parser for let-statement. Last matched token must be js2-LET."
9067 (let ((pos (js2-current-token-beg))
9068 expr pn)
9069 (if (= (js2-peek-token) js2-LP)
9070 ;; let expression in statement context
9071 (setq expr (js2-parse-let pos 'statement)
9072 pn (js2-wrap-with-expr-stmt pos expr t))
9073 ;; else we're looking at a statement like let x=6, y=7;
9074 (setf expr (js2-parse-variables js2-LET pos)
9075 pn (js2-wrap-with-expr-stmt pos expr t)
9076 (js2-node-type pn) js2-EXPR_RESULT))
9077 pn))
9078
9079 (defun js2-parse-ret-yield ()
9080 (js2-parse-return-or-yield (js2-current-token-type) nil))
9081
9082 (defconst js2-parse-return-stmt-enders
9083 (list js2-SEMI js2-RC js2-EOF js2-EOL js2-ERROR js2-RB js2-RP js2-YIELD))
9084
9085 (defsubst js2-now-all-set (before after mask)
9086 "Return whether or not the bits in the mask have changed to all set.
9087 BEFORE is bits before change, AFTER is bits after change, and MASK is
9088 the mask for bits. Returns t if all the bits in the mask are set in AFTER
9089 but not BEFORE."
9090 (and (/= (logand before mask) mask)
9091 (= (logand after mask) mask)))
9092
9093 (defun js2-parse-return-or-yield (tt expr-context)
9094 (let* ((pos (js2-current-token-beg))
9095 (end (js2-current-token-end))
9096 (before js2-end-flags)
9097 (inside-function (js2-inside-function))
9098 (gen-type (and inside-function (js2-function-node-generator-type
9099 js2-current-script-or-fn)))
9100 e ret name yield-star-p)
9101 (unless inside-function
9102 (js2-report-error (if (eq tt js2-RETURN)
9103 "msg.bad.return"
9104 "msg.bad.yield")))
9105 (when (and inside-function
9106 (eq gen-type 'STAR)
9107 (js2-match-token js2-MUL))
9108 (setq yield-star-p t))
9109 ;; This is ugly, but we don't want to require a semicolon.
9110 (unless (memq (js2-peek-token-or-eol) js2-parse-return-stmt-enders)
9111 (setq e (js2-parse-expr)
9112 end (js2-node-end e)))
9113 (cond
9114 ((eq tt js2-RETURN)
9115 (js2-set-flag js2-end-flags (if (null e)
9116 js2-end-returns
9117 js2-end-returns-value))
9118 (setq ret (make-js2-return-node :pos pos
9119 :len (- end pos)
9120 :retval e))
9121 (js2-node-add-children ret e)
9122 ;; See if we need a strict mode warning.
9123 ;; TODO: The analysis done by `js2-has-consistent-return-usage' is
9124 ;; more thorough and accurate than this before/after flag check.
9125 ;; E.g. if there's a finally-block that always returns, we shouldn't
9126 ;; show a warning generated by inconsistent returns in the catch blocks.
9127 ;; Basically `js2-has-consistent-return-usage' needs to keep more state,
9128 ;; so we know which returns/yields to highlight, and we should get rid of
9129 ;; all the checking in `js2-parse-return-or-yield'.
9130 (if (and js2-strict-inconsistent-return-warning
9131 (js2-now-all-set before js2-end-flags
9132 (logior js2-end-returns js2-end-returns-value)))
9133 (js2-add-strict-warning "msg.return.inconsistent" nil pos end)))
9134 ((eq gen-type 'COMPREHENSION)
9135 ;; FIXME: We should probably switch to saving and using lastYieldOffset,
9136 ;; like SpiderMonkey does.
9137 (js2-report-error "msg.syntax" nil pos 5))
9138 (t
9139 (setq ret (make-js2-yield-node :pos pos
9140 :len (- end pos)
9141 :value e
9142 :star-p yield-star-p))
9143 (js2-node-add-children ret e)
9144 (unless expr-context
9145 (setq e ret
9146 ret (js2-wrap-with-expr-stmt pos e t))
9147 (js2-set-requires-activation)
9148 (js2-set-is-generator))))
9149 ;; see if we are mixing yields and value returns.
9150 (when (and inside-function
9151 (js2-flag-set-p js2-end-flags js2-end-returns-value)
9152 (eq (js2-function-node-generator-type js2-current-script-or-fn)
9153 'LEGACY))
9154 (setq name (js2-function-name js2-current-script-or-fn))
9155 (if (zerop (length name))
9156 (js2-report-error "msg.anon.generator.returns" nil pos (- end pos))
9157 (js2-report-error "msg.generator.returns" name pos (- end pos))))
9158 ret))
9159
9160 (defun js2-parse-debugger ()
9161 (make-js2-keyword-node :type js2-DEBUGGER))
9162
9163 (defun js2-parse-block ()
9164 "Parser for a curly-delimited statement block.
9165 Last token matched must be `js2-LC'."
9166 (let ((pos (js2-current-token-beg))
9167 (pn (make-js2-scope)))
9168 (js2-push-scope pn)
9169 (unwind-protect
9170 (progn
9171 (js2-parse-statements pn)
9172 (js2-must-match js2-RC "msg.no.brace.block")
9173 (setf (js2-node-len pn) (- (js2-current-token-end) pos)))
9174 (js2-pop-scope))
9175 pn))
9176
9177 ;; For `js2-ERROR' too, to have a node for error recovery to work on.
9178 (defun js2-parse-semi ()
9179 "Parse a statement or handle an error.
9180 Current token type is `js2-SEMI' or `js2-ERROR'."
9181 (let ((tt (js2-current-token-type)) pos len)
9182 (if (eq tt js2-SEMI)
9183 (make-js2-empty-expr-node :len 1)
9184 (setq pos (js2-current-token-beg)
9185 len (- (js2-current-token-end) pos))
9186 (js2-report-error "msg.syntax" nil pos len)
9187 (make-js2-error-node :pos pos :len len))))
9188
9189 (defun js2-parse-default-xml-namespace ()
9190 "Parse a `default xml namespace = <expr>' e4x statement."
9191 (let ((pos (js2-current-token-beg))
9192 end len expr unary)
9193 (js2-must-have-xml)
9194 (js2-set-requires-activation)
9195 (setq len (- js2-ts-cursor pos))
9196 (unless (and (js2-match-token js2-NAME)
9197 (string= (js2-current-token-string) "xml"))
9198 (js2-report-error "msg.bad.namespace" nil pos len))
9199 (unless (and (js2-match-token js2-NAME)
9200 (string= (js2-current-token-string) "namespace"))
9201 (js2-report-error "msg.bad.namespace" nil pos len))
9202 (unless (js2-match-token js2-ASSIGN)
9203 (js2-report-error "msg.bad.namespace" nil pos len))
9204 (setq expr (js2-parse-expr)
9205 end (js2-node-end expr)
9206 unary (make-js2-unary-node :type js2-DEFAULTNAMESPACE
9207 :pos pos
9208 :len (- end pos)
9209 :operand expr))
9210 (js2-node-add-children unary expr)
9211 (make-js2-expr-stmt-node :pos pos
9212 :len (- end pos)
9213 :expr unary)))
9214
9215 (defun js2-record-label (label bundle)
9216 ;; current token should be colon that `js2-parse-primary-expr' left untouched
9217 (js2-get-token)
9218 (let ((name (js2-label-node-name label))
9219 labeled-stmt
9220 dup)
9221 (when (setq labeled-stmt (cdr (assoc name js2-label-set)))
9222 ;; flag both labels if possible when used in editing mode
9223 (if (and js2-parse-ide-mode
9224 (setq dup (js2-get-label-by-name labeled-stmt name)))
9225 (js2-report-error "msg.dup.label" nil
9226 (js2-node-abs-pos dup) (js2-node-len dup)))
9227 (js2-report-error "msg.dup.label" nil
9228 (js2-node-pos label) (js2-node-len label)))
9229 (js2-labeled-stmt-node-add-label bundle label)
9230 (js2-node-add-children bundle label)
9231 ;; Add one reference to the bundle per label in `js2-label-set'
9232 (push (cons name bundle) js2-label-set)))
9233
9234 (defun js2-parse-name-or-label ()
9235 "Parser for identifier or label. Last token matched must be js2-NAME.
9236 Called when we found a name in a statement context. If it's a label, we gather
9237 up any following labels and the next non-label statement into a
9238 `js2-labeled-stmt-node' bundle and return that. Otherwise we parse an
9239 expression and return it wrapped in a `js2-expr-stmt-node'."
9240 (let ((pos (js2-current-token-beg))
9241 expr stmt bundle
9242 (continue t))
9243 ;; set check for label and call down to `js2-parse-primary-expr'
9244 (setq expr (js2-maybe-parse-label))
9245 (if (null expr)
9246 ;; Parse the non-label expression and wrap with expression stmt.
9247 (js2-wrap-with-expr-stmt pos (js2-parse-expr) t)
9248 ;; else parsed a label
9249 (setq bundle (make-js2-labeled-stmt-node :pos pos))
9250 (js2-record-label expr bundle)
9251 ;; look for more labels
9252 (while (and continue (= (js2-get-token) js2-NAME))
9253 (if (setq expr (js2-maybe-parse-label))
9254 (js2-record-label expr bundle)
9255 (setq expr (js2-parse-expr)
9256 stmt (js2-wrap-with-expr-stmt (js2-node-pos expr) expr t)
9257 continue nil)
9258 (js2-auto-insert-semicolon stmt)))
9259 ;; no more labels; now parse the labeled statement
9260 (unwind-protect
9261 (unless stmt
9262 (let ((js2-labeled-stmt bundle)) ; bind dynamically
9263 (js2-unget-token)
9264 (setq stmt (js2-statement-helper))))
9265 ;; remove the labels for this statement from the global set
9266 (dolist (label (js2-labeled-stmt-node-labels bundle))
9267 (setq js2-label-set (remove label js2-label-set))))
9268 (setf (js2-labeled-stmt-node-stmt bundle) stmt
9269 (js2-node-len bundle) (- (js2-node-end stmt) pos))
9270 (js2-node-add-children bundle stmt)
9271 bundle)))
9272
9273 (defun js2-maybe-parse-label ()
9274 (cl-assert (= (js2-current-token-type) js2-NAME))
9275 (let (label-pos
9276 (next-tt (js2-get-token))
9277 (label-end (js2-current-token-end)))
9278 ;; Do not consume colon, it is used as unwind indicator
9279 ;; to return to statementHelper.
9280 (js2-unget-token)
9281 (if (= next-tt js2-COLON)
9282 (prog2
9283 (setq label-pos (js2-current-token-beg))
9284 (make-js2-label-node :pos label-pos
9285 :len (- label-end label-pos)
9286 :name (js2-current-token-string))
9287 (js2-set-face label-pos
9288 label-end
9289 'font-lock-variable-name-face 'record))
9290 ;; Backtrack from the name token, too.
9291 (js2-unget-token)
9292 nil)))
9293
9294 (defun js2-parse-expr-stmt ()
9295 "Default parser in statement context, if no recognized statement found."
9296 (js2-wrap-with-expr-stmt (js2-current-token-beg)
9297 (progn
9298 (js2-unget-token)
9299 (js2-parse-expr)) t))
9300
9301 (defun js2-parse-variables (decl-type pos)
9302 "Parse a comma-separated list of variable declarations.
9303 Could be a 'var', 'const' or 'let' expression, possibly in a for-loop initializer.
9304
9305 DECL-TYPE is a token value: either VAR, CONST, or LET depending on context.
9306 For 'var' or 'const', the keyword should be the token last scanned.
9307
9308 POS is the position where the node should start. It's sometimes the
9309 var/const/let keyword, and other times the beginning of the first token
9310 in the first variable declaration.
9311
9312 Returns the parsed `js2-var-decl-node' expression node."
9313 (let* ((result (make-js2-var-decl-node :decl-type decl-type
9314 :pos pos))
9315 destructuring kid-pos tt init name end nbeg nend vi
9316 (continue t))
9317 ;; Example:
9318 ;; var foo = {a: 1, b: 2}, bar = [3, 4];
9319 ;; var {b: s2, a: s1} = foo, x = 6, y, [s3, s4] = bar;
9320 ;; var {a, b} = baz;
9321 (while continue
9322 (setq destructuring nil
9323 name nil
9324 tt (js2-get-token)
9325 kid-pos (js2-current-token-beg)
9326 end (js2-current-token-end)
9327 init nil)
9328 (if (or (= tt js2-LB) (= tt js2-LC))
9329 ;; Destructuring assignment, e.g., var [a, b] = ...
9330 (setq destructuring (js2-parse-destruct-primary-expr)
9331 end (js2-node-end destructuring))
9332 ;; Simple variable name
9333 (js2-unget-token)
9334 (when (js2-must-match-name "msg.bad.var")
9335 (setq name (js2-create-name-node)
9336 nbeg (js2-current-token-beg)
9337 nend (js2-current-token-end)
9338 end nend)
9339 (js2-define-symbol decl-type (js2-current-token-string) name js2-in-for-init)))
9340 (when (js2-match-token js2-ASSIGN)
9341 (setq init (js2-parse-assign-expr)
9342 end (js2-node-end init))
9343 (js2-record-imenu-functions init name))
9344 (when name
9345 (js2-set-face nbeg nend (if (js2-function-node-p init)
9346 'font-lock-function-name-face
9347 'font-lock-variable-name-face)
9348 'record))
9349 (setq vi (make-js2-var-init-node :pos kid-pos
9350 :len (- end kid-pos)
9351 :type decl-type))
9352 (if destructuring
9353 (progn
9354 (if (and (null init) (not js2-in-for-init))
9355 (js2-report-error "msg.destruct.assign.no.init"))
9356 (js2-define-destruct-symbols destructuring
9357 decl-type
9358 'font-lock-variable-name-face)
9359 (setf (js2-var-init-node-target vi) destructuring))
9360 (setf (js2-var-init-node-target vi) name))
9361 (setf (js2-var-init-node-initializer vi) init)
9362 (js2-node-add-children vi name destructuring init)
9363 (js2-block-node-push result vi)
9364 (unless (js2-match-token js2-COMMA)
9365 (setq continue nil)))
9366 (setf (js2-node-len result) (- end pos))
9367 result))
9368
9369 (defun js2-parse-let (pos &optional stmt-p)
9370 "Parse a let expression or statement.
9371 A let-expression is of the form `let (vars) expr'.
9372 A let-statement is of the form `let (vars) {statements}'.
9373 The third form of let is a variable declaration list, handled
9374 by `js2-parse-variables'."
9375 (let ((pn (make-js2-let-node :pos pos))
9376 beg vars body)
9377 (if (js2-must-match js2-LP "msg.no.paren.after.let")
9378 (setf (js2-let-node-lp pn) (- (js2-current-token-beg) pos)))
9379 (js2-push-scope pn)
9380 (unwind-protect
9381 (progn
9382 (setq vars (js2-parse-variables js2-LET (js2-current-token-beg)))
9383 (if (js2-must-match js2-RP "msg.no.paren.let")
9384 (setf (js2-let-node-rp pn) (- (js2-current-token-beg) pos)))
9385 (if (and stmt-p (js2-match-token js2-LC))
9386 ;; let statement
9387 (progn
9388 (setf beg (js2-current-token-beg) ; position stmt at LC
9389 body (js2-parse-statements))
9390 (js2-must-match js2-RC "msg.no.curly.let")
9391 (setf (js2-node-len body) (- (js2-current-token-end) beg)
9392 (js2-node-len pn) (- (js2-current-token-end) pos)
9393 (js2-let-node-body pn) body
9394 (js2-node-type pn) js2-LET))
9395 ;; let expression
9396 (setf body (js2-parse-expr)
9397 (js2-node-len pn) (- (js2-node-end body) pos)
9398 (js2-let-node-body pn) body))
9399 (setf (js2-let-node-vars pn) vars)
9400 (js2-node-add-children pn vars body))
9401 (js2-pop-scope))
9402 pn))
9403
9404 (defun js2-define-new-symbol (decl-type name node &optional scope)
9405 (js2-scope-put-symbol (or scope js2-current-scope)
9406 name
9407 (make-js2-symbol decl-type name node)))
9408
9409 (defun js2-define-symbol (decl-type name &optional node ignore-not-in-block)
9410 "Define a symbol in the current scope.
9411 If NODE is non-nil, it is the AST node associated with the symbol."
9412 (let* ((defining-scope (js2-get-defining-scope js2-current-scope name))
9413 (symbol (if defining-scope
9414 (js2-scope-get-symbol defining-scope name)))
9415 (sdt (if symbol (js2-symbol-decl-type symbol) -1))
9416 (pos (if node (js2-node-abs-pos node)))
9417 (len (if node (js2-node-len node))))
9418 (cond
9419 ((and symbol ; already defined
9420 (or (= sdt js2-CONST) ; old version is const
9421 (= decl-type js2-CONST) ; new version is const
9422 ;; two let-bound vars in this block have same name
9423 (and (= sdt js2-LET)
9424 (eq defining-scope js2-current-scope))))
9425 (js2-report-error
9426 (cond
9427 ((= sdt js2-CONST) "msg.const.redecl")
9428 ((= sdt js2-LET) "msg.let.redecl")
9429 ((= sdt js2-VAR) "msg.var.redecl")
9430 ((= sdt js2-FUNCTION) "msg.function.redecl")
9431 (t "msg.parm.redecl"))
9432 name pos len))
9433 ((= decl-type js2-LET)
9434 (if (and (not ignore-not-in-block)
9435 (or (= (js2-node-type js2-current-scope) js2-IF)
9436 (js2-loop-node-p js2-current-scope)))
9437 (js2-report-error "msg.let.decl.not.in.block")
9438 (js2-define-new-symbol decl-type name node)))
9439 ((or (= decl-type js2-VAR)
9440 (= decl-type js2-CONST)
9441 (= decl-type js2-FUNCTION))
9442 (if symbol
9443 (if (and js2-strict-var-redeclaration-warning (= sdt js2-VAR))
9444 (js2-add-strict-warning "msg.var.redecl" name)
9445 (if (and js2-strict-var-hides-function-arg-warning (= sdt js2-LP))
9446 (js2-add-strict-warning "msg.var.hides.arg" name)))
9447 (js2-define-new-symbol decl-type name node
9448 js2-current-script-or-fn)))
9449 ((= decl-type js2-LP)
9450 (if symbol
9451 ;; must be duplicate parameter. Second parameter hides the
9452 ;; first, so go ahead and add the second pararameter
9453 (js2-report-warning "msg.dup.parms" name))
9454 (js2-define-new-symbol decl-type name node))
9455 (t (js2-code-bug)))))
9456
9457 (defun js2-parse-paren-expr-or-generator-comp ()
9458 (let ((px-pos (js2-current-token-beg)))
9459 (cond
9460 ((and (>= js2-language-version 200)
9461 (js2-match-token js2-FOR))
9462 (js2-parse-generator-comp px-pos))
9463 ((and (>= js2-language-version 200)
9464 (js2-match-token js2-RP))
9465 ;; Not valid expression syntax, but this is valid in an arrow
9466 ;; function with no params: () => body.
9467 (if (eq (js2-peek-token) js2-ARROW)
9468 ;; Return whatever, it will hopefully be rewinded and
9469 ;; reparsed when we reach the =>.
9470 (make-js2-keyword-node :type js2-NULL)
9471 (js2-report-error "msg.syntax")
9472 (make-js2-error-node)))
9473 (t
9474 (let* ((js2-in-for-init nil)
9475 (expr (js2-parse-expr))
9476 (pn (make-js2-paren-node :pos px-pos
9477 :expr expr)))
9478 (js2-node-add-children pn (js2-paren-node-expr pn))
9479 (js2-must-match js2-RP "msg.no.paren")
9480 (setf (js2-node-len pn) (- (js2-current-token-end) px-pos))
9481 pn)))))
9482
9483 (defun js2-parse-expr (&optional oneshot)
9484 (let* ((pn (js2-parse-assign-expr))
9485 (pos (js2-node-pos pn))
9486 left
9487 right
9488 op-pos)
9489 (while (and (not oneshot)
9490 (js2-match-token js2-COMMA))
9491 (setq op-pos (- (js2-current-token-beg) pos)) ; relative
9492 (if (= (js2-peek-token) js2-YIELD)
9493 (js2-report-error "msg.yield.parenthesized"))
9494 (setq right (js2-parse-assign-expr)
9495 left pn
9496 pn (make-js2-infix-node :type js2-COMMA
9497 :pos pos
9498 :len (- js2-ts-cursor pos)
9499 :op-pos op-pos
9500 :left left
9501 :right right))
9502 (js2-node-add-children pn left right))
9503 pn))
9504
9505 (defun js2-parse-assign-expr ()
9506 (let ((tt (js2-get-token))
9507 (pos (js2-current-token-beg))
9508 pn left right op-pos
9509 ts-state recorded-identifiers parsed-errors)
9510 (if (= tt js2-YIELD)
9511 (js2-parse-return-or-yield tt t)
9512 ;; Save the tokenizer state in case we find an arrow function
9513 ;; and have to rewind.
9514 (setq ts-state (make-js2-ts-state)
9515 recorded-identifiers js2-recorded-identifiers
9516 parsed-errors js2-parsed-errors)
9517 ;; not yield - parse assignment expression
9518 (setq pn (js2-parse-cond-expr)
9519 tt (js2-get-token))
9520 (cond
9521 ((and (<= js2-first-assign tt)
9522 (<= tt js2-last-assign))
9523 ;; tt express assignment (=, |=, ^=, ..., %=)
9524 (setq op-pos (- (js2-current-token-beg) pos) ; relative
9525 left pn)
9526 (setq right (js2-parse-assign-expr)
9527 pn (make-js2-assign-node :type tt
9528 :pos pos
9529 :len (- (js2-node-end right) pos)
9530 :op-pos op-pos
9531 :left left
9532 :right right))
9533 (when js2-parse-ide-mode
9534 (js2-highlight-assign-targets pn left right)
9535 (js2-record-imenu-functions right left))
9536 ;; do this last so ide checks above can use absolute positions
9537 (js2-node-add-children pn left right))
9538 ((and (= tt js2-ARROW)
9539 (>= js2-language-version 200))
9540 (js2-ts-seek ts-state)
9541 (setq js2-recorded-identifiers recorded-identifiers
9542 js2-parsed-errors parsed-errors)
9543 (setq pn (js2-parse-function 'FUNCTION_ARROW (js2-current-token-beg) nil)))
9544 (t
9545 (js2-unget-token)))
9546 pn)))
9547
9548 (defun js2-parse-cond-expr ()
9549 (let ((pos (js2-current-token-beg))
9550 (pn (js2-parse-or-expr))
9551 test-expr
9552 if-true
9553 if-false
9554 q-pos
9555 c-pos)
9556 (when (js2-match-token js2-HOOK)
9557 (setq q-pos (- (js2-current-token-beg) pos)
9558 if-true (let (js2-in-for-init) (js2-parse-assign-expr)))
9559 (js2-must-match js2-COLON "msg.no.colon.cond")
9560 (setq c-pos (- (js2-current-token-beg) pos)
9561 if-false (js2-parse-assign-expr)
9562 test-expr pn
9563 pn (make-js2-cond-node :pos pos
9564 :len (- (js2-node-end if-false) pos)
9565 :test-expr test-expr
9566 :true-expr if-true
9567 :false-expr if-false
9568 :q-pos q-pos
9569 :c-pos c-pos))
9570 (js2-node-add-children pn test-expr if-true if-false))
9571 pn))
9572
9573 (defun js2-make-binary (type left parser)
9574 "Helper for constructing a binary-operator AST node.
9575 LEFT is the left-side-expression, already parsed, and the
9576 binary operator should have just been matched.
9577 PARSER is a function to call to parse the right operand,
9578 or a `js2-node' struct if it has already been parsed.
9579 FIXME: The latter option is unused?"
9580 (let* ((pos (js2-node-pos left))
9581 (op-pos (- (js2-current-token-beg) pos))
9582 (right (if (js2-node-p parser)
9583 parser
9584 (js2-get-token)
9585 (funcall parser)))
9586 (pn (make-js2-infix-node :type type
9587 :pos pos
9588 :len (- (js2-node-end right) pos)
9589 :op-pos op-pos
9590 :left left
9591 :right right)))
9592 (js2-node-add-children pn left right)
9593 pn))
9594
9595 (defun js2-parse-or-expr ()
9596 (let ((pn (js2-parse-and-expr)))
9597 (when (js2-match-token js2-OR)
9598 (setq pn (js2-make-binary js2-OR
9599 pn
9600 'js2-parse-or-expr)))
9601 pn))
9602
9603 (defun js2-parse-and-expr ()
9604 (let ((pn (js2-parse-bit-or-expr)))
9605 (when (js2-match-token js2-AND)
9606 (setq pn (js2-make-binary js2-AND
9607 pn
9608 'js2-parse-and-expr)))
9609 pn))
9610
9611 (defun js2-parse-bit-or-expr ()
9612 (let ((pn (js2-parse-bit-xor-expr)))
9613 (while (js2-match-token js2-BITOR)
9614 (setq pn (js2-make-binary js2-BITOR
9615 pn
9616 'js2-parse-bit-xor-expr)))
9617 pn))
9618
9619 (defun js2-parse-bit-xor-expr ()
9620 (let ((pn (js2-parse-bit-and-expr)))
9621 (while (js2-match-token js2-BITXOR)
9622 (setq pn (js2-make-binary js2-BITXOR
9623 pn
9624 'js2-parse-bit-and-expr)))
9625 pn))
9626
9627 (defun js2-parse-bit-and-expr ()
9628 (let ((pn (js2-parse-eq-expr)))
9629 (while (js2-match-token js2-BITAND)
9630 (setq pn (js2-make-binary js2-BITAND
9631 pn
9632 'js2-parse-eq-expr)))
9633 pn))
9634
9635 (defconst js2-parse-eq-ops
9636 (list js2-EQ js2-NE js2-SHEQ js2-SHNE))
9637
9638 (defun js2-parse-eq-expr ()
9639 (let ((pn (js2-parse-rel-expr))
9640 tt)
9641 (while (memq (setq tt (js2-get-token)) js2-parse-eq-ops)
9642 (setq pn (js2-make-binary tt
9643 pn
9644 'js2-parse-rel-expr)))
9645 (js2-unget-token)
9646 pn))
9647
9648 (defconst js2-parse-rel-ops
9649 (list js2-IN js2-INSTANCEOF js2-LE js2-LT js2-GE js2-GT))
9650
9651 (defun js2-parse-rel-expr ()
9652 (let ((pn (js2-parse-shift-expr))
9653 (continue t)
9654 tt)
9655 (while continue
9656 (setq tt (js2-get-token))
9657 (cond
9658 ((and js2-in-for-init (= tt js2-IN))
9659 (js2-unget-token)
9660 (setq continue nil))
9661 ((memq tt js2-parse-rel-ops)
9662 (setq pn (js2-make-binary tt pn 'js2-parse-shift-expr)))
9663 (t
9664 (js2-unget-token)
9665 (setq continue nil))))
9666 pn))
9667
9668 (defconst js2-parse-shift-ops
9669 (list js2-LSH js2-URSH js2-RSH))
9670
9671 (defun js2-parse-shift-expr ()
9672 (let ((pn (js2-parse-add-expr))
9673 tt
9674 (continue t))
9675 (while continue
9676 (setq tt (js2-get-token))
9677 (if (memq tt js2-parse-shift-ops)
9678 (setq pn (js2-make-binary tt pn 'js2-parse-add-expr))
9679 (js2-unget-token)
9680 (setq continue nil)))
9681 pn))
9682
9683 (defun js2-parse-add-expr ()
9684 (let ((pn (js2-parse-mul-expr))
9685 tt
9686 (continue t))
9687 (while continue
9688 (setq tt (js2-get-token))
9689 (if (or (= tt js2-ADD) (= tt js2-SUB))
9690 (setq pn (js2-make-binary tt pn 'js2-parse-mul-expr))
9691 (js2-unget-token)
9692 (setq continue nil)))
9693 pn))
9694
9695 (defconst js2-parse-mul-ops
9696 (list js2-MUL js2-DIV js2-MOD))
9697
9698 (defun js2-parse-mul-expr ()
9699 (let ((pn (js2-parse-unary-expr))
9700 tt
9701 (continue t))
9702 (while continue
9703 (setq tt (js2-get-token))
9704 (if (memq tt js2-parse-mul-ops)
9705 (setq pn (js2-make-binary tt pn 'js2-parse-unary-expr))
9706 (js2-unget-token)
9707 (setq continue nil)))
9708 pn))
9709
9710 (defun js2-make-unary (type parser &rest args)
9711 "Make a unary node of type TYPE.
9712 PARSER is either a node (for postfix operators) or a function to call
9713 to parse the operand (for prefix operators)."
9714 (let* ((pos (js2-current-token-beg))
9715 (postfix (js2-node-p parser))
9716 (expr (if postfix
9717 parser
9718 (apply parser args)))
9719 end
9720 pn)
9721 (if postfix ; e.g. i++
9722 (setq pos (js2-node-pos expr)
9723 end (js2-current-token-end))
9724 (setq end (js2-node-end expr)))
9725 (setq pn (make-js2-unary-node :type type
9726 :pos pos
9727 :len (- end pos)
9728 :operand expr))
9729 (js2-node-add-children pn expr)
9730 pn))
9731
9732 (defconst js2-incrementable-node-types
9733 (list js2-NAME js2-GETPROP js2-GETELEM js2-GET_REF js2-CALL)
9734 "Node types that can be the operand of a ++ or -- operator.")
9735
9736 (defun js2-check-bad-inc-dec (tt beg end unary)
9737 (unless (memq (js2-node-type (js2-unary-node-operand unary))
9738 js2-incrementable-node-types)
9739 (js2-report-error (if (= tt js2-INC)
9740 "msg.bad.incr"
9741 "msg.bad.decr")
9742 nil beg (- end beg))))
9743
9744 (defun js2-parse-unary-expr ()
9745 (let ((tt (js2-current-token-type))
9746 pn expr beg end)
9747 (cond
9748 ((or (= tt js2-VOID)
9749 (= tt js2-NOT)
9750 (= tt js2-BITNOT)
9751 (= tt js2-TYPEOF))
9752 (js2-get-token)
9753 (js2-make-unary tt 'js2-parse-unary-expr))
9754 ((= tt js2-ADD)
9755 (js2-get-token)
9756 ;; Convert to special POS token in decompiler and parse tree
9757 (js2-make-unary js2-POS 'js2-parse-unary-expr))
9758 ((= tt js2-SUB)
9759 (js2-get-token)
9760 ;; Convert to special NEG token in decompiler and parse tree
9761 (js2-make-unary js2-NEG 'js2-parse-unary-expr))
9762 ((or (= tt js2-INC)
9763 (= tt js2-DEC))
9764 (js2-get-token)
9765 (prog1
9766 (setq beg (js2-current-token-beg)
9767 end (js2-current-token-end)
9768 expr (js2-make-unary tt 'js2-parse-member-expr t))
9769 (js2-check-bad-inc-dec tt beg end expr)))
9770 ((= tt js2-DELPROP)
9771 (js2-get-token)
9772 (js2-make-unary js2-DELPROP 'js2-parse-unary-expr))
9773 ((= tt js2-ERROR)
9774 (js2-get-token)
9775 (make-js2-error-node)) ; try to continue
9776 ((and (= tt js2-LT)
9777 js2-compiler-xml-available)
9778 ;; XML stream encountered in expression.
9779 (js2-parse-member-expr-tail t (js2-parse-xml-initializer)))
9780 (t
9781 (setq pn (js2-parse-member-expr t)
9782 ;; Don't look across a newline boundary for a postfix incop.
9783 tt (js2-peek-token-or-eol))
9784 (when (or (= tt js2-INC) (= tt js2-DEC))
9785 (js2-get-token)
9786 (setf expr pn
9787 pn (js2-make-unary tt expr))
9788 (js2-node-set-prop pn 'postfix t)
9789 (js2-check-bad-inc-dec tt (js2-current-token-beg) (js2-current-token-end) pn))
9790 pn))))
9791
9792 (defun js2-parse-xml-initializer ()
9793 "Parse an E4X XML initializer.
9794 I'm parsing it the way Rhino parses it, but without the tree-rewriting.
9795 Then I'll postprocess the result, depending on whether we're in IDE
9796 mode or codegen mode, and generate the appropriate rewritten AST.
9797 IDE mode uses a rich AST that models the XML structure. Codegen mode
9798 just concatenates everything and makes a new XML or XMLList out of it."
9799 (let ((tt (js2-get-first-xml-token))
9800 pn-xml pn expr kids expr-pos
9801 (continue t)
9802 (first-token t))
9803 (when (not (or (= tt js2-XML) (= tt js2-XMLEND)))
9804 (js2-report-error "msg.syntax"))
9805 (setq pn-xml (make-js2-xml-node))
9806 (while continue
9807 (if first-token
9808 (setq first-token nil)
9809 (setq tt (js2-get-next-xml-token)))
9810 (cond
9811 ;; js2-XML means we found a {expr} in the XML stream.
9812 ;; The token string is the XML up to the left-curly.
9813 ((= tt js2-XML)
9814 (push (make-js2-string-node :pos (js2-current-token-beg)
9815 :len (- js2-ts-cursor (js2-current-token-beg)))
9816 kids)
9817 (js2-must-match js2-LC "msg.syntax")
9818 (setq expr-pos js2-ts-cursor
9819 expr (if (eq (js2-peek-token) js2-RC)
9820 (make-js2-empty-expr-node :pos expr-pos)
9821 (js2-parse-expr)))
9822 (js2-must-match js2-RC "msg.syntax")
9823 (setq pn (make-js2-xml-js-expr-node :pos (js2-node-pos expr)
9824 :len (js2-node-len expr)
9825 :expr expr))
9826 (js2-node-add-children pn expr)
9827 (push pn kids))
9828 ;; a js2-XMLEND token means we hit the final close-tag.
9829 ((= tt js2-XMLEND)
9830 (push (make-js2-string-node :pos (js2-current-token-beg)
9831 :len (- js2-ts-cursor (js2-current-token-beg)))
9832 kids)
9833 (dolist (kid (nreverse kids))
9834 (js2-block-node-push pn-xml kid))
9835 (setf (js2-node-len pn-xml) (- js2-ts-cursor
9836 (js2-node-pos pn-xml))
9837 continue nil))
9838 (t
9839 (js2-report-error "msg.syntax")
9840 (setq continue nil))))
9841 pn-xml))
9842
9843
9844 (defun js2-parse-argument-list ()
9845 "Parse an argument list and return it as a Lisp list of nodes.
9846 Returns the list in reverse order. Consumes the right-paren token."
9847 (let (result)
9848 (unless (js2-match-token js2-RP)
9849 (cl-loop do
9850 (let ((tt (js2-get-token)))
9851 (if (= tt js2-YIELD)
9852 (js2-report-error "msg.yield.parenthesized"))
9853 (if (and (= tt js2-TRIPLEDOT)
9854 (>= js2-language-version 200))
9855 (push (js2-make-unary tt 'js2-parse-assign-expr) result)
9856 (js2-unget-token)
9857 (push (js2-parse-assign-expr) result)))
9858 while
9859 (js2-match-token js2-COMMA))
9860 (js2-must-match js2-RP "msg.no.paren.arg")
9861 result)))
9862
9863 (defun js2-parse-member-expr (&optional allow-call-syntax)
9864 (let ((tt (js2-current-token-type))
9865 pn pos target args beg end init)
9866 (if (/= tt js2-NEW)
9867 (setq pn (js2-parse-primary-expr))
9868 ;; parse a 'new' expression
9869 (js2-get-token)
9870 (setq pos (js2-current-token-beg)
9871 beg pos
9872 target (js2-parse-member-expr)
9873 end (js2-node-end target)
9874 pn (make-js2-new-node :pos pos
9875 :target target
9876 :len (- end pos)))
9877 (js2-highlight-function-call (js2-current-token))
9878 (js2-node-add-children pn target)
9879 (when (js2-match-token js2-LP)
9880 ;; Add the arguments to pn, if any are supplied.
9881 (setf beg pos ; start of "new" keyword
9882 pos (js2-current-token-beg)
9883 args (nreverse (js2-parse-argument-list))
9884 (js2-new-node-args pn) args
9885 end (js2-current-token-end)
9886 (js2-new-node-lp pn) (- pos beg)
9887 (js2-new-node-rp pn) (- end 1 beg))
9888 (apply #'js2-node-add-children pn args))
9889 (when (and js2-allow-rhino-new-expr-initializer
9890 (js2-match-token js2-LC))
9891 (setf init (js2-parse-object-literal)
9892 end (js2-node-end init)
9893 (js2-new-node-initializer pn) init)
9894 (js2-node-add-children pn init))
9895 (setf (js2-node-len pn) (- end beg))) ; end outer if
9896 (js2-parse-member-expr-tail allow-call-syntax pn)))
9897
9898 (defun js2-parse-member-expr-tail (allow-call-syntax pn)
9899 "Parse a chain of property/array accesses or function calls.
9900 Includes parsing for E4X operators like `..' and `.@'.
9901 If ALLOW-CALL-SYNTAX is nil, stops when we encounter a left-paren.
9902 Returns an expression tree that includes PN, the parent node."
9903 (let (tt
9904 (continue t))
9905 (while continue
9906 (setq tt (js2-get-token))
9907 (cond
9908 ((or (= tt js2-DOT) (= tt js2-DOTDOT))
9909 (setq pn (js2-parse-property-access tt pn)))
9910 ((= tt js2-DOTQUERY)
9911 (setq pn (js2-parse-dot-query pn)))
9912 ((= tt js2-LB)
9913 (setq pn (js2-parse-element-get pn)))
9914 ((= tt js2-LP)
9915 (js2-unget-token)
9916 (if allow-call-syntax
9917 (setq pn (js2-parse-function-call pn))
9918 (setq continue nil)))
9919 ((= tt js2-TEMPLATE_HEAD)
9920 (setq pn (js2-parse-tagged-template pn (js2-parse-template-literal))))
9921 ((= tt js2-NO_SUBS_TEMPLATE)
9922 (setq pn (js2-parse-tagged-template pn (make-js2-string-node :type tt))))
9923 (t
9924 (js2-unget-token)
9925 (setq continue nil))))
9926 (if (>= js2-highlight-level 2)
9927 (js2-parse-highlight-member-expr-node pn))
9928 pn))
9929
9930 (defun js2-parse-tagged-template (tag-node tpl-node)
9931 "Parse tagged template expression."
9932 (let* ((beg (js2-node-pos tag-node))
9933 (pn (make-js2-tagged-template-node :beg beg
9934 :len (- (js2-current-token-end) beg)
9935 :tag tag-node
9936 :template tpl-node)))
9937 (js2-node-add-children pn tag-node tpl-node)
9938 pn))
9939
9940 (defun js2-parse-dot-query (pn)
9941 "Parse a dot-query expression, e.g. foo.bar.(@name == 2)
9942 Last token parsed must be `js2-DOTQUERY'."
9943 (let ((pos (js2-node-pos pn))
9944 op-pos expr end)
9945 (js2-must-have-xml)
9946 (js2-set-requires-activation)
9947 (setq op-pos (js2-current-token-beg)
9948 expr (js2-parse-expr)
9949 end (js2-node-end expr)
9950 pn (make-js2-xml-dot-query-node :left pn
9951 :pos pos
9952 :op-pos op-pos
9953 :right expr))
9954 (js2-node-add-children pn
9955 (js2-xml-dot-query-node-left pn)
9956 (js2-xml-dot-query-node-right pn))
9957 (if (js2-must-match js2-RP "msg.no.paren")
9958 (setf (js2-xml-dot-query-node-rp pn) (js2-current-token-beg)
9959 end (js2-current-token-end)))
9960 (setf (js2-node-len pn) (- end pos))
9961 pn))
9962
9963 (defun js2-parse-element-get (pn)
9964 "Parse an element-get expression, e.g. foo[bar].
9965 Last token parsed must be `js2-RB'."
9966 (let ((lb (js2-current-token-beg))
9967 (pos (js2-node-pos pn))
9968 rb expr)
9969 (setq expr (js2-parse-expr))
9970 (if (js2-must-match js2-RB "msg.no.bracket.index")
9971 (setq rb (js2-current-token-beg)))
9972 (setq pn (make-js2-elem-get-node :target pn
9973 :pos pos
9974 :element expr
9975 :lb (js2-relpos lb pos)
9976 :rb (js2-relpos rb pos)
9977 :len (- (js2-current-token-end) pos)))
9978 (js2-node-add-children pn
9979 (js2-elem-get-node-target pn)
9980 (js2-elem-get-node-element pn))
9981 pn))
9982
9983 (defun js2-highlight-function-call (token)
9984 (when (eq (js2-token-type token) js2-NAME)
9985 (js2-record-face 'js2-function-call token)))
9986
9987 (defun js2-parse-function-call (pn)
9988 (js2-highlight-function-call (js2-current-token))
9989 (js2-get-token)
9990 (let (args
9991 (pos (js2-node-pos pn)))
9992 (setq pn (make-js2-call-node :pos pos
9993 :target pn
9994 :lp (- (js2-current-token-beg) pos)))
9995 (js2-node-add-children pn (js2-call-node-target pn))
9996 ;; Add the arguments to pn, if any are supplied.
9997 (setf args (nreverse (js2-parse-argument-list))
9998 (js2-call-node-rp pn) (- (js2-current-token-beg) pos)
9999 (js2-call-node-args pn) args)
10000 (apply #'js2-node-add-children pn args)
10001 (setf (js2-node-len pn) (- js2-ts-cursor pos))
10002 pn))
10003
10004 (defun js2-parse-property-access (tt pn)
10005 "Parse a property access, XML descendants access, or XML attr access."
10006 (let ((member-type-flags 0)
10007 (dot-pos (js2-current-token-beg))
10008 (dot-len (if (= tt js2-DOTDOT) 2 1))
10009 name
10010 ref ; right side of . or .. operator
10011 result)
10012 (when (= tt js2-DOTDOT)
10013 (js2-must-have-xml)
10014 (setq member-type-flags js2-descendants-flag))
10015 (if (not js2-compiler-xml-available)
10016 (progn
10017 (js2-must-match-prop-name "msg.no.name.after.dot")
10018 (setq name (js2-create-name-node t js2-GETPROP)
10019 result (make-js2-prop-get-node :left pn
10020 :pos (js2-current-token-beg)
10021 :right name
10022 :len (js2-current-token-len)))
10023 (js2-node-add-children result pn name)
10024 result)
10025 ;; otherwise look for XML operators
10026 (setf result (if (= tt js2-DOT)
10027 (make-js2-prop-get-node)
10028 (make-js2-infix-node :type js2-DOTDOT))
10029 (js2-node-pos result) (js2-node-pos pn)
10030 (js2-infix-node-op-pos result) dot-pos
10031 (js2-infix-node-left result) pn ; do this after setting position
10032 tt (js2-get-prop-name-token))
10033 (cond
10034 ;; handles: name, ns::name, ns::*, ns::[expr]
10035 ((= tt js2-NAME)
10036 (setq ref (js2-parse-property-name -1 nil member-type-flags)))
10037 ;; handles: *, *::name, *::*, *::[expr]
10038 ((= tt js2-MUL)
10039 (setq ref (js2-parse-property-name nil "*" member-type-flags)))
10040 ;; handles: '@attr', '@ns::attr', '@ns::*', '@ns::[expr]', etc.
10041 ((= tt js2-XMLATTR)
10042 (setq result (js2-parse-attribute-access)))
10043 (t
10044 (js2-report-error "msg.no.name.after.dot" nil dot-pos dot-len)))
10045 (if ref
10046 (setf (js2-node-len result) (- (js2-node-end ref)
10047 (js2-node-pos result))
10048 (js2-infix-node-right result) ref))
10049 (if (js2-infix-node-p result)
10050 (js2-node-add-children result
10051 (js2-infix-node-left result)
10052 (js2-infix-node-right result)))
10053 result)))
10054
10055 (defun js2-parse-attribute-access ()
10056 "Parse an E4X XML attribute expression.
10057 This includes expressions of the forms:
10058
10059 @attr @ns::attr @ns::*
10060 @* @*::attr @*::*
10061 @[expr] @*::[expr] @ns::[expr]
10062
10063 Called if we peeked an '@' token."
10064 (let ((tt (js2-get-prop-name-token))
10065 (at-pos (js2-current-token-beg)))
10066 (cond
10067 ;; handles: @name, @ns::name, @ns::*, @ns::[expr]
10068 ((= tt js2-NAME)
10069 (js2-parse-property-name at-pos nil 0))
10070 ;; handles: @*, @*::name, @*::*, @*::[expr]
10071 ((= tt js2-MUL)
10072 (js2-parse-property-name (js2-current-token-beg) "*" 0))
10073 ;; handles @[expr]
10074 ((= tt js2-LB)
10075 (js2-parse-xml-elem-ref at-pos))
10076 (t
10077 (js2-report-error "msg.no.name.after.xmlAttr")
10078 ;; Avoid cascaded errors that happen if we make an error node here.
10079 (js2-parse-property-name (js2-current-token-beg) "" 0)))))
10080
10081 (defun js2-parse-property-name (at-pos s member-type-flags)
10082 "Check if :: follows name in which case it becomes qualified name.
10083
10084 AT-POS is a natural number if we just read an '@' token, else nil.
10085 S is the name or string that was matched: an identifier, 'throw' or '*'.
10086 MEMBER-TYPE-FLAGS is a bit set tracking whether we're a '.' or '..' child.
10087
10088 Returns a `js2-xml-ref-node' if it's an attribute access, a child of a '..'
10089 operator, or the name is followed by ::. For a plain name, returns a
10090 `js2-name-node'. Returns a `js2-error-node' for malformed XML expressions."
10091 (let ((pos (or at-pos (js2-current-token-beg)))
10092 colon-pos
10093 (name (js2-create-name-node t (js2-current-token-type) s))
10094 ns tt pn)
10095 (catch 'return
10096 (when (js2-match-token js2-COLONCOLON)
10097 (setq ns name
10098 colon-pos (js2-current-token-beg)
10099 tt (js2-get-prop-name-token))
10100 (cond
10101 ;; handles name::name
10102 ((= tt js2-NAME)
10103 (setq name (js2-create-name-node)))
10104 ;; handles name::*
10105 ((= tt js2-MUL)
10106 (setq name (js2-create-name-node nil nil "*")))
10107 ;; handles name::[expr]
10108 ((= tt js2-LB)
10109 (throw 'return (js2-parse-xml-elem-ref at-pos ns colon-pos)))
10110 (t
10111 (js2-report-error "msg.no.name.after.coloncolon"))))
10112 (if (and (null ns) (zerop member-type-flags))
10113 name
10114 (prog1
10115 (setq pn
10116 (make-js2-xml-prop-ref-node :pos pos
10117 :len (- (js2-node-end name) pos)
10118 :at-pos at-pos
10119 :colon-pos colon-pos
10120 :propname name))
10121 (js2-node-add-children pn name))))))
10122
10123 (defun js2-parse-xml-elem-ref (at-pos &optional namespace colon-pos)
10124 "Parse the [expr] portion of an xml element reference.
10125 For instance, @[expr], @*::[expr], or ns::[expr]."
10126 (let* ((lb (js2-current-token-beg))
10127 (pos (or at-pos lb))
10128 rb
10129 (expr (js2-parse-expr))
10130 (end (js2-node-end expr))
10131 pn)
10132 (if (js2-must-match js2-RB "msg.no.bracket.index")
10133 (setq rb (js2-current-token-beg)
10134 end (js2-current-token-end)))
10135 (prog1
10136 (setq pn
10137 (make-js2-xml-elem-ref-node :pos pos
10138 :len (- end pos)
10139 :namespace namespace
10140 :colon-pos colon-pos
10141 :at-pos at-pos
10142 :expr expr
10143 :lb (js2-relpos lb pos)
10144 :rb (js2-relpos rb pos)))
10145 (js2-node-add-children pn namespace expr))))
10146
10147 (defun js2-parse-destruct-primary-expr ()
10148 (let ((js2-is-in-destructuring t))
10149 (js2-parse-primary-expr)))
10150
10151 (defun js2-parse-primary-expr ()
10152 "Parse a literal (leaf) expression of some sort.
10153 Includes complex literals such as functions, object-literals,
10154 array-literals, array comprehensions and regular expressions."
10155 (let (tt)
10156 (setq tt (js2-current-token-type))
10157 (cond
10158 ((= tt js2-CLASS)
10159 (js2-parse-class-expr))
10160 ((= tt js2-FUNCTION)
10161 (js2-parse-function-expr))
10162 ((= tt js2-LB)
10163 (js2-parse-array-comp-or-literal))
10164 ((= tt js2-LC)
10165 (js2-parse-object-literal))
10166 ((= tt js2-LET)
10167 (js2-parse-let (js2-current-token-beg)))
10168 ((= tt js2-LP)
10169 (js2-parse-paren-expr-or-generator-comp))
10170 ((= tt js2-XMLATTR)
10171 (js2-must-have-xml)
10172 (js2-parse-attribute-access))
10173 ((= tt js2-NAME)
10174 (js2-parse-name tt))
10175 ((= tt js2-NUMBER)
10176 (make-js2-number-node))
10177 ((or (= tt js2-STRING) (= tt js2-NO_SUBS_TEMPLATE))
10178 (make-js2-string-node :type tt))
10179 ((= tt js2-TEMPLATE_HEAD)
10180 (js2-parse-template-literal))
10181 ((or (= tt js2-DIV) (= tt js2-ASSIGN_DIV))
10182 ;; Got / or /= which in this context means a regexp literal
10183 (let ((px-pos (js2-current-token-beg))
10184 (flags (js2-read-regexp tt))
10185 (end (js2-current-token-end)))
10186 (prog1
10187 (make-js2-regexp-node :pos px-pos
10188 :len (- end px-pos)
10189 :value (js2-current-token-string)
10190 :flags flags)
10191 (js2-set-face px-pos end 'font-lock-string-face 'record)
10192 (js2-record-text-property px-pos end 'syntax-table '(2)))))
10193 ((or (= tt js2-NULL)
10194 (= tt js2-THIS)
10195 (= tt js2-SUPER)
10196 (= tt js2-FALSE)
10197 (= tt js2-TRUE))
10198 (make-js2-keyword-node :type tt))
10199 ((= tt js2-TRIPLEDOT)
10200 ;; Likewise, only valid in an arrow function with a rest param.
10201 (if (and (js2-match-token js2-NAME)
10202 (js2-match-token js2-RP)
10203 (eq (js2-peek-token) js2-ARROW))
10204 (progn
10205 (js2-unget-token) ; Put back the right paren.
10206 ;; See the previous case.
10207 (make-js2-keyword-node :type js2-NULL))
10208 (js2-report-error "msg.syntax")
10209 (make-js2-error-node)))
10210 ((= tt js2-RESERVED)
10211 (js2-report-error "msg.reserved.id")
10212 (make-js2-name-node))
10213 ((= tt js2-ERROR)
10214 ;; the scanner or one of its subroutines reported the error.
10215 (make-js2-error-node))
10216 ((= tt js2-EOF)
10217 (let* ((px-pos (point-at-bol))
10218 (len (- js2-ts-cursor px-pos)))
10219 (js2-report-error "msg.unexpected.eof" nil px-pos len))
10220 (make-js2-error-node :pos (1- js2-ts-cursor)))
10221 (t
10222 (js2-report-error "msg.syntax")
10223 (make-js2-error-node)))))
10224
10225 (defun js2-parse-template-literal ()
10226 (let ((beg (js2-current-token-beg))
10227 (kids (list (make-js2-string-node :type js2-TEMPLATE_HEAD)))
10228 (tt js2-TEMPLATE_HEAD))
10229 (while (eq tt js2-TEMPLATE_HEAD)
10230 (push (js2-parse-expr) kids)
10231 (js2-must-match js2-RC "msg.syntax")
10232 (setq tt (js2-get-token 'TEMPLATE_TAIL))
10233 (push (make-js2-string-node :type tt) kids))
10234 (setq kids (nreverse kids))
10235 (let ((tpl (make-js2-template-node :beg beg
10236 :len (- (js2-current-token-end) beg)
10237 :kids kids)))
10238 (apply #'js2-node-add-children tpl kids)
10239 tpl)))
10240
10241 (defun js2-parse-name (_tt)
10242 (let ((name (js2-current-token-string))
10243 node)
10244 (setq node (if js2-compiler-xml-available
10245 (js2-parse-property-name nil name 0)
10246 (js2-create-name-node 'check-activation nil name)))
10247 (if js2-highlight-external-variables
10248 (js2-record-name-node node))
10249 node))
10250
10251 (defun js2-parse-warn-trailing-comma (msg pos elems comma-pos)
10252 (js2-add-strict-warning
10253 msg nil
10254 ;; back up from comma to beginning of line or array/objlit
10255 (max (if elems
10256 (js2-node-pos (car elems))
10257 pos)
10258 (save-excursion
10259 (goto-char comma-pos)
10260 (back-to-indentation)
10261 (point)))
10262 comma-pos))
10263
10264 (defun js2-parse-array-comp-or-literal ()
10265 (let ((pos (js2-current-token-beg)))
10266 (if (and (>= js2-language-version 200)
10267 (js2-match-token js2-FOR))
10268 (js2-parse-array-comp pos)
10269 (js2-parse-array-literal pos))))
10270
10271 (defun js2-parse-array-literal (pos)
10272 (let ((after-lb-or-comma t)
10273 after-comma tt elems pn
10274 (continue t))
10275 (unless js2-is-in-destructuring
10276 (js2-push-scope (make-js2-scope))) ; for the legacy array comp
10277 (while continue
10278 (setq tt (js2-get-token))
10279 (cond
10280 ;; comma
10281 ((= tt js2-COMMA)
10282 (setq after-comma (js2-current-token-end))
10283 (if (not after-lb-or-comma)
10284 (setq after-lb-or-comma t)
10285 (push nil elems)))
10286 ;; end of array
10287 ((or (= tt js2-RB)
10288 (= tt js2-EOF)) ; prevent infinite loop
10289 (if (= tt js2-EOF)
10290 (js2-report-error "msg.no.bracket.arg" nil pos))
10291 (when (and after-comma (< js2-language-version 170))
10292 (js2-parse-warn-trailing-comma "msg.array.trailing.comma"
10293 pos (remove nil elems) after-comma))
10294 (setq continue nil
10295 pn (make-js2-array-node :pos pos
10296 :len (- js2-ts-cursor pos)
10297 :elems (nreverse elems)))
10298 (apply #'js2-node-add-children pn (js2-array-node-elems pn)))
10299 ;; destructuring binding
10300 (js2-is-in-destructuring
10301 (push (if (or (= tt js2-LC)
10302 (= tt js2-LB)
10303 (= tt js2-NAME))
10304 ;; [a, b, c] | {a, b, c} | {a:x, b:y, c:z} | a
10305 (js2-parse-destruct-primary-expr)
10306 ;; invalid pattern
10307 (js2-report-error "msg.bad.var")
10308 (make-js2-error-node))
10309 elems)
10310 (setq after-lb-or-comma nil
10311 after-comma nil))
10312 ;; array comp
10313 ((and (>= js2-language-version 170)
10314 (= tt js2-FOR) ; check for array comprehension
10315 (not after-lb-or-comma) ; "for" can't follow a comma
10316 elems ; must have at least 1 element
10317 (not (cdr elems))) ; but no 2nd element
10318 (js2-unget-token)
10319 (setf continue nil
10320 pn (js2-parse-legacy-array-comp (car elems) pos)))
10321 ;; another element
10322 (t
10323 (unless after-lb-or-comma
10324 (js2-report-error "msg.no.bracket.arg"))
10325 (if (and (= tt js2-TRIPLEDOT)
10326 (>= js2-language-version 200))
10327 ;; spread operator
10328 (push (js2-make-unary tt 'js2-parse-assign-expr)
10329 elems)
10330 (js2-unget-token)
10331 (push (js2-parse-assign-expr) elems))
10332 (setq after-lb-or-comma nil
10333 after-comma nil))))
10334 (unless js2-is-in-destructuring
10335 (js2-pop-scope))
10336 pn))
10337
10338 (defun js2-parse-legacy-array-comp (expr pos)
10339 "Parse a legacy array comprehension (JavaScript 1.7).
10340 EXPR is the first expression after the opening left-bracket.
10341 POS is the beginning of the LB token preceding EXPR.
10342 We should have just parsed the 'for' keyword before calling this function."
10343 (let ((current-scope js2-current-scope)
10344 loops first filter result)
10345 (unwind-protect
10346 (progn
10347 (while (js2-match-token js2-FOR)
10348 (let ((loop (make-js2-comp-loop-node)))
10349 (js2-push-scope loop)
10350 (push loop loops)
10351 (js2-parse-comp-loop loop)))
10352 ;; First loop takes expr scope's parent.
10353 (setf (js2-scope-parent-scope (setq first (car (last loops))))
10354 (js2-scope-parent-scope current-scope))
10355 ;; Set expr scope's parent to the last loop.
10356 (setf (js2-scope-parent-scope current-scope) (car loops))
10357 (if (/= (js2-get-token) js2-IF)
10358 (js2-unget-token)
10359 (setq filter (js2-parse-condition))))
10360 (dotimes (_ (1- (length loops)))
10361 (js2-pop-scope)))
10362 (js2-must-match js2-RB "msg.no.bracket.arg" pos)
10363 (setq result (make-js2-comp-node :pos pos
10364 :len (- js2-ts-cursor pos)
10365 :result expr
10366 :loops (nreverse loops)
10367 :filters (and filter (list (car filter)))
10368 :form 'LEGACY_ARRAY))
10369 ;; Set comp loop's parent to the last loop.
10370 ;; TODO: Get rid of the bogus expr scope.
10371 (setf (js2-scope-parent-scope result) first)
10372 (apply #'js2-node-add-children result expr (car filter)
10373 (js2-comp-node-loops result))
10374 result))
10375
10376 (defun js2-parse-array-comp (pos)
10377 "Parse an ES6 array comprehension.
10378 POS is the beginning of the LB token.
10379 We should have just parsed the 'for' keyword before calling this function."
10380 (let ((pn (js2-parse-comprehension pos 'ARRAY)))
10381 (js2-must-match js2-RB "msg.no.bracket.arg" pos)
10382 pn))
10383
10384 (defun js2-parse-generator-comp (pos)
10385 (let* ((js2-nesting-of-function (1+ js2-nesting-of-function))
10386 (js2-current-script-or-fn
10387 (make-js2-function-node :generator-type 'COMPREHENSION))
10388 (pn (js2-parse-comprehension pos 'STAR_GENERATOR)))
10389 (js2-must-match js2-RP "msg.no.paren" pos)
10390 pn))
10391
10392 (defun js2-parse-comprehension (pos form)
10393 (let (loops filters expr result last)
10394 (unwind-protect
10395 (progn
10396 (js2-unget-token)
10397 (while (js2-match-token js2-FOR)
10398 (let ((loop (make-js2-comp-loop-node)))
10399 (js2-push-scope loop)
10400 (push loop loops)
10401 (js2-parse-comp-loop loop)))
10402 (while (js2-match-token js2-IF)
10403 (push (car (js2-parse-condition)) filters))
10404 (setq expr (js2-parse-assign-expr))
10405 (setq last (car loops)))
10406 (dolist (_ loops)
10407 (js2-pop-scope)))
10408 (setq result (make-js2-comp-node :pos pos
10409 :len (- js2-ts-cursor pos)
10410 :result expr
10411 :loops (nreverse loops)
10412 :filters (nreverse filters)
10413 :form form))
10414 (apply #'js2-node-add-children result (js2-comp-node-loops result))
10415 (apply #'js2-node-add-children result expr (js2-comp-node-filters result))
10416 (setf (js2-scope-parent-scope result) last)
10417 result))
10418
10419 (defun js2-parse-comp-loop (pn &optional only-of-p)
10420 "Parse a 'for [each] (foo [in|of] bar)' expression in an Array comprehension.
10421 The current token should be the initial FOR.
10422 If ONLY-OF-P is non-nil, only the 'for (foo of bar)' form is allowed."
10423 (let ((pos (js2-comp-loop-node-pos pn))
10424 tt iter obj foreach-p forof-p in-pos each-pos lp rp)
10425 (when (and (not only-of-p) (js2-match-token js2-NAME))
10426 (if (string= (js2-current-token-string) "each")
10427 (progn
10428 (setq foreach-p t
10429 each-pos (- (js2-current-token-beg) pos)) ; relative
10430 (js2-record-face 'font-lock-keyword-face))
10431 (js2-report-error "msg.no.paren.for")))
10432 (if (js2-must-match js2-LP "msg.no.paren.for")
10433 (setq lp (- (js2-current-token-beg) pos)))
10434 (setq tt (js2-peek-token))
10435 (cond
10436 ((or (= tt js2-LB)
10437 (= tt js2-LC))
10438 (js2-get-token)
10439 (setq iter (js2-parse-destruct-primary-expr))
10440 (js2-define-destruct-symbols iter js2-LET
10441 'font-lock-variable-name-face t))
10442 ((js2-match-token js2-NAME)
10443 (setq iter (js2-create-name-node)))
10444 (t
10445 (js2-report-error "msg.bad.var")))
10446 ;; Define as a let since we want the scope of the variable to
10447 ;; be restricted to the array comprehension
10448 (if (js2-name-node-p iter)
10449 (js2-define-symbol js2-LET (js2-name-node-name iter) pn t))
10450 (if (or (and (not only-of-p) (js2-match-token js2-IN))
10451 (and (>= js2-language-version 200)
10452 (js2-match-contextual-kwd "of")
10453 (setq forof-p t)))
10454 (setq in-pos (- (js2-current-token-beg) pos))
10455 (js2-report-error "msg.in.after.for.name"))
10456 (setq obj (js2-parse-expr))
10457 (if (js2-must-match js2-RP "msg.no.paren.for.ctrl")
10458 (setq rp (- (js2-current-token-beg) pos)))
10459 (setf (js2-node-pos pn) pos
10460 (js2-node-len pn) (- js2-ts-cursor pos)
10461 (js2-comp-loop-node-iterator pn) iter
10462 (js2-comp-loop-node-object pn) obj
10463 (js2-comp-loop-node-in-pos pn) in-pos
10464 (js2-comp-loop-node-each-pos pn) each-pos
10465 (js2-comp-loop-node-foreach-p pn) foreach-p
10466 (js2-comp-loop-node-forof-p pn) forof-p
10467 (js2-comp-loop-node-lp pn) lp
10468 (js2-comp-loop-node-rp pn) rp)
10469 (js2-node-add-children pn iter obj)
10470 pn))
10471
10472 (defun js2-parse-class-stmt ()
10473 (let ((pos (js2-current-token-beg))
10474 (_ (js2-must-match-name "msg.unnamed.class.stmt"))
10475 (name (js2-create-name-node t)))
10476 (js2-set-face (js2-node-pos name) (js2-node-end name)
10477 'font-lock-function-name-face 'record)
10478 (let ((node (js2-parse-class pos 'CLASS_STATEMENT name)))
10479 (js2-define-symbol js2-FUNCTION
10480 (js2-name-node-name name)
10481 node)
10482 node)))
10483
10484 (defun js2-parse-class-expr ()
10485 (let ((pos (js2-current-token-beg))
10486 name)
10487 (when (js2-match-token js2-NAME)
10488 (setq name (js2-create-name-node t)))
10489 (js2-parse-class pos 'CLASS_EXPRESSION name)))
10490
10491 (defun js2-parse-class (pos form name)
10492 ;; class X [extends ...] {
10493 (let (pn elems extends)
10494 (if (js2-match-token js2-EXTENDS)
10495 (if (= (js2-peek-token) js2-LC)
10496 (js2-report-error "msg.missing.extends")
10497 ;; TODO(sdh): this should be left-hand-side-expr, not assign-expr
10498 (setq extends (js2-parse-assign-expr))
10499 (if (not extends)
10500 (js2-report-error "msg.bad.extends"))))
10501 (js2-must-match js2-LC "msg.no.brace.class")
10502 (setq elems (js2-parse-object-literal-elems t)
10503 pn (make-js2-class-node :pos pos
10504 :len (- js2-ts-cursor pos)
10505 :form form
10506 :name name
10507 :extends extends
10508 :elems elems))
10509 (apply #'js2-node-add-children pn (js2-class-node-elems pn))
10510 pn))
10511
10512 (defun js2-parse-object-literal ()
10513 (let* ((pos (js2-current-token-beg))
10514 (elems (js2-parse-object-literal-elems))
10515 (result (make-js2-object-node :pos pos
10516 :len (- js2-ts-cursor pos)
10517 :elems elems)))
10518 (apply #'js2-node-add-children result (js2-object-node-elems result))
10519 result))
10520
10521 (defun js2-parse-object-literal-elems (&optional class-p)
10522 (let ((pos (js2-current-token-beg))
10523 (static nil)
10524 (continue t)
10525 tt elems elem after-comma previous-token)
10526 (while continue
10527 (setq tt (js2-get-prop-name-token)
10528 static nil
10529 elem nil
10530 previous-token nil)
10531 ;; Handle 'static' keyword only if we're in a class
10532 (when (and class-p (= js2-NAME tt)
10533 (string= "static" (js2-current-token-string)))
10534 (js2-record-face 'font-lock-keyword-face)
10535 (setq static t
10536 tt (js2-get-prop-name-token)))
10537 ;; Handle generator * before the property name for in-line functions
10538 (when (and (>= js2-language-version 200)
10539 (= js2-MUL tt))
10540 (setq previous-token (js2-current-token)
10541 tt (js2-get-prop-name-token)))
10542 ;; Handle 'get' or 'set' keywords
10543 (let ((prop (js2-current-token-string)))
10544 (when (and (>= js2-language-version 200)
10545 (= js2-NAME tt)
10546 (or (string= prop "get")
10547 (string= prop "set"))
10548 (member (js2-peek-token)
10549 (list js2-NAME js2-STRING js2-NUMBER js2-LB)))
10550 (setq previous-token (js2-current-token)
10551 tt (js2-get-prop-name-token))))
10552 (cond
10553 ;; Found a property (of any sort)
10554 ((member tt (list js2-NAME js2-STRING js2-NUMBER js2-LB))
10555 (setq after-comma nil
10556 elem (js2-parse-named-prop tt pos previous-token))
10557 (if (and (null elem)
10558 (not js2-recover-from-parse-errors))
10559 (setq continue nil)))
10560 ;; Break out of loop, and handle trailing commas.
10561 ((or (= tt js2-RC)
10562 (= tt js2-EOF))
10563 (js2-unget-token)
10564 (setq continue nil)
10565 (if after-comma
10566 (js2-parse-warn-trailing-comma "msg.extra.trailing.comma"
10567 pos elems after-comma)))
10568 (t
10569 (js2-report-error "msg.bad.prop")
10570 (unless js2-recover-from-parse-errors
10571 (setq continue nil)))) ; end switch
10572 ;; Handle static for classes' codegen.
10573 (if static
10574 (if elem (js2-node-set-prop elem 'STATIC t)
10575 (js2-report-error "msg.unexpected.static")))
10576 ;; Handle commas, depending on class-p.
10577 (let ((tok (js2-get-prop-name-token)))
10578 (if (eq tok js2-COMMA)
10579 (if class-p
10580 (js2-report-error "msg.class.unexpected.comma")
10581 (setq after-comma (js2-current-token-end)))
10582 (js2-unget-token)
10583 (unless class-p (setq continue nil))))
10584 ;; Append any parsed element.
10585 (if elem (push elem elems))) ; end loop
10586 (js2-must-match js2-RC "msg.no.brace.prop")
10587 (nreverse elems)))
10588
10589 (defun js2-parse-named-prop (tt pos previous-token)
10590 "Parse a name, string, or getter/setter object property.
10591 When `js2-is-in-destructuring' is t, forms like {a, b, c} will be permitted."
10592 (let ((key (cond
10593 ;; Literal string keys: {'foo': 'bar'}
10594 ((= tt js2-STRING)
10595 (make-js2-string-node))
10596 ;; Handle computed keys: {[Symbol.iterator]: ...}, *[1+2]() {...}},
10597 ;; {[foo + bar]() { ... }}, {[get ['x' + 1]() {...}}
10598 ((and (= tt js2-LB)
10599 (>= js2-language-version 200))
10600 (prog1 (js2-parse-expr)
10601 (js2-must-match js2-RB "msg.missing.computed.rb")))
10602 ;; Numeric keys: {12: 'foo'}, {10.7: 'bar'}
10603 ((= tt js2-NUMBER)
10604 (make-js2-number-node))
10605 ;; Unquoted names: {foo: 12}
10606 ((= tt js2-NAME)
10607 (js2-create-name-node))
10608 ;; Anything else is an error
10609 (t (js2-report-error "msg.bad.prop"))))
10610 (prop (and previous-token (js2-token-string previous-token)))
10611 (property-type (when previous-token
10612 (if (= (js2-token-type previous-token) js2-MUL)
10613 "*"
10614 (js2-token-string previous-token)))))
10615 (when (or (string= prop "get")
10616 (string= prop "set"))
10617 (js2-set-face (js2-token-beg previous-token)
10618 (js2-token-end previous-token)
10619 'font-lock-keyword-face 'record)) ; get/set
10620 (cond
10621 ;; method definition: {f() {...}}
10622 ((and (= (js2-peek-token) js2-LP)
10623 (>= js2-language-version 200))
10624 (when (js2-name-node-p key) ; highlight function name properties
10625 (js2-record-face 'font-lock-function-name-face))
10626 (js2-parse-getter-setter-prop pos key property-type))
10627 ;; regular prop
10628 (t
10629 (let ((beg (js2-current-token-beg))
10630 (end (js2-current-token-end))
10631 (expr (js2-parse-plain-property key)))
10632 (when (and (= tt js2-NAME)
10633 (not js2-is-in-destructuring)
10634 js2-highlight-external-variables
10635 (js2-node-get-prop expr 'SHORTHAND))
10636 (js2-record-name-node key))
10637 (js2-set-face beg end
10638 (if (js2-function-node-p
10639 (js2-object-prop-node-right expr))
10640 'font-lock-function-name-face
10641 'font-lock-variable-name-face)
10642 'record)
10643 expr)))))
10644
10645 (defun js2-parse-plain-property (prop)
10646 "Parse a non-getter/setter property in an object literal.
10647 PROP is the node representing the property: a number, name or string."
10648 (let* ((tt (js2-get-token))
10649 (pos (js2-node-pos prop))
10650 colon expr result)
10651 (cond
10652 ;; Abbreviated property, as in {foo, bar}
10653 ((and (>= js2-language-version 200)
10654 (or (= tt js2-COMMA)
10655 (= tt js2-RC))
10656 (not (js2-number-node-p prop)))
10657 (js2-unget-token)
10658 (setq result (make-js2-object-prop-node
10659 :pos pos
10660 :left prop
10661 :right prop
10662 :op-pos (js2-current-token-len)))
10663 (js2-node-add-children result prop)
10664 (js2-node-set-prop result 'SHORTHAND t)
10665 result)
10666 ;; Normal property
10667 (t
10668 (if (= tt js2-COLON)
10669 (setq colon (- (js2-current-token-beg) pos)
10670 expr (js2-parse-assign-expr))
10671 (js2-report-error "msg.no.colon.prop")
10672 (setq expr (make-js2-error-node)))
10673 (setq result (make-js2-object-prop-node
10674 :pos pos
10675 ;; don't include last consumed token in length
10676 :len (- (+ (js2-node-pos expr)
10677 (js2-node-len expr))
10678 pos)
10679 :left prop
10680 :right expr
10681 :op-pos colon))
10682 (js2-node-add-children result prop expr)
10683 result))))
10684
10685 (defun js2-parse-getter-setter-prop (pos prop type-string)
10686 "Parse getter or setter property in an object literal.
10687 JavaScript syntax is:
10688
10689 { get foo() {...}, set foo(x) {...} }
10690
10691 and expression closure style is also supported
10692
10693 { get foo() x, set foo(x) _x = x }
10694
10695 POS is the start position of the `get' or `set' keyword.
10696 PROP is the `js2-name-node' representing the property name.
10697 TYPE-STRING is a string `get', `set', `*', or nil, indicating a found keyword."
10698 (let ((type (cond
10699 ((string= "get" type-string) js2-GET)
10700 ((string= "set" type-string) js2-SET)
10701 (t js2-FUNCTION)))
10702 result end
10703 (fn (js2-parse-function-expr)))
10704 ;; it has to be an anonymous function, as we already parsed the name
10705 (if (/= (js2-node-type fn) js2-FUNCTION)
10706 (js2-report-error "msg.bad.prop")
10707 (if (cl-plusp (length (js2-function-name fn)))
10708 (js2-report-error "msg.bad.prop")))
10709 (js2-node-set-prop fn 'GETTER_SETTER type) ; for codegen
10710 (when (string= type-string "*")
10711 (setf (js2-function-node-generator-type fn) 'STAR))
10712 (setq end (js2-node-end fn)
10713 result (make-js2-getter-setter-node :type type
10714 :pos pos
10715 :len (- end pos)
10716 :left prop
10717 :right fn))
10718 (js2-node-add-children result prop fn)
10719 result))
10720
10721 (defun js2-create-name-node (&optional check-activation-p token string)
10722 "Create a name node using the current token and, optionally, STRING.
10723 And, if CHECK-ACTIVATION-P is non-nil, use the value of TOKEN."
10724 (let* ((beg (js2-current-token-beg))
10725 (tt (js2-current-token-type))
10726 (s (or string
10727 (if (= js2-NAME tt)
10728 (js2-current-token-string)
10729 (js2-tt-name tt))))
10730 name)
10731 (setq name (make-js2-name-node :pos beg
10732 :name s
10733 :len (length s)))
10734 (if check-activation-p
10735 (js2-check-activation-name s (or token js2-NAME)))
10736 name))
10737
10738 ;;; Indentation support
10739
10740 ;; This indenter is based on Karl Landström's "javascript.el" indenter.
10741 ;; Karl cleverly deduces that the desired indentation level is often a
10742 ;; function of paren/bracket/brace nesting depth, which can be determined
10743 ;; quickly via the built-in `parse-partial-sexp' function. His indenter
10744 ;; then does some equally clever checks to see if we're in the context of a
10745 ;; substatement of a possibly braceless statement keyword such as if, while,
10746 ;; or finally. This approach yields pretty good results.
10747
10748 ;; The indenter is often "wrong", however, and needs to be overridden.
10749 ;; The right long-term solution is probably to emulate (or integrate
10750 ;; with) cc-engine, but it's a nontrivial amount of coding. Even when a
10751 ;; parse tree from `js2-parse' is present, which is not true at the
10752 ;; moment the user is typing, computing indentation is still thousands
10753 ;; of lines of code to handle every possible syntactic edge case.
10754
10755 ;; In the meantime, the compromise solution is that we offer a "bounce
10756 ;; indenter", configured with `js2-bounce-indent-p', which cycles the
10757 ;; current line indent among various likely guess points. This approach
10758 ;; is far from perfect, but should at least make it slightly easier to
10759 ;; move the line towards its desired indentation when manually
10760 ;; overriding Karl's heuristic nesting guesser.
10761
10762 ;; I've made miscellaneous tweaks to Karl's code to handle some Ecma
10763 ;; extensions such as `let' and Array comprehensions. Major kudos to
10764 ;; Karl for coming up with the initial approach, which packs a lot of
10765 ;; punch for so little code.
10766
10767 (defconst js2-possibly-braceless-keywords-re
10768 (concat "else[ \t]+if\\|for[ \t]+each\\|"
10769 (regexp-opt '("catch" "do" "else" "finally" "for" "if"
10770 "try" "while" "with" "let")))
10771 "Regular expression matching keywords that are optionally
10772 followed by an opening brace.")
10773
10774 (defconst js2-indent-operator-re
10775 (concat "[-+*/%<>&^|?:.]\\([^-+*/]\\|$\\)\\|!?=\\|"
10776 (regexp-opt '("in" "instanceof") 'words))
10777 "Regular expression matching operators that affect indentation
10778 of continued expressions.")
10779
10780 (defconst js2-declaration-keyword-re
10781 (regexp-opt '("var" "let" "const") 'words)
10782 "Regular expression matching variable declaration keywords.")
10783
10784 (defun js2-re-search-forward-inner (regexp &optional bound count)
10785 "Auxiliary function for `js2-re-search-forward'."
10786 (let (parse saved-point)
10787 (while (> count 0)
10788 (re-search-forward regexp bound)
10789 (setq parse (if saved-point
10790 (parse-partial-sexp saved-point (point))
10791 (syntax-ppss (point))))
10792 (cond ((nth 3 parse)
10793 (re-search-forward
10794 (concat "\\(\\=\\|[^\\]\\|^\\)" (string (nth 3 parse)))
10795 (save-excursion (end-of-line) (point)) t))
10796 ((nth 7 parse)
10797 (forward-line))
10798 ((or (nth 4 parse)
10799 (and (eq (char-before) ?\/) (eq (char-after) ?\*)))
10800 (re-search-forward "\\*/"))
10801 (t
10802 (setq count (1- count))))
10803 (setq saved-point (point))))
10804 (point))
10805
10806 (defun js2-re-search-forward (regexp &optional bound noerror count)
10807 "Search forward but ignore strings and comments.
10808 Invokes `re-search-forward' but treats the buffer as if strings
10809 and comments have been removed."
10810 (let ((saved-point (point)))
10811 (condition-case err
10812 (cond ((null count)
10813 (js2-re-search-forward-inner regexp bound 1))
10814 ((< count 0)
10815 (js2-re-search-backward-inner regexp bound (- count)))
10816 ((> count 0)
10817 (js2-re-search-forward-inner regexp bound count)))
10818 (search-failed
10819 (goto-char saved-point)
10820 (unless noerror
10821 (error (error-message-string err)))))))
10822
10823 (defun js2-re-search-backward-inner (regexp &optional bound count)
10824 "Auxiliary function for `js2-re-search-backward'."
10825 (let (parse)
10826 (while (> count 0)
10827 (re-search-backward regexp bound)
10828 (setq parse (syntax-ppss (point)))
10829 (cond ((nth 3 parse)
10830 (re-search-backward
10831 (concat "\\([^\\]\\|^\\)" (string (nth 3 parse)))
10832 (line-beginning-position) t))
10833 ((nth 7 parse)
10834 (goto-char (nth 8 parse)))
10835 ((or (nth 4 parse)
10836 (and (eq (char-before) ?/) (eq (char-after) ?*)))
10837 (re-search-backward "/\\*"))
10838 (t
10839 (setq count (1- count))))))
10840 (point))
10841
10842 (defun js2-re-search-backward (regexp &optional bound noerror count)
10843 "Search backward but ignore strings and comments.
10844 Invokes `re-search-backward' but treats the buffer as if strings
10845 and comments have been removed."
10846 (let ((saved-point (point)))
10847 (condition-case err
10848 (cond ((null count)
10849 (js2-re-search-backward-inner regexp bound 1))
10850 ((< count 0)
10851 (js2-re-search-forward-inner regexp bound (- count)))
10852 ((> count 0)
10853 (js2-re-search-backward-inner regexp bound count)))
10854 (search-failed
10855 (goto-char saved-point)
10856 (unless noerror
10857 (error (error-message-string err)))))))
10858
10859 (defun js2-looking-at-operator-p ()
10860 "Return non-nil if text after point is a non-comma operator."
10861 (and (looking-at js2-indent-operator-re)
10862 (or (not (looking-at ":"))
10863 (save-excursion
10864 (and (js2-re-search-backward "[?:{]\\|\\<case\\>" nil t)
10865 (looking-at "?"))))))
10866
10867 (defun js2-continued-expression-p ()
10868 "Return non-nil if the current line continues an expression."
10869 (save-excursion
10870 (back-to-indentation)
10871 (or (js2-looking-at-operator-p)
10872 (when (catch 'found
10873 (while (and (re-search-backward "\n" nil t)
10874 (let ((state (syntax-ppss)))
10875 (when (nth 4 state)
10876 (goto-char (nth 8 state))) ;; skip comments
10877 (skip-chars-backward " \t")
10878 (if (bolp)
10879 t
10880 (throw 'found t))))))
10881 (backward-char)
10882 (when (js2-looking-at-operator-p)
10883 (backward-char)
10884 (not (looking-at "\\*\\|\\+\\+\\|--\\|/[/*]")))))))
10885
10886 (defun js2-end-of-do-while-loop-p ()
10887 "Return non-nil if word after point is `while' of a do-while
10888 statement, else returns nil. A braceless do-while statement
10889 spanning several lines requires that the start of the loop is
10890 indented to the same column as the current line."
10891 (interactive)
10892 (save-excursion
10893 (when (looking-at "\\s-*\\<while\\>")
10894 (if (save-excursion
10895 (skip-chars-backward "[ \t\n]*}")
10896 (looking-at "[ \t\n]*}"))
10897 (save-excursion
10898 (backward-list) (backward-word 1) (looking-at "\\<do\\>"))
10899 (js2-re-search-backward "\\<do\\>" (point-at-bol) t)
10900 (or (looking-at "\\<do\\>")
10901 (let ((saved-indent (current-indentation)))
10902 (while (and (js2-re-search-backward "^[ \t]*\\<" nil t)
10903 (/= (current-indentation) saved-indent)))
10904 (and (looking-at "[ \t]*\\<do\\>")
10905 (not (js2-re-search-forward
10906 "\\<while\\>" (point-at-eol) t))
10907 (= (current-indentation) saved-indent))))))))
10908
10909 (defun js2-multiline-decl-indentation ()
10910 "Return the declaration indentation column if the current line belongs
10911 to a multiline declaration statement. See `js2-pretty-multiline-declarations'."
10912 (let (forward-sexp-function ; use Lisp version
10913 at-opening-bracket)
10914 (save-excursion
10915 (back-to-indentation)
10916 (when (not (looking-at js2-declaration-keyword-re))
10917 (when (looking-at js2-indent-operator-re)
10918 (goto-char (match-end 0))) ; continued expressions are ok
10919 (while (and (not at-opening-bracket)
10920 (not (bobp))
10921 (let ((pos (point)))
10922 (save-excursion
10923 (js2-backward-sws)
10924 (or (eq (char-before) ?,)
10925 (and (not (eq (char-before) ?\;))
10926 (prog2 (skip-syntax-backward ".")
10927 (looking-at js2-indent-operator-re)
10928 (js2-backward-sws))
10929 (not (eq (char-before) ?\;)))
10930 (js2-same-line pos)))))
10931 (condition-case _
10932 (backward-sexp)
10933 (scan-error (setq at-opening-bracket t))))
10934 (when (looking-at js2-declaration-keyword-re)
10935 (goto-char (match-end 0))
10936 (1+ (current-column)))))))
10937
10938 (defun js2-ctrl-statement-indentation ()
10939 "Return the proper indentation of current line if it is a control statement.
10940 Returns an indentation if this line starts the body of a control
10941 statement without braces, else returns nil."
10942 (let (forward-sexp-function)
10943 (save-excursion
10944 (back-to-indentation)
10945 (when (and (not (js2-same-line (point-min)))
10946 (not (looking-at "{"))
10947 (js2-re-search-backward "[[:graph:]]" nil t)
10948 (not (looking-at "[{([]"))
10949 (progn
10950 (forward-char)
10951 (when (= (char-before) ?\))
10952 ;; scan-sexps sometimes throws an error
10953 (ignore-errors (backward-sexp))
10954 (skip-chars-backward " \t" (point-at-bol)))
10955 (let ((pt (point)))
10956 (back-to-indentation)
10957 (when (looking-at "}[ \t]*")
10958 (goto-char (match-end 0)))
10959 (and (looking-at js2-possibly-braceless-keywords-re)
10960 (= (match-end 0) pt)
10961 (not (js2-end-of-do-while-loop-p))))))
10962 (+ (current-indentation) js2-basic-offset)))))
10963
10964 (defun js2-indent-in-array-comp (parse-status)
10965 "Return non-nil if we think we're in an array comprehension.
10966 In particular, return the buffer position of the first `for' kwd."
10967 (let ((bracket (nth 1 parse-status))
10968 (end (point)))
10969 (when bracket
10970 (save-excursion
10971 (goto-char bracket)
10972 (when (looking-at "\\[")
10973 (forward-char 1)
10974 (js2-forward-sws)
10975 (if (looking-at "[[{]")
10976 (let (forward-sexp-function) ; use Lisp version
10977 (forward-sexp) ; skip destructuring form
10978 (js2-forward-sws)
10979 (if (and (/= (char-after) ?,) ; regular array
10980 (looking-at "for"))
10981 (match-beginning 0)))
10982 ;; to skip arbitrary expressions we need the parser,
10983 ;; so we'll just guess at it.
10984 (if (and (> end (point)) ; not empty literal
10985 (re-search-forward "[^,]]* \\(for\\) " end t)
10986 ;; not inside comment or string literal
10987 (let ((state (parse-partial-sexp bracket (point))))
10988 (not (or (nth 3 state) (nth 4 state)))))
10989 (match-beginning 1))))))))
10990
10991 (defun js2-array-comp-indentation (parse-status for-kwd)
10992 (if (js2-same-line for-kwd)
10993 ;; first continuation line
10994 (save-excursion
10995 (goto-char (nth 1 parse-status))
10996 (forward-char 1)
10997 (skip-chars-forward " \t")
10998 (current-column))
10999 (save-excursion
11000 (goto-char for-kwd)
11001 (current-column))))
11002
11003 (defun js2-maybe-goto-declaration-keyword-end (bracket)
11004 "Helper function for `js2-proper-indentation'.
11005 Depending on the value of `js2-pretty-multiline-declarations',
11006 move point to the end of a variable declaration keyword so that
11007 indentation is aligned to that column."
11008 (cond
11009 ((eq js2-pretty-multiline-declarations 'all)
11010 (when (looking-at js2-declaration-keyword-re)
11011 (goto-char (1+ (match-end 0)))))
11012 ((eq js2-pretty-multiline-declarations 'dynamic)
11013 (let (declaration-keyword-end
11014 at-closing-bracket-p
11015 comma-p)
11016 (when (looking-at js2-declaration-keyword-re)
11017 ;; Preserve the match data lest it somehow be overridden.
11018 (setq declaration-keyword-end (match-end 0))
11019 (save-excursion
11020 (goto-char bracket)
11021 (setq at-closing-bracket-p
11022 ;; Handle scan errors gracefully.
11023 (condition-case nil
11024 (progn
11025 ;; Use the regular `forward-sexp-function' because the
11026 ;; normal one for this mode uses the AST.
11027 (let (forward-sexp-function)
11028 (forward-sexp))
11029 t)
11030 (error nil)))
11031 (when at-closing-bracket-p
11032 (js2-forward-sws)
11033 (setq comma-p (looking-at-p ","))))
11034 (when comma-p
11035 (goto-char (1+ declaration-keyword-end))))))))
11036
11037 (defun js2-proper-indentation (parse-status)
11038 "Return the proper indentation for the current line."
11039 (save-excursion
11040 (back-to-indentation)
11041 (let* ((ctrl-stmt-indent (js2-ctrl-statement-indentation))
11042 (at-closing-bracket (looking-at "[]})]"))
11043 (same-indent-p (or at-closing-bracket
11044 (looking-at "\\<case\\>[^:]")
11045 (and (looking-at "\\<default:")
11046 (save-excursion
11047 (js2-backward-sws)
11048 (not (memq (char-before) '(?, ?{)))))))
11049 (continued-expr-p (js2-continued-expression-p))
11050 (declaration-indent (and js2-pretty-multiline-declarations
11051 (js2-multiline-decl-indentation)))
11052 (bracket (nth 1 parse-status))
11053 beg indent)
11054 (cond
11055 ;; indent array comprehension continuation lines specially
11056 ((and bracket
11057 (>= js2-language-version 170)
11058 (not (js2-same-line bracket))
11059 (setq beg (js2-indent-in-array-comp parse-status))
11060 (>= (point) (save-excursion
11061 (goto-char beg)
11062 (point-at-bol)))) ; at or after first loop?
11063 (js2-array-comp-indentation parse-status beg))
11064
11065 (ctrl-stmt-indent)
11066
11067 ((and declaration-indent continued-expr-p)
11068 (+ declaration-indent js2-basic-offset))
11069
11070 (declaration-indent)
11071
11072 (bracket
11073 (goto-char bracket)
11074 (cond
11075 ((looking-at "[({[][ \t]*\\(/[/*]\\|$\\)")
11076 (when (save-excursion (skip-chars-backward " \t)")
11077 (looking-at ")"))
11078 (backward-list))
11079 (back-to-indentation)
11080 (js2-maybe-goto-declaration-keyword-end bracket)
11081 (setq indent
11082 (cond (same-indent-p
11083 (current-column))
11084 (continued-expr-p
11085 (+ (current-column) (* 2 js2-basic-offset)))
11086 (t
11087 (+ (current-column) js2-basic-offset))))
11088 (if (and js2-indent-switch-body
11089 (not at-closing-bracket)
11090 (looking-at "\\_<switch\\_>"))
11091 (+ indent js2-basic-offset)
11092 indent))
11093 (t
11094 (unless same-indent-p
11095 (forward-char)
11096 (skip-chars-forward " \t"))
11097 (current-column))))
11098
11099 (continued-expr-p js2-basic-offset)
11100
11101 (t 0)))))
11102
11103 (defun js2-lineup-comment (parse-status)
11104 "Indent a multi-line block comment continuation line."
11105 (let* ((beg (nth 8 parse-status))
11106 (first-line (js2-same-line beg))
11107 (offset (save-excursion
11108 (goto-char beg)
11109 (if (looking-at "/\\*")
11110 (+ 1 (current-column))
11111 0))))
11112 (unless first-line
11113 (indent-line-to offset))))
11114
11115 (defun js2-backward-sws ()
11116 "Move backward through whitespace and comments."
11117 (interactive)
11118 (while (forward-comment -1)))
11119
11120 (defun js2-forward-sws ()
11121 "Move forward through whitespace and comments."
11122 (interactive)
11123 (while (forward-comment 1)))
11124
11125 (defun js2-current-indent (&optional pos)
11126 "Return column of indentation on current line.
11127 If POS is non-nil, go to that point and return indentation for that line."
11128 (save-excursion
11129 (if pos
11130 (goto-char pos))
11131 (back-to-indentation)
11132 (current-column)))
11133
11134 (defun js2-arglist-close ()
11135 "Return non-nil if we're on a line beginning with a close-paren/brace."
11136 (save-excursion
11137 (goto-char (point-at-bol))
11138 (js2-forward-sws)
11139 (looking-at "[])}]")))
11140
11141 (defun js2-indent-looks-like-label-p ()
11142 (goto-char (point-at-bol))
11143 (js2-forward-sws)
11144 (looking-at (concat js2-mode-identifier-re ":")))
11145
11146 (defun js2-indent-in-objlit-p (parse-status)
11147 "Return non-nil if this looks like an object-literal entry."
11148 (let ((start (nth 1 parse-status)))
11149 (and
11150 start
11151 (save-excursion
11152 (and (zerop (forward-line -1))
11153 (not (< (point) start)) ; crossed a {} boundary
11154 (js2-indent-looks-like-label-p)))
11155 (save-excursion
11156 (js2-indent-looks-like-label-p)))))
11157
11158 ;; If prev line looks like foobar({ then we're passing an object
11159 ;; literal to a function call, and people pretty much always want to
11160 ;; de-dent back to the previous line, so move the 'basic-offset'
11161 ;; position to the front.
11162 (defun js2-indent-objlit-arg-p (parse-status)
11163 (save-excursion
11164 (back-to-indentation)
11165 (js2-backward-sws)
11166 (and (eq (1- (point)) (nth 1 parse-status))
11167 (eq (char-before) ?{)
11168 (progn
11169 (forward-char -1)
11170 (skip-chars-backward " \t")
11171 (eq (char-before) ?\()))))
11172
11173 (defun js2-indent-case-block-p ()
11174 (save-excursion
11175 (back-to-indentation)
11176 (js2-backward-sws)
11177 (goto-char (point-at-bol))
11178 (skip-chars-forward " \t")
11179 (looking-at "case\\s-.+:")))
11180
11181 (defun js2-bounce-indent (normal-col parse-status &optional backwards)
11182 "Cycle among alternate computed indentation positions.
11183 PARSE-STATUS is the result of `parse-partial-sexp' from the beginning
11184 of the buffer to the current point. NORMAL-COL is the indentation
11185 column computed by the heuristic guesser based on current paren,
11186 bracket, brace and statement nesting. If BACKWARDS, cycle positions
11187 in reverse."
11188 (let ((cur-indent (js2-current-indent))
11189 (old-buffer-undo-list buffer-undo-list)
11190 ;; Emacs 21 only has `count-lines', not `line-number-at-pos'
11191 (current-line (save-excursion
11192 (forward-line 0) ; move to bol
11193 (1+ (count-lines (point-min) (point)))))
11194 positions pos main-pos anchor arglist-cont same-indent
11195 basic-offset computed-pos)
11196 ;; temporarily don't record undo info, if user requested this
11197 (when js2-mode-indent-inhibit-undo
11198 (setq buffer-undo-list t))
11199 (unwind-protect
11200 (progn
11201 ;; First likely point: indent from beginning of previous code line
11202 (push (setq basic-offset
11203 (+ (save-excursion
11204 (back-to-indentation)
11205 (js2-backward-sws)
11206 (back-to-indentation)
11207 (current-column))
11208 js2-basic-offset))
11209 positions)
11210
11211 ;; (First + epsilon) likely point: indent 2x from beginning of
11212 ;; previous code line. Google does it this way.
11213 (push (setq basic-offset
11214 (+ (save-excursion
11215 (back-to-indentation)
11216 (js2-backward-sws)
11217 (back-to-indentation)
11218 (current-column))
11219 (* 2 js2-basic-offset)))
11220 positions)
11221
11222 ;; Second likely point: indent from assign-expr RHS. This
11223 ;; is just a crude guess based on finding " = " on the previous
11224 ;; line containing actual code.
11225 (setq pos (save-excursion
11226 (forward-line -1)
11227 (goto-char (point-at-bol))
11228 (when (re-search-forward "\\s-+\\(=\\)\\s-+"
11229 (point-at-eol) t)
11230 (goto-char (match-end 1))
11231 (skip-chars-forward " \t\r\n")
11232 (current-column))))
11233 (when pos
11234 (cl-incf pos js2-basic-offset)
11235 (push pos positions))
11236
11237 ;; Third likely point: same indent as previous line of code.
11238 ;; Make it the first likely point if we're not on an
11239 ;; arglist-close line and previous line ends in a comma, or
11240 ;; both this line and prev line look like object-literal
11241 ;; elements.
11242 (setq pos (save-excursion
11243 (goto-char (point-at-bol))
11244 (js2-backward-sws)
11245 (back-to-indentation)
11246 (prog1
11247 (current-column)
11248 ;; while we're here, look for trailing comma
11249 (if (save-excursion
11250 (goto-char (point-at-eol))
11251 (js2-backward-sws)
11252 (eq (char-before) ?,))
11253 (setq arglist-cont (1- (point)))))))
11254 (when pos
11255 (if (and (or arglist-cont
11256 (js2-indent-in-objlit-p parse-status))
11257 (not (js2-arglist-close)))
11258 (setq same-indent pos))
11259 (push pos positions))
11260
11261 ;; Fourth likely point: first preceding code with less indentation.
11262 ;; than the immediately preceding code line.
11263 (setq pos (save-excursion
11264 (back-to-indentation)
11265 (js2-backward-sws)
11266 (back-to-indentation)
11267 (setq anchor (current-column))
11268 (while (and (zerop (forward-line -1))
11269 (>= (progn
11270 (back-to-indentation)
11271 (current-column))
11272 anchor)))
11273 (setq pos (current-column))))
11274 (push pos positions)
11275
11276 ;; nesting-heuristic position, main by default
11277 (push (setq main-pos normal-col) positions)
11278
11279 ;; delete duplicates and sort positions list
11280 (setq positions (sort (delete-dups positions) '<))
11281
11282 ;; comma-list continuation lines: prev line indent takes precedence
11283 (if same-indent
11284 (setq main-pos same-indent))
11285
11286 ;; common special cases where we want to indent in from previous line
11287 (if (or (js2-indent-case-block-p)
11288 (js2-indent-objlit-arg-p parse-status))
11289 (setq main-pos basic-offset))
11290
11291 ;; if bouncing backwards, reverse positions list
11292 (if backwards
11293 (setq positions (reverse positions)))
11294
11295 ;; record whether we're already sitting on one of the alternatives
11296 (setq pos (member cur-indent positions))
11297
11298 (cond
11299 ;; case 0: we're one one of the alternatives and this is the
11300 ;; first time they've pressed TAB on this line (best-guess).
11301 ((and js2-mode-indent-ignore-first-tab
11302 pos
11303 ;; first time pressing TAB on this line?
11304 (not (eq js2-mode-last-indented-line current-line)))
11305 ;; do nothing
11306 (setq computed-pos nil))
11307 ;; case 1: only one computed position => use it
11308 ((null (cdr positions))
11309 (setq computed-pos 0))
11310 ;; case 2: not on any of the computed spots => use main spot
11311 ((not pos)
11312 (setq computed-pos (js2-position main-pos positions)))
11313 ;; case 3: on last position: cycle to first position
11314 ((null (cdr pos))
11315 (setq computed-pos 0))
11316 ;; case 4: on intermediate position: cycle to next position
11317 (t
11318 (setq computed-pos (js2-position (cl-second pos) positions))))
11319
11320 ;; see if any hooks want to indent; otherwise we do it
11321 (cl-loop with result = nil
11322 for hook in js2-indent-hook
11323 while (null result)
11324 do
11325 (setq result (funcall hook positions computed-pos))
11326 finally do
11327 (unless (or result (null computed-pos))
11328 (indent-line-to (nth computed-pos positions)))))
11329
11330 ;; finally
11331 (if js2-mode-indent-inhibit-undo
11332 (setq buffer-undo-list old-buffer-undo-list))
11333 ;; see commentary for `js2-mode-last-indented-line'
11334 (setq js2-mode-last-indented-line current-line))))
11335
11336 (defun js2-indent-bounce-backwards ()
11337 "Calls `js2-indent-line'. When `js2-bounce-indent-p',
11338 cycles between the computed indentation positions in reverse order."
11339 (interactive)
11340 (js2-indent-line t))
11341
11342 (defun js2-1-line-comment-continuation-p ()
11343 "Return t if we're in a 1-line comment continuation.
11344 If so, we don't ever want to use bounce-indent."
11345 (save-excursion
11346 (and (progn
11347 (forward-line 0)
11348 (looking-at "\\s-*//"))
11349 (progn
11350 (forward-line -1)
11351 (forward-line 0)
11352 (when (looking-at "\\s-*$")
11353 (js2-backward-sws)
11354 (forward-line 0))
11355 (looking-at "\\s-*//")))))
11356
11357 (defun js2-indent-line (&optional bounce-backwards)
11358 "Indent the current line as JavaScript source text."
11359 (interactive)
11360 (let (parse-status offset indent-col
11361 ;; Don't whine about errors/warnings when we're indenting.
11362 ;; This has to be set before calling parse-partial-sexp below.
11363 (inhibit-point-motion-hooks t))
11364 (setq parse-status (save-excursion
11365 (syntax-ppss (point-at-bol)))
11366 offset (- (point) (save-excursion
11367 (back-to-indentation)
11368 (point))))
11369 ;; Don't touch multiline strings.
11370 (unless (nth 3 parse-status)
11371 (js2-with-underscore-as-word-syntax
11372 (if (nth 4 parse-status)
11373 (js2-lineup-comment parse-status)
11374 (setq indent-col (js2-proper-indentation parse-status))
11375 ;; See comments below about `js2-mode-last-indented-line'.
11376 (cond
11377 ;; bounce-indenting is disabled during electric-key indent.
11378 ;; It doesn't work well on first line of buffer.
11379 ((and js2-bounce-indent-p
11380 (not (js2-same-line (point-min)))
11381 (not (js2-1-line-comment-continuation-p)))
11382 (js2-bounce-indent indent-col parse-status bounce-backwards))
11383 ;; just indent to the guesser's likely spot
11384 (t (indent-line-to indent-col))))
11385 (when (cl-plusp offset)
11386 (forward-char offset))))))
11387
11388 (defun js2-indent-region (start end)
11389 "Indent the region, but don't use bounce indenting."
11390 (let ((js2-bounce-indent-p nil)
11391 (indent-region-function nil)
11392 (after-change-functions (remq 'js2-mode-edit
11393 after-change-functions)))
11394 (indent-region start end nil) ; nil for byte-compiler
11395 (js2-mode-edit start end (- end start))))
11396
11397 (defvar js2-minor-mode-map
11398 (let ((map (make-sparse-keymap)))
11399 (define-key map (kbd "C-c C-`") #'js2-next-error)
11400 (define-key map [mouse-1] #'js2-mode-show-node)
11401 map)
11402 "Keymap used when `js2-minor-mode' is active.")
11403
11404 ;;;###autoload
11405 (define-minor-mode js2-minor-mode
11406 "Minor mode for running js2 as a background linter.
11407 This allows you to use a different major mode for JavaScript editing,
11408 such as `js-mode', while retaining the asynchronous error/warning
11409 highlighting features of `js2-mode'."
11410 :group 'js2-mode
11411 :lighter " js-lint"
11412 (if js2-minor-mode
11413 (js2-minor-mode-enter)
11414 (js2-minor-mode-exit)))
11415
11416 (defun js2-minor-mode-enter ()
11417 "Initialization for `js2-minor-mode'."
11418 (set (make-local-variable 'max-lisp-eval-depth)
11419 (max max-lisp-eval-depth 3000))
11420 (setq next-error-function #'js2-next-error)
11421 (js2-set-default-externs)
11422 ;; Experiment: make reparse-delay longer for longer files.
11423 (if (cl-plusp js2-dynamic-idle-timer-adjust)
11424 (setq js2-idle-timer-delay
11425 (* js2-idle-timer-delay
11426 (/ (point-max) js2-dynamic-idle-timer-adjust))))
11427 (setq js2-mode-buffer-dirty-p t
11428 js2-mode-parsing nil)
11429 (set (make-local-variable 'js2-highlight-level) 0) ; no syntax highlighting
11430 (add-hook 'after-change-functions #'js2-minor-mode-edit nil t)
11431 (add-hook 'change-major-mode-hook #'js2-minor-mode-exit nil t)
11432 (when js2-include-jslint-globals
11433 (add-hook 'js2-post-parse-callbacks 'js2-apply-jslint-globals nil t))
11434 (run-hooks 'js2-init-hook)
11435 (js2-reparse))
11436
11437 (defun js2-minor-mode-exit ()
11438 "Turn off `js2-minor-mode'."
11439 (setq next-error-function nil)
11440 (remove-hook 'after-change-functions #'js2-mode-edit t)
11441 (remove-hook 'change-major-mode-hook #'js2-minor-mode-exit t)
11442 (when js2-mode-node-overlay
11443 (delete-overlay js2-mode-node-overlay)
11444 (setq js2-mode-node-overlay nil))
11445 (js2-remove-overlays)
11446 (remove-hook 'js2-post-parse-callbacks 'js2-apply-jslint-globals t)
11447 (setq js2-mode-ast nil))
11448
11449 (defvar js2-source-buffer nil "Linked source buffer for diagnostics view")
11450 (make-variable-buffer-local 'js2-source-buffer)
11451
11452 (cl-defun js2-display-error-list ()
11453 "Display a navigable buffer listing parse errors/warnings."
11454 (interactive)
11455 (unless (js2-have-errors-p)
11456 (message "No errors")
11457 (cl-return-from js2-display-error-list))
11458 (cl-labels ((annotate-list
11459 (lst type)
11460 "Add diagnostic TYPE and line number to errs list"
11461 (mapcar (lambda (err)
11462 (list err type (line-number-at-pos (nth 1 err))))
11463 lst)))
11464 (let* ((srcbuf (current-buffer))
11465 (errbuf (get-buffer-create "*js-lint*"))
11466 (errors (annotate-list
11467 (when js2-mode-ast (js2-ast-root-errors js2-mode-ast))
11468 'js2-error)) ; must be a valid face name
11469 (warnings (annotate-list
11470 (when js2-mode-ast (js2-ast-root-warnings js2-mode-ast))
11471 'js2-warning)) ; must be a valid face name
11472 (all-errs (sort (append errors warnings)
11473 (lambda (e1 e2) (< (cl-cadar e1) (cl-cadar e2))))))
11474 (with-current-buffer errbuf
11475 (let ((inhibit-read-only t))
11476 (erase-buffer)
11477 (dolist (err all-errs)
11478 (cl-destructuring-bind ((msg-key beg _end &rest) type line) err
11479 (insert-text-button
11480 (format "line %d: %s" line (js2-get-msg msg-key))
11481 'face type
11482 'follow-link "\C-m"
11483 'action 'js2-error-buffer-jump
11484 'js2-msg (js2-get-msg msg-key)
11485 'js2-pos beg)
11486 (insert "\n"))))
11487 (js2-error-buffer-mode)
11488 (setq js2-source-buffer srcbuf)
11489 (pop-to-buffer errbuf)
11490 (goto-char (point-min))
11491 (unless (eobp)
11492 (js2-error-buffer-view))))))
11493
11494 (defvar js2-error-buffer-mode-map
11495 (let ((map (make-sparse-keymap)))
11496 (define-key map "n" #'js2-error-buffer-next)
11497 (define-key map "p" #'js2-error-buffer-prev)
11498 (define-key map (kbd "RET") #'js2-error-buffer-jump)
11499 (define-key map "o" #'js2-error-buffer-view)
11500 (define-key map "q" #'js2-error-buffer-quit)
11501 map)
11502 "Keymap used for js2 diagnostics buffers.")
11503
11504 (defun js2-error-buffer-mode ()
11505 "Major mode for js2 diagnostics buffers.
11506 Selecting an error will jump it to the corresponding source-buffer error.
11507 \\{js2-error-buffer-mode-map}"
11508 (interactive)
11509 (setq major-mode 'js2-error-buffer-mode
11510 mode-name "JS Lint Diagnostics")
11511 (use-local-map js2-error-buffer-mode-map)
11512 (setq truncate-lines t)
11513 (set-buffer-modified-p nil)
11514 (setq buffer-read-only t)
11515 (run-hooks 'js2-error-buffer-mode-hook))
11516
11517 (defun js2-error-buffer-next ()
11518 "Move to next error and view it."
11519 (interactive)
11520 (when (zerop (forward-line 1))
11521 (js2-error-buffer-view)))
11522
11523 (defun js2-error-buffer-prev ()
11524 "Move to previous error and view it."
11525 (interactive)
11526 (when (zerop (forward-line -1))
11527 (js2-error-buffer-view)))
11528
11529 (defun js2-error-buffer-quit ()
11530 "Kill the current buffer."
11531 (interactive)
11532 (kill-buffer))
11533
11534 (defun js2-error-buffer-jump (&rest ignored)
11535 "Jump cursor to current error in source buffer."
11536 (interactive)
11537 (when (js2-error-buffer-view)
11538 (pop-to-buffer js2-source-buffer)))
11539
11540 (defun js2-error-buffer-view ()
11541 "Scroll source buffer to show error at current line."
11542 (interactive)
11543 (cond
11544 ((not (eq major-mode 'js2-error-buffer-mode))
11545 (message "Not in a js2 errors buffer"))
11546 ((not (buffer-live-p js2-source-buffer))
11547 (message "Source buffer has been killed"))
11548 ((not (wholenump (get-text-property (point) 'js2-pos)))
11549 (message "There does not seem to be an error here"))
11550 (t
11551 (let ((pos (get-text-property (point) 'js2-pos))
11552 (msg (get-text-property (point) 'js2-msg)))
11553 (save-selected-window
11554 (pop-to-buffer js2-source-buffer)
11555 (goto-char pos)
11556 (message msg))))))
11557
11558 ;;;###autoload
11559 (define-derived-mode js2-mode prog-mode "Javascript-IDE"
11560 ;; FIXME: Should derive from js-mode.
11561 "Major mode for editing JavaScript code."
11562 ;; Used by comment-region; don't change it.
11563 (set (make-local-variable 'comment-start) "//")
11564 (set (make-local-variable 'comment-end) "")
11565 (set (make-local-variable 'comment-start-skip) js2-comment-start-skip)
11566 (set (make-local-variable 'max-lisp-eval-depth)
11567 (max max-lisp-eval-depth 3000))
11568 (set (make-local-variable 'indent-line-function) #'js2-indent-line)
11569 (set (make-local-variable 'indent-region-function) #'js2-indent-region)
11570 (set (make-local-variable 'fill-paragraph-function) #'c-fill-paragraph)
11571 (set (make-local-variable 'comment-line-break-function) #'js2-line-break)
11572 (set (make-local-variable 'beginning-of-defun-function) #'js2-beginning-of-defun)
11573 (set (make-local-variable 'end-of-defun-function) #'js2-end-of-defun)
11574 ;; We un-confuse `parse-partial-sexp' by setting syntax-table properties
11575 ;; for characters inside regexp literals.
11576 (set (make-local-variable 'parse-sexp-lookup-properties) t)
11577 ;; this is necessary to make `show-paren-function' work properly
11578 (set (make-local-variable 'parse-sexp-ignore-comments) t)
11579 ;; needed for M-x rgrep, among other things
11580 (put 'js2-mode 'find-tag-default-function #'js2-mode-find-tag)
11581
11582 (set (make-local-variable 'electric-indent-chars)
11583 (append "{}()[]:;,*." electric-indent-chars))
11584 (set (make-local-variable 'electric-layout-rules)
11585 '((?\; . after) (?\{ . after) (?\} . before)))
11586
11587 ;; some variables needed by cc-engine for paragraph-fill, etc.
11588 (setq c-comment-prefix-regexp js2-comment-prefix-regexp
11589 c-comment-start-regexp "/[*/]\\|\\s|"
11590 c-line-comment-starter "//"
11591 c-paragraph-start js2-paragraph-start
11592 c-paragraph-separate "$"
11593 c-syntactic-ws-start js2-syntactic-ws-start
11594 c-syntactic-ws-end js2-syntactic-ws-end
11595 c-syntactic-eol js2-syntactic-eol)
11596
11597 (let ((c-buffer-is-cc-mode t))
11598 ;; Copied from `js-mode'. Also see Bug#6071.
11599 (make-local-variable 'paragraph-start)
11600 (make-local-variable 'paragraph-separate)
11601 (make-local-variable 'paragraph-ignore-fill-prefix)
11602 (make-local-variable 'adaptive-fill-mode)
11603 (make-local-variable 'adaptive-fill-regexp)
11604 (c-setup-paragraph-variables))
11605
11606 (setq font-lock-defaults '(nil t))
11607
11608 ;; Experiment: make reparse-delay longer for longer files.
11609 (when (cl-plusp js2-dynamic-idle-timer-adjust)
11610 (setq js2-idle-timer-delay
11611 (* js2-idle-timer-delay
11612 (/ (point-max) js2-dynamic-idle-timer-adjust))))
11613
11614 (add-hook 'change-major-mode-hook #'js2-mode-exit nil t)
11615 (add-hook 'after-change-functions #'js2-mode-edit nil t)
11616 (setq imenu-create-index-function #'js2-mode-create-imenu-index)
11617 (setq next-error-function #'js2-next-error)
11618 (imenu-add-to-menubar (concat "IM-" mode-name))
11619 (add-to-invisibility-spec '(js2-outline . t))
11620 (set (make-local-variable 'line-move-ignore-invisible) t)
11621 (set (make-local-variable 'forward-sexp-function) #'js2-mode-forward-sexp)
11622 (when (fboundp 'cursor-sensor-mode) (cursor-sensor-mode 1))
11623
11624 (setq js2-mode-functions-hidden nil
11625 js2-mode-comments-hidden nil
11626 js2-mode-buffer-dirty-p t
11627 js2-mode-parsing nil)
11628
11629 (js2-set-default-externs)
11630
11631 (when js2-include-jslint-globals
11632 (add-hook 'js2-post-parse-callbacks 'js2-apply-jslint-globals nil t))
11633
11634 (run-hooks 'js2-init-hook)
11635
11636 (js2-reparse))
11637
11638 (defun js2-mode-exit ()
11639 "Exit `js2-mode' and clean up."
11640 (interactive)
11641 (when js2-mode-node-overlay
11642 (delete-overlay js2-mode-node-overlay)
11643 (setq js2-mode-node-overlay nil))
11644 (js2-remove-overlays)
11645 (setq js2-mode-ast nil)
11646 (remove-hook 'change-major-mode-hook #'js2-mode-exit t)
11647 (remove-from-invisibility-spec '(js2-outline . t))
11648 (js2-mode-show-all)
11649 (with-silent-modifications
11650 (js2-clear-face (point-min) (point-max))))
11651
11652 (defun js2-mode-reset-timer ()
11653 "Cancel any existing parse timer and schedule a new one."
11654 (if js2-mode-parse-timer
11655 (cancel-timer js2-mode-parse-timer))
11656 (setq js2-mode-parsing nil)
11657 (let ((timer (timer-create)))
11658 (setq js2-mode-parse-timer timer)
11659 (timer-set-function timer 'js2-mode-idle-reparse (list (current-buffer)))
11660 (timer-set-idle-time timer js2-idle-timer-delay)
11661 ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=12326
11662 (timer-activate-when-idle timer nil)))
11663
11664 (defun js2-mode-idle-reparse (buffer)
11665 "Run `js2-reparse' if BUFFER is the current buffer, or schedule
11666 it to be reparsed when the buffer is selected."
11667 (cond ((eq buffer (current-buffer))
11668 (js2-reparse))
11669 ((buffer-live-p buffer)
11670 ;; reparse when the buffer is selected again
11671 (with-current-buffer buffer
11672 (add-hook 'window-configuration-change-hook
11673 #'js2-mode-idle-reparse-inner
11674 nil t)))))
11675
11676 (defun js2-mode-idle-reparse-inner ()
11677 (remove-hook 'window-configuration-change-hook
11678 #'js2-mode-idle-reparse-inner
11679 t)
11680 (js2-reparse))
11681
11682 (defun js2-mode-edit (_beg _end _len)
11683 "Schedule a new parse after buffer is edited.
11684 Buffer edit spans from BEG to END and is of length LEN."
11685 (setq js2-mode-buffer-dirty-p t)
11686 (js2-mode-hide-overlay)
11687 (js2-mode-reset-timer))
11688
11689 (defun js2-minor-mode-edit (_beg _end _len)
11690 "Callback for buffer edits in `js2-mode'.
11691 Schedules a new parse after buffer is edited.
11692 Buffer edit spans from BEG to END and is of length LEN."
11693 (setq js2-mode-buffer-dirty-p t)
11694 (js2-mode-hide-overlay)
11695 (js2-mode-reset-timer))
11696
11697 (defun js2-reparse (&optional force)
11698 "Re-parse current buffer after user finishes some data entry.
11699 If we get any user input while parsing, including cursor motion,
11700 we discard the parse and reschedule it. If FORCE is nil, then the
11701 buffer will only rebuild its `js2-mode-ast' if the buffer is dirty."
11702 (let (time
11703 interrupted-p
11704 (js2-compiler-strict-mode js2-mode-show-strict-warnings))
11705 (unless js2-mode-parsing
11706 (setq js2-mode-parsing t)
11707 (unwind-protect
11708 (when (or js2-mode-buffer-dirty-p force)
11709 (js2-remove-overlays)
11710 (setq js2-mode-buffer-dirty-p nil
11711 js2-mode-fontifications nil
11712 js2-mode-deferred-properties nil)
11713 (if js2-mode-verbose-parse-p
11714 (message "parsing..."))
11715 (setq time
11716 (js2-time
11717 (setq interrupted-p
11718 (catch 'interrupted
11719 (js2-parse)
11720 (with-silent-modifications
11721 ;; if parsing is interrupted, comments and regex
11722 ;; literals stay ignored by `parse-partial-sexp'
11723 (remove-text-properties (point-min) (point-max)
11724 '(syntax-table))
11725 (js2-mode-apply-deferred-properties)
11726 (js2-mode-remove-suppressed-warnings)
11727 (js2-mode-show-warnings)
11728 (js2-mode-show-errors)
11729 (if (>= js2-highlight-level 1)
11730 (js2-highlight-jsdoc js2-mode-ast)))
11731 nil))))
11732 (if interrupted-p
11733 (progn
11734 ;; unfinished parse => try again
11735 (setq js2-mode-buffer-dirty-p t)
11736 (js2-mode-reset-timer))
11737 (if js2-mode-verbose-parse-p
11738 (message "Parse time: %s" time))))
11739 (setq js2-mode-parsing nil)
11740 (unless interrupted-p
11741 (setq js2-mode-parse-timer nil))))))
11742
11743 (defun js2-mode-show-node (event)
11744 "Debugging aid: highlight selected AST node on mouse click."
11745 (interactive "e")
11746 (mouse-set-point event)
11747 (setq deactivate-mark t)
11748 (when js2-mode-show-overlay
11749 (let ((node (js2-node-at-point))
11750 beg end)
11751 (if (null node)
11752 (message "No node found at location %s" (point))
11753 (setq beg (js2-node-abs-pos node)
11754 end (+ beg (js2-node-len node)))
11755 (if js2-mode-node-overlay
11756 (move-overlay js2-mode-node-overlay beg end)
11757 (setq js2-mode-node-overlay (make-overlay beg end))
11758 (overlay-put js2-mode-node-overlay 'font-lock-face 'highlight))
11759 (with-silent-modifications
11760 (if (fboundp 'cursor-sensor-mode)
11761 (put-text-property beg end 'cursor-sensor-functions
11762 '(js2-mode-hide-overlay))
11763 (put-text-property beg end 'point-left #'js2-mode-hide-overlay)))
11764 (message "%s, parent: %s"
11765 (js2-node-short-name node)
11766 (if (js2-node-parent node)
11767 (js2-node-short-name (js2-node-parent node))
11768 "nil"))))))
11769
11770 (defun js2-mode-hide-overlay (&optional arg1 arg2 _arg3)
11771 "Remove the debugging overlay when point moves.
11772 ARG1, ARG2 and ARG3 have different values depending on whether this function
11773 was found on `point-left' or in `cursor-sensor-functions'."
11774 (when js2-mode-node-overlay
11775 (let ((beg (overlay-start js2-mode-node-overlay))
11776 (end (overlay-end js2-mode-node-overlay))
11777 (p2 (if (windowp arg1)
11778 ;; Called from cursor-sensor-functions.
11779 (window-point arg1)
11780 ;; Called from point-left.
11781 arg2)))
11782 ;; Sometimes we're called spuriously.
11783 (unless (and p2
11784 (>= p2 beg)
11785 (<= p2 end))
11786 (with-silent-modifications
11787 (remove-text-properties beg end
11788 '(point-left nil cursor-sensor-functions)))
11789 (delete-overlay js2-mode-node-overlay)
11790 (setq js2-mode-node-overlay nil)))))
11791
11792 (defun js2-mode-reset ()
11793 "Debugging helper: reset everything."
11794 (interactive)
11795 (js2-mode-exit)
11796 (js2-mode))
11797
11798 (defun js2-mode-show-warn-or-err (e face)
11799 "Highlight a warning or error E with FACE.
11800 E is a list of ((MSG-KEY MSG-ARG) BEG LEN OVERRIDE-FACE).
11801 The last element is optional. When present, use instead of FACE."
11802 (let* ((key (cl-first e))
11803 (beg (cl-second e))
11804 (end (+ beg (cl-third e)))
11805 ;; Don't inadvertently go out of bounds.
11806 (beg (max (point-min) (min beg (point-max))))
11807 (end (max (point-min) (min end (point-max))))
11808 (ovl (make-overlay beg end)))
11809 ;; FIXME: Why a mix of overlays and text-properties?
11810 (overlay-put ovl 'font-lock-face (or (cl-fourth e) face))
11811 (overlay-put ovl 'js2-error t)
11812 (put-text-property beg end 'help-echo (js2-get-msg key))
11813 (if (fboundp 'cursor-sensor-mode)
11814 (put-text-property beg end 'cursor-sensor-functions '(js2-echo-error))
11815 (put-text-property beg end 'point-entered #'js2-echo-error))))
11816
11817 (defun js2-remove-overlays ()
11818 "Remove overlays from buffer that have a `js2-error' property."
11819 (let ((beg (point-min))
11820 (end (point-max)))
11821 (save-excursion
11822 (dolist (o (overlays-in beg end))
11823 (when (overlay-get o 'js2-error)
11824 (delete-overlay o))))))
11825
11826 (defun js2-mode-apply-deferred-properties ()
11827 "Apply fontifications and other text properties recorded during parsing."
11828 (when (cl-plusp js2-highlight-level)
11829 ;; We defer clearing faces as long as possible to eliminate flashing.
11830 (js2-clear-face (point-min) (point-max))
11831 ;; Have to reverse the recorded fontifications list so that errors
11832 ;; and warnings overwrite the normal fontifications.
11833 (dolist (f (nreverse js2-mode-fontifications))
11834 (put-text-property (cl-first f) (cl-second f) 'font-lock-face (cl-third f)))
11835 (setq js2-mode-fontifications nil))
11836 (dolist (p js2-mode-deferred-properties)
11837 (apply #'put-text-property p))
11838 (setq js2-mode-deferred-properties nil))
11839
11840 (defun js2-mode-show-errors ()
11841 "Highlight syntax errors."
11842 (when js2-mode-show-parse-errors
11843 (dolist (e (js2-ast-root-errors js2-mode-ast))
11844 (js2-mode-show-warn-or-err e 'js2-error))))
11845
11846 (defun js2-mode-remove-suppressed-warnings ()
11847 "Take suppressed warnings out of the AST warnings list.
11848 This ensures that the counts and `next-error' are correct."
11849 (setf (js2-ast-root-warnings js2-mode-ast)
11850 (js2-delete-if
11851 (lambda (e)
11852 (let ((key (caar e)))
11853 (or
11854 (and (not js2-strict-trailing-comma-warning)
11855 (string-match "trailing\\.comma" key))
11856 (and (not js2-strict-cond-assign-warning)
11857 (string= key "msg.equal.as.assign"))
11858 (and js2-missing-semi-one-line-override
11859 (string= key "msg.missing.semi")
11860 (let* ((beg (cl-second e))
11861 (node (js2-node-at-point beg))
11862 (fn (js2-mode-find-parent-fn node))
11863 (body (and fn (js2-function-node-body fn)))
11864 (lc (and body (js2-node-abs-pos body)))
11865 (rc (and lc (+ lc (js2-node-len body)))))
11866 (and fn
11867 (or (null body)
11868 (save-excursion
11869 (goto-char beg)
11870 (and (js2-same-line lc)
11871 (js2-same-line rc))))))))))
11872 (js2-ast-root-warnings js2-mode-ast))))
11873
11874 (defun js2-mode-show-warnings ()
11875 "Highlight strict-mode warnings."
11876 (when js2-mode-show-strict-warnings
11877 (dolist (e (js2-ast-root-warnings js2-mode-ast))
11878 (js2-mode-show-warn-or-err e 'js2-warning))))
11879
11880 (defun js2-echo-error (arg1 arg2 &optional _arg3)
11881 "Called by point-motion hooks.
11882 ARG1, ARG2 and ARG3 have different values depending on whether this function
11883 was found on `point-entered' or in `cursor-sensor-functions'."
11884 (let* ((new-point (if (windowp arg1)
11885 ;; Called from cursor-sensor-functions.
11886 (window-point arg1)
11887 ;; Called from point-left.
11888 arg2))
11889 (msg (get-text-property new-point 'help-echo)))
11890 (when (and (stringp msg)
11891 (not (active-minibuffer-window))
11892 (not (current-message)))
11893 (message msg))))
11894
11895 (defun js2-line-break (&optional _soft)
11896 "Break line at point and indent, continuing comment if within one.
11897 If inside a string, and `js2-concat-multiline-strings' is not
11898 nil, turn it into concatenation."
11899 (interactive)
11900 (let ((parse-status (syntax-ppss)))
11901 (cond
11902 ;; Check if we're inside a string.
11903 ((nth 3 parse-status)
11904 (if js2-concat-multiline-strings
11905 (js2-mode-split-string parse-status)
11906 (insert "\n")))
11907 ;; Check if inside a block comment.
11908 ((nth 4 parse-status)
11909 (js2-mode-extend-comment (nth 8 parse-status)))
11910 (t
11911 (newline-and-indent)))))
11912
11913 (defun js2-mode-split-string (parse-status)
11914 "Turn a newline in mid-string into a string concatenation.
11915 PARSE-STATUS is as documented in `parse-partial-sexp'."
11916 (let* ((quote-char (nth 3 parse-status))
11917 (at-eol (eq js2-concat-multiline-strings 'eol)))
11918 (insert quote-char)
11919 (insert (if at-eol " +\n" "\n"))
11920 (unless at-eol
11921 (insert "+ "))
11922 (js2-indent-line)
11923 (insert quote-char)
11924 (when (eolp)
11925 (insert quote-char)
11926 (backward-char 1))))
11927
11928 (defun js2-mode-extend-comment (start-pos)
11929 "Indent the line and, when inside a comment block, add comment prefix."
11930 (let (star single col first-line needs-close)
11931 (save-excursion
11932 (back-to-indentation)
11933 (when (< (point) start-pos)
11934 (goto-char start-pos))
11935 (cond
11936 ((looking-at "\\*[^/]")
11937 (setq star t
11938 col (current-column)))
11939 ((looking-at "/\\*")
11940 (setq star t
11941 first-line t
11942 col (1+ (current-column))))
11943 ((looking-at "//")
11944 (setq single t
11945 col (current-column)))))
11946 ;; Heuristic for whether we need to close the comment:
11947 ;; if we've got a parse error here, assume it's an unterminated
11948 ;; comment.
11949 (setq needs-close
11950 (or
11951 (get-char-property (1- (point)) 'js2-error)
11952 ;; The heuristic above doesn't work well when we're
11953 ;; creating a comment and there's another one downstream,
11954 ;; as our parser thinks this one ends at the end of the
11955 ;; next one. (You can have a /* inside a js block comment.)
11956 ;; So just close it if the next non-ws char isn't a *.
11957 (and first-line
11958 (eolp)
11959 (save-excursion
11960 (skip-chars-forward " \t\r\n")
11961 (not (eq (char-after) ?*))))))
11962 (delete-horizontal-space)
11963 (insert "\n")
11964 (cond
11965 (star
11966 (indent-to col)
11967 (insert "* ")
11968 (if (and first-line needs-close)
11969 (save-excursion
11970 (insert "\n")
11971 (indent-to col)
11972 (insert "*/"))))
11973 ((and single
11974 (save-excursion
11975 (and (zerop (forward-line 1))
11976 (looking-at "\\s-*//"))))
11977 (indent-to col)
11978 (insert "// ")))
11979 ;; Don't need to extend the comment after all.
11980 (js2-indent-line)))
11981
11982 (defun js2-beginning-of-line ()
11983 "Toggle point between bol and first non-whitespace char in line.
11984 Also moves past comment delimiters when inside comments."
11985 (interactive)
11986 (let (node)
11987 (cond
11988 ((bolp)
11989 (back-to-indentation))
11990 ((looking-at "//")
11991 (skip-chars-forward "/ \t"))
11992 ((and (eq (char-after) ?*)
11993 (setq node (js2-comment-at-point))
11994 (memq (js2-comment-node-format node) '(jsdoc block))
11995 (save-excursion
11996 (skip-chars-backward " \t")
11997 (bolp)))
11998 (skip-chars-forward "\* \t"))
11999 (t
12000 (goto-char (point-at-bol))))))
12001
12002 (defun js2-end-of-line ()
12003 "Toggle point between eol and last non-whitespace char in line."
12004 (interactive)
12005 (if (eolp)
12006 (skip-chars-backward " \t")
12007 (goto-char (point-at-eol))))
12008
12009 (defun js2-mode-wait-for-parse (callback)
12010 "Invoke CALLBACK when parsing is finished.
12011 If parsing is already finished, calls CALLBACK immediately."
12012 (if (not js2-mode-buffer-dirty-p)
12013 (funcall callback)
12014 (push callback js2-mode-pending-parse-callbacks)
12015 (add-hook 'js2-parse-finished-hook #'js2-mode-parse-finished)))
12016
12017 (defun js2-mode-parse-finished ()
12018 "Invoke callbacks in `js2-mode-pending-parse-callbacks'."
12019 ;; We can't let errors propagate up, since it prevents the
12020 ;; `js2-parse' method from completing normally and returning
12021 ;; the ast, which makes things mysteriously not work right.
12022 (unwind-protect
12023 (dolist (cb js2-mode-pending-parse-callbacks)
12024 (condition-case err
12025 (funcall cb)
12026 (error (message "%s" err))))
12027 (setq js2-mode-pending-parse-callbacks nil)))
12028
12029 (defun js2-mode-flag-region (from to flag)
12030 "Hide or show text from FROM to TO, according to FLAG.
12031 If FLAG is nil then text is shown, while if FLAG is t the text is hidden.
12032 Returns the created overlay if FLAG is non-nil."
12033 (remove-overlays from to 'invisible 'js2-outline)
12034 (when flag
12035 (let ((o (make-overlay from to)))
12036 (overlay-put o 'invisible 'js2-outline)
12037 (overlay-put o 'isearch-open-invisible
12038 'js2-isearch-open-invisible)
12039 o)))
12040
12041 ;; Function to be set as an outline-isearch-open-invisible' property
12042 ;; to the overlay that makes the outline invisible (see
12043 ;; `js2-mode-flag-region').
12044 (defun js2-isearch-open-invisible (_overlay)
12045 ;; We rely on the fact that isearch places point on the matched text.
12046 (js2-mode-show-element))
12047
12048 (defun js2-mode-invisible-overlay-bounds (&optional pos)
12049 "Return cons cell of bounds of folding overlay at POS.
12050 Returns nil if not found."
12051 (let ((overlays (overlays-at (or pos (point))))
12052 o)
12053 (while (and overlays
12054 (not o))
12055 (if (overlay-get (car overlays) 'invisible)
12056 (setq o (car overlays))
12057 (setq overlays (cdr overlays))))
12058 (if o
12059 (cons (overlay-start o) (overlay-end o)))))
12060
12061 (defun js2-mode-function-at-point (&optional pos)
12062 "Return the innermost function node enclosing current point.
12063 Returns nil if point is not in a function."
12064 (let ((node (js2-node-at-point pos)))
12065 (while (and node (not (js2-function-node-p node)))
12066 (setq node (js2-node-parent node)))
12067 (if (js2-function-node-p node)
12068 node)))
12069
12070 (defun js2-mode-toggle-element ()
12071 "Hide or show the foldable element at the point."
12072 (interactive)
12073 (let (comment fn pos)
12074 (save-excursion
12075 (cond
12076 ;; /* ... */ comment?
12077 ((js2-block-comment-p (setq comment (js2-comment-at-point)))
12078 (if (js2-mode-invisible-overlay-bounds
12079 (setq pos (+ 3 (js2-node-abs-pos comment))))
12080 (progn
12081 (goto-char pos)
12082 (js2-mode-show-element))
12083 (js2-mode-hide-element)))
12084 ;; //-comment?
12085 ((save-excursion
12086 (back-to-indentation)
12087 (looking-at js2-mode-//-comment-re))
12088 (js2-mode-toggle-//-comment))
12089 ;; function?
12090 ((setq fn (js2-mode-function-at-point))
12091 (setq pos (and (js2-function-node-body fn)
12092 (js2-node-abs-pos (js2-function-node-body fn))))
12093 (goto-char (1+ pos))
12094 (if (js2-mode-invisible-overlay-bounds)
12095 (js2-mode-show-element)
12096 (js2-mode-hide-element)))
12097 (t
12098 (message "Nothing at point to hide or show"))))))
12099
12100 (defun js2-mode-hide-element ()
12101 "Fold/hide contents of a block, showing ellipses.
12102 Show the hidden text with \\[js2-mode-show-element]."
12103 (interactive)
12104 (if js2-mode-buffer-dirty-p
12105 (js2-mode-wait-for-parse #'js2-mode-hide-element))
12106 (let (node body beg end)
12107 (cond
12108 ((js2-mode-invisible-overlay-bounds)
12109 (message "already hidden"))
12110 (t
12111 (setq node (js2-node-at-point))
12112 (cond
12113 ((js2-block-comment-p node)
12114 (js2-mode-hide-comment node))
12115 (t
12116 (while (and node (not (js2-function-node-p node)))
12117 (setq node (js2-node-parent node)))
12118 (if (and node
12119 (setq body (js2-function-node-body node)))
12120 (progn
12121 (setq beg (js2-node-abs-pos body)
12122 end (+ beg (js2-node-len body)))
12123 (js2-mode-flag-region (1+ beg) (1- end) 'hide))
12124 (message "No collapsable element found at point"))))))))
12125
12126 (defun js2-mode-show-element ()
12127 "Show the hidden element at current point."
12128 (interactive)
12129 (let ((bounds (js2-mode-invisible-overlay-bounds)))
12130 (if bounds
12131 (js2-mode-flag-region (car bounds) (cdr bounds) nil)
12132 (message "Nothing to un-hide"))))
12133
12134 (defun js2-mode-show-all ()
12135 "Show all of the text in the buffer."
12136 (interactive)
12137 (js2-mode-flag-region (point-min) (point-max) nil))
12138
12139 (defun js2-mode-toggle-hide-functions ()
12140 (interactive)
12141 (if js2-mode-functions-hidden
12142 (js2-mode-show-functions)
12143 (js2-mode-hide-functions)))
12144
12145 (defun js2-mode-hide-functions ()
12146 "Hides all non-nested function bodies in the buffer.
12147 Use \\[js2-mode-show-all] to reveal them, or \\[js2-mode-show-element]
12148 to open an individual entry."
12149 (interactive)
12150 (if js2-mode-buffer-dirty-p
12151 (js2-mode-wait-for-parse #'js2-mode-hide-functions))
12152 (if (null js2-mode-ast)
12153 (message "Oops - parsing failed")
12154 (setq js2-mode-functions-hidden t)
12155 (js2-visit-ast js2-mode-ast #'js2-mode-function-hider)))
12156
12157 (defun js2-mode-function-hider (n endp)
12158 (when (not endp)
12159 (let ((tt (js2-node-type n))
12160 body beg end)
12161 (cond
12162 ((and (= tt js2-FUNCTION)
12163 (setq body (js2-function-node-body n)))
12164 (setq beg (js2-node-abs-pos body)
12165 end (+ beg (js2-node-len body)))
12166 (js2-mode-flag-region (1+ beg) (1- end) 'hide)
12167 nil) ; don't process children of function
12168 (t
12169 t))))) ; keep processing other AST nodes
12170
12171 (defun js2-mode-show-functions ()
12172 "Un-hide any folded function bodies in the buffer."
12173 (interactive)
12174 (setq js2-mode-functions-hidden nil)
12175 (save-excursion
12176 (goto-char (point-min))
12177 (while (/= (goto-char (next-overlay-change (point)))
12178 (point-max))
12179 (dolist (o (overlays-at (point)))
12180 (when (and (overlay-get o 'invisible)
12181 (not (overlay-get o 'comment)))
12182 (js2-mode-flag-region (overlay-start o) (overlay-end o) nil))))))
12183
12184 (defun js2-mode-hide-comment (n)
12185 (let* ((head (if (eq (js2-comment-node-format n) 'jsdoc)
12186 3 ; /**
12187 2)) ; /*
12188 (beg (+ (js2-node-abs-pos n) head))
12189 (end (- (+ beg (js2-node-len n)) head 2))
12190 (o (js2-mode-flag-region beg end 'hide)))
12191 (overlay-put o 'comment t)))
12192
12193 (defun js2-mode-toggle-hide-comments ()
12194 "Folds all block comments in the buffer.
12195 Use \\[js2-mode-show-all] to reveal them, or \\[js2-mode-show-element]
12196 to open an individual entry."
12197 (interactive)
12198 (if js2-mode-comments-hidden
12199 (js2-mode-show-comments)
12200 (js2-mode-hide-comments)))
12201
12202 (defun js2-mode-hide-comments ()
12203 (interactive)
12204 (if js2-mode-buffer-dirty-p
12205 (js2-mode-wait-for-parse #'js2-mode-hide-comments))
12206 (if (null js2-mode-ast)
12207 (message "Oops - parsing failed")
12208 (setq js2-mode-comments-hidden t)
12209 (dolist (n (js2-ast-root-comments js2-mode-ast))
12210 (when (js2-block-comment-p n)
12211 (js2-mode-hide-comment n)))
12212 (js2-mode-hide-//-comments)))
12213
12214 (defun js2-mode-extend-//-comment (direction)
12215 "Find start or end of a block of similar //-comment lines.
12216 DIRECTION is -1 to look back, 1 to look forward.
12217 INDENT is the indentation level to match.
12218 Returns the end-of-line position of the furthest adjacent
12219 //-comment line with the same indentation as the current line.
12220 If there is no such matching line, returns current end of line."
12221 (let ((pos (point-at-eol))
12222 (indent (current-indentation)))
12223 (save-excursion
12224 (while (and (zerop (forward-line direction))
12225 (looking-at js2-mode-//-comment-re)
12226 (eq indent (length (match-string 1))))
12227 (setq pos (point-at-eol)))
12228 pos)))
12229
12230 (defun js2-mode-hide-//-comments ()
12231 "Fold adjacent 1-line comments, showing only snippet of first one."
12232 (let (beg end)
12233 (save-excursion
12234 (goto-char (point-min))
12235 (while (re-search-forward js2-mode-//-comment-re nil t)
12236 (setq beg (point)
12237 end (js2-mode-extend-//-comment 1))
12238 (unless (eq beg end)
12239 (overlay-put (js2-mode-flag-region beg end 'hide)
12240 'comment t))
12241 (goto-char end)
12242 (forward-char 1)))))
12243
12244 (defun js2-mode-toggle-//-comment ()
12245 "Fold or un-fold any multi-line //-comment at point.
12246 Caller should have determined that this line starts with a //-comment."
12247 (let* ((beg (point-at-eol))
12248 (end beg))
12249 (save-excursion
12250 (goto-char end)
12251 (if (js2-mode-invisible-overlay-bounds)
12252 (js2-mode-show-element)
12253 ;; else hide the comment
12254 (setq beg (js2-mode-extend-//-comment -1)
12255 end (js2-mode-extend-//-comment 1))
12256 (unless (eq beg end)
12257 (overlay-put (js2-mode-flag-region beg end 'hide)
12258 'comment t))))))
12259
12260 (defun js2-mode-show-comments ()
12261 "Un-hide any hidden comments, leaving other hidden elements alone."
12262 (interactive)
12263 (setq js2-mode-comments-hidden nil)
12264 (save-excursion
12265 (goto-char (point-min))
12266 (while (/= (goto-char (next-overlay-change (point)))
12267 (point-max))
12268 (dolist (o (overlays-at (point)))
12269 (when (overlay-get o 'comment)
12270 (js2-mode-flag-region (overlay-start o) (overlay-end o) nil))))))
12271
12272 (defun js2-mode-display-warnings-and-errors ()
12273 "Turn on display of warnings and errors."
12274 (interactive)
12275 (setq js2-mode-show-parse-errors t
12276 js2-mode-show-strict-warnings t)
12277 (js2-reparse 'force))
12278
12279 (defun js2-mode-hide-warnings-and-errors ()
12280 "Turn off display of warnings and errors."
12281 (interactive)
12282 (setq js2-mode-show-parse-errors nil
12283 js2-mode-show-strict-warnings nil)
12284 (js2-reparse 'force))
12285
12286 (defun js2-mode-toggle-warnings-and-errors ()
12287 "Toggle the display of warnings and errors.
12288 Some users don't like having warnings/errors reported while they type."
12289 (interactive)
12290 (setq js2-mode-show-parse-errors (not js2-mode-show-parse-errors)
12291 js2-mode-show-strict-warnings (not js2-mode-show-strict-warnings))
12292 (if (called-interactively-p 'any)
12293 (message "warnings and errors %s"
12294 (if js2-mode-show-parse-errors
12295 "enabled"
12296 "disabled")))
12297 (js2-reparse 'force))
12298
12299 (defun js2-mode-customize ()
12300 (interactive)
12301 (customize-group 'js2-mode))
12302
12303 (defun js2-mode-forward-sexp (&optional arg)
12304 "Move forward across one statement or balanced expression.
12305 With ARG, do it that many times. Negative arg -N means
12306 move backward across N balanced expressions."
12307 (interactive "p")
12308 (setq arg (or arg 1))
12309 (save-restriction
12310 (widen) ;; `blink-matching-open' calls `narrow-to-region'
12311 (js2-reparse)
12312 (let (forward-sexp-function
12313 node (start (point)) pos lp rp child)
12314 (cond
12315 ;; backward-sexp
12316 ;; could probably make this better for some cases:
12317 ;; - if in statement block (e.g. function body), go to parent
12318 ;; - infix exprs like (foo in bar) - maybe go to beginning
12319 ;; of infix expr if in the right-side expression?
12320 ((and arg (cl-minusp arg))
12321 (dotimes (_ (- arg))
12322 (js2-backward-sws)
12323 (forward-char -1) ; Enter the node we backed up to.
12324 (when (setq node (js2-node-at-point (point) t))
12325 (setq pos (js2-node-abs-pos node))
12326 (let ((parens (js2-mode-forward-sexp-parens node pos)))
12327 (setq lp (car parens)
12328 rp (cdr parens)))
12329 (when (and lp (> start lp))
12330 (if (and rp (<= start rp))
12331 ;; Between parens, check if there's a child node we can jump.
12332 (when (setq child (js2-node-closest-child node (point) lp t))
12333 (setq pos (js2-node-abs-pos child)))
12334 ;; Before both parens.
12335 (setq pos lp)))
12336 (let ((state (parse-partial-sexp start pos)))
12337 (goto-char (if (not (zerop (car state)))
12338 ;; Stumble at the unbalanced paren if < 0, or
12339 ;; jump a bit further if > 0.
12340 (scan-sexps start -1)
12341 pos))))
12342 (unless pos (goto-char (point-min)))))
12343 (t
12344 ;; forward-sexp
12345 (dotimes (_ arg)
12346 (js2-forward-sws)
12347 (when (setq node (js2-node-at-point (point) t))
12348 (setq pos (js2-node-abs-pos node))
12349 (let ((parens (js2-mode-forward-sexp-parens node pos)))
12350 (setq lp (car parens)
12351 rp (cdr parens)))
12352 (or
12353 (when (and rp (<= start rp))
12354 (if (> start lp)
12355 (when (setq child (js2-node-closest-child node (point) rp))
12356 (setq pos (js2-node-abs-end child)))
12357 (setq pos (1+ rp))))
12358 ;; No parens or child nodes, looks for the end of the current node.
12359 (cl-incf pos (js2-node-len
12360 (if (js2-expr-stmt-node-p (js2-node-parent node))
12361 ;; Stop after the semicolon.
12362 (js2-node-parent node)
12363 node))))
12364 (let ((state (save-excursion (parse-partial-sexp start pos))))
12365 (goto-char (if (not (zerop (car state)))
12366 (scan-sexps start 1)
12367 pos))))
12368 (unless pos (goto-char (point-max)))))))))
12369
12370 (defun js2-mode-forward-sexp-parens (node abs-pos)
12371 "Return a cons cell with positions of main parens in NODE."
12372 (cond
12373 ((or (js2-array-node-p node)
12374 (js2-object-node-p node)
12375 (js2-comp-node-p node)
12376 (memq (aref node 0) '(cl-struct-js2-block-node cl-struct-js2-scope)))
12377 (cons abs-pos (+ abs-pos (js2-node-len node) -1)))
12378 ((js2-paren-expr-node-p node)
12379 (let ((lp (js2-node-lp node))
12380 (rp (js2-node-rp node)))
12381 (cons (when lp (+ abs-pos lp))
12382 (when rp (+ abs-pos rp)))))))
12383
12384 (defun js2-node-closest-child (parent point limit &optional before)
12385 (let* ((parent-pos (js2-node-abs-pos parent))
12386 (rpoint (- point parent-pos))
12387 (rlimit (- limit parent-pos))
12388 (min (min rpoint rlimit))
12389 (max (max rpoint rlimit))
12390 found)
12391 (catch 'done
12392 (js2-visit-ast
12393 parent
12394 (lambda (node _end-p)
12395 (if (eq node parent)
12396 t
12397 (let ((pos (js2-node-pos node)) ;; Both relative values.
12398 (end (+ (js2-node-pos node) (js2-node-len node))))
12399 (when (and (>= pos min) (<= end max)
12400 (if before (< pos rpoint) (> end rpoint)))
12401 (setq found node))
12402 (when (> end rpoint)
12403 (throw 'done nil)))
12404 nil))))
12405 found))
12406
12407 (defun js2-errors ()
12408 "Return a list of errors found."
12409 (and js2-mode-ast
12410 (js2-ast-root-errors js2-mode-ast)))
12411
12412 (defun js2-warnings ()
12413 "Return a list of warnings found."
12414 (and js2-mode-ast
12415 (js2-ast-root-warnings js2-mode-ast)))
12416
12417 (defun js2-have-errors-p ()
12418 "Return non-nil if any parse errors or warnings were found."
12419 (or (js2-errors) (js2-warnings)))
12420
12421 (defun js2-errors-and-warnings ()
12422 "Return a copy of the concatenated errors and warnings lists.
12423 They are appended: first the errors, then the warnings.
12424 Entries are of the form (MSG BEG END)."
12425 (when js2-mode-ast
12426 (append (js2-ast-root-errors js2-mode-ast)
12427 (copy-sequence (js2-ast-root-warnings js2-mode-ast)))))
12428
12429 (defun js2-next-error (&optional arg reset)
12430 "Move to next parse error.
12431 Typically invoked via \\[next-error].
12432 ARG is the number of errors, forward or backward, to move.
12433 RESET means start over from the beginning."
12434 (interactive "p")
12435 (if (not (or (js2-errors) (js2-warnings)))
12436 (message "No errors")
12437 (when reset
12438 (goto-char (point-min)))
12439 (let* ((errs (js2-errors-and-warnings))
12440 (continue t)
12441 (start (point))
12442 (count (or arg 1))
12443 (backward (cl-minusp count))
12444 (sorter (if backward '> '<))
12445 (stopper (if backward '< '>))
12446 (count (abs count))
12447 all-errs err)
12448 ;; Sort by start position.
12449 (setq errs (sort errs (lambda (e1 e2)
12450 (funcall sorter (cl-second e1) (cl-second e2))))
12451 all-errs errs)
12452 ;; Find nth error with pos > start.
12453 (while (and errs continue)
12454 (when (funcall stopper (cl-cadar errs) start)
12455 (setq err (car errs))
12456 (if (zerop (cl-decf count))
12457 (setq continue nil)))
12458 (setq errs (cdr errs)))
12459 ;; Clear for `js2-echo-error'.
12460 (message nil)
12461 (if err
12462 (goto-char (cl-second err))
12463 ;; Wrap around to first error.
12464 (goto-char (cl-second (car all-errs)))
12465 ;; If we were already on it, echo msg again.
12466 (if (= (point) start)
12467 (js2-echo-error (point) (point)))))))
12468
12469 (defun js2-down-mouse-3 ()
12470 "Make right-click move the point to the click location.
12471 This makes right-click context menu operations a bit more intuitive.
12472 The point will not move if the region is active, however, to avoid
12473 destroying the region selection."
12474 (interactive)
12475 (when (and js2-move-point-on-right-click
12476 (not mark-active))
12477 (let ((e last-input-event))
12478 (ignore-errors
12479 (goto-char (cl-cadadr e))))))
12480
12481 (defun js2-mode-create-imenu-index ()
12482 "Return an alist for `imenu--index-alist'."
12483 ;; This is built up in `js2-parse-record-imenu' during parsing.
12484 (when js2-mode-ast
12485 ;; if we have an ast but no recorder, they're requesting a rescan
12486 (unless js2-imenu-recorder
12487 (js2-reparse 'force))
12488 (prog1
12489 (js2-build-imenu-index)
12490 (setq js2-imenu-recorder nil
12491 js2-imenu-function-map nil))))
12492
12493 (defun js2-mode-find-tag ()
12494 "Replacement for `find-tag-default'.
12495 `find-tag-default' returns a ridiculous answer inside comments."
12496 (let (beg end)
12497 (js2-with-underscore-as-word-syntax
12498 (save-excursion
12499 (if (and (not (looking-at "[[:alnum:]_$]"))
12500 (looking-back "[[:alnum:]_$]"))
12501 (setq beg (progn (forward-word -1) (point))
12502 end (progn (forward-word 1) (point)))
12503 (setq beg (progn (forward-word 1) (point))
12504 end (progn (forward-word -1) (point))))
12505 (replace-regexp-in-string
12506 "[\"']" ""
12507 (buffer-substring-no-properties beg end))))))
12508
12509 (defun js2-mode-forward-sibling ()
12510 "Move to the end of the sibling following point in parent.
12511 Returns non-nil if successful, or nil if there was no following sibling."
12512 (let* ((node (js2-node-at-point))
12513 (parent (js2-mode-find-enclosing-fn node))
12514 sib)
12515 (when (setq sib (js2-node-find-child-after (point) parent))
12516 (goto-char (+ (js2-node-abs-pos sib)
12517 (js2-node-len sib))))))
12518
12519 (defun js2-mode-backward-sibling ()
12520 "Move to the beginning of the sibling node preceding point in parent.
12521 Parent is defined as the enclosing script or function."
12522 (let* ((node (js2-node-at-point))
12523 (parent (js2-mode-find-enclosing-fn node))
12524 sib)
12525 (when (setq sib (js2-node-find-child-before (point) parent))
12526 (goto-char (js2-node-abs-pos sib)))))
12527
12528 (defun js2-beginning-of-defun (&optional arg)
12529 "Go to line on which current function starts, and return t on success.
12530 If we're not in a function or already at the beginning of one, go
12531 to beginning of previous script-level element.
12532 With ARG N, do that N times. If N is negative, move forward."
12533 (setq arg (or arg 1))
12534 (if (cl-plusp arg)
12535 (let ((parent (js2-node-parent-script-or-fn (js2-node-at-point))))
12536 (when (cond
12537 ((js2-function-node-p parent)
12538 (goto-char (js2-node-abs-pos parent)))
12539 (t
12540 (js2-mode-backward-sibling)))
12541 (if (> arg 1)
12542 (js2-beginning-of-defun (1- arg))
12543 t)))
12544 (when (js2-end-of-defun)
12545 (js2-beginning-of-defun (if (>= arg -1) 1 (1+ arg))))))
12546
12547 (defun js2-end-of-defun ()
12548 "Go to the char after the last position of the current function
12549 or script-level element."
12550 (let* ((node (js2-node-at-point))
12551 (parent (or (and (js2-function-node-p node) node)
12552 (js2-node-parent-script-or-fn node)))
12553 script)
12554 (unless (js2-function-node-p parent)
12555 ;; Use current script-level node, or, if none, the next one.
12556 (setq script (or parent node)
12557 parent (js2-node-find-child-before (point) script))
12558 (when (or (null parent)
12559 (>= (point) (+ (js2-node-abs-pos parent)
12560 (js2-node-len parent))))
12561 (setq parent (js2-node-find-child-after (point) script))))
12562 (when parent
12563 (goto-char (+ (js2-node-abs-pos parent)
12564 (js2-node-len parent))))))
12565
12566 (defun js2-mark-defun (&optional allow-extend)
12567 "Put mark at end of this function, point at beginning.
12568 The function marked is the one that contains point.
12569
12570 Interactively, if this command is repeated,
12571 or (in Transient Mark mode) if the mark is active,
12572 it marks the next defun after the ones already marked."
12573 (interactive "p")
12574 (let (extended)
12575 (when (and allow-extend
12576 (or (and (eq last-command this-command) (mark t))
12577 (and transient-mark-mode mark-active)))
12578 (let ((sib (save-excursion
12579 (goto-char (mark))
12580 (if (js2-mode-forward-sibling)
12581 (point)))))
12582 (if sib
12583 (progn
12584 (set-mark sib)
12585 (setq extended t))
12586 ;; no more siblings - try extending to enclosing node
12587 (goto-char (mark t)))))
12588 (when (not extended)
12589 (let ((node (js2-node-at-point (point) t)) ; skip comments
12590 ast fn stmt parent beg end)
12591 (when (js2-ast-root-p node)
12592 (setq ast node
12593 node (or (js2-node-find-child-after (point) node)
12594 (js2-node-find-child-before (point) node))))
12595 ;; only mark whole buffer if we can't find any children
12596 (if (null node)
12597 (setq node ast))
12598 (if (js2-function-node-p node)
12599 (setq parent node)
12600 (setq fn (js2-mode-find-enclosing-fn node)
12601 stmt (if (or (null fn)
12602 (js2-ast-root-p fn))
12603 (js2-mode-find-first-stmt node))
12604 parent (or stmt fn)))
12605 (setq beg (js2-node-abs-pos parent)
12606 end (+ beg (js2-node-len parent)))
12607 (push-mark beg)
12608 (goto-char end)
12609 (exchange-point-and-mark)))))
12610
12611 (defun js2-narrow-to-defun ()
12612 "Narrow to the function enclosing point."
12613 (interactive)
12614 (let* ((node (js2-node-at-point (point) t)) ; skip comments
12615 (fn (if (js2-script-node-p node)
12616 node
12617 (js2-mode-find-enclosing-fn node)))
12618 (beg (js2-node-abs-pos fn)))
12619 (unless (js2-ast-root-p fn)
12620 (narrow-to-region beg (+ beg (js2-node-len fn))))))
12621
12622 (provide 'js2-mode)
12623
12624 ;;; js2-mode.el ends here