Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 4fb12ff942
Fetching contributors…

Cannot retrieve contributors at this time

executable file 11928 lines (10682 sloc) 450.507 kB
;;; js2.el -- an improved JavaScript editing mode
;;;
;;; This file was auto-generated on Thu Jul 23 16:21:42 2009 from files:
;;; js2-externs.el
;;; js2-vars.el
;;; js2-util.el
;;; js2-scan.el
;;; js2-messages.el
;;; js2-ast.el
;;; js2-highlight.el
;;; js2-browse.el
;;; js2-parse.el
;;; js2-indent.el
;;; js2-mode.el
;;; js2-mode.el --- an improved JavaScript editing mode
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Version: 20090723
;; 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
;; - "bouncing" line indentation to choose among alternate indentation points
;; - 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 `js2.el' somewhere in your emacs load path
;; - M-x byte-compile-file RET <path-to-js2.el> RET
;; Note: it will refuse to run unless byte-compiled
;; - add these lines to your .emacs file:
;; (autoload 'js2-mode "js2" nil t)
;; (add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
;;
;; To customize how it works:
;; M-x customize-group RET js2-mode RET
;;
;; The variable `js2-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 `js2-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
;; js2.el with Emacs 21, you should re-compile it for Emacs 22.
;;
;; Unlike cc-engine based language modes, js2-mode's line-indentation is not
;; customizable. It is a surprising amount of work to support customizable
;; indentation. The current compromise is that the tab key lets you cycle among
;; various likely indentation points, similar to the behavior of python-mode.
;;
;; 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 js2-mode is currently only useful for editing JavaScript
;; files, and not for editing JavaScript within <script> tags or templates.
;;
;; This code is part of a larger project, in progress, to enable writing
;; Emacs customizations in JavaScript.
;;
;; Please email bug reports and suggestions to the author, or submit them
;; at http://code.google.com/p/js2-mode/issues
;; TODO:
;; - add unreachable-code warning (error?) using the inconsistent-return analysis
;; - labeled stmt length is now 1
;; - "anonymous function does not always return a value" - use getter/setter name
;; - extend js2-missing-semi-one-line-override to handle catch (e) {return x}
;; - set a text prop on autoinserted delimiters and don't biff user-entered ones
;; - when inserting magic curlies, look for matching close-curly before inserting
;; - get more use out of the symbol table:
;; - jump to declaration (put hyperlinks on all non-decl var usages?)
;; - rename variable/function
;; - warn on unused var
;; - add some dabbrev-expansions for built-in keywords like finally, function
;; - add at least some completion support, e.g. for built-ins
;; - code formatting
;;; Code:
;;; js2-externs.el -- JavaScript extern definitions for js2-mode
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Keywords: javascript languages
;;; Code:
(defvar js2-ecma-262-externs
(mapcar 'symbol-name
'(Array
Boolean
Date
Error
EvalError
Function
Infinity
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 `js2-externs' by default.")
(defvar js2-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
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 `js2-include-browser-externs'.")
(defvar js2-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 `js2-include-rhino-externs' to t to include them.")
(defvar js2-gears-externs
(mapcar 'symbol-name
'(
;; finish me!
))
"Google Gears externs.
Set `js2-include-gears-externs' to t to include them.")
(provide 'js2-externs)
;;; js2-externs.el ends here
;;; js2-vars.el -- byte-compiler support for js2-mode
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Keywords: javascript languages
;;; Code:
(eval-when-compile
(require 'cl))
(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 js2-emacs22 (>= emacs-major-version 22))
(defcustom js2-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 'js2-mode
:type '(choice (const :tag "None" nil)
(const :tag "Basic" 1)
(const :tag "Include Properties" 2)
(const :tag "Include Functions" 3)))
(defvar js2-mode-dev-mode-p nil
"Non-nil if running in development mode. Normally nil.")
(defgroup js2-mode nil
"An improved JavaScript mode."
:group 'languages)
(defcustom js2-basic-offset (if (and (boundp 'c-basic-offset)
(numberp c-basic-offset))
c-basic-offset
4)
"Number of spaces to indent nested statements.
Similar to `c-basic-offset'."
:group 'js2-mode
:type 'integer)
(make-variable-buffer-local 'js2-basic-offset)
(defcustom js2-cleanup-whitespace nil
"Non-nil to invoke `delete-trailing-whitespace' before saves."
:type 'boolean
:group 'js2-mode)
(defcustom js2-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 'js2-mode
:type 'boolean)
(defcustom js2-mirror-mode t
"Non-nil to insert closing brackets, parens, etc. automatically."
:group 'js2-mode
:type 'boolean)
(defcustom js2-auto-indent-p nil
"Automatic indentation with punctuation characters. If non-nil, the
current line is indented when certain punctuations are inserted."
:group 'js2-mode
:type 'boolean)
(defcustom js2-bounce-indent-p nil
"Non-nil to have indent-line function choose among alternatives.
If nil, the indent-line function will indent to a predetermined column
based on heuristic guessing. If non-nil, then if the current line is
already indented to that predetermined column, indenting will choose
another likely column and indent to that spot. Repeated invocation of
the indent-line function will cycle among the computed alternatives.
See the function `js2-bounce-indent' for details."
:type 'boolean
:group 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-rebind-eol-bol-keys t
"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 'js2-mode
:type 'boolean)
(defcustom js2-electric-keys '("{" "}" "(" ")" "[" "]" ":" ";" "," "*")
"Keys that auto-indent when `js2-auto-indent-p' is non-nil.
Each value in the list is passed to `define-key'."
:type 'list
:group 'js2-mode)
(defcustom js2-idle-timer-delay 0.2
"Delay in secs before re-parsing after user makes changes.
Multiplied by `js2-dynamic-idle-timer-adjust', which see."
:type 'number
:group 'js2-mode)
(make-variable-buffer-local 'js2-idle-timer-delay)
(defcustom js2-dynamic-idle-timer-adjust 0
"Positive to adjust `js2-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 `js2-idle-timer-delay' for the
buffer. For example, a 21k file and 10k adjust yields 21k/10k
== 2, so js2-idle-timer-delay is multiplied by 2.
If `js2-dynamic-idle-timer-adjust' is 0 or negative,
`js2-idle-timer-delay' is not dependent on the file size."
:type 'number
:group 'js2-mode)
(defcustom js2-mode-escape-quotes t
"Non-nil to disable automatic quote-escaping inside strings."
:type 'boolean
:group 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-mode-show-parse-errors t
"True to highlight parse errors."
:type 'boolean
:group 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-strict-missing-semi-warning t
"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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-strict-var-redeclaration-warning t
"Non-nil to warn about redeclaring variables in a script or function."
:type 'boolean
:group 'js2-mode)
(defcustom js2-strict-var-hides-function-arg-warning t
"Non-nil to warn about a var decl hiding a function argument."
:type 'boolean
:group 'js2-mode)
(defcustom js2-skip-preprocessor-directives nil
"Non-nil to treat lines beginning with # as comments.
Useful for viewing Mozilla JavaScript source code."
:type 'boolean
:group 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defcustom js2-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 'js2-mode)
(defvar js2-mode-version 20090723
"Release number for `js2-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 js2-token-beg 1)
(deflocal js2-token-end -1)
(defvar js2-EOF_CHAR -1
"Represents end of stream. Distinct from js2-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 `js2-' prefix in front of each name.
(defvar js2-ERROR -1)
(defvar js2-EOF 0)
(defvar js2-EOL 1)
(defvar js2-ENTERWITH 2) ; begin interpreter bytecodes
(defvar js2-LEAVEWITH 3)
(defvar js2-RETURN 4)
(defvar js2-GOTO 5)
(defvar js2-IFEQ 6)
(defvar js2-IFNE 7)
(defvar js2-SETNAME 8)
(defvar js2-BITOR 9)
(defvar js2-BITXOR 10)
(defvar js2-BITAND 11)
(defvar js2-EQ 12)
(defvar js2-NE 13)
(defvar js2-LT 14)
(defvar js2-LE 15)
(defvar js2-GT 16)
(defvar js2-GE 17)
(defvar js2-LSH 18)
(defvar js2-RSH 19)
(defvar js2-URSH 20)
(defvar js2-ADD 21) ; infix plus
(defvar js2-SUB 22) ; infix minus
(defvar js2-MUL 23)
(defvar js2-DIV 24)
(defvar js2-MOD 25)
(defvar js2-NOT 26)
(defvar js2-BITNOT 27)
(defvar js2-POS 28) ; unary plus
(defvar js2-NEG 29) ; unary minus
(defvar js2-NEW 30)
(defvar js2-DELPROP 31)
(defvar js2-TYPEOF 32)
(defvar js2-GETPROP 33)
(defvar js2-GETPROPNOWARN 34)
(defvar js2-SETPROP 35)
(defvar js2-GETELEM 36)
(defvar js2-SETELEM 37)
(defvar js2-CALL 38)
(defvar js2-NAME 39) ; an identifier
(defvar js2-NUMBER 40)
(defvar js2-STRING 41)
(defvar js2-NULL 42)
(defvar js2-THIS 43)
(defvar js2-FALSE 44)
(defvar js2-TRUE 45)
(defvar js2-SHEQ 46) ; shallow equality (===)
(defvar js2-SHNE 47) ; shallow inequality (!==)
(defvar js2-REGEXP 48)
(defvar js2-BINDNAME 49)
(defvar js2-THROW 50)
(defvar js2-RETHROW 51) ; rethrow caught exception: catch (e if ) uses it
(defvar js2-IN 52)
(defvar js2-INSTANCEOF 53)
(defvar js2-LOCAL_LOAD 54)
(defvar js2-GETVAR 55)
(defvar js2-SETVAR 56)
(defvar js2-CATCH_SCOPE 57)
(defvar js2-ENUM_INIT_KEYS 58)
(defvar js2-ENUM_INIT_VALUES 59)
(defvar js2-ENUM_INIT_ARRAY 60)
(defvar js2-ENUM_NEXT 61)
(defvar js2-ENUM_ID 62)
(defvar js2-THISFN 63)
(defvar js2-RETURN_RESULT 64) ; to return previously stored return result
(defvar js2-ARRAYLIT 65) ; array literal
(defvar js2-OBJECTLIT 66) ; object literal
(defvar js2-GET_REF 67) ; *reference
(defvar js2-SET_REF 68) ; *reference = something
(defvar js2-DEL_REF 69) ; delete reference
(defvar js2-REF_CALL 70) ; f(args) = something or f(args)++
(defvar js2-REF_SPECIAL 71) ; reference for special properties like __proto
(defvar js2-YIELD 72) ; JS 1.7 yield pseudo keyword
;; XML support
(defvar js2-DEFAULTNAMESPACE 73)
(defvar js2-ESCXMLATTR 74)
(defvar js2-ESCXMLTEXT 75)
(defvar js2-REF_MEMBER 76) ; Reference for x.@y, x..y etc.
(defvar js2-REF_NS_MEMBER 77) ; Reference for x.ns::y, x..ns::y etc.
(defvar js2-REF_NAME 78) ; Reference for @y, @[y] etc.
(defvar js2-REF_NS_NAME 79) ; Reference for ns::y, @ns::y@[y] etc.
(defvar js2-first-bytecode js2-ENTERWITH)
(defvar js2-last-bytecode js2-REF_NS_NAME)
(defvar js2-TRY 80)
(defvar js2-SEMI 81) ; semicolon
(defvar js2-LB 82) ; left and right brackets
(defvar js2-RB 83)
(defvar js2-LC 84) ; left and right curly-braces
(defvar js2-RC 85)
(defvar js2-LP 86) ; left and right parens
(defvar js2-RP 87)
(defvar js2-COMMA 88) ; comma operator
(defvar js2-ASSIGN 89) ; simple assignment (=)
(defvar js2-ASSIGN_BITOR 90) ; |=
(defvar js2-ASSIGN_BITXOR 91) ; ^=
(defvar js2-ASSIGN_BITAND 92) ; &=
(defvar js2-ASSIGN_LSH 93) ; <<=
(defvar js2-ASSIGN_RSH 94) ; >>=
(defvar js2-ASSIGN_URSH 95) ; >>>=
(defvar js2-ASSIGN_ADD 96) ; +=
(defvar js2-ASSIGN_SUB 97) ; -=
(defvar js2-ASSIGN_MUL 98) ; *=
(defvar js2-ASSIGN_DIV 99) ; /=
(defvar js2-ASSIGN_MOD 100) ; %=
(defvar js2-first-assign js2-ASSIGN)
(defvar js2-last-assign js2-ASSIGN_MOD)
(defvar js2-HOOK 101) ; conditional (?:)
(defvar js2-COLON 102)
(defvar js2-OR 103) ; logical or (||)
(defvar js2-AND 104) ; logical and (&&)
(defvar js2-INC 105) ; increment/decrement (++ --)
(defvar js2-DEC 106)
(defvar js2-DOT 107) ; member operator (.)
(defvar js2-FUNCTION 108) ; function keyword
(defvar js2-EXPORT 109) ; export keyword
(defvar js2-IMPORT 110) ; import keyword
(defvar js2-IF 111) ; if keyword
(defvar js2-ELSE 112) ; else keyword
(defvar js2-SWITCH 113) ; switch keyword
(defvar js2-CASE 114) ; case keyword
(defvar js2-DEFAULT 115) ; default keyword
(defvar js2-WHILE 116) ; while keyword
(defvar js2-DO 117) ; do keyword
(defvar js2-FOR 118) ; for keyword
(defvar js2-BREAK 119) ; break keyword
(defvar js2-CONTINUE 120) ; continue keyword
(defvar js2-VAR 121) ; var keyword
(defvar js2-WITH 122) ; with keyword
(defvar js2-CATCH 123) ; catch keyword
(defvar js2-FINALLY 124) ; finally keyword
(defvar js2-VOID 125) ; void keyword
(defvar js2-RESERVED 126) ; reserved keywords
(defvar js2-EMPTY 127)
;; Types used for the parse tree - never returned by scanner.
(defvar js2-BLOCK 128) ; statement block
(defvar js2-LABEL 129) ; label
(defvar js2-TARGET 130)
(defvar js2-LOOP 131)
(defvar js2-EXPR_VOID 132) ; expression statement in functions
(defvar js2-EXPR_RESULT 133) ; expression statement in scripts
(defvar js2-JSR 134)
(defvar js2-SCRIPT 135) ; top-level node for entire script
(defvar js2-TYPEOFNAME 136) ; for typeof(simple-name)
(defvar js2-USE_STACK 137)
(defvar js2-SETPROP_OP 138) ; x.y op= something
(defvar js2-SETELEM_OP 139) ; x[y] op= something
(defvar js2-LOCAL_BLOCK 140)
(defvar js2-SET_REF_OP 141) ; *reference op= something
;; For XML support:
(defvar js2-DOTDOT 142) ; member operator (..)
(defvar js2-COLONCOLON 143) ; namespace::name
(defvar js2-XML 144) ; XML type
(defvar js2-DOTQUERY 145) ; .() -- e.g., x.emps.emp.(name == "terry")
(defvar js2-XMLATTR 146) ; @
(defvar js2-XMLEND 147)
;; Optimizer-only tokens
(defvar js2-TO_OBJECT 148)
(defvar js2-TO_DOUBLE 149)
(defvar js2-GET 150) ; JS 1.5 get pseudo keyword
(defvar js2-SET 151) ; JS 1.5 set pseudo keyword
(defvar js2-LET 152) ; JS 1.7 let pseudo keyword
(defvar js2-CONST 153)
(defvar js2-SETCONST 154)
(defvar js2-SETCONSTVAR 155)
(defvar js2-ARRAYCOMP 156)
(defvar js2-LETEXPR 157)
(defvar js2-WITHEXPR 158)
(defvar js2-DEBUGGER 159)
(defvar js2-COMMENT 160)
(defvar js2-ENUM 161) ; for "enum" reserved word
(defconst js2-num-tokens (1+ js2-ENUM))
(defconst js2-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 js2-ts-dirty-line nil
"Token stream buffer-local variable.
Indicates stuff other than whitespace since start of line.")
(deflocal js2-ts-regexp-flags nil
"Token stream buffer-local variable.")
(deflocal js2-ts-string ""
"Token stream buffer-local variable.
Last string scanned.")
(deflocal js2-ts-number nil
"Token stream buffer-local variable.
Last literal number scanned.")
(deflocal js2-ts-hit-eof nil
"Token stream buffer-local variable.")
(deflocal js2-ts-line-start 0
"Token stream buffer-local variable.")
(deflocal js2-ts-lineno 1
"Token stream buffer-local variable.")
(deflocal js2-ts-line-end-char -1
"Token stream buffer-local variable.")
(deflocal js2-ts-cursor 1 ; emacs buffers are 1-indexed
"Token stream buffer-local variable.
Current scan position.")
(deflocal js2-ts-is-xml-attribute nil
"Token stream buffer-local variable.")
(deflocal js2-ts-xml-is-tag-content nil
"Token stream buffer-local variable.")
(deflocal js2-ts-xml-open-tags-count 0
"Token stream buffer-local variable.")
(deflocal js2-ts-string-buffer nil
"Token stream buffer-local variable.
List of chars built up while scanning various tokens.")
(deflocal js2-ts-comment-type nil
"Token stream buffer-local variable.")
;;; Parser variables
(deflocal js2-parsed-errors nil
"List of errors produced during scanning/parsing.")
(deflocal js2-parsed-warnings nil
"List of warnings produced during scanning/parsing.")
(deflocal js2-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 js2-parse-hook nil
"List of callbacks for receiving parsing progress.")
(defvar js2-parse-finished-hook nil
"List of callbacks to notify when parsing finishes.
Not called if parsing was interrupted.")
(deflocal js2-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 js2-parse-ide-mode t
"Non-nil if the parser is being used for `js2-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 js2-parse)
(defconst js2-clear-ti-mask #xFFFF
"Mask to clear token information bits.")
(defconst js2-ti-after-eol (lsh 1 16)
"Flag: first token of the source line.")
(defconst js2-ti-check-label (lsh 1 17)
"Flag: indicates to check for label.")
;; Inline Rhino's CompilerEnvirons vars as buffer-locals.
(deflocal js2-compiler-generate-debug-info t)
(deflocal js2-compiler-use-dynamic-scope nil)
(deflocal js2-compiler-reserved-keywords-as-identifier nil)
(deflocal js2-compiler-xml-available t)
(deflocal js2-compiler-optimization-level 0)
(deflocal js2-compiler-generating-source t)
(deflocal js2-compiler-strict-mode nil)
(deflocal js2-compiler-report-warning-as-error nil)
(deflocal js2-compiler-generate-observer-count nil)
(deflocal js2-compiler-activation-names nil)
;; SKIP: sourceURI
;; There's a compileFunction method in Context.java - may need it.
(deflocal js2-called-by-compile-function nil
"True if `js2-parse' was called by `js2-compile-function'.
Will only be used when we finish implementing the interpreter.")
;; SKIP: ts (we just call `js2-init-scanner' and use its vars)
(deflocal js2-current-flagged-token js2-EOF)
(deflocal js2-current-token js2-EOF)
;; SKIP: node factory - we're going to just call functions directly,
;; and eventually go to a unified AST format.
(deflocal js2-nesting-of-function 0)
(deflocal js2-recorded-assignments nil
"Tracks assignments found during parsing.")
(defcustom js2-global-externs nil
"A list of any extern names you'd like to consider always declared.
This list is global and is used by all js2-mode files.
You can create buffer-local externs list using `js2-additional-externs'.
There is also a buffer-local variable `js2-default-externs',
which is initialized by default to include the Ecma-262 externs
and the standard browser externs. The three lists are all
checked during highlighting."
:type 'list
:group 'js2-mode)
(deflocal js2-default-externs nil
"Default external declarations.
These are currently only used for highlighting undeclared variables,
which only worries about top-level (unqualified) references.
As js2-mode's processing improves, we will flesh out this list.
The initial value is set to `js2-ecma-262-externs', unless you
have set `js2-include-browser-externs', in which case the browser
externs are also included.
See `js2-additional-externs' for more information.")
(defcustom js2-include-browser-externs t
"Non-nil to include browser externs in the master externs list.
If you work on JavaScript files that are not intended for browsers,
such as Mozilla Rhino server-side JavaScript, set this to nil.
You can always include them on a per-file basis by calling
`js2-add-browser-externs' from a function on `js2-mode-hook'.
See `js2-additional-externs' for more information about externs."
:type 'boolean
:group 'js2-mode)
(defcustom js2-include-rhino-externs t
"Non-nil to include Mozilla Rhino externs in the master externs list.
See `js2-additional-externs' for more information about externs."
:type 'boolean
:group 'js2-mode)
(defcustom js2-include-gears-externs t
"Non-nil to include Google Gears externs in the master externs list.
See `js2-additional-externs' for more information about externs."
:type 'boolean
:group 'js2-mode)
(deflocal js2-additional-externs nil
"A buffer-local list of additional external declarations.
It is used to decide whether variables are considered undeclared
for purposes of highlighting.
Each entry is a lisp string. The string should be the fully qualified
name of an external entity. All externs should be added to this list,
so that as js2-mode's processing improves it can take advantage of them.
You may want to declare your externs in three ways.
First, you can add externs that are valid for all your JavaScript files.
You should probably do this by adding them to `js2-global-externs', which
is a global list used for all js2-mode files.
Next, you can add a function to `js2-mode-hook' that adds additional
externs appropriate for the specific file, perhaps based on its path.
These should go in `js2-additional-externs', which is buffer-local.
Finally, you can add a function to `js2-post-parse-callbacks',
which is called after parsing completes, and `root' is bound to
the root of the parse tree. At this stage you can set up an AST
node visitor using `js2-visit-ast' and examine the parse tree
for specific import patterns that may imply the existence of
other externs, possibly tied to your build system. These should also
be added to `js2-additional-externs'.
Your post-parse callback may of course also use the simpler and
faster (but perhaps less robust) approach of simply scanning the
buffer text for your imports, using regular expressions.")
;; SKIP: decompiler
;; SKIP: encoded-source
;;; The following variables are per-function and should be saved/restored
;;; during function parsing...
(deflocal js2-current-script-or-fn nil)
(deflocal js2-current-scope nil)
(deflocal js2-nesting-of-with 0)
(deflocal js2-label-set nil
"An alist mapping label names to nodes.")
(deflocal js2-loop-set nil)
(deflocal js2-loop-and-switch-set nil)
(deflocal js2-has-return-value nil)
(deflocal js2-end-flags 0)
;;; ...end of per function variables
;; Without 2-token lookahead, labels are a problem.
;; These vars store the token info of the last matched name,
;; iff it wasn't the last matched token. Only valid in some contexts.
(defvar js2-prev-name-token-start nil)
(defvar js2-prev-name-token-string nil)
(defsubst js2-save-name-token-data (pos name)
(setq js2-prev-name-token-start pos
js2-prev-name-token-string name))
;; These flags enumerate the possible ways a statement/function can
;; terminate. These flags are used by endCheck() and by the Parser to
;; detect inconsistent return usage.
;;
;; END_UNREACHED is reserved for code paths that are assumed to always be
;; able to execute (example: throw, continue)
;;
;; END_DROPS_OFF indicates if the statement can transfer control to the
;; next one. Statement such as return dont. A compound statement may have
;; some branch that drops off control to the next statement.
;;
;; END_RETURNS indicates that the statement can return (without arguments)
;; END_RETURNS_VALUE indicates that the statement can return a value.
;;
;; A compound statement such as
;; if (condition) {
;; return value;
;; }
;; Will be detected as (END_DROPS_OFF | END_RETURN_VALUE) by endCheck()
(defconst js2-end-unreached #x0)
(defconst js2-end-drops-off #x1)
(defconst js2-end-returns #x2)
(defconst js2-end-returns-value #x4)
(defconst js2-end-yields #x8)
;; Rhino awkwardly passes a statementLabel parameter to the
;; statementHelper() function, the main statement parser, which
;; is then used by quite a few of the sub-parsers. We just make
;; it a buffer-local variable and make sure it's cleaned up properly.
(deflocal js2-labeled-stmt nil) ; type `js2-labeled-stmt-node'
;; Similarly, Rhino passes an inForInit boolean through about half
;; the expression parsers. We use a dynamically-scoped variable,
;; which makes it easier to funcall the parsers individually without
;; worrying about whether they take the parameter or not.
(deflocal js2-in-for-init nil)
(deflocal js2-temp-name-counter 0)
(deflocal js2-parse-stmt-count 0)
(defsubst js2-get-next-temp-name ()
(format "$%d" (incf js2-temp-name-counter)))
(defvar js2-parse-interruptable-p t
"Set this to nil to force parse to continue until finished.
This will mostly be useful for interpreters.")
(defvar js2-statements-per-pause 50
"Pause after this many statements to check for user input.
If user input is pending, stop the parse and discard the tree.
This makes for a smoother user experience for large files.
You may have to wait a second or two before the highlighting
and error-reporting appear, but you can always type ahead if
you wish. This appears to be more or less how Eclipse, IntelliJ
and other editors work.")
(deflocal js2-record-comments t
"Instructs the scanner to record comments in `js2-scanned-comments'.")
(deflocal js2-scanned-comments nil
"List of all comments from the current parse.")
(defun js2-underline-color (color)
"Return a legal value for the :underline face attribute based on COLOR."
;; In XEmacs the :underline attribute can only be a boolean.
;; In GNU it can be the name of a colour.
(if (featurep 'xemacs)
(if color t nil)
color))
(defcustom js2-mode-indent-inhibit-undo nil
"Non-nil to disable collection of Undo information when indenting lines.
Some users have requested this behavior. It's nil by default because
other Emacs modes don't work this way."
:type 'boolean
:group 'js2-mode)
(defcustom js2-mode-indent-ignore-first-tab nil
"If non-nil, ignore first TAB keypress if we look indented properly.
It's fairly common for users to navigate to an already-indented line
and press TAB for reassurance that it's been indented. For this class
of users, we want the first TAB press on a line to be ignored if the
line is already indented to one of the precomputed alternatives.
This behavior is only partly implemented. If you TAB-indent a line,
navigate to another line, and then navigate back, it fails to clear
the last-indented variable, so it thinks you've already hit TAB once,
and performs the indent. A full solution would involve getting on the
point-motion hooks for the entire buffer. If we come across another
use cases that requires watching point motion, I'll consider doing it.
If you set this variable to nil, then the TAB key will always change
the indentation of the current line, if more than one alternative
indentation spot exists."
:type 'boolean
:group 'js2-mode)
(defvar js2-indent-hook nil
"A hook for user-defined indentation rules.
Functions on this hook should expect two arguments: (LIST INDEX)
The LIST argument is the list of computed indentation points for
the current line. INDEX is the list index of the indentation point
that `js2-bounce-indent' plans to use. If INDEX is nil, then the
indent function is not going to change the current line indentation.
If a hook function on this list returns a non-nil value, then
`js2-bounce-indent' assumes the hook function has performed its own
indentation, and will do nothing. If all hook functions on the list
return nil, then `js2-bounce-indent' will use its computed indentation
and reindent the line.
When hook functions on this hook list are called, the variable
`js2-mode-ast' may or may not be set, depending on whether the
parse tree is available. If the variable is nil, you can pass a
callback to `js2-mode-wait-for-parse', and your callback will be
called after the new parse tree is built. This can take some time
in large files.")
(defface js2-warning-face
`((((class color) (background light))
(:underline ,(js2-underline-color "orange")))
(((class color) (background dark))
(:underline ,(js2-underline-color "orange")))
(t (:underline t)))
"Face for JavaScript warnings."
:group 'js2-mode)
(defface js2-error-face
`((((class color) (background light))
(:foreground "red"))
(((class color) (background dark))
(:foreground "red"))
(t (:foreground "red")))
"Face for JavaScript errors."
:group 'js2-mode)
(defface js2-jsdoc-tag-face
'((t :foreground "SlateGray"))
"Face used to highlight @whatever tags in jsdoc comments."
:group 'js2-mode)
(defface js2-jsdoc-type-face
'((t :foreground "SteelBlue"))
"Face used to highlight {FooBar} types in jsdoc comments."
:group 'js2-mode)
(defface js2-jsdoc-value-face
'((t :foreground "PeachPuff3"))
"Face used to highlight tag values in jsdoc comments."
:group 'js2-mode)
(defface js2-function-param-face
'((t :foreground "SeaGreen"))
"Face used to highlight function parameters in javascript."
:group 'js2-mode)
(defface js2-instance-member-face
'((t :foreground "DarkOrchid"))
"Face used to highlight instance variables in javascript.
Not currently used."
:group 'js2-mode)
(defface js2-private-member-face
'((t :foreground "PeachPuff3"))
"Face used to highlight calls to private methods in javascript.
Not currently used."
:group 'js2-mode)
(defface js2-private-function-call-face
'((t :foreground "goldenrod"))
"Face used to highlight calls to private functions in javascript.
Not currently used."
:group 'js2-mode)
(defface js2-jsdoc-html-tag-name-face
(if js2-emacs22
'((((class color) (min-colors 88) (background light))
(:foreground "rosybrown"))
(((class color) (min-colors 8) (background dark))
(:foreground "yellow"))
(((class color) (min-colors 8) (background light))
(:foreground "magenta")))
'((((type tty pc) (class color) (background light))
(:foreground "magenta"))
(((type tty pc) (class color) (background dark))
(:foreground "yellow"))
(t (:foreground "RosyBrown"))))
"Face used to highlight jsdoc html tag names"
:group 'js2-mode)
(defface js2-jsdoc-html-tag-delimiter-face
(if js2-emacs22
'((((class color) (min-colors 88) (background light))
(:foreground "dark khaki"))
(((class color) (min-colors 8) (background dark))
(:foreground "green"))
(((class color) (min-colors 8) (background light))
(:foreground "green")))
'((((type tty pc) (class color) (background light))
(:foreground "green"))
(((type tty pc) (class color) (background dark))
(:foreground "green"))
(t (:foreground "dark khaki"))))
"Face used to highlight brackets in jsdoc html tags."
:group 'js2-mode)
(defface js2-magic-paren-face
'((t :underline t))
"Face used to color parens that will be auto-overwritten."
:group 'js2-mode)
(defcustom js2-post-parse-callbacks nil
"A list of callback functions invoked after parsing finishes.
Currently, the main use for this function is to add synthetic
declarations to `js2-recorded-assignments', which see."
:type 'list
:group 'js2-mode)
(defface js2-external-variable-face
'((t :foreground "orange"))
"Face used to highlight assignments to undeclared variables.
An undeclared variable is any variable not declared with var or let
in the current scope or any lexically enclosing scope. If you assign
to such a variable, then you are either expecting it to originate from
another file, or you've got a potential bug."
:group 'js2-mode)
(defcustom js2-highlight-external-variables t
"Non-nil to higlight assignments to undeclared variables."
:type 'boolean
:group 'js2-mode)
(defcustom js2-auto-insert-catch-block t
"Non-nil to insert matching catch block on open-curly after `try'."
:type 'boolean
:group 'js2-mode)
(defvar js2-mode-map
(let ((map (make-sparse-keymap))
keys)
(define-key map [mouse-1] #'js2-mode-show-node)
(define-key map (kbd "C-m") #'js2-enter-key)
(when js2-rebind-eol-bol-keys
(define-key map (kbd "C-a") #'js2-beginning-of-line)
(define-key map (kbd "C-e") #'js2-end-of-line))
(define-key map (kbd "C-c C-e") #'js2-mode-hide-element)
(define-key map (kbd "C-c C-s") #'js2-mode-show-element)
(define-key map (kbd "C-c C-a") #'js2-mode-show-all)
(define-key map (kbd "C-c C-f") #'js2-mode-toggle-hide-functions)
(define-key map (kbd "C-c C-t") #'js2-mode-toggle-hide-comments)
(define-key map (kbd "C-c C-o") #'js2-mode-toggle-element)
(define-key map (kbd "C-c C-w") #'js2-mode-toggle-warnings-and-errors)
(define-key map (kbd "C-c C-`") #'js2-next-error)
;; also define user's preference for next-error, if available
(if (setq keys (where-is-internal #'next-error))
(define-key map (car keys) #'js2-next-error))
(define-key map (or (car (where-is-internal #'mark-defun))
(kbd "M-C-h"))
#'js2-mark-defun)
(define-key map (or (car (where-is-internal #'narrow-to-defun))
(kbd "C-x nd"))
#'js2-narrow-to-defun)
(define-key map [down-mouse-3] #'js2-down-mouse-3)
(when js2-auto-indent-p
(mapc (lambda (key)
(define-key map key #'js2-insert-and-indent))
js2-electric-keys))
(define-key map [menu-bar javascript]
(cons "JavaScript" (make-sparse-keymap "JavaScript")))
(define-key map [menu-bar javascript customize-js2-mode]
'(menu-item "Customize js2-mode" js2-mode-customize
:help "Customize the behavior of this mode"))
(define-key map [menu-bar javascript js2-force-refresh]
'(menu-item "Force buffer refresh" js2-mode-reset
:help "Re-parse the buffer from scratch"))
(define-key map [menu-bar javascript separator-2]
'("--"))
(define-key map [menu-bar javascript next-error]
'(menu-item "Next warning or error" js2-next-error
:enabled (and js2-mode-ast
(or (js2-ast-root-errors js2-mode-ast)
(js2-ast-root-warnings js2-mode-ast)))
:help "Move to next warning or error"))
(define-key map [menu-bar javascript display-errors]
'(menu-item "Show errors and warnings" js2-mode-display-warnings-and-errors
:visible (not js2-mode-show-parse-errors)
:help "Turn on display of warnings and errors"))
(define-key map [menu-bar javascript hide-errors]
'(menu-item "Hide errors and warnings" js2-mode-hide-warnings-and-errors
:visible js2-mode-show-parse-errors
:help "Turn off display of warnings and errors"))
(define-key map [menu-bar javascript separator-1]
'("--"))
(define-key map [menu-bar javascript js2-toggle-function]
'(menu-item "Show/collapse element" js2-mode-toggle-element
:help "Hide or show function body or comment"))
(define-key map [menu-bar javascript show-comments]
'(menu-item "Show block comments" js2-mode-toggle-hide-comments
:visible js2-mode-comments-hidden
:help "Expand all hidden block comments"))
(define-key map [menu-bar javascript hide-comments]
'(menu-item "Hide block comments" js2-mode-toggle-hide-comments
:visible (not js2-mode-comments-hidden)
:help "Show block comments as /*...*/"))
(define-key map [menu-bar javascript show-all-functions]
'(menu-item "Show function bodies" js2-mode-toggle-hide-functions
:visible js2-mode-functions-hidden
:help "Expand all hidden function bodies"))
(define-key map [menu-bar javascript hide-all-functions]
'(menu-item "Hide function bodies" js2-mode-toggle-hide-functions
:visible (not js2-mode-functions-hidden)
:help "Show {...} for all top-level function bodies"))
map)
"Keymap used in `js2-mode' buffers.")
(defconst js2-mode-identifier-re "[a-zA-Z_$][a-zA-Z0-9_$]*")
(defvar js2-mode-//-comment-re "^\\(\\s-*\\)//.+"
"Matches a //-comment line. Must be first non-whitespace on line.
First match-group is the leading whitespace.")
(defvar js2-mode-hook nil)
(deflocal js2-mode-ast nil "Private variable.")
(deflocal js2-mode-parse-timer nil "Private variable.")
(deflocal js2-mode-buffer-dirty-p nil "Private variable.")
(deflocal js2-mode-parsing nil "Private variable.")
(deflocal js2-mode-node-overlay nil)
(defvar js2-mode-show-overlay js2-mode-dev-mode-p
"Debug: Non-nil to highlight AST nodes on mouse-down.")
(deflocal js2-mode-fontifications nil "Private variable")
(deflocal js2-mode-deferred-properties nil "Private variable")
(deflocal js2-imenu-recorder nil "Private variable")
(deflocal js2-imenu-function-map nil "Private variable")
(defvar js2-paragraph-start
"\\(@[a-zA-Z]+\\>\\|$\\)")
;; Note that we also set a 'c-in-sws text property in html comments,
;; so that `c-forward-sws' and `c-backward-sws' work properly.
(defvar js2-syntactic-ws-start
"\\s \\|/[*/]\\|[\n\r]\\|\\\\[\n\r]\\|\\s!\\|<!--\\|^\\s-*-->")
(defvar js2-syntactic-ws-end
"\\s \\|[\n\r/]\\|\\s!")
(defvar js2-syntactic-eol
(concat "\\s *\\(/\\*[^*\n\r]*"
"\\(\\*+[^*\n\r/][^*\n\r]*\\)*"
"\\*+/\\s *\\)*"
"\\(//\\|/\\*[^*\n\r]*"
"\\(\\*+[^*\n\r/][^*\n\r]*\\)*$"
"\\|\\\\$\\|$\\)")
"Copied from java-mode. Needed for some cc-engine functions.")
(defvar js2-comment-prefix-regexp
"//+\\|\\**")
(defvar js2-comment-start-skip
"\\(//+\\|/\\*+\\)\\s *")
(defvar js2-mode-verbose-parse-p js2-mode-dev-mode-p
"Non-nil to emit status messages during parsing.")
(defvar js2-mode-functions-hidden nil "private variable")
(defvar js2-mode-comments-hidden nil "private variable")
(defvar js2-mode-syntax-table
(let ((table (make-syntax-table)))
(c-populate-syntax-table table)
table)
"Syntax table used in js2-mode buffers.")
(defvar js2-mode-abbrev-table nil
"Abbrev table in use in `js2-mode' buffers.")
(define-abbrev-table 'js2-mode-abbrev-table ())
(defvar js2-mode-must-byte-compile (not js2-mode-dev-mode-p)
"Non-nil to have `js2-mode' signal an error if not byte-compiled.")
(defvar js2-mode-pending-parse-callbacks nil
"List of functions waiting to be notified that parse is finished.")
(defvar js2-mode-last-indented-line -1)
(eval-when-compile
(defvar c-paragraph-start nil)
(defvar c-paragraph-separate nil)
(defvar c-syntactic-ws-start nil)
(defvar c-syntactic-ws-end nil)
(defvar c-syntactic-eol nil)
(defvar running-xemacs nil)
(defvar font-lock-mode nil)
(defvar font-lock-keywords nil))
;; Workaround for buggy Emacs 21 behavior.
(eval-when-compile
(if (< emacs-major-version 22)
(defun c-setup-paragraph-variables () nil)))
(provide 'js2-vars)
;;; js2-vars.el ends here
;;; js2-util.el -- JavaScript utilities
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Keywords: javascript languages
;;; Code
(eval-when-compile
(require 'cl))
;; Emacs21 compatibility, plus some stuff to avoid runtime dependency on CL
(unless (fboundp #'looking-back)
(defun looking-back (regexp &optional limit greedy)
"Return non-nil if text before point matches regular expression REGEXP.
Like `looking-at' except matches before point, and is slower.
LIMIT if non-nil speeds up the search by specifying a minimum
starting position, to avoid checking matches that would start
before LIMIT.
If GREEDY is non-nil, extend the match backwards as far as possible,
stopping when a single additional previous character cannot be part
of a match for REGEXP."
(let ((start (point))
(pos
(save-excursion
(and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
(point)))))
(if (and greedy pos)
(save-restriction
(narrow-to-region (point-min) start)
(while (and (> pos (point-min))
(save-excursion
(goto-char pos)
(backward-char 1)
(looking-at (concat "\\(?:" regexp "\\)\\'"))))
(setq pos (1- pos)))
(save-excursion
(goto-char pos)
(looking-at (concat "\\(?:" regexp "\\)\\'")))))
(not (null pos)))))
(unless (fboundp #'copy-overlay)
(defun copy-overlay (o)
"Return a copy of overlay O."
(let ((o1 (make-overlay (overlay-start o) (overlay-end o)
;; FIXME: there's no easy way to find the
;; insertion-type of the two markers.
(overlay-buffer o)))
(props (overlay-properties o)))
(while props
(overlay-put o1 (pop props) (pop props)))
o1)))
(unless (fboundp #'remove-overlays)
(defun remove-overlays (&optional beg end name val)
"Clear BEG and END of overlays whose property NAME has value VAL.
Overlays might be moved and/or split.
BEG and END default respectively to the beginning and end of buffer."
(unless beg (setq beg (point-min)))
(unless end (setq end (point-max)))
(if (< end beg)
(setq beg (prog1 end (setq end beg))))
(save-excursion
(dolist (o (overlays-in beg end))
(when (eq (overlay-get o name) val)
;; Either push this overlay outside beg...end
;; or split it to exclude beg...end
;; or delete it entirely (if it is contained in beg...end).
(if (< (overlay-start o) beg)
(if (> (overlay-end o) end)
(progn
(move-overlay (copy-overlay o)
(overlay-start o) beg)
(move-overlay o end (overlay-end o)))
(move-overlay o (overlay-start o) beg))
(if (> (overlay-end o) end)
(move-overlay o end (overlay-end o))
(delete-overlay o))))))))
;; we don't want a runtime dependency on the CL package, so define
;; our own versions of these functions.
(defun js2-delete-if (predicate list)
"Remove all items satisfying PREDICATE in LIST."
(loop for item in list
if (not (funcall predicate item))
collect item))
(defun js2-position (element list)
"Find 0-indexed position of ELEMENT in LIST comparing with `eq'.
Returns nil if element is not found in the list."
(let ((count 0)
found)
(while (and list (not found))
(if (eq element (car list))
(setq found t)
(setq count (1+ count)
list (cdr list))))
(if found count)))
(defun js2-find-if (predicate list)
"Find first item satisfying PREDICATE in LIST."
(let (result)
(while (and list (not result))
(if (funcall predicate (car list))
(setq result (car list)))
(setq list (cdr list)))
result))
;;; end Emacs 21 compat
(defmacro js2-time (form)
"Evaluate FORM, discard result, and return elapsed time in sec"
(let ((beg (make-symbol "--js2-time-beg--"))
(delta (make-symbol "--js2-time-end--")))
`(let ((,beg (current-time))
,delta)
,form
(/ (truncate (* (- (float-time (current-time))
(float-time ,beg)))
10000)
10000.0))))
(def-edebug-spec js2-time t)
(defsubst neq (expr1 expr2)
"Return (not (eq expr1 expr2))."
(not (eq expr1 expr2)))
(defsubst js2-same-line (pos)
"Return t if POS is on the same line as current point."
(and (>= pos (point-at-bol))
(<= pos (point-at-eol))))
(defsubst js2-same-line-2 (p1 p2)
"Return t if p1 is on the same line as p2."
(save-excursion
(goto-char p1)
(js2-same-line p2)))
(defun js2-code-bug ()
"Signal an error when we encounter an unexpected code path."
(error "failed assertion"))
;; I'd like to associate errors with nodes, but for now the
;; easiest thing to do is get the context info from the last token.
(defsubst js2-record-parse-error (msg &optional arg pos len)
(push (list (list msg arg)
(or pos js2-token-beg)
(or len (- js2-token-end js2-token-beg)))
js2-parsed-errors))
(defsubst js2-report-error (msg &optional msg-arg pos len)
"Signal a syntax error or record a parse error."
(if js2-recover-from-parse-errors
(js2-record-parse-error msg msg-arg pos len)
(signal 'js2-syntax-error
(list msg
js2-ts-lineno
(save-excursion
(goto-char js2-ts-cursor)
(current-column))
js2-ts-hit-eof))))
(defsubst js2-report-warning (msg &optional msg-arg pos len)
(if js2-compiler-report-warning-as-error
(js2-report-error msg msg-arg pos len)
(push (list (list msg msg-arg)
(or pos js2-token-beg)
(or len (- js2-token-end js2-token-beg)))
js2-parsed-warnings)))
(defsubst js2-add-strict-warning (msg-id &optional msg-arg beg end)
(if js2-compiler-strict-mode
(js2-report-warning msg-id msg-arg beg
(and beg end (- end beg)))))
(put 'js2-syntax-error 'error-conditions
'(error syntax-error js2-syntax-error))
(put 'js2-syntax-error 'error-message "Syntax error")
(put 'js2-parse-error 'error-conditions
'(error parse-error js2-parse-error))
(put 'js2-parse-error 'error-message "Parse error")
(defmacro js2-clear-flag (flags flag)
`(setq ,flags (logand ,flags (lognot ,flag))))
(defmacro js2-set-flag (flags flag)
"Logical-or FLAG into FLAGS."
`(setq ,flags (logior ,flags ,flag)))
(defsubst js2-flag-set-p (flags flag)
(/= 0 (logand flags flag)))
(defsubst js2-flag-not-set-p (flags flag)
(zerop (logand flags flag)))
;; Stolen shamelessly from James Clark's nxml-mode.
(defmacro js2-with-unmodifying-text-property-changes (&rest body)
"Evaluate BODY without any text property changes modifying the buffer.
Any text properties changes happen as usual but the changes are not treated as
modifications to the buffer."
(let ((modified (make-symbol "modified")))
`(let ((,modified (buffer-modified-p))
(inhibit-read-only t)
(inhibit-modification-hooks t)
(buffer-undo-list t)
(deactivate-mark nil)
;; Apparently these avoid file locking problems.
(buffer-file-name nil)
(buffer-file-truename nil))
(unwind-protect
(progn ,@body)
(unless ,modified
(restore-buffer-modified-p nil))))))
(put 'js2-with-unmodifying-text-property-changes 'lisp-indent-function 0)
(def-edebug-spec js2-with-unmodifying-text-property-changes t)
(defmacro js2-with-underscore-as-word-syntax (&rest body)
"Evaluate BODY with the _ character set to be word-syntax."
(let ((old-syntax (make-symbol "old-syntax")))
`(let ((,old-syntax (string (char-syntax ?_))))
(unwind-protect
(progn
(modify-syntax-entry ?_ "w" js2-mode-syntax-table)
,@body)
(modify-syntax-entry ?_ ,old-syntax js2-mode-syntax-table)))))
(put 'js2-with-underscore-as-word-syntax 'lisp-indent-function 0)
(def-edebug-spec js2-with-underscore-as-word-syntax t)
(defmacro with-buffer (buf form)
"Executes FORM in buffer BUF.
BUF can be a buffer name or a buffer object.
If the buffer doesn't exist, it's created."
`(let ((buffer (gentemp)))
(setq buffer
(if (stringp ,buf)
(get-buffer-create ,buf)
,buf))
(save-excursion
(set-buffer buffer)
,form)))
(defsubst char-is-uppercase (c)
"Return t if C is an uppercase character.
Handles unicode and latin chars properly."
(/= c (downcase c)))
(defsubst char-is-lowercase (c)
"Return t if C is an uppercase character.
Handles unicode and latin chars properly."
(/= c (upcase c)))
(put 'with-buffer 'lisp-indent-function 1)
(def-edebug-spec with-buffer t)
(provide 'js2-util)
;;; js2-util.el ends here
;;; js2-scan.el --- JavaScript scanner
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Keywords: javascript languages
;;; Commentary:
;; A port of Mozilla Rhino's scanner.
;; Corresponds to Rhino files Token.java and TokenStream.java.
;;; Code:
(eval-when-compile
(require 'cl))
(defvar js2-tokens nil
"List of all defined token names.") ; initialized in `js2-token-names'
(defconst js2-token-names
(let* ((names (make-vector js2-num-tokens -1))
(case-fold-search nil) ; only match js2-UPPER_CASE
(syms (apropos-internal "^js2-\\(?:[A-Z_]+\\)")))
(loop for sym in syms
for i from 0
do
(unless (or (memq sym '(js2-EOF_CHAR js2-ERROR))
(not (boundp sym)))
(aset names (symbol-value sym) ; code, e.g. 152
(substring (symbol-name sym) 4)) ; name, e.g. "LET"
(push sym js2-tokens)))
names)
"Vector mapping int values to token string names, sans `js2-' prefix.")
(defun js2-token-name (tok)
"Return a string name for TOK, a token symbol or code.
Signals an error if it's not a recognized token."
(let ((code tok))
(if (symbolp tok)
(setq code (symbol-value tok)))
(if (eq code -1)
"ERROR"
(if (and (numberp code)
(not (minusp code))
(< code js2-num-tokens))
(aref js2-token-names code)
(error "Invalid token: %s" code)))))
(defsubst js2-token-sym (tok)
"Return symbol for TOK given its code, e.g. 'js2-LP for code 86."
(intern (js2-token-name tok)))
(defconst js2-token-codes
(let ((table (make-hash-table :test 'eq :size 256)))
(loop for name across js2-token-names
for sym = (intern (concat "js2-" name))
do
(puthash sym (symbol-value sym) table))
;; clean up a few that are "wrong" in Rhino's token codes
(puthash 'js2-DELETE js2-DELPROP table)
table)
"Hashtable mapping token symbols to their bytecodes.")
(defsubst js2-token-code (sym)
"Return code for token symbol SYM, e.g. 86 for 'js2-LP."
(or (gethash sym js2-token-codes)
(error "Invalid token symbol: %s " sym))) ; signal code bug
(defsubst js2-report-scan-error (msg &optional no-throw beg len)
(setq js2-token-end js2-ts-cursor)
(js2-report-error msg nil
(or beg js2-token-beg)
(or len (- js2-token-end js2-token-beg)))
(unless no-throw
(throw 'return js2-ERROR)))
(defsubst js2-get-string-from-buffer ()
"Reverse the char accumulator and return it as a string."
(setq js2-token-end js2-ts-cursor)
(if js2-ts-string-buffer
(apply #'string (nreverse js2-ts-string-buffer))
""))
;; TODO: could potentially avoid a lot of consing by allocating a
;; char buffer the way Rhino does.
(defsubst js2-add-to-string (c)
(push c js2-ts-string-buffer))
;; Note that when we "read" the end-of-file, we advance js2-ts-cursor
;; to (1+ (point-max)), which lets the scanner treat end-of-file like
;; any other character: when it's not part of the current token, we
;; unget it, allowing it to be read again by the following call.
(defsubst js2-unget-char ()
(decf js2-ts-cursor))
;; Rhino distinguishes \r and \n line endings. We don't need to
;; because we only scan from Emacs buffers, which always use \n.
(defsubst js2-get-char ()
"Read and return the next character from the input buffer.
Increments `js2-ts-lineno' if the return value is a newline char.
Updates `js2-ts-cursor' to the point after the returned char.
Returns `js2-EOF_CHAR' if we hit the end of the buffer.
Also updates `js2-ts-hit-eof' and `js2-ts-line-start' as needed."
(let (c)
;; check for end of buffer
(if (>= js2-ts-cursor (point-max))
(setq js2-ts-hit-eof t
js2-ts-cursor (1+ js2-ts-cursor)
c js2-EOF_CHAR) ; return value
;; otherwise read next char
(setq c (char-before (incf js2-ts-cursor)))
;; if we read a newline, update counters
(if (= c ?\n)
(setq js2-ts-line-start js2-ts-cursor
js2-ts-lineno (1+ js2-ts-lineno)))
;; TODO: skip over format characters
c)))
(defsubst js2-read-unicode-escape ()
"Read a \\uNNNN sequence from the input.
Assumes the ?\ and ?u have already been read.
Returns the unicode character, or nil if it wasn't a valid character.
Doesn't change the values of any scanner variables."
;; I really wish I knew a better way to do this, but I can't
;; find the Emacs function that takes a 16-bit int and converts
;; it to a Unicode/utf-8 character. So I basically eval it with (read).
;; Have to first check that it's 4 hex characters or it may stop
;; the read early.
(ignore-errors
(let ((s (buffer-substring-no-properties js2-ts-cursor
(+ 4 js2-ts-cursor))))
(if (string-match "[a-zA-Z0-9]\\{4\\}" s)
(read (concat "?\\u" s))))))
(defsubst js2-match-char (test)
"Consume and return next character if it matches TEST, a character.
Returns nil and consumes nothing if TEST is not the next character."
(let ((c (js2-get-char)))
(if (eq c test)
t
(js2-unget-char)
nil)))
(defsubst js2-peek-char ()
(prog1
(js2-get-char)
(js2-unget-char)))
(defsubst js2-java-identifier-start-p (c)
(or
(memq c '(?$ ?_))
(char-is-uppercase c)
(char-is-lowercase c)))
(defsubst js2-java-identifier-part-p (c)
"Implementation of java.lang.Character.isJavaIdentifierPart()"
;; TODO: make me Unicode-friendly. See comments above.
(or
(memq c '(?$ ?_))
(char-is-uppercase c)
(char-is-lowercase c)
(and (>= c ?0) (<= c ?9))))
(defsubst js2-alpha-p (c)
(cond ((and (<= ?A c) (<= c ?Z)) t)
((and (<= ?a c) (<= c ?z)) t)
(t nil)))
(defsubst js2-digit-p (c)
(and (<= ?0 c) (<= c ?9)))
(defsubst js2-js-space-p (c)
(if (<= c 127)
(memq c '(#x20 #x9 #xB #xC #xD))
(or
(eq c #xA0)
;; TODO: change this nil to check for Unicode space character
nil)))
(defconst js2-eol-chars (list js2-EOF_CHAR ?\n ?\r))
(defsubst js2-skip-line ()
"Skip to end of line"
(let (c)
(while (not (memq (setq c (js2-get-char)) js2-eol-chars)))
(js2-unget-char)
(setq js2-token-end js2-ts-cursor)))
(defun js2-init-scanner (&optional buf line)
"Create token stream for BUF starting on LINE.
BUF defaults to current-buffer and line defaults to 1.
A buffer can only have one scanner active at a time, which yields
dramatically simpler code than using a defstruct. If you need to
have simultaneous scanners in a buffer, copy the regions to scan
into temp buffers."
(save-excursion
(when buf
(set-buffer buf))
(setq js2-ts-dirty-line nil
js2-ts-regexp-flags nil
js2-ts-string ""
js2-ts-number nil
js2-ts-hit-eof nil
js2-ts-line-start 0
js2-ts-lineno (or line 1)
js2-ts-line-end-char -1
js2-ts-cursor (point-min)
js2-ts-is-xml-attribute nil
js2-ts-xml-is-tag-content nil
js2-ts-xml-open-tags-count 0
js2-ts-string-buffer nil)))
;; This function uses the cached op, string and number fields in
;; TokenStream; if getToken has been called since the passed token
;; was scanned, the op or string printed may be incorrect.
(defun js2-token-to-string (token)
;; Not sure where this function is used in Rhino. Not tested.
(if (not js2-debug-print-trees)
""
(let ((name (js2-token-name token)))
(cond
((memq token (list js2-STRING js2-REGEXP js2-NAME))
(concat name " `" js2-ts-string "'"))
((eq token js2-NUMBER)
(format "NUMBER %g" js2-ts-number))
(t
name)))))
(defconst js2-keywords
'(break
case catch const continue
debugger default delete do
else enum
false finally for function
if in instanceof import
let
new null
return
switch
this throw true try typeof
var void
while with
yield))
;; Token names aren't exactly the same as the keywords, unfortunately.
;; E.g. enum isn't in the tokens, and delete is js2-DELPROP.
(defconst js2-kwd-tokens
(let ((table (make-vector js2-num-tokens nil))
(tokens
(list js2-BREAK
js2-CASE js2-CATCH js2-CONST js2-CONTINUE
js2-DEBUGGER js2-DEFAULT js2-DELPROP js2-DO
js2-ELSE
js2-FALSE js2-FINALLY js2-FOR js2-FUNCTION
js2-IF js2-IN js2-INSTANCEOF js2-IMPORT
js2-LET
js2-NEW js2-NULL
js2-RETURN
js2-SWITCH
js2-THIS js2-THROW js2-TRUE js2-TRY js2-TYPEOF
js2-VAR
js2-WHILE js2-WITH
js2-YIELD)))
(dolist (i tokens)
(aset table i 'font-lock-keyword-face))
(aset table js2-STRING 'font-lock-string-face)
(aset table js2-REGEXP 'font-lock-string-face)
(aset table js2-COMMENT 'font-lock-comment-face)
(aset table js2-THIS 'font-lock-builtin-face)
(aset table js2-VOID 'font-lock-constant-face)
(aset table js2-NULL 'font-lock-constant-face)
(aset table js2-TRUE 'font-lock-constant-face)
(aset table js2-FALSE 'font-lock-constant-face)
table)
"Vector whose values are non-nil for tokens that are keywords.
The values are default faces to use for highlighting the keywords.")
(defconst js2-reserved-words
'(abstract
boolean byte
char class
double
enum export extends
final float
goto
implements import int interface
long
native
package private protected public
short static super synchronized
throws transient
volatile))
(defconst js2-keyword-names
(let ((table (make-hash-table :test 'equal)))
(loop for k in js2-keywords
do (puthash
(symbol-name k) ; instanceof
(intern (concat "js2-"
(upcase (symbol-name k)))) ; js2-INSTANCEOF
table))
table)
"JavaScript keywords by name, mapped to their symbols.")
(defconst js2-reserved-word-names
(let ((table (make-hash-table :test 'equal)))
(loop for k in js2-reserved-words
do
(puthash (symbol-name k) 'js2-RESERVED table))
table)
"JavaScript reserved words by name, mapped to 'js2-RESERVED.")
(defsubst js2-collect-string (buf)
"Convert BUF, a list of chars, to a string.
Reverses BUF before converting."
(cond
((stringp buf)
buf)
((null buf) ; for emacs21 compat
"")
(t
(if buf
(apply #'string (nreverse buf))
""))))
(defun js2-string-to-keyword (s)
"Return token for S, a string, if S is a keyword or reserved word.
Returns a symbol such as 'js2-BREAK, or nil if not keyword/reserved."
(or (gethash s js2-keyword-names)
(gethash s js2-reserved-word-names)))
(defsubst js2-ts-set-char-token-bounds ()
"Used when next token is one character."
(setq js2-token-beg (1- js2-ts-cursor)
js2-token-end js2-ts-cursor))
(defsubst js2-ts-return (token)
"Return an N-character TOKEN from `js2-get-token'.
Updates `js2-token-end' accordingly."
(setq js2-token-end js2-ts-cursor)
(throw 'return token))
(defsubst js2-x-digit-to-int (c accumulator)
"Build up a hex number.
If C is a hexadecimal digit, return ACCUMULATOR * 16 plus
corresponding number. Otherwise return -1."
(catch 'return
(catch 'check
;; Use 0..9 < A..Z < a..z
(cond
((<= c ?9)
(decf c ?0)
(if (<= 0 c)
(throw 'check nil)))
((<= c ?F)
(when (<= ?A c)
(decf c (- ?A 10))
(throw 'check nil)))
((<= c ?f)
(when (<= ?a c)
(decf c (- ?a 10))
(throw 'check nil))))
(throw 'return -1))
(logior c (lsh accumulator 4))))
(defun js2-get-token ()
"Return next JavaScript token, an int such as js2-RETURN."
(let (c
c1
identifier-start
is-unicode-escape-start
contains-escape
escape-val
escape-start
str
result
base
is-integer
quote-char
val
look-for-slash
continue)
(catch 'return
(while t
;; Eat whitespace, possibly sensitive to newlines.
(setq continue t)
(while continue
(setq c (js2-get-char))
(cond
((eq c js2-EOF_CHAR)
(js2-ts-set-char-token-bounds)
(throw 'return js2-EOF))
((eq c ?\n)
(js2-ts-set-char-token-bounds)
(setq js2-ts-dirty-line nil)
(throw 'return js2-EOL))
((not (js2-js-space-p c))
(if (/= c ?-) ; in case end of HTML comment
(setq js2-ts-dirty-line t))
(setq continue nil))))
;; Assume the token will be 1 char - fixed up below.
(js2-ts-set-char-token-bounds)
(when (eq c ?@)
(throw 'return js2-XMLATTR))
;; identifier/keyword/instanceof?
;; watch out for starting with a <backslash>
(cond
((eq c ?\\)
(setq c (js2-get-char))
(if (eq c ?u)
(setq identifier-start t
is-unicode-escape-start t
js2-ts-string-buffer nil)
(setq identifier-start nil)
(js2-unget-char)
(setq c ?\\)))
(t
(when (setq identifier-start (js2-java-identifier-start-p c))
(setq js2-ts-string-buffer nil)
(js2-add-to-string c))))
(when identifier-start
(setq contains-escape is-unicode-escape-start)
(catch 'break
(while t
(if is-unicode-escape-start
;; strictly speaking we should probably push-back
;; all the bad characters if the <backslash>uXXXX
;; sequence is malformed. But since there isn't a
;; correct context(is there?) for a bad Unicode
;; escape sequence in an identifier, we can report
;; an error here.
(progn
(setq escape-val 0)
(dotimes (i 4)
(setq c (js2-get-char)
escape-val (js2-x-digit-to-int c escape-val))
;; Next check takes care of c < 0 and bad escape
(if (minusp escape-val)
(throw 'break nil)))
(if (minusp escape-val)
(js2-report-scan-error "msg.invalid.escape" t))
(js2-add-to-string escape-val)
(setq is-unicode-escape-start nil))
(setq c (js2-get-char))
(cond
((eq c ?\\)
(setq c (js2-get-char))
(if (eq c ?u)
(setq is-unicode-escape-start t
contains-escape t)
(js2-report-scan-error "msg.illegal.character" t)))
(t
(if (or (eq c js2-EOF_CHAR)
(not (js2-java-identifier-part-p c)))
(throw 'break nil))
(js2-add-to-string c))))))
(js2-unget-char)
(setq str (js2-get-string-from-buffer))
(unless contains-escape
;; OPT we shouldn't have to make a string (object!) to
;; check if it's a keyword.
;; Return the corresponding token if it's a keyword
(when (setq result (js2-string-to-keyword str))
(if (and (< js2-language-version 170)
(memq result '(js2-LET js2-YIELD)))
;; LET and YIELD are tokens only in 1.7 and later
(setq result 'js2-NAME))
(if (neq result js2-RESERVED)
(throw 'return (js2-token-code result)))
(js2-report-warning "msg.reserved.keyword" str)))
;; If we want to intern these as Rhino does, just use (intern str)
(setq js2-ts-string str)
(throw 'return js2-NAME)) ; end identifier/kwd check
;; is it a number?
(when (or (js2-digit-p c)
(and (eq c ?.) (js2-digit-p (js2-peek-char))))
(setq js2-ts-string-buffer nil
base 10)
(when (eq c ?0)
(setq c (js2-get-char))
(cond
((or (eq c ?x) (eq c ?X))
(setq base 16)
(setq c (js2-get-char)))
((js2-digit-p c)
(setq base 8))
(t
(js2-add-to-string ?0))))
(if (eq base 16)
(while (<= 0 (js2-x-digit-to-int c 0))
(js2-add-to-string c)
(setq c (js2-get-char)))
(while (and (<= ?0 c) (<= c ?9))
;; We permit 08 and 09 as decimal numbers, which
;; makes our behavior a superset of the ECMA
;; numeric grammar. We might not always be so
;; permissive, so we warn about it.
(when (and (eq base 8) (>= c ?8))
(js2-report-warning "msg.bad.octal.literal"
(if (eq c ?8) "8" "9"))
(setq base 10))
(js2-add-to-string c)
(setq c (js2-get-char))))
(setq is-integer t)
(when (and (eq base 10) (memq c '(?. ?e ?E)))
(setq is-integer nil)
(when (eq c ?.)
(loop do
(js2-add-to-string c)
(setq c (js2-get-char))
while (js2-digit-p c)))
(when (memq c '(?e ?E))
(js2-add-to-string c)
(setq c (js2-get-char))
(when (memq c '(?+ ?-))
(js2-add-to-string c)
(setq c (js2-get-char)))
(unless (js2-digit-p c)
(js2-report-scan-error "msg.missing.exponent" t))
(loop do
(js2-add-to-string c)
(setq c (js2-get-char))
while (js2-digit-p c))))
(js2-unget-char)
(setq js2-ts-string (js2-get-string-from-buffer)
js2-ts-number
(if (and (eq base 10) (not is-integer))
(string-to-number js2-ts-string)
;; TODO: call runtime number-parser. Some of it is in
;; js2-util.el, but I need to port ScriptRuntime.stringToNumber.
(string-to-number js2-ts-string)))
(throw 'return js2-NUMBER))
;; is it a string?
(when (memq c '(?\" ?\'))
;; We attempt to accumulate a string the fast way, by
;; building it directly out of the reader. But if there
;; are any escaped characters in the string, we revert to
;; building it out of a string buffer.
(setq quote-char c
js2-ts-string-buffer nil
c (js2-get-char))
(catch 'break
(while (/= c quote-char)
(catch 'continue
(when (or (eq c ?\n) (eq c js2-EOF_CHAR))
(js2-unget-char)
(setq js2-token-end js2-ts-cursor)
(js2-report-error "msg.unterminated.string.lit")
(throw 'return js2-STRING))
(when (eq c ?\\)
;; We've hit an escaped character
(setq c (js2-get-char))
(case c
(?b (setq c ?\b))
(?f (setq c ?\f))
(?n (setq c ?\n))
(?r (setq c ?\r))
(?t (setq c ?\t))
(?v (setq c ?\v))
(?u
(setq c1 (js2-read-unicode-escape))
(if js2-parse-ide-mode
(if c1
(progn
;; just copy the string in IDE-mode
(js2-add-to-string ?\\)
(js2-add-to-string ?u)
(dotimes (i 3)
(js2-add-to-string (js2-get-char)))
(setq c (js2-get-char))) ; added at end of loop
;; flag it as an invalid escape
(js2-report-warning "msg.invalid.escape"
nil (- js2-ts-cursor 2) 6))
;; Get 4 hex digits; if the u escape is not
;; followed by 4 hex digits, use 'u' + the
;; literal character sequence that follows.
(js2-add-to-string ?u)
(setq escape-val 0)
(dotimes (i 4)
(setq c (js2-get-char)
escape-val (js2-x-digit-to-int c escape-val))
(if (minusp escape-val)
(throw 'continue nil))
(js2-add-to-string c))
;; prepare for replace of stored 'u' sequence by escape value
(setq js2-ts-string-buffer (nthcdr 5 js2-ts-string-buffer)
c escape-val)))
(?x
;; Get 2 hex digits, defaulting to 'x'+literal
;; sequence, as above.
(setq c (js2-get-char)
escape-val (js2-x-digit-to-int c 0))
(if (minusp escape-val)
(progn
(js2-add-to-string ?x)
(throw 'continue nil))
(setq c1 c
c (js2-get-char)
escape-val (js2-x-digit-to-int c escape-val))
(if (minusp escape-val)
(progn
(js2-add-to-string ?x)
(js2-add-to-string c1)
(throw 'continue nil))
;; got 2 hex digits
(setq c escape-val))))
(?\n
;; Remove line terminator after escape to follow
;; SpiderMonkey and C/C++
(setq c (js2-get-char))
(throw 'continue nil))
(t
(when (and (<= ?0 c) (< c ?8))
(setq val (- c ?0)
c (js2-get-char))
(when (and (<= ?0 c) (< c ?8))
(setq val (- (+ (* 8 val) c) ?0)
c (js2-get-char))
(when (and (<= ?0 c)
(< c ?8)
(< val #o37))
;; c is 3rd char of octal sequence only
;; if the resulting val <= 0377
(setq val (- (+ (* 8 val) c) ?0)
c (js2-get-char))))
(js2-unget-char)
(setq c val)))))
(js2-add-to-string c)
(setq c (js2-get-char)))))
(setq js2-ts-string (js2-get-string-from-buffer))
(throw 'return js2-STRING))
(case c
(?\;
(throw 'return js2-SEMI))
(?\[
(throw 'return js2-LB))
(?\]
(throw 'return js2-RB))
(?{
(throw 'return js2-LC))
(?}
(throw 'return js2-RC))
(?\(
(throw 'return js2-LP))
(?\)
(throw 'return js2-RP))
(?,
(throw 'return js2-COMMA))
(??
(throw 'return js2-HOOK))
(?:
(if (js2-match-char ?:)
(js2-ts-return js2-COLONCOLON)
(throw 'return js2-COLON)))
(?.
(if (js2-match-char ?.)
(js2-ts-return js2-DOTDOT)
(if (js2-match-char ?\()
(js2-ts-return js2-DOTQUERY)
(throw 'return js2-DOT))))
(?|
(if (js2-match-char ?|)
(throw 'return js2-OR)
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_BITOR)
(throw 'return js2-BITOR))))
(?^
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_BITOR)
(throw 'return js2-BITXOR)))
(?&
(if (js2-match-char ?&)
(throw 'return js2-AND)
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_BITAND)
(throw 'return js2-BITAND))))
(?=
(if (js2-match-char ?=)
(if (js2-match-char ?=)
(js2-ts-return js2-SHEQ)
(throw 'return js2-EQ))
(throw 'return js2-ASSIGN)))
(?!
(if (js2-match-char ?=)
(if (js2-match-char ?=)
(js2-ts-return js2-SHNE)
(js2-ts-return js2-NE))
(throw 'return js2-NOT)))
(?<
;; NB:treat HTML begin-comment as comment-till-eol
(when (js2-match-char ?!)
(when (js2-match-char ?-)
(when (js2-match-char ?-)
(js2-skip-line)
(setq js2-ts-comment-type 'html)
(throw 'return js2-COMMENT)))
(js2-unget-char))
(if (js2-match-char ?<)
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_LSH)
(js2-ts-return js2-LSH))
(if (js2-match-char ?=)
(js2-ts-return js2-LE)
(throw 'return js2-LT))))
(?>
(if (js2-match-char ?>)
(if (js2-match-char ?>)
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_URSH)
(js2-ts-return js2-URSH))
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_RSH)
(js2-ts-return js2-RSH)))
(if (js2-match-char ?=)
(js2-ts-return js2-GE)
(throw 'return js2-GT))))
(?*
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_MUL)
(throw 'return js2-MUL)))
(?/
;; is it a // comment?
(when (js2-match-char ?/)
(setq js2-token-beg (- js2-ts-cursor 2))
(js2-skip-line)
(setq js2-ts-comment-type 'line)
;; include newline so highlighting goes to end of window
(incf js2-token-end)
(throw 'return js2-COMMENT))
;; is it a /* comment?
(when (js2-match-char ?*)
(setq look-for-slash nil
js2-token-beg (- js2-ts-cursor 2)
js2-ts-comment-type
(if (js2-match-char ?*)
(progn
(setq look-for-slash t)
'jsdoc)
'block))
(while t
(setq c (js2-get-char))
(cond
((eq c js2-EOF_CHAR)
(setq js2-token-end (1- js2-ts-cursor))
(js2-report-error "msg.unterminated.comment")
(throw 'return js2-COMMENT))
((eq c ?*)
(setq look-for-slash t))
((eq c ?/)
(if look-for-slash
(js2-ts-return js2-COMMENT)))
(t
(setq look-for-slash nil
js2-token-end js2-ts-cursor)))))
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_DIV)
(throw 'return js2-DIV)))
(?#
(when js2-skip-preprocessor-directives
(js2-skip-line)
(setq js2-ts-comment-type 'preprocessor
js2-token-end js2-ts-cursor)
(throw 'return js2-COMMENT))
(throw 'return js2-ERROR))
(?%
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_MOD)
(throw 'return js2-MOD)))
(?~
(throw 'return js2-BITNOT))
(?+
(if (js2-match-char ?=)
(js2-ts-return js2-ASSIGN_ADD)
(if (js2-match-char ?+)
(js2-ts-return js2-INC)
(throw 'return js2-ADD))))
(?-
(cond
((js2-match-char ?=)
(setq c js2-ASSIGN_SUB))
((js2-match-char ?-)
(unless js2-ts-dirty-line
;; treat HTML end-comment after possible whitespace
;; after line start as comment-until-eol
(when (js2-match-char ?>)
(js2-skip-line)
(setq js2-ts-comment-type 'html)
(throw 'return js2-COMMENT)))
(setq c js2-DEC))
(t
(setq c js2-SUB)))
(setq js2-ts-dirty-line t)
(js2-ts-return c))
(otherwise
(js2-report-scan-error "msg.illegal.character")))))))
(defun js2-read-regexp (start-token)
"Called by parser when it gets / or /= in literal context."
(let (c
err
in-class ; inside a '[' .. ']' character-class
flags
(continue t))
(setq js2-token-beg js2-ts-cursor
js2-ts-string-buffer nil
js2-ts-regexp-flags nil)
(if (eq start-token js2-ASSIGN_DIV)
;; mis-scanned /=
(js2-add-to-string ?=)
(if (neq start-token js2-DIV)
(error "failed assertion")))
(while (and (not err)
(or (/= (setq c (js2-get-char)) ?/)
in-class))
(cond
((or (= c ?\n)
(= c js2-EOF_CHAR))
(setq js2-token-end (1- js2-ts-cursor)
err t
js2-ts-string (js2-collect-string js2-ts-string-buffer))
(js2-report-error "msg.unterminated.re.lit"))
(t (cond
((= c ?\\)
(js2-add-to-string c)
(setq c (js2-get-char)))
((= c ?\[)
(setq in-class t))
((= c ?\])
(setq in-class nil)))
(js2-add-to-string c))))
(unless err
(while continue
(cond
((js2-match-char ?g)
(push ?g flags))
((js2-match-char ?i)
(push ?i flags))
((js2-match-char ?m)
(push ?m flags))
(t
(setq continue nil))))
(if (js2-alpha-p (js2-peek-char))
(js2-report-scan-error "msg.invalid.re.flag" t
js2-ts-cursor 1))
(setq js2-ts-string (js2-collect-string js2-ts-string-buffer)
js2-ts-regexp-flags (js2-collect-string flags)
js2-token-end js2-ts-cursor)
;; tell `parse-partial-sexp' to ignore this range of chars
(put-text-property js2-token-beg js2-token-end 'syntax-class '(2)))))
(defun js2-get-first-xml-token ()
(setq js2-ts-xml-open-tags-count 0
js2-ts-is-xml-attribute nil
js2-ts-xml-is-tag-content nil)
(js2-unget-char)
(js2-get-next-xml-token))
(defsubst js2-xml-discard-string ()
"Throw away the string in progress and flag an XML parse error."
(setq js2-ts-string-buffer nil
js2-ts-string nil)
(js2-report-scan-error "msg.XML.bad.form" t))
(defun js2-get-next-xml-token ()
(setq js2-ts-string-buffer nil ; for recording the XML
js2-token-beg js2-ts-cursor)
(let (c result)
(setq result
(catch 'return
(while t
(setq c (js2-get-char))
(cond
((= c js2-EOF_CHAR)
(throw 'return js2-ERROR))
(js2-ts-xml-is-tag-content
(case c
(?>
(js2-add-to-string c)
(setq js2-ts-xml-is-tag-content nil
js2-ts-is-xml-attribute nil))
(?/
(js2-add-to-string c)
(when (eq ?> (js2-peek-char))
(setq c (js2-get-char))
(js2-add-to-string c)
(setq js2-ts-xml-is-tag-content nil)
(decf js2-ts-xml-open-tags-count)))
(?{
(js2-unget-char)
(setq js2-ts-string (js2-get-string-from-buffer))
(throw 'return js2-XML))
((?\' ?\")
(js2-add-to-string c)
(unless (js2-read-quoted-string c)
(throw 'return js2-ERROR)))
(?=
(js2-add-to-string c)
(setq js2-ts-is-xml-attribute t))
((? ?\t ?\r ?\n)
(js2-add-to-string c))
(t
(js2-add-to-string c)
(setq js2-ts-is-xml-attribute nil)))
(when (and (not js2-ts-xml-is-tag-content)
(zerop js2-ts-xml-open-tags-count))
(setq js2-ts-string (js2-get-string-from-buffer))
(throw 'return js2-XMLEND)))
(t
;; else not tag content
(case c
(?<
(js2-add-to-string c)
(setq c (js2-peek-char))
(case c
(?!
(setq c (js2-get-char)) ;; skip !
(js2-add-to-string c)
(setq c (js2-peek-char))
(case c
(?-
(setq c (js2-get-char)) ;; skip -
(js2-add-to-string c)
(if (eq c ?-)
(progn
(js2-add-to-string c)
(unless (js2-read-xml-comment)
(throw 'return js2-ERROR)))
(js2-xml-discard-string)
(throw 'return js2-ERROR)))
(?\[
(setq c (js2-get-char)) ;; skip [
(js2-add-to-string c)
(if (and (= (js2-get-char) ?C)
(= (js2-get-char) ?D)
(= (js2-get-char) ?A)
(= (js2-get-char) ?T)
(= (js2-get-char) ?A)
(= (js2-get-char) ?\[))
(progn
(js2-add-to-string ?C)
(js2-add-to-string ?D)
(js2-add-to-string ?A)
(js2-add-to-string ?T)
(js2-add-to-string ?A)
(js2-add-to-string ?\[)
(unless (js2-read-cdata)
(throw 'return js2-ERROR)))
(js2-xml-discard-string)
(throw 'return js2-ERROR)))
(t
(unless (js2-read-entity)
(throw 'return js2-ERROR)))))
(??
(setq c (js2-get-char)) ;; skip ?
(js2-add-to-string c)
(unless (js2-read-PI)
(throw 'return js2-ERROR)))
(?/
;; end tag
(setq c (js2-get-char)) ;; skip /
(js2-add-to-string c)
(when (zerop js2-ts-xml-open-tags-count)
(js2-xml-discard-string)
(throw 'return js2-ERROR))
(setq js2-ts-xml-is-tag-content t)
(decf js2-ts-xml-open-tags-count))
(t
;; start tag
(setq js2-ts-xml-is-tag-content t)
(incf js2-ts-xml-open-tags-count))))
(?{
(js2-unget-char)
(setq js2-ts-string (js2-get-string-from-buffer))
(throw 'return js2-XML))
(t
(js2-add-to-string c))))))))
(setq js2-token-end js2-ts-cursor)
result))
(defun js2-read-quoted-string (quote)
(let (c)
(catch 'return
(while (/= (setq c (js2-get-char)) js2-EOF_CHAR)
(js2-add-to-string c)
(if (eq c quote)
(throw 'return t)))
(js2-xml-discard-string) ;; throw away string in progress
nil)))
(defun js2-read-xml-comment ()
(let ((c (js2-get-char)))
(catch 'return
(while (/= c js2-EOF_CHAR)
(catch 'continue
(js2-add-to-string c)
(when (and (eq c ?-) (eq ?- (js2-peek-char)))
(setq c (js2-get-char))
(js2-add-to-string c)
(if (eq (js2-peek-char) ?>)
(progn
(setq c (js2-get-char)) ;; skip >
(js2-add-to-string c)
(throw 'return t))
(throw 'continue nil)))
(setq c (js2-get-char))))
(js2-xml-discard-string)
nil)))
(defun js2-read-cdata ()
(let ((c (js2-get-char)))
(catch 'return
(while (/= c js2-EOF_CHAR)
(catch 'continue
(js2-add-to-string c)
(when (and (eq c ?\]) (eq (js2-peek-char) ?\]))
(setq c (js2-get-char))
(js2-add-to-string c)
(if (eq (js2-peek-char) ?>)
(progn
(setq c (js2-get-char)) ;; Skip >
(js2-add-to-string c)
(throw 'return t))
(throw 'continue nil)))
(setq c (js2-get-char))))
(js2-xml-discard-string)
nil)))
(defun js2-read-entity ()
(let ((decl-tags 1)
c)
(catch 'return
(while (/= js2-EOF_CHAR (setq c (js2-get-char)))
(js2-add-to-string c)
(case c
(?<
(incf decl-tags))
(?>
(decf decl-tags)
(if (zerop decl-tags)
(throw 'return t)))))
(js2-xml-discard-string)
nil)))
(defun js2-read-PI ()
"Scan an XML processing instruction."
(let (c)
(catch 'return
(while (/= js2-EOF_CHAR (setq c (js2-get-char)))
(js2-add-to-string c)
(when (and (eq c ??) (eq (js2-peek-char) ?>))
(setq c (js2-get-char)) ;; Skip >
(js2-add-to-string c)
(throw 'return t)))
(js2-xml-discard-string)
nil)))
(defun js2-scanner-get-line ()
"Return the text of the current scan line."
(buffer-substring (point-at-bol) (point-at-eol)))
(provide 'js2-scan)
;;; js2-scan.el ends here
;;; js2-messages: localizable messages for js2-mode
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Keywords: javascript languages
;;; Commentary:
;; Messages are copied from Rhino's Messages.properties.
;; Many of the Java-specific messages have been elided.
;; Add any js2-specific ones at the end, so we can keep
;; this file synced with changes to Rhino's.
;;
;; TODO:
;; - move interpreter messages into separate file
;;; Code:
(defvar js2-message-table
(make-hash-table :test 'equal :size 250)
"Contains localized messages for js2-mode.")
;; TODO: construct this hashtable at compile-time.
(defmacro js2-msg (key &rest strings)
`(puthash ,key (funcall #'concat ,@strings)
js2-message-table))
(defun js2-get-msg (msg-key)
"Look up a localized message.
MSG-KEY is a list of (MSG ARGS). If the message takes parameters,
the correct number of ARGS must be provided."
(let* ((key (if (listp msg-key) (car msg-key) msg-key))
(args (if (listp msg-key) (cdr msg-key)))
(msg (gethash key js2-message-table)))
(if msg
(apply #'format msg args)
key))) ; default to showing the key
(js2-msg "msg.dup.parms"
"Duplicate parameter name '%s'.")
(js2-msg "msg.too.big.jump"
"Program too complex: jump offset too big.")
(js2-msg "msg.too.big.index"
"Program too complex: internal index exceeds 64K limit.")
(js2-msg "msg.while.compiling.fn"
"Encountered code generation error while compiling function '%s': %s")
(js2-msg "msg.while.compiling.script"
"Encountered code generation error while compiling script: %s")
;; Context
(js2-msg "msg.ctor.not.found"
"Constructor for '%s' not found.")
(js2-msg "msg.not.ctor"
"'%s' is not a constructor.")
;; FunctionObject
(js2-msg "msg.varargs.ctor"
"Method or constructor '%s' must be static "
"with the signature (Context cx, Object[] args, "
"Function ctorObj, boolean inNewExpr) "
"to define a variable arguments constructor.")
(js2-msg "msg.varargs.fun"
"Method '%s' must be static with the signature "
"(Context cx, Scriptable thisObj, Object[] args, Function funObj) "
"to define a variable arguments function.")
(js2-msg "msg.incompat.call"
"Method '%s' called on incompatible object.")
(js2-msg "msg.bad.parms"
"Unsupported parameter type '%s' in method '%s'.")
(js2-msg "msg.bad.method.return"
"Unsupported return type '%s' in method '%s'.")
(js2-msg "msg.bad.ctor.return"
"Construction of objects of type '%s' is not supported.")
(js2-msg "msg.no.overload"
"Method '%s' occurs multiple times in class '%s'.")
(js2-msg "msg.method.not.found"
"Method '%s' not found in '%s'.")
;; IRFactory
(js2-msg "msg.bad.for.in.lhs"
"Invalid left-hand side of for..in loop.")
(js2-msg "msg.mult.index"
"Only one variable allowed in for..in loop.")
(js2-msg "msg.bad.for.in.destruct"
"Left hand side of for..in loop must be an array of "
"length 2 to accept key/value pair.")
(js2-msg "msg.cant.convert"
"Can't convert to type '%s'.")
(js2-msg "msg.bad.assign.left"
"Invalid assignment left-hand side.")
(js2-msg "msg.bad.decr"
"Invalid decerement operand.")
(js2-msg "msg.bad.incr"
"Invalid increment operand.")
(js2-msg "msg.bad.yield"
"yield must be in a function.")
(js2-msg "msg.yield.parenthesized"
"yield expression must be parenthesized.")
;; NativeGlobal
(js2-msg "msg.cant.call.indirect"
"Function '%s' must be called directly, and not by way of a "
"function of another name.")
(js2-msg "msg.eval.nonstring"
"Calling eval() with anything other than a primitive "
"string value will simply return the value. "
"Is this what you intended?")
(js2-msg "msg.eval.nonstring.strict"
"Calling eval() with anything other than a primitive "
"string value is not allowed in strict mode.")
(js2-msg "msg.bad.destruct.op"
"Invalid destructuring assignment operator")
;; NativeCall
(js2-msg "msg.only.from.new"
"'%s' may only be invoked from a `new' expression.")
(js2-msg "msg.deprec.ctor"
"The '%s' constructor is deprecated.")
;; NativeFunction
(js2-msg "msg.no.function.ref.found"
"no source found to decompile function reference %s")
(js2-msg "msg.arg.isnt.array"
"second argument to Function.prototype.apply must be an array")
;; NativeGlobal
(js2-msg "msg.bad.esc.mask"
"invalid string escape mask")
;; NativeRegExp
(js2-msg "msg.bad.quant"
"Invalid quantifier %s")
(js2-msg "msg.overlarge.backref"
"Overly large back reference %s")
(js2-msg "msg.overlarge.min"
"Overly large minimum %s")
(js2-msg "msg.overlarge.max"
"Overly large maximum %s")
(js2-msg "msg.zero.quant"
"Zero quantifier %s")
(js2-msg "msg.max.lt.min"
"Maximum %s less than minimum")
(js2-msg "msg.unterm.quant"
"Unterminated quantifier %s")
(js2-msg "msg.unterm.paren"
"Unterminated parenthetical %s")
(js2-msg "msg.unterm.class"
"Unterminated character class %s")
(js2-msg "msg.bad.range"
"Invalid range in character class.")
(js2-msg "msg.trail.backslash"
"Trailing \\ in regular expression.")
(js2-msg "msg.re.unmatched.right.paren"
"unmatched ) in regular expression.")
(js2-msg "msg.no.regexp"
"Regular expressions are not available.")
(js2-msg "msg.bad.backref"
"back-reference exceeds number of capturing parentheses.")
(js2-msg "msg.bad.regexp.compile"
"Only one argument may be specified if the first "
"argument to RegExp.prototype.compile is a RegExp object.")
;; Parser
(js2-msg "msg.got.syntax.errors"
"Compilation produced %s syntax errors.")
(js2-msg "msg.var.redecl"
"TypeError: redeclaration of var %s.")
(js2-msg "msg.const.redecl"
"TypeError: redeclaration of const %s.")
(js2-msg "msg.let.redecl"
"TypeError: redeclaration of variable %s.")
(js2-msg "msg.parm.redecl"
"TypeError: redeclaration of formal parameter %s.")
(js2-msg "msg.fn.redecl"
"TypeError: redeclaration of function %s.")
(js2-msg "msg.let.decl.not.in.block"
"SyntaxError: let declaration not directly within block")
;; NodeTransformer
(js2-msg "msg.dup.label"
"duplicated label")
(js2-msg "msg.undef.label"
"undefined label")
(js2-msg "msg.bad.break"
"unlabelled break must be inside loop or switch")
(js2-msg "msg.continue.outside"
"continue must be inside loop")
(js2-msg "msg.continue.nonloop"
"continue can only use labels of iteration statements")
(js2-msg "msg.bad.throw.eol"
"Line terminator is not allowed between the throw "
"keyword and throw expression.")
(js2-msg "msg.no.paren.parms"
"missing ( before function parameters.")
(js2-msg "msg.no.parm"
"missing formal parameter")
(js2-msg "msg.no.paren.after.parms"
"missing ) after formal parameters")
(js2-msg "msg.no.brace.body"
"missing '{' before function body")
(js2-msg "msg.no.brace.after.body"
"missing } after function body")
(js2-msg "msg.no.paren.cond"
"missing ( before condition")
(js2-msg "msg.no.paren.after.cond"
"missing ) after condition")
(js2-msg "msg.no.semi.stmt"
"missing ; before statement")
(js2-msg "msg.missing.semi"
"missing ; after statement")
(js2-msg "msg.no.name.after.dot"
"missing name after . operator")
(js2-msg "msg.no.name.after.coloncolon"
"missing name after :: operator")
(js2-msg "msg.no.name.after.dotdot"
"missing name after .. operator")
(js2-msg "msg.no.name.after.xmlAttr"
"missing name after .@")
(js2-msg "msg.no.bracket.index"
"missing ] in index expression")
(js2-msg "msg.no.paren.switch"
"missing ( before switch expression")
(js2-msg "msg.no.paren.after.switch"
"missing ) after switch expression")
(js2-msg "msg.no.brace.switch"
"missing '{' before switch body")
(js2-msg "msg.bad.switch"
"invalid switch statement")
(js2-msg "msg.no.colon.case"
"missing : after case expression")
(js2-msg "msg.double.switch.default"
"double default label in the switch statement")
(js2-msg "msg.no.while.do"
"missing while after do-loop body")
(js2-msg "msg.no.paren.for"
"missing ( after for")
(js2-msg "msg.no.semi.for"
"missing ; after for-loop initializer")
(js2-msg "msg.no.semi.for.cond"
"missing ; after for-loop condition")
(js2-msg "msg.in.after.for.name"
"missing in after for")
(js2-msg "msg.no.paren.for.ctrl"
"missing ) after for-loop control")
(js2-msg "msg.no.paren.with"
"missing ( before with-statement object")
(js2-msg "msg.no.paren.after.with"
"missing ) after with-statement object")
(js2-msg "msg.no.paren.after.let"
"missing ( after let")
(js2-msg "msg.no.paren.let"
"missing ) after variable list")
(js2-msg "msg.no.curly.let"
"missing } after let statement")
(js2-msg "msg.bad.return"
"invalid return")
(js2-msg "msg.no.brace.block"
"missing } in compound statement")
(js2-msg "msg.bad.label"
"invalid label")
(js2-msg "msg.bad.var"
"missing variable name")
(js2-msg "msg.bad.var.init"
"invalid variable initialization")
(js2-msg "msg.no.colon.cond"
"missing : in conditional expression")
(js2-msg "msg.no.paren.arg"
"missing ) after argument list")
(js2-msg "msg.no.bracket.arg"
"missing ] after element list")
(js2-msg "msg.bad.prop"
"invalid property id")
(js2-msg "msg.no.colon.prop"
"missing : after property id")
(js2-msg "msg.no.brace.prop"
"missing } after property list")
(js2-msg "msg.no.paren"
"missing ) in parenthetical")
(js2-msg "msg.reserved.id"
"identifier is a reserved word")
(js2-msg "msg.no.paren.catch"
"missing ( before catch-block condition")
(js2-msg "msg.bad.catchcond"
"invalid catch block condition")
(js2-msg "msg.catch.unreachable"
"any catch clauses following an unqualified catch are unreachable")
(js2-msg "msg.no.brace.try"
"missing '{' before try block")
(js2-msg "msg.no.brace.catchblock"
"missing '{' before catch-block body")
(js2-msg "msg.try.no.catchfinally"
"'try' without 'catch' or 'finally'")
(js2-msg "msg.no.return.value"
"function %s does not always return a value")
(js2-msg "msg.anon.no.return.value"
"anonymous function does not always return a value")
(js2-msg "msg.return.inconsistent"
"return statement is inconsistent with previous usage")
(js2-msg "msg.generator.returns"
"TypeError: generator function '%s' returns a value")
(js2-msg "msg.anon.generator.returns"
"TypeError: anonymous generator function returns a value")
(js2-msg "msg.syntax"
"syntax error")
(js2-msg "msg.unexpected.eof"
"Unexpected end of file")
(js2-msg "msg.XML.bad.form"
"illegally formed XML syntax")
(js2-msg "msg.XML.not.available"
"XML runtime not available")
(js2-msg "msg.too.deep.parser.recursion"
"Too deep recursion while parsing")
(js2-msg "msg.no.side.effects"
"Code has no side effects")
(js2-msg "msg.extra.trailing.comma"
"Trailing comma is not legal in an ECMA-262 object initializer")
(js2-msg "msg.array.trailing.comma"
"Trailing comma yields different behavior across browsers")
(js2-msg "msg.equal.as.assign"
(concat "Test for equality (==) mistyped as assignment (=)?"
" (parenthesize to suppress warning)"))
(js2-msg "msg.var.hides.arg"
"Variable %s hides argument")
(js2-msg "msg.destruct.assign.no.init"
"Missing = in destructuring declaration")
;; ScriptRuntime
(js2-msg "msg.no.properties"
"%s has no properties.")
(js2-msg "msg.invalid.iterator"
"Invalid iterator value")
(js2-msg "msg.iterator.primitive"
"__iterator__ returned a primitive value")
(js2-msg "msg.assn.create.strict"
"Assignment to undeclared variable %s")
(js2-msg "msg.ref.undefined.prop"
"Reference to undefined property '%s'")
(js2-msg "msg.prop.not.found"
"Property %s not found.")
(js2-msg "msg.invalid.type"
"Invalid JavaScript value of type %s")
(js2-msg "msg.primitive.expected"
"Primitive type expected (had %s instead)")
(js2-msg "msg.namespace.expected"
"Namespace object expected to left of :: (found %s instead)")
(js2-msg "msg.null.to.object"
"Cannot convert null to an object.")
(js2-msg "msg.undef.to.object"
"Cannot convert undefined to an object.")
(js2-msg "msg.cyclic.value"
"Cyclic %s value not allowed.")
(js2-msg "msg.is.not.defined"
"'%s' is not defined.")
(js2-msg "msg.undef.prop.read"
"Cannot read property '%s' from %s")
(js2-msg "msg.undef.prop.write"
"Cannot set property '%s' of %s to '%s'")
(js2-msg "msg.undef.prop.delete"
"Cannot delete property '%s' of %s")
(js2-msg "msg.undef.method.call"
"Cannot call method '%s' of %s")
(js2-msg "msg.undef.with"
"Cannot apply 'with' to %s")
(js2-msg "msg.isnt.function"
"%s is not a function, it is %s.")
(js2-msg "msg.isnt.function.in"
"Cannot call property %s in object %s. "
"It is not a function, it is '%s'.")
(js2-msg "msg.function.not.found"
"Cannot find function %s.")
(js2-msg "msg.function.not.found.in"
"Cannot find function %s in object %s.")
(js2-msg "msg.isnt.xml.object"
"%s is not an xml object.")
(js2-msg "msg.no.ref.to.get"
"%s is not a reference to read reference value.")
(js2-msg "msg.no.ref.to.set"
"%s is not a reference to set reference value to %s.")
(js2-msg "msg.no.ref.from.function"
"Function %s can not be used as the left-hand "
"side of assignment or as an operand of ++ or -- operator.")
(js2-msg "msg.bad.default.value"
"Object's getDefaultValue() method returned an object.")
(js2-msg "msg.instanceof.not.object"
"Can't use instanceof on a non-object.")
(js2-msg "msg.instanceof.bad.prototype"
"'prototype' property of %s is not an object.")
(js2-msg "msg.bad.radix"
"illegal radix %s.")
;; ScriptableObject
(js2-msg "msg.default.value"
"Cannot find default value for object.")
(js2-msg "msg.zero.arg.ctor"
"Cannot load class '%s' which has no zero-parameter constructor.")
(js2-msg "msg.ctor.multiple.parms"
"Can't define constructor or class %s since more than "
"one constructor has multiple parameters.")
(js2-msg "msg.extend.scriptable"
"%s must extend ScriptableObject in order to define property %s.")
(js2-msg "msg.bad.getter.parms"
"In order to define a property, getter %s must have zero "
"parameters or a single ScriptableObject parameter.")
(js2-msg "msg.obj.getter.parms"
"Expected static or delegated getter %s to take "
"a ScriptableObject parameter.")
(js2-msg "msg.getter.static"
"Getter and setter must both be static or neither be static.")
(js2-msg "msg.setter.return"
"Setter must have void return type: %s")
(js2-msg "msg.setter2.parms"
"Two-parameter setter must take a ScriptableObject as "
"its first parameter.")
(js2-msg "msg.setter1.parms"
"Expected single parameter setter for %s")
(js2-msg "msg.setter2.expected"
"Expected static or delegated setter %s to take two parameters.")
(js2-msg "msg.setter.parms"
"Expected either one or two parameters for setter.")
(js2-msg "msg.setter.bad.type"
"Unsupported parameter type '%s' in setter '%s'.")
(js2-msg "msg.add.sealed"
"Cannot add a property to a sealed object: %s.")
(js2-msg "msg.remove.sealed"
"Cannot remove a property from a sealed object: %s.")
(js2-msg "msg.modify.sealed"
"Cannot modify a property of a sealed object: %s.")
(js2-msg "msg.modify.readonly"
"Cannot modify readonly property: %s.")
;; TokenStream
(js2-msg "msg.missing.exponent"
"missing exponent")
(js2-msg "msg.caught.nfe"
"number format error")
(js2-msg "msg.unterminated.string.lit"
"unterminated string literal")
(js2-msg "msg.unterminated.comment"
"unterminated comment")
(js2-msg "msg.unterminated.re.lit"
"unterminated regular expression literal")
(js2-msg "msg.invalid.re.flag"
"invalid flag after regular expression")
(js2-msg "msg.no.re.input.for"
"no input for %s")
(js2-msg "msg.illegal.character"
"illegal character")
(js2-msg "msg.invalid.escape"
"invalid Unicode escape sequence")
(js2-msg "msg.bad.namespace"
"not a valid default namespace statement. "
"Syntax is: default xml namespace = EXPRESSION;")
;; TokensStream warnings
(js2-msg "msg.bad.octal.literal"
"illegal octal literal digit %s; "
"interpreting it as a decimal digit")
(js2-msg "msg.reserved.keyword"
"illegal usage of future reserved keyword %s; "
"interpreting it as ordinary identifier")
(js2-msg "msg.script.is.not.constructor"
"Script objects are not constructors.")
;; Arrays
(js2-msg "msg.arraylength.bad"
"Inappropriate array length.")
;; Arrays
(js2-msg "msg.arraylength.too.big"
"Array length %s exceeds supported capacity limit.")
;; URI
(js2-msg "msg.bad.uri"
"Malformed URI sequence.")
;; Number
(js2-msg "msg.bad.precision"
"Precision %s out of range.")
;; NativeGenerator
(js2-msg "msg.send.newborn"
"Attempt to send value to newborn generator")
(js2-msg "msg.already.exec.gen"
"Already executing generator")
(js2-msg "msg.StopIteration.invalid"
"StopIteration may not be changed to an arbitrary object.")
;; Interpreter
(js2-msg "msg.yield.closing"
"Yield from closing generator")
(provide 'js2-messages)
;;; js2-ast.el --- JavaScript syntax tree node definitions
;; Author: Steve Yegge (steve.yegge@gmail.com)
;; Keywords: javascript languages
;;; Code:
(eval-and-compile
(require 'cl))
;; flags for ast node property 'member-type (used for e4x operators)
(defvar js2-property-flag #x1 "property access: element is valid name")
(defvar js2-attribute-flag #x2 "x.@y or x..@y")
(defvar js2-descendants-flag #x4 "x..y or x..@i")
(defsubst js2-relpos (pos anchor)
"Convert POS to be relative to ANCHOR.
If POS is nil, returns nil."
(and pos (- pos anchor)))
(defsubst js2-make-pad (indent)
(if (zerop indent)
""
(make-string (* indent js2-basic-offset) ? )))
(defsubst js2-visit-ast (node callback)
"Visit every node in ast NODE with visitor CALLBACK.
CALLBACK is a function that takes two arguments: (NODE END-P). It is
called twice: once to visit the node, and again after all the node's
children have been processed. The END-P argument is nil on the first
call and non-nil on the second call. The return value of the callback
affects the traversal: if non-nil, the children of NODE are processed.
If the callback returns nil, or if the node has no children, then the
callback is called immediately with a non-nil END-P argument.
The node traversal is approximately lexical-order, although there
are currently no guarantees around this."
(let ((vfunc (get (aref node 0) 'js2-visitor)))
;; visit the node
(when (funcall callback node nil)
;; visit the kids
(cond
((eq vfunc 'js2-visit-none)
nil) ; don't even bother calling it
;; Each AST node type has to define a `js2-visitor' function
;; that takes a node and a callback, and calls `js2-visit-ast'
;; on each child of the node.
(vfunc
(funcall vfunc node callback))
(t
(error "%s does not define a visitor-traversal function"
(aref node 0)))))
;; call the end-visit
(funcall callback node t)))
(defstruct (js2-node
(:constructor nil)) ; abstract
"Base AST node type."
(type -1) ; token type
(pos -1) ; start position of this AST node in parsed input
(len 1) ; num characters spanned by the node
props ; optional node property list (an alist)
parent) ; link to parent node; null for root
(defsubst js2-node-get-prop (node prop &optional default)
(or (cadr (assoc prop (js2-node-props node))) default))
(defsubst js2-node-set-prop (node prop value)
(setf (js2-node-props node)
(cons (list prop value) (js2-node-props node))))
(defsubst js2-fixup-starts (n nodes)
"Adjust the start positions of NODES to be relative to N.
Any node in the list may be nil, for convenience."
(dolist (node nodes)
(when node
(setf (js2-node-pos node) (- (js2-node-pos node)
(js2-node-pos n))))))
(defsubst js2-node-add-children (parent &rest nodes)
"Set parent node of NODES to PARENT, and return PARENT.
Does nothing if we're not recording parent links.
If any given node in NODES is nil, doesn't record that link."
(js2-fixup-starts parent nodes)
(dolist (node nodes)
(and node
(setf (js2-node-parent node) parent))))
;; Non-recursive since it's called a frightening number of times.
(defsubst js2-node-abs-pos (n)
(let ((pos (js2-node-pos n)))
(while (setq n (js2-node-parent n))
(setq pos (+ pos (js2-node-pos n))))
pos))
(defsubst js2-node-abs-end (n)
"Return absolute buffer position of end of N."
(+ (js2-node-abs-pos n) (js2-node-len n)))
;; It's important to make sure block nodes have a lisp list for the
;; child nodes, to limit printing recursion depth in an AST that
;; otherwise consists of defstruct vectors. Emacs will crash printing
;; a sufficiently large vector tree.
(defstruct (js2-block-node
(:include js2-node)
(:constructor nil)
(:constructor make-js2-block-node (&key (type js2-BLOCK)
(pos js2-token-beg)
len
props
kids)))
"A block of statements."
kids) ; a lisp list of the child statement nodes
(put 'cl-struct-js2-block-node 'js2-visitor 'js2-visit-block)
(put 'cl-struct-js2-block-node 'js2-printer 'js2-print-block)
(defsubst js2-visit-block (ast callback)
"Visit the `js2-block-node' children of AST."
(dolist (kid (js2-block-node-kids ast))
(js2-visit-ast kid callback)))
(defun js2-print-block (n i)
(let ((pad (js2-make-pad i)))
(insert pad "{\n")
(dolist (kid (js2-block-node-kids n))
(js2-print-ast kid (1+ i)))
(insert pad "}")))
(defstruct (js2-scope
(:include js2-block-node)
(:constructor nil)
(:constructor make-js2-scope (&key (type js2-BLOCK)
(pos js2-token-beg)
len
kids)))
;; The symbol-table is a LinkedHashMap<String,Symbol> in Rhino.
;; I don't have one of those handy, so I'll use an alist for now.
;; It's as fast as an emacs hashtable for up to about 50 elements,
;; and is much lighter-weight to construct (both CPU and mem).
;; The keys are interned strings (symbols) for faster lookup.
;; Should switch to hybrid alist/hashtable eventually.
symbol-table ; an alist of (symbol . js2-symbol)
parent-scope ; a `js2-scope'
top) ; top-level `js2-scope' (script/function)
(put 'cl-struct-js2-scope 'js2-visitor 'js2-visit-none)
(put 'cl-struct-js2-scope 'js2-printer 'js2-print-none)
(defun js2-scope-set-parent-scope (scope parent)
(setf (js2-scope-parent-scope scope) parent
(js2-scope-top scope) (if (null parent)
scope
(js2-scope-top parent))))
(defun js2-node-get-enclosing-scope (node)
"Return the innermost `js2-scope' node surrounding NODE.
Returns nil if there is no enclosing scope node."
(let ((parent (js2-node-parent node)))
(while (not (js2-scope-p parent))
(setq parent (js2-node-parent parent)))
parent))
(defun js2-get-defining-scope (scope name)
"Search up scope chain from SCOPE looking for NAME, a string or symbol.
Returns `js2-scope' in which NAME is defined, or nil if not found."
(let ((sym (if (symbolp name)
name
(intern name)))
table
result
(continue t))
(while (and scope continue)
(if (and (setq table (js2-scope-symbol-table scope))
(assq sym table))
(setq continue nil
result scope)
(setq scope (js2-scope-parent-scope scope))))
result))
(defsubst js2-scope-get-symbol (scope name)
"Return symbol table entry for NAME in SCOPE.
NAME can be a string or symbol. Returns a `js2-symbol' or nil if not found."
(and (js2-scope-symbol-table scope)
(cdr (assq (if (symbolp name)
name
(intern name))
(js2-scope-symbol-table scope)))))
(defsubst js2-scope-put-symbol (scope name symbol)
"Enter SYMBOL into symbol-table for SCOPE under NAME.
NAME can be a lisp symbol or string. SYMBOL is a `js2-symbol'."
(let* ((table (js2-scope-symbol-table scope))
(sym (if (symbolp name) name (intern name)))
(entry (assq sym table)))
(if entry
(setcdr entry symbol)
(push (cons sym symbol)
(js2-scope-symbol-table scope)))))
(defstruct (js2-symbol
(:constructor nil)
(:constructor make-js2-symbol (decl-type name &optional ast-node)))
"A symbol table entry."
;; One of js2-FUNCTION, js2-LP (for parameters), js2-VAR,
;; js2-LET, or js2-CONST
decl-type
name ; string
ast-node) ; a `js2-node'
(defstruct (js2-error-node
(:include js2-node)
(:constructor nil) ; silence emacs21 byte-compiler
(:constructor make-js2-error-node (&key (type js2-ERROR)
(pos js2-token-beg)
len)))
"AST node representing a parse error.")
(put 'cl-struct-js2-error-node 'js2-visitor 'js2-visit-none)
(put 'cl-struct-js2-error-node 'js2-printer 'js2-print-none)
(defstruct (js2-script-node
(:include js2-scope)
(:constructor nil)
(:constructor make-js2-script-node (&key (type js2-SCRIPT)
(pos js2-token-beg)
len
var-decls
fun-decls)))
functions ; lisp list of nested functions
regexps ; lisp list of (string . flags)
symbols ; alist (every symbol gets unique index)
(param-count 0)
var-names ; vector of string names
consts ; bool-vector matching var-decls
(temp-number 0)) ; for generating temp variables
(put 'cl-struct-js2-script-node 'js2-visitor 'js2-visit-block)
(put 'cl-struct-js2-script-node 'js2-printer 'js2-print-script)
(defun js2-print-script (node indent)
(dolist (kid (js2-block-node-kids node))
(js2-print-ast kid indent)))
(defstruct (js2-ast-root
(:include js2-script-node)
(:constructor nil)
(:constructor make-js2-ast-root (&key (type js2-SCRIPT)
(pos js2-token-beg)
len
buffer)))
"The root node of a js2 AST."
buffer ; the source buffer from which the code was parsed
comments ; a lisp list of comments, ordered by start position
errors ; a lisp list of errors found during parsing
warnings ; a lisp list of warnings found during parsing
node-count) ; number of nodes in the tree, including the root
(put 'cl-struct-js2-ast-root 'js2-visitor 'js2-visit-ast-root)
(put 'cl-struct-js2-ast-root 'js2-printer 'js2-print-script)
(defun js2-visit-ast-root (ast callback)
(dolist (kid (js2-ast-root-kids ast))
(js2-visit-ast kid callback))
(dolist (comment (js2-ast-root-comments ast))
(js2-visit-ast comment callback)))
(defstruct (js2-comment-node
(:include js2-node)
(:constructor nil)
(:constructor make-js2-comment-node (&key (type js2-COMMENT)
(pos js2-token-beg)
len
(format js2-ts-comment-type))))
format) ; 'line, 'block, 'jsdoc or 'html
(put 'cl-struct-js2-comment-node 'js2-visitor 'js2-visit-none)
(put 'cl-struct-js2-comment-node 'js2-printer 'js2-print-comment)
(defun js2-print-comment (n i)
;; We really ought to link end-of-line comments to their nodes.
;; Or maybe we could add a new comment type, 'endline.
(insert (js2-make-pad i)
(js2-node-string n)))
(defstruct (js2-expr-stmt-node
(:include js2-node)
(:constructor nil)
(:constructor make-js2-expr-stmt-node (&key (type js2-EXPR_VOID)
(pos js2-ts-cursor)
len
expr)))
"An expression statement."
expr)
(defsubst js2-expr-stmt-node-set-has-result (node)
"Change the node type to `js2-EXPR_RESULT'. Used for code generation."
(setf (js2-node-type node) js2-EXPR_RESULT))
(put 'cl-struct-js2-expr-stmt-node 'js2-visitor 'js2-visit-expr-stmt-node)
(put 'cl-struct-js2-expr-stmt-node 'js2-printer 'js2-print-expr-stmt-node)
(defun js2-visit-expr-stmt-node (n v)
(js2-visit-ast (js2-expr-stmt-node-expr n) v))
(defun js2-print-expr-stmt-node (n indent)
(js2-print-ast (js2-expr-stmt-node-expr n) indent)
(insert ";\n"))
(defstruct (js2-loop-node
(:include js2-scope)
(:constructor nil))
"Abstract supertype of loop nodes."
body ; a `js2-block-node'
lp ; position of left-paren, nil if omitted
rp) ; position of right-paren, nil if omitted
(defstruct (js2-do-node
(:include js2-loop-node)
(:constructor nil)
(:constructor make-js2-do-node (&key (type js2-DO)
(pos js2-token-beg)
len
body
condition
while-pos
lp
rp)))
"AST node for do-loop."
condition ; while (expression)
while-pos) ; buffer position of 'while' keyword
(put 'cl-struct-js2-do-node 'js2-visitor 'js2-visit-do-node)
(put 'cl-struct-js2-do-node 'js2-printer 'js2-print-do-node)
(defun js2-visit-do-node (n v)
(js2-visit-ast (js2-do-node-body n) v)
(js2-visit-ast (js2-do-node-condition n) v))
(defun js2-print-do-node (n i)
(let ((pad (js2-make-pad i)))
(insert pad "do {\n")
(dolist (kid (js2-block-node-kids (js2-do-node-body n)))
(js2-print-ast kid (1+ i)))
(insert pad "} while (")
(js2-print-ast (js2-do-node-condition n) 0)
(insert ");\n")))
(defstruct (js2-while-node
(:include js2-loop-node)
(:constructor nil)
(:constructor make-js2-while-node (&key (type js2-WHILE)
(pos js2-token-beg)
len
body
condition
lp
rp)))
"AST node for while-loop."
condition) ; while-condition
(put 'cl-struct-js2-while-node 'js2-visitor 'js2-visit-while-node)
(put 'cl-struct-js2-while-node 'js2-printer 'js2-print-while-node)
(defun js2-visit-while-node (n v)
(js2-visit-ast (js2-while-node-condition n) v)
(js2-visit-ast (js2-while-node-body n) v))
(defun js2-print-while-node (n i)
(let ((pad (js2-make-pad i)))
(insert pad "while (")
(js2-print-ast (js2-while-node-condition n) 0)
(insert ") {\n")
(js2-print-body (js2-while-node-body n) (1+ i))
(insert pad "}\n")))
(defstruct (js2-for-node
(:include js2-loop-node)
(:constructor nil)
(:constructor make-js2-for-node (&key (type js2-FOR)
(pos js2-ts-cursor)
len
body
init
condition
update
lp
rp)))
"AST node for a C-style for-loop."
init ; initialization expression
condition ; loop condition
update) ; update clause
(put 'cl-struct-js2-for-node 'js2-visitor 'js2-visit-for-node)
(put 'cl-struct-js2-for-node 'js2-printer 'js2-print-for-node)
(defun js2-visit-for-node (n v)
(js2-visit-ast (js2-for-node-init n) v)
(js2-visit-ast (js2-for-node-condition n) v)
(js2-visit-ast (js2-for-node-update n) v)
(js2-visit-ast (js2-for-node-body n) v))
(defun js2-print-for-node (n i)
(let ((pad (js2-make-pad i)))
(insert pad "for (")
(js2-print-ast (js2-for-node-init n) 0)
(insert "; ")
(js2-print-ast (js2-for-node-condition n) 0)
(insert "; ")
(js2-print-ast (js2-for-node-update n) 0)
(insert ") {\n")
(js2-print-body (js2-for-node-body n) (1+ i))
(insert pad "}\n")))
(defstruct (js2-for-in-node
(:include js2-loop-node)
(:constructor nil)
(:constructor make-js2-for-in-node (&key (type js2-FOR)
(pos js2-ts-cursor)
len
body
iterator
object
in-pos
each-pos
foreach-p
lp
rp)))
"AST node for a for..in loop."
iterator ; [var] foo in ...
object ; object over which we're iterating
in-pos ; buffer position of 'in' keyword
each-pos ; buffer position of 'each' keyword, if foreach-p
foreach-p) ; t if it's a for-each loop
(put 'cl-struct-js2-for-in-node 'js2-visitor 'js2-visit-for-in-node)
(put 'cl-struct-js2-for-in-node 'js2-printer 'js2-print-for-in-node)
(defun js2-visit-for-in-node (n v)
(js2-visit-ast (js2-for-in-node-iterator n) v)
(js2-visit-ast (js2-for-in-node-object n) v)
(js2-visit-ast (js2-for-in-node-body n) v))
(defun js2-print-for-in-node (n i)
(let ((pad (js2-make-pad i))
(foreach (js2-for-in-node-foreach-p n)))
(insert pad "for ")
(if foreach
(insert "each "))
(insert "(")
(js2-print-ast (js2-for-in-node-iterator n) 0)
(insert " in ")
(js2-print-ast (js2-for-in-node-object n) 0)
(insert ") {\n")
(js2-print-body (js2-for-in-node-body n) (1+ i))
(insert pad "}\n")))
(defstruct (js2-return-node
(:include js2-node)
(:constructor nil)
(:constructor make-js2-return-node (&key (type js2-RETURN)
(pos js2-ts-cursor)
len
retval)))
"AST node for a return statement."
retval) ; expression to return, or 'undefined
(put 'cl-struct-js2-return-node 'js2-visitor 'js2-visit-return-node)
(put 'cl-struct-js2-return-node 'js2-printer 'js2-print-return-node)
(defun js2-visit-return-node (n v)
(if (js2-return-node-retval n)
(js2-visit-ast (js2-return-node-retval n) v)))
(defun js2-print-return-node (n i)
(insert (js2-make-pad i) "return")
(when (js2-return-node-retval n)
(insert " ")
(js2-print-ast (js2-return-node-retval n) 0))
(insert ";\n"))
(defstruct (js2-if-node
(:include js2-node)
(:constructor nil)
(:constructor make-js2-if-node (&key (type js2-IF)
(pos js2-ts-cursor)
len
condition
then-part
else-pos
else-part
lp
rp)))
"AST node for an if-statement."
condition ; expression
then-part ; statement or block
else-pos ; optional buffer position of 'else' keyword
else-part ; optional statement or block
lp ; position of left-paren, nil if omitted
rp) ; position of right-paren, nil if omitted
(put 'cl-struct-js2-if-node 'js2-visitor 'js2-visit-if-node)
(put 'cl-struct-js2-if-node 'js2-printer 'js2-print-if-node)
(defun js2-visit-if-node (n v)
(js2-visit-ast (js2-if-node-condition n) v)
(js2-visit-ast (js2-if-node-then-part n) v)
(if (js2-if-node-else-part n)
(js2-visit-ast (js2-if-node-else-part n) v)))
(defun js2-print-if-node (n i)
(let ((pad (js2-make-pad i))
(then-part (js2-if-node-then-part n))
(else-part (js2-if-node-else-part n)))
(insert pad "if (")
(js2-print-ast (js2-if-node-condition n) 0)
(insert ") {\n")
(js2-print-body then-part (1+ i))
(insert pad "}")
(cond
((not else-part)
(insert "\n"))
((js2-if-node-p else-part)
(insert " else ")
(js2-print-body else-part i))
(t
(insert " else {\n")
(js2-print-body else-part (1+ i))
(insert pad "}\n")))))
(defstruct (js2-try-node
(:include js2-node)
(:constructor nil)
(:constructor make-js2-try-node (&key (type js2-TRY)
(pos js2-ts-cursor)
len
try-block
catch-clauses
finally-block)))
"AST node for a try-statement."
try-block
catch-clauses ; a lisp list of `js2-catch-node'
finally-block) ; a `js2-finally-node'
(put 'cl-struct-js2-try-node 'js2-visitor 'js2-visit-try-node)
(put 'cl-struct-js2-try-node 'js2-printer 'js2-print-try-node)
(defun js2-visit-try-node (n v)
(js2-visit-ast (js2-try-node-try-block n) v)
(dolist (clause (js2-try-node-catch-clauses n))
(js2-visit-ast clause v))
(if (js2-try-node-finally-block n)
(js2-visit-ast (js2-try-node-finally-block n) v)))
(defun js2-print-try-node (n i)
(let ((pad (js2-make-pad i))
(catches (js2-try-node-catch-clauses n))
(finally (js2-try-node-finally-block n)))
(insert pad "try {\n")
(js2-print-body (js2-try-node-try-block n) (1+ i))
(insert pad "}")
(when catches
(dolist (catch catches)
(js2-print-ast catch i)))