-
Notifications
You must be signed in to change notification settings - Fork 0
/
js3-mode.el
11965 lines (10693 loc) · 416 KB
/
js3-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; js3.el -- an improved JavaScript editing mode
;;;
;;; js3-head.el
;; Author: Thom Blake ([email protected])
;; Authors of historical versions:
;; (espresso-mode) Karl Landstrom <[email protected]>
;; (js2-mode) Steve Yegge ([email protected])
;; (js-mode) Daniel Colascione <[email protected]>
;; With some code from: https://github.com/mooz/js2-mode/
;;
;; Keywords: javascript languages
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;; Commentary:
;; This JavaScript editing mode supports:
;;
;; - the full JavaScript language through version 1.8
;; - support for most Rhino and SpiderMonkey extensions from 1.5 to 1.8
;; - accurate syntax highlighting using a recursive-descent parser
;; - syntax-error and strict-mode warning reporting
;; - smart line-wrapping within comments (Emacs 22+) and strings
;; - code folding:
;; - show some or all function bodies as {...}
;; - show some or all block comments as /*...*/
;; - context-sensitive menu bar and popup menus
;; - code browsing using the `imenu' package
;; - typing helpers (e.g. inserting matching braces/parens)
;; - many customization options
;;
;; It is only compatible with GNU Emacs versions 21 and higher (not XEmacs).
;;
;; Installation:
;;
;; - put `js3.el' somewhere in your emacs load path
;; - M-x byte-compile-file RET <path-to-js3.el> RET
;; Note: it will refuse to run unless byte-compiled
;; - add these lines to your .emacs file:
;; (autoload 'js3-mode "js3" nil t)
;; (add-to-list 'auto-mode-alist '("\\.js$" . js3-mode))
;;
;; To customize how it works:
;; M-x customize-group RET js3-mode RET
;;
;; The variable `js3-mode-version' is a date stamp. When you upgrade
;; to a newer version, you must byte-compile the file again.
;;
;; Notes:
;;
;; This mode is different in many ways from standard Emacs language editing
;; modes, inasmuch as it attempts to be more like an IDE. If this drives
;; you crazy, it IS possible to customize it to be more like other Emacs
;; editing modes. Please customize the group `js3-mode' to see all of the
;; configuration options.
;;
;; Some of the functionality does not work in Emacs 21 -- upgrading to
;; Emacs 22 or higher will get you better results. If you byte-compiled
;; js3.el with Emacs 21, you should re-compile it for Emacs 22.
;;
;; This mode does not yet work with "multi-mode" modes such as mmm-mode
;; and mumamo, although it could possibly be made to do so with some effort.
;; This means that js3-mode is currently only useful for editing JavaScript
;; files, and not for editing JavaScript within <script> tags or templates.
;;
;; Please submit bug reports to github at https://github.com/thomblake/js3-mode
;;; Code:
;;; js3-head.el ends here
;;; js3-externs.el -- external name definitions
(defvar js3-ecma-262-externs
(mapcar 'symbol-name
'(Array
Boolean
Date
Error
EvalError
Function
Infinity
JSON
Math
NaN
Number
Object
RangeError
ReferenceError
RegExp
String
SyntaxError
TypeError
URIError
arguments
decodeURI
decodeURIComponent
encodeURI
encodeURIComponent
escape
eval
isFinite
isNaN
parseFloat
parseInt
undefined
unescape))
"Ecma-262 externs. Included in `js3-externs' by default.")
(defvar js3-browser-externs
(mapcar 'symbol-name
'(;; DOM level 1
Attr
CDATASection
CharacterData
Comment
DOMException
DOMImplementation
Document
DocumentFragment
DocumentType
Element
Entity
EntityReference
ExceptionCode
NamedNodeMap
Node
NodeList
Notation
ProcessingInstruction
Text
;; DOM level 2
HTMLAnchorElement
HTMLAppletElement
HTMLAreaElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCollection
HTMLDListElement
HTMLDirectoryElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLFieldSetElement
HTMLFontElement
HTMLFormElement
HTMLFrameElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMenuElement
HTMLMetaElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLParagraphElement
HTMLParamElement
HTMLPreElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTextAreaElement
HTMLTitleElement
HTMLUListElement
;; DOM level 3
DOMConfiguration
DOMError
DOMException
DOMImplementationList
DOMImplementationSource
DOMLocator
DOMStringList
NameList
TypeInfo
UserDataHandler
;; Window
window
alert
confirm
document
java
navigator
prompt
screen
self
top
;; W3C CSS
CSSCharsetRule
CSSFontFace
CSSFontFaceRule
CSSImportRule
CSSMediaRule
CSSPageRule
CSSPrimitiveValue
CSSProperties
CSSRule
CSSRuleList
CSSStyleDeclaration
CSSStyleRule
CSSStyleSheet
CSSValue
CSSValueList
Counter
DOMImplementationCSS
DocumentCSS
DocumentStyle
ElementCSSInlineStyle
LinkStyle
MediaList
RGBColor
Rect
StyleSheet
StyleSheetList
ViewCSS
;; W3C Event
EventListener
EventTarget
Event
DocumentEvent
UIEvent
MouseEvent
MutationEvent
KeyboardEvent
;; W3C Range
DocumentRange
Range
RangeException
;; W3C XML
XPathResult
XMLHttpRequest
))
"Browser externs.
You can cause these to be included or excluded with the custom
variable `js3-include-browser-externs'.")
(defvar js3-rhino-externs
(mapcar 'symbol-name
'(Packages
importClass
importPackage
com
org
java
;; Global object (shell) externs
defineClass
deserialize
doctest
gc
help
load
loadClass
print
quit
readFile
readUrl
runCommand
seal
serialize
spawn
sync
toint32
version))
"Mozilla Rhino externs.
Set `js3-include-rhino-externs' to t to include them.")
(defvar js3-gears-externs
(mapcar 'symbol-name
'(
;; finish me!
))
"Google Gears externs.
Set `js3-include-gears-externs' to t to include them.")
(provide 'js3-externs)
;;; js3-externs.el ends here
;;; js3-vars.el -- byte-compiler support for js3-mode
;;; Code:
(eval-when-compile
(require 'cl))
(require 'thingatpt) ; forward-symbol etc
(eval-and-compile
(require 'cc-mode) ; (only) for `c-populate-syntax-table'
(require 'cc-langs) ; it's here in Emacs 21...
(require 'cc-engine)) ; for `c-paragraph-start' et. al.
(defvar js3-emacs22 (>= emacs-major-version 22))
(defun js3-mark-safe-local (name pred)
"Make the variable NAME buffer-local and mark it as safe file-local
variable with predicate PRED."
(make-variable-buffer-local name)
(put name 'safe-local-variable pred))
(defcustom js3-highlight-level 2
"Amount of syntax highlighting to perform.
nil, zero or negative means none.
1 adds basic syntax highlighting.
2 adds highlighting of some Ecma built-in properties.
3 adds highlighting of many Ecma built-in functions."
:group 'js3-mode
:type '(choice (const :tag "None" nil)
(const :tag "Basic" 1)
(const :tag "Include Properties" 2)
(const :tag "Include Functions" 3)))
(defgroup js3-mode nil
"An improved JavaScript mode."
:group 'languages)
(defcustom js3-mode-dev-mode-p nil
"Non-nil if running in development mode. Normally nil."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact t
"Printing variable.
If set to t, try to shorten as much as possible onto one line.
Overrides other compact settings."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-while nil
"Printing variable.
If set to t, try to shorten while statements onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-for nil
"Printing variable.
If set to t, try to shorten for statements onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-if nil
"Printing variable.
If set to t, try to shorten if statements onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-infix nil
"Printing variable.
If set to t, try to shorten infix expressions onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-expr nil
"Printing variable.
If set to t, try to shorten expressions onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-list nil
"Printing variable.
If set to t, try to shorten lists onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-compact-case nil
"Printing variable.
If set to t, try to shorten case statements onto one line."
:group 'js3-mode
:type 'boolean)
(defcustom js3-max-columns 72
"Printing variable.
Max number of columns per line."
:group 'js3-mode
:type 'integer)
(defcustom js3-indent-tabs-mode nil
"Default setting for indent-tabs-mode for js3-mode."
:group 'js3-mode
:type 'boolean)
(js3-mark-safe-local 'js3-indent-tabs-mode 'booleanp)
(defcustom js3-pretty-vars t
"Non-nil to try to indent comma-last continued var statements in a pretty way.
Does not affect comma-first continued var statements.
Note that this forces a reparse so should be turned off if not being used"
:group 'js3-mode
:type 'boolean)
(js3-mark-safe-local 'js3-pretty-vars 'booleanp)
(defcustom js3-pretty-lazy-vars t
"Non-nil to try to indent comma-first continued var statements correctly
when `js3-lazy-commas' is t"
:group 'js3-mode
:type 'boolean)
(js3-mark-safe-local 'js3-pretty-lazy-vars 'booleanp)
(defcustom js3-cleanup-whitespace t
"Non-nil to invoke `delete-trailing-whitespace' before saves."
:type 'boolean
:group 'js3-mode)
(defcustom js3-move-point-on-right-click t
"Non-nil to move insertion point when you right-click.
This makes right-click context menu behavior a bit more intuitive,
since menu operations generally apply to the point. The exception
is if there is a region selection, in which case the point does -not-
move, so cut/copy/paste etc. can work properly.
Note that IntelliJ moves the point, and Eclipse leaves it alone,
so this behavior is customizable."
:group 'js3-mode
:type 'boolean)
(defcustom js3-mirror-mode nil
"Non-nil to insert closing brackets, parens, etc. automatically."
:group 'js3-mode
:type 'boolean)
(defcustom js3-auto-indent-p nil
"Automatic indentation with punctuation characters. If non-nil, the
current line is indented when certain punctuations are inserted."
:group 'js3-mode
:type 'boolean)
(js3-mark-safe-local 'js3-auto-indent-p 'booleanp)
(defcustom js3-consistent-level-indent-inner-bracket nil
"Non-nil to make indentation level inner bracket consistent,
regardless of the beginning bracket position."
:group 'js3-mode
:type 'boolean)
(js3-mark-safe-local 'js3-consistent-level-indent-inner-bracket 'booleanp)
(defcustom js3-indent-on-enter-key nil
"Non-nil to have Enter/Return key indent the line.
This is unusual for Emacs modes but common in IDEs like Eclipse."
:type 'boolean
:group 'js3-mode)
(js3-mark-safe-local 'js3-indent-on-enter-key 'booleanp)
(defcustom js3-enter-indents-newline nil
"Non-nil to have Enter/Return key indent the newly-inserted line.
This is unusual for Emacs modes but common in IDEs like Eclipse."
:type 'boolean
:group 'js3-mode)
(js3-mark-safe-local 'js3-enter-indents-newline 'booleanp)
(defcustom js3-rebind-eol-bol-keys nil
"Non-nil to rebind beginning-of-line and end-of-line keys.
If non-nil, bounce between bol/eol and first/last non-whitespace char."
:group 'js3-mode
:type 'boolean)
(defcustom js3-electric-keys '("{" "}" "(" ")" "[" "]" ":" ";" "," "*")
"Keys that auto-indent when `js3-auto-indent-p' is non-nil.
Each value in the list is passed to `define-key'."
:type 'list
:group 'js3-mode)
(defcustom js3-idle-timer-delay 0.2
"Delay in secs before re-parsing after user makes changes.
Multiplied by `js3-dynamic-idle-timer-adjust', which see."
:type 'number
:group 'js3-mode)
(make-variable-buffer-local 'js3-idle-timer-delay)
(defcustom js3-dynamic-idle-timer-adjust 0
"Positive to adjust `js3-idle-timer-delay' based on file size.
The idea is that for short files, parsing is faster so we can be
more responsive to user edits without interfering with editing.
The buffer length in characters (typically bytes) is divided by
this value and used to multiply `js3-idle-timer-delay' for the
buffer. For example, a 21k file and 10k adjust yields 21k/10k
== 2, so js3-idle-timer-delay is multiplied by 2.
If `js3-dynamic-idle-timer-adjust' is 0 or negative,
`js3-idle-timer-delay' is not dependent on the file size."
:type 'number
:group 'js3-mode)
(defcustom js3-mode-escape-quotes t
"Non-nil to disable automatic quote-escaping inside strings."
:type 'boolean
:group 'js3-mode)
(defcustom js3-mode-squeeze-spaces t
"Non-nil to normalize whitespace when filling in comments.
Multiple runs of spaces are converted to a single space."
:type 'boolean
:group 'js3-mode)
(defcustom js3-mode-show-parse-errors t
"True to highlight parse errors."
:type 'boolean
:group 'js3-mode)
(defcustom js3-mode-show-strict-warnings t
"Non-nil to emit Ecma strict-mode warnings.
Some of the warnings can be individually disabled by other flags,
even if this flag is non-nil."
:type 'boolean
:group 'js3-mode)
(defcustom js3-strict-trailing-comma-warning t
"Non-nil to warn about trailing commas in array literals.
Ecma-262 forbids them, but many browsers permit them. IE is the
big exception, and can produce bugs if you have trailing commas."
:type 'boolean
:group 'js3-mode)
(defcustom js3-strict-missing-semi-warning nil
"Non-nil to warn about semicolon auto-insertion after statement.
Technically this is legal per Ecma-262, but some style guides disallow
depending on it."
:type 'boolean
:group 'js3-mode)
(defcustom js3-missing-semi-one-line-override nil
"Non-nil to permit missing semicolons in one-line functions.
In one-liner functions such as `function identity(x) {return x}'
people often omit the semicolon for a cleaner look. If you are
such a person, you can suppress the missing-semicolon warning
by setting this variable to t."
:type 'boolean
:group 'js3-mode)
(defcustom js3-strict-inconsistent-return-warning t
"Non-nil to warn about mixing returns with value-returns.
It's perfectly legal to have a `return' and a `return foo' in the
same function, but it's often an indicator of a bug, and it also
interferes with type inference (in systems that support it.)"
:type 'boolean
:group 'js3-mode)
(defcustom js3-strict-cond-assign-warning t
"Non-nil to warn about expressions like if (a = b).
This often should have been '==' instead of '='. If the warning
is enabled, you can suppress it on a per-expression basis by
parenthesizing the expression, e.g. if ((a = b)) ..."
:type 'boolean
:group 'js3-mode)
(defcustom js3-strict-var-redeclaration-warning t
"Non-nil to warn about redeclaring variables in a script or function."
:type 'boolean
:group 'js3-mode)
(defcustom js3-strict-var-hides-function-arg-warning t
"Non-nil to warn about a var decl hiding a function argument."
:type 'boolean
:group 'js3-mode)
(defcustom js3-skip-preprocessor-directives nil
"Non-nil to treat lines beginning with # as comments.
Useful for viewing Mozilla JavaScript source code."
:type 'boolean
:group 'js3-mode)
(defcustom js3-language-version 180
"Configures what JavaScript language version to recognize.
Currently versions 150, 160, 170 and 180 are supported, corresponding
to JavaScript 1.5, 1.6, 1.7 and 1.8, respectively. In a nutshell,
1.6 adds E4X support, 1.7 adds let, yield, and Array comprehensions,
and 1.8 adds function closures."
:type 'integer
:group 'js3-mode)
(defcustom js3-allow-keywords-as-property-names t
"If non-nil, you can use JavaScript keywords as object property names.
Examples:
var foo = {int: 5, while: 6, continue: 7};
foo.return = 8;
Ecma-262 forbids this syntax, but many browsers support it."
:type 'boolean
:group 'js3-mode)
(defcustom js3-instanceof-has-side-effects nil
"If non-nil, treats the instanceof operator as having side effects.
This is useful for xulrunner apps."
:type 'boolean
:group 'js3-mode)
(defcustom js3-allow-rhino-new-expr-initializer nil
"Non-nil to support a Rhino's experimental syntactic construct.
Rhino supports the ability to follow a `new' expression with an object
literal, which is used to set additional properties on the new object
after calling its constructor. Syntax:
new <expr> [ ( arglist ) ] [initializer]
Hence, this expression:
new Object {a: 1, b: 2}
results in an Object with properties a=1 and b=2. This syntax is
apparently not configurable in Rhino - it's currently always enabled,
as of Rhino version 1.7R2."
:type 'boolean
:group 'js3-mode)
(defcustom js3-allow-member-expr-as-function-name nil
"Non-nil to support experimental Rhino syntax for function names.
Rhino supports an experimental syntax configured via the Rhino Context
setting `allowMemberExprAsFunctionName'. The experimental syntax is:
function <member-expr> ( [ arg-list ] ) { <body> }
Where member-expr is a non-parenthesized 'member expression', which
is anything at the grammar level of a new-expression or lower, meaning
any expression that does not involve infix or unary operators.
When <member-expr> is not a simple identifier, then it is syntactic
sugar for assigning the anonymous function to the <member-expr>. Hence,
this code:
function a.b().c[2] (x, y) { ... }
is rewritten as:
a.b().c[2] = function(x, y) {...}
which doesn't seem particularly useful, but Rhino permits it."
:type 'boolean
:group 'js3-mode)
(defvar js3-mode-version 20110809
"Release number for `js3-mode'.")
;; scanner variables
(defmacro deflocal (name value &optional comment)
`(progn
(defvar ,name ,value ,comment)
(make-variable-buffer-local ',name)))
;; We record the start and end position of each token.
(deflocal js3-token-beg 1)
(deflocal js3-token-end -1)
(defvar js3-EOF_CHAR -1
"Represents end of stream. Distinct from js3-EOF token type.")
;; I originally used symbols to represent tokens, but Rhino uses
;; ints and then sets various flag bits in them, so ints it is.
;; The upshot is that we need a `js3-' prefix in front of each name.
(defvar js3-ERROR -1)
(defvar js3-EOF 0)
(defvar js3-EOL 1)
(defvar js3-ENTERWITH 2) ; begin interpreter bytecodes
(defvar js3-LEAVEWITH 3)
(defvar js3-RETURN 4)
(defvar js3-GOTO 5)
(defvar js3-IFEQ 6)
(defvar js3-IFNE 7)
(defvar js3-SETNAME 8)
(defvar js3-BITOR 9)
(defvar js3-BITXOR 10)
(defvar js3-BITAND 11)
(defvar js3-ADD 12) ; infix plus
(defvar js3-SUB 13) ; infix minus
(defvar js3-MUL 14)
(defvar js3-DIV 15)
(defvar js3-MOD 16)
(defvar js3-LT 17)
(defvar js3-GT 18)
(defvar js3-EQ 19)
(defvar js3-NE 20)
(defvar js3-LE 21)
(defvar js3-GE 22)
(defvar js3-LSH 23)
(defvar js3-RSH 24)
(defvar js3-URSH 25)
(defvar js3-NOT 26)
(defvar js3-BITNOT 27)
(defvar js3-POS 28) ; unary plus
(defvar js3-NEG 29) ; unary minus
(defvar js3-NEW 30)
(defvar js3-DELPROP 31)
(defvar js3-TYPEOF 32)
(defvar js3-GETPROP 33)
(defvar js3-GETPROPNOWARN 34)
(defvar js3-SETPROP 35)
(defvar js3-GETELEM 36)
(defvar js3-SETELEM 37)
(defvar js3-CALL 38)
(defvar js3-NAME 39) ; an identifier
(defvar js3-NUMBER 40)
(defvar js3-STRING 41)
(defvar js3-NULL 42)
(defvar js3-THIS 43)
(defvar js3-FALSE 44)
(defvar js3-TRUE 45)
(defvar js3-SHEQ 46) ; shallow equality (===)
(defvar js3-SHNE 47) ; shallow inequality (!==)
(defvar js3-REGEXP 48)
(defvar js3-BINDNAME 49)
(defvar js3-THROW 50)
(defvar js3-RETHROW 51) ; rethrow caught exception: catch (e if ) uses it
(defvar js3-IN 52)
(defvar js3-INSTANCEOF 53)
(defvar js3-LOCAL_LOAD 54)
(defvar js3-GETVAR 55)
(defvar js3-SETVAR 56)
(defvar js3-CATCH_SCOPE 57)
(defvar js3-ENUM_INIT_KEYS 58)
(defvar js3-ENUM_INIT_VALUES 59)
(defvar js3-ENUM_INIT_ARRAY 60)
(defvar js3-ENUM_NEXT 61)
(defvar js3-ENUM_ID 62)
(defvar js3-THISFN 63)
(defvar js3-RETURN_RESULT 64) ; to return previously stored return result
(defvar js3-ARRAYLIT 65) ; array literal
(defvar js3-OBJECTLIT 66) ; object literal
(defvar js3-GET_REF 67) ; *reference
(defvar js3-SET_REF 68) ; *reference = something
(defvar js3-DEL_REF 69) ; delete reference
(defvar js3-REF_CALL 70) ; f(args) = something or f(args)++
(defvar js3-REF_SPECIAL 71) ; reference for special properties like __proto
(defvar js3-YIELD 72) ; JS 1.7 yield pseudo keyword
;; deprecated
(defvar js3-DEPRECATED-A 73)
(defvar js3-DEPRECATED-B 74)
(defvar js3-DEPRECATED-C 75)
(defvar js3-DEPRECATED-D 76)
(defvar js3-DEPRECATED-E 77)
(defvar js3-DEPRECATED-F 78)
(defvar js3-DEPRECATED-G 79)
(defvar js3-TRY 80)
(defvar js3-SEMI 81) ; semicolon
(defvar js3-LB 82) ; left and right brackets
(defvar js3-RB 83)
(defvar js3-LC 84) ; left and right curly-braces
(defvar js3-RC 85)
(defvar js3-LP 86) ; left and right parens
(defvar js3-RP 87)
(defvar js3-COMMA 88) ; comma operator
(defvar js3-ASSIGN 89) ; simple assignment (=)
(defvar js3-ASSIGN_BITOR 90) ; |=
(defvar js3-ASSIGN_BITXOR 91) ; ^=
(defvar js3-ASSIGN_BITAND 92) ; &=
(defvar js3-ASSIGN_ADD 93) ; +=
(defvar js3-ASSIGN_SUB 94) ; -=
(defvar js3-ASSIGN_MUL 95) ; *=
(defvar js3-ASSIGN_DIV 96) ; /=
(defvar js3-ASSIGN_MOD 97) ; %=
(defvar js3-ASSIGN_LSH 98) ; <<=
(defvar js3-ASSIGN_RSH 99) ; >>=
(defvar js3-ASSIGN_URSH 100) ; >>>=
(defvar js3-first-assign js3-ASSIGN)
(defvar js3-last-assign js3-ASSIGN_MOD)
(defvar js3-HOOK 101) ; conditional (?:)
(defvar js3-COLON 102)
(defvar js3-OR 103) ; logical or (||)
(defvar js3-AND 104) ; logical and (&&)
(defvar js3-INC 105) ; increment/decrement (++ --)
(defvar js3-DEC 106)
(defvar js3-DOT 107) ; member operator (.)
(defvar js3-FUNCTION 108) ; function keyword
(defvar js3-EXPORT 109) ; export keyword
(defvar js3-IMPORT 110) ; import keyword
(defvar js3-IF 111) ; if keyword
(defvar js3-ELSE 112) ; else keyword
(defvar js3-SWITCH 113) ; switch keyword
(defvar js3-CASE 114) ; case keyword
(defvar js3-DEFAULT 115) ; default keyword
(defvar js3-WHILE 116) ; while keyword
(defvar js3-DO 117) ; do keyword
(defvar js3-FOR 118) ; for keyword
(defvar js3-BREAK 119) ; break keyword
(defvar js3-CONTINUE 120) ; continue keyword
(defvar js3-VAR 121) ; var keyword
(defvar js3-WITH 122) ; with keyword
(defvar js3-CATCH 123) ; catch keyword
(defvar js3-FINALLY 124) ; finally keyword
(defvar js3-VOID 125) ; void keyword
(defvar js3-RESERVED 126) ; reserved keywords
(defvar js3-EMPTY 127)
;; Types used for the parse tree - never returned by scanner.
(defvar js3-BLOCK 128) ; statement block
(defvar js3-LABEL 129) ; label
(defvar js3-TARGET 130)
(defvar js3-LOOP 131)
(defvar js3-EXPR_VOID 132) ; expression statement in functions
(defvar js3-EXPR_RESULT 133) ; expression statement in scripts
(defvar js3-JSR 134)
(defvar js3-SCRIPT 135) ; top-level node for entire script
(defvar js3-TYPEOFNAME 136) ; for typeof(simple-name)
(defvar js3-USE_STACK 137)
(defvar js3-SETPROP_OP 138) ; x.y op= something
(defvar js3-SETELEM_OP 139) ; x[y] op= something
(defvar js3-LOCAL_BLOCK 140)
(defvar js3-SET_REF_OP 141) ; *reference op= something
;; deprecated
(defvar js3-DEPRECATED-H 142)
(defvar js3-DEPRECATED-I 143)
(defvar js3-DEPRECATED-J 144)
(defvar js3-DEPRECATED-K 145)
(defvar js3-DEPRECATED-L 146)
(defvar js3-DEPRECATED-M 147)
;; Optimizer-only tokens
(defvar js3-TO_OBJECT 148)
(defvar js3-TO_DOUBLE 149)
(defvar js3-GET 150) ; JS 1.5 get pseudo keyword
(defvar js3-SET 151) ; JS 1.5 set pseudo keyword
(defvar js3-LET 152) ; JS 1.7 let pseudo keyword
(defvar js3-CONST 153)
(defvar js3-SETCONST 154)
(defvar js3-SETCONSTVAR 155)
(defvar js3-ARRAYCOMP 156)
(defvar js3-LETEXPR 157)
(defvar js3-WITHEXPR 158)
(defvar js3-DEBUGGER 159)
(defvar js3-COMMENT 160)
(defvar js3-ENUM 161) ; for "enum" reserved word
(defconst js3-num-tokens (1+ js3-ENUM))
(defconst js3-debug-print-trees nil)
;; Rhino accepts any string or stream as input.
;; Emacs character processing works best in buffers, so we'll
;; assume the input is a buffer. JavaScript strings can be
;; copied into temp buffers before scanning them.
;; Buffer-local variables yield much cleaner code than using `defstruct'.
;; They're the Emacs equivalent of instance variables, more or less.
(deflocal js3-ts-dirty-line nil
"Token stream buffer-local variable.
Indicates stuff other than whitespace since start of line.")
(deflocal js3-ts-regexp-flags nil
"Token stream buffer-local variable.")
(deflocal js3-ts-string ""
"Token stream buffer-local variable.
Last string scanned.")
(deflocal js3-ts-number nil
"Token stream buffer-local variable.
Last literal number scanned.")
(deflocal js3-ts-hit-eof nil
"Token stream buffer-local variable.")
(deflocal js3-ts-line-start 0
"Token stream buffer-local variable.")
(deflocal js3-ts-lineno 1
"Token stream buffer-local variable.")
(deflocal js3-ts-line-end-char -1
"Token stream buffer-local variable.")
(deflocal js3-ts-cursor 1 ; emacs buffers are 1-indexed
"Token stream buffer-local variable.
Current scan position.")
(deflocal js3-ts-string-buffer nil
"Token stream buffer-local variable.
List of chars built up while scanning various tokens.")
(deflocal js3-ts-comment-type nil
"Token stream buffer-local variable.")
;;; Parser variables
(deflocal js3-parsed-errors nil
"List of errors produced during scanning/parsing.")
(deflocal js3-parsed-warnings nil
"List of warnings produced during scanning/parsing.")
(deflocal js3-recover-from-parse-errors t
"Non-nil to continue parsing after a syntax error.
In recovery mode, the AST will be built in full, and any error
nodes will be flagged with appropriate error information. If
this flag is nil, a syntax error will result in an error being
signaled.
The variable is automatically buffer-local, because different
modes that use the parser will need different settings.")
(deflocal js3-parse-hook nil
"List of callbacks for receiving parsing progress.")
(defvar js3-parse-finished-hook nil
"List of callbacks to notify when parsing finishes.
Not called if parsing was interrupted.")
(deflocal js3-is-eval-code nil
"True if we're evaluating code in a string.
If non-nil, the tokenizer will record the token text, and the AST nodes
will record their source text. Off by default for IDE modes, since the
text is available in the buffer.")
(defvar js3-parse-ide-mode t
"Non-nil if the parser is being used for `js3-mode'.
If non-nil, the parser will set text properties for fontification
and the syntax-table. The value should be nil when using the
parser as a frontend to an interpreter or byte compiler.")
;;; Parser instance variables (buffer-local vars for js3-parse)
(defconst js3-clear-ti-mask #xFFFF
"Mask to clear token information bits.")
(defconst js3-ti-after-eol (lsh 1 16)
"Flag: first token of the source line.")
(defconst js3-ti-check-label (lsh 1 17)
"Flag: indicates to check for label.")
;; Inline Rhino's CompilerEnvirons vars as buffer-locals.
(deflocal js3-compiler-generate-debug-info t)
(deflocal js3-compiler-use-dynamic-scope nil)
(deflocal js3-compiler-reserved-keywords-as-identifier nil)
(deflocal js3-compiler-optimization-level 0)
(deflocal js3-compiler-generating-source t)
(deflocal js3-compiler-strict-mode nil)
(deflocal js3-compiler-report-warning-as-error nil)
(deflocal js3-compiler-generate-observer-count nil)
(deflocal js3-compiler-activation-names nil)
;; SKIP: sourceURI
;; There's a compileFunction method in Context.java - may need it.
(deflocal js3-called-by-compile-function nil
"True if `js3-parse' was called by `js3-compile-function'.
Will only be used when we finish implementing the interpreter.")
;; SKIP: ts (we just call `js3-init-scanner' and use its vars)
(deflocal js3-current-flagged-token js3-EOF)
(deflocal js3-current-token js3-EOF)
;; SKIP: node factory - we're going to just call functions directly,
;; and eventually go to a unified AST format.