Created
June 27, 2013 10:33
-
-
Save chakrit/5875491 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Taken from Stevie's Blog Rant http://steve-yegge.blogspot.com/2008/02/portrait-of-n00b.html | |
(defun js2-parse-variables (in-for decl-type) | |
"Parse a 'var', 'const' or 'let' statement or for-loop initializer. | |
IN-FOR is true if we are currently in the midst of the init clause of a for. | |
DECL-TYPE is a token value: either VAR, CONST, or LET depending on context. | |
Returns the parsed statement node." | |
(let ((result (make-js2-var-decl-node)) | |
destructuring-init | |
destructuring | |
s start tt init name node | |
(continue t)) | |
;; Examples: | |
;; var foo = {a: 1, b: 2}, bar = [3, 4]; | |
;; var {b: s2, a: s1} = foo, x = 6, y, [s3, s4] = bar; | |
(while continue | |
(setq destructuring nil | |
s nil | |
tt (js2-peek-token) | |
start js2-token-start | |
init nil) | |
(if (or (= tt js2-LB) (= tt js2-LC)) | |
;; Destructuring assignment, e.g., var [a, b] = ... | |
(setq destructuring (js2-parse-primary-expr)) | |
;; Simple variable name | |
(js2-must-match js2-NAME "msg.bad.var") | |
(setq name (make-js2-name-node)) | |
(js2-define-symbol decl-type js2-ts-string)) | |
(when (js2-match-token js2-ASSIGN) | |
(setq init (js2-parse-assign-expr in-for))) | |
(if destructuring | |
(progn | |
(if (null init) | |
;; for (var [k, v] in foo) is initialized differently | |
(unless in-for | |
(js2-report-error "msg.destruct.assign.no.init"))) | |
(setq node (make-js2-destructuring-init-node :start start | |
:end js2-ts-cursor | |
:lhs destructuring | |
:initializer init)) | |
(js2-node-add-children node destructuring init)) | |
;; simple variable, possibly with initializer | |
(setq node (make-js2-var-init-node :start start | |
:end js2-ts-cursor | |
:name name | |
:initializer init)) | |
(js2-node-add-children node name init)) | |
(js2-block-node-push result node) | |
(js2-node-add-children result node) | |
(unless (js2-match-token js2-COMMA) | |
(setq continue nil))) | |
result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment