[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 07/46: Cut down on reserved words; improve error reporting
From: |
Dmitry Gutov |
Subject: |
[elpa] 07/46: Cut down on reserved words; improve error reporting |
Date: |
Sat, 15 Nov 2014 20:57:57 +0000 |
dgutov pushed a commit to branch master
in repository elpa.
commit 0815f85d5460242febe06756abfb209fad4aa701
Author: Dmitry Gutov <address@hidden>
Date: Sun Apr 6 06:26:54 2014 +0300
Cut down on reserved words; improve error reporting
Closes #100
---
js2-mode.el | 58 +++++++++++++++++++++++++-----------------------------
tests/parser.el | 6 ++--
2 files changed, 30 insertions(+), 34 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index da9aab9..2b3af58 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -1649,7 +1649,7 @@ the correct number of ARGS must be provided."
"missing ) in parenthetical")
(js2-msg "msg.reserved.id"
- "identifier is a reserved word")
+ "'%s' is a reserved identifier")
(js2-msg "msg.no.paren.catch"
"missing ( before catch-block condition")
@@ -1907,10 +1907,6 @@ the correct number of ARGS must be provided."
"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.")
@@ -5284,21 +5280,10 @@ into temp buffers."
"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))
+;; FIXME: Support strict mode-only future reserved words, after we know
+;; which parts scopes are in strict mode, and which are not.
+(defconst js2-reserved-words '(class export extends import super)
+ "Future reserved keywords in ECMAScript 5.")
(defconst js2-keyword-names
(let ((table (make-hash-table :test 'equal)))
@@ -5521,9 +5506,9 @@ its relevant fields and puts it into `js2-ti-tokens'."
(memq result '(js2-LET js2-YIELD)))
;; LET and YIELD are tokens only in 1.7 and later
(setq result 'js2-NAME))
- (if (not (eq result 'js2-RESERVED))
- (throw 'return (js2-tt-code result)))
- (js2-report-warning "msg.reserved.keyword" str)))
+ (when (eq result 'js2-RESERVED)
+ (setf (js2-token-string token) str))
+ (throw 'return (js2-tt-code result))))
;; If we want to intern these as Rhino does, just use (intern str)
(setf (js2-token-string token) str)
(throw 'return js2-NAME)) ; end identifier/kwd check
@@ -7024,11 +7009,11 @@ from `js2-ti-tokens'. Otherwise, call `js2-get-token'."
(defalias 'js2-next-token 'js2-get-token)
-(defun js2-match-token (match)
+(defun js2-match-token (match &optional dont-unget)
"Get next token and return t if it matches MATCH, a bytecode.
Returns nil and consumes nothing if MATCH is not the next token."
(if (/= (js2-get-token) match)
- (ignore (js2-unget-token))
+ (ignore (unless dont-unget (js2-unget-token)))
t))
(defun js2-match-contextual-kwd (name)
@@ -7046,7 +7031,8 @@ string is NAME. Returns nil and keeps current token
otherwise."
(or (= tt js2-NAME)
(and js2-allow-keywords-as-property-names
(plusp tt)
- (aref js2-kwd-tokens tt))))
+ (or (= tt js2-RESERVED)
+ (aref js2-kwd-tokens tt)))))
(defun js2-match-prop-name ()
"Consume token and return t if next token is a valid property name.
@@ -7080,9 +7066,19 @@ same line as its operand."
"Match next token to token code TOKEN, or record a syntax error.
MSG-ID is the error message to report if the match fails.
Returns t on match, nil if no match."
- (if (js2-match-token token)
+ (if (js2-match-token token t)
t
(js2-report-error msg-id nil pos len)
+ (js2-unget-token)
+ nil))
+
+(defun js2-must-match-name (msg-id)
+ (if (js2-match-token js2-NAME t)
+ t
+ (if (eq (js2-current-token-type) js2-RESERVED)
+ (js2-report-error "msg.reserved.id" (js2-current-token-string))
+ (js2-report-error msg-id)
+ (js2-unget-token))
nil))
(defsubst js2-inside-function ()
@@ -7343,7 +7339,7 @@ NODE is either `js2-array-node', `js2-object-node', or
`js2-name-node'."
(not rest-param-at))
;; to report errors if there are more parameters
(setq rest-param-at (length params)))
- (js2-must-match js2-NAME "msg.no.parm")
+ (js2-must-match-name "msg.no.parm")
(js2-record-face 'js2-function-param)
(setq param (js2-create-name-node))
(js2-define-symbol js2-LP (js2-current-token-string) param)
@@ -7404,7 +7400,7 @@ Last token scanned is the close-curly for the function
body."
(defun js2-parse-function-stmt ()
(let ((pos (js2-current-token-beg))
(star-p (js2-match-token js2-MUL)))
- (js2-must-match js2-NAME "msg.unnamed.function.stmt")
+ (js2-must-match-name "msg.unnamed.function.stmt")
(let ((name (js2-create-name-node t))
pn member-expr)
(cond
@@ -7914,7 +7910,7 @@ Parses for, for-in, and for each-in statements."
(js2-define-destruct-symbols param js2-LET nil))
;; simple name
(t
- (js2-must-match js2-NAME "msg.bad.catchcond")
+ (js2-must-match-name "msg.bad.catchcond")
(setq param (js2-create-name-node))
(js2-define-symbol js2-LET (js2-current-token-string) param))))
;; pattern guard
@@ -8352,7 +8348,7 @@ Returns the parsed `js2-var-decl-node' expression node."
end (js2-node-end destructuring))
;; Simple variable name
(js2-unget-token)
- (when (js2-must-match js2-NAME "msg.bad.var")
+ (when (js2-must-match-name "msg.bad.var")
(setq name (js2-create-name-node)
nbeg (js2-current-token-beg)
nend (js2-current-token-end)
diff --git a/tests/parser.el b/tests/parser.el
index 60634bb..1da9108 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -97,7 +97,7 @@ the test."
"function foo() {\n}")
(js2-deftest-parse function-expression-statements-are-verboten
- "function() {}" :syntax-error "function")
+ "function() {}" :syntax-error "(")
(js2-deftest-parse member-expr-as-function-name
"function a.b.c[2](x, y) {\n}"
@@ -199,10 +199,10 @@ the test."
"(a, b = 1, ...c) => { c;\n};")
(js2-deftest-parse parenless-arrow-function-prohibits-rest
- "...b => {b + 1;};" :syntax-error "b" :errors-count 2)
+ "...b => {b + 1;};" :syntax-error "=>" :errors-count 2)
(js2-deftest-parse parenless-arrow-function-prohibits-destructuring
- "[a, b] => {a + b;};" :syntax-error "]" :errors-count 5)
+ "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 5)
;;; Automatic semicolon insertion
- [elpa] branch master updated (46fb875 -> 4e4c53b), Dmitry Gutov, 2014/11/15
- [elpa] 01/46: Add support for React classes in js2-imenu-extras, Dmitry Gutov, 2014/11/15
- [elpa] 02/46: Merge pull request #133 from johnmastro/imenu-react, Dmitry Gutov, 2014/11/15
- [elpa] 06/46: Merge pull request #138 from martinp/fix-electric-indent, Dmitry Gutov, 2014/11/15
- [elpa] 03/46: Reference #72 in the README, Dmitry Gutov, 2014/11/15
- [elpa] 05/46: Fix electric-indent-mode, Dmitry Gutov, 2014/11/15
- [elpa] 08/46: Fix parsing of function statements in blocks, Dmitry Gutov, 2014/11/15
- [elpa] 04/46: Try to improve formatting, Dmitry Gutov, 2014/11/15
- [elpa] 09/46: Rename mention of espresso-mode to js-mode, Dmitry Gutov, 2014/11/15
- [elpa] 10/46: js2-parse-array-literal: improve trailing comma warning, Dmitry Gutov, 2014/11/15
- [elpa] 07/46: Cut down on reserved words; improve error reporting,
Dmitry Gutov <=
- [elpa] 11/46: Support yield*, Dmitry Gutov, 2014/11/15
- [elpa] 16/46: Support Unicode Identifiers, Dmitry Gutov, 2014/11/15
- [elpa] 17/46: Check if buffer is alive before reparsing., Dmitry Gutov, 2014/11/15
- [elpa] 19/46: Better docstrings, Dmitry Gutov, 2014/11/15
- [elpa] 14/46: Support ES6 generator comprehensions, Dmitry Gutov, 2014/11/15
- [elpa] 13/46: Support ES6 array comprehensions, Dmitry Gutov, 2014/11/15
- [elpa] 12/46: Add `.' to electric-indent-chars for method continuations, Dmitry Gutov, 2014/11/15
- [elpa] 15/46: Add Contributing section, Dmitry Gutov, 2014/11/15
- [elpa] 24/46: Small tweak, Dmitry Gutov, 2014/11/15
- [elpa] 23/46: Add js2-language-version checks, Dmitry Gutov, 2014/11/15