[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master c910d1b 19/45: Parse tagged templates
From: |
Dmitry Gutov |
Subject: |
[elpa] master c910d1b 19/45: Parse tagged templates |
Date: |
Mon, 02 Feb 2015 03:18:40 +0000 |
branch: master
commit c910d1b04dfe2796755b4a1e5e60aa75e90b671b
Author: Dmitry Gutov <address@hidden>
Commit: Dmitry Gutov <address@hidden>
Parse tagged templates
Closes #153
---
js2-mode.el | 39 +++++++++++++++++++++++++++++++++++++--
tests/parser.el | 3 +++
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index 98120e5..0534702 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -666,8 +666,9 @@ which doesn't seem particularly useful, but Rhino permits
it."
(defvar js2-SUPER 166)
(defvar js2-TEMPLATE_HEAD 167) ; part of template literal before
substitution
(defvar js2-NO_SUBS_TEMPLATE 168) ; template literal without substitutions
+(defvar js2-TAGGED_TEMPLATE 169) ; tagged template literal
-(defconst js2-num-tokens (1+ js2-NO_SUBS_TEMPLATE))
+(defconst js2-num-tokens (1+ js2-TAGGED_TEMPLATE))
(defconst js2-debug-print-trees nil)
@@ -3512,7 +3513,6 @@ You can tell the quote type by looking at the first
character."
(put 'cl-struct-js2-template-node 'js2-printer 'js2-print-template)
(defun js2-visit-template (n callback)
- "Visit the `js2-template-node' children of AST."
(dolist (kid (js2-template-node-kids n))
(js2-visit-ast kid callback)))
@@ -3523,6 +3523,27 @@ You can tell the quote type by looking at the first
character."
(insert (js2-node-string kid))
(js2-print-ast kid))))
+(defstruct (js2-tagged-template-node
+ (:include js2-node)
+ (:constructor nil)
+ (:constructor make-js2-tagged-template-node (&key (type
js2-TAGGED_TEMPLATE)
+ beg len tag
template)))
+ "Tagged template literal."
+ tag ; `js2-node' with the tag expression.
+ template) ; `js2-template-node' with the template.
+
+(put 'cl-struct-js2-tagged-template-node 'js2-visitor
'js2-visit-tagged-template)
+(put 'cl-struct-js2-tagged-template-node 'js2-printer
'js2-print-tagged-template)
+
+(defun js2-visit-tagged-template (n callback)
+ (js2-visit-ast (js2-tagged-template-node-tag n) kid callback)
+ (js2-visit-ast (js2-tagged-template-node-template n) kid callback))
+
+(defun js2-print-tagged-template (n i)
+ (insert (js2-make-pad i))
+ (js2-print-ast (js2-tagged-template-node-tag n))
+ (js2-print-ast (js2-tagged-template-node-template n)))
+
(defstruct (js2-array-node
(:include js2-node)
(:constructor nil)
@@ -9145,6 +9166,10 @@ Returns an expression tree that includes PN, the parent
node."
(if allow-call-syntax
(setq pn (js2-parse-function-call pn))
(setq continue nil)))
+ ((= tt js2-TEMPLATE_HEAD)
+ (setq pn (js2-parse-tagged-template pn (js2-parse-template-literal))))
+ ((= tt js2-NO_SUBS_TEMPLATE)
+ (setq pn (js2-parse-tagged-template pn (make-js2-string-node :type
tt))))
(t
(js2-unget-token)
(setq continue nil))))
@@ -9152,6 +9177,16 @@ Returns an expression tree that includes PN, the parent
node."
(js2-parse-highlight-member-expr-node pn))
pn))
+(defun js2-parse-tagged-template (tag-node tpl-node)
+ "Parse tagged template expression."
+ (let* ((beg (js2-node-pos tag-node))
+ (pn (make-js2-tagged-template-node :beg beg
+ :len (- (js2-current-token-end)
beg)
+ :tag tag-node
+ :template tpl-node)))
+ (js2-node-add-children pn tag-node tpl-node)
+ pn))
+
(defun js2-parse-dot-query (pn)
"Parse a dot-query expression, e.g. foo.bar.(@name == 2)
Last token parsed must be `js2-DOTQUERY'."
diff --git a/tests/parser.el b/tests/parser.el
index 39a55e9..dc210a5 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -350,6 +350,9 @@ the test."
(js2-deftest-parse template-with-substitutions
"var y = `${a + b} ${d + e + f}`;")
+(js2-deftest-parse tagged-template
+ "foo.args`${++x, \"o\"}k`;")
+
;;; Classes
(js2-deftest-parse parse-harmony-class-statement
- [elpa] master b1f7e6c 11/45: Simplify parsing of destructuring literals, (continued)
- [elpa] master b1f7e6c 11/45: Simplify parsing of destructuring literals, Dmitry Gutov, 2015/02/01
- [elpa] master 68b4abd 02/45: Add ES6 class externs, Dmitry Gutov, 2015/02/01
- [elpa] master ee17084 13/45: js2-parse-plain-property: Disallow numbers as abbreviated props, Dmitry Gutov, 2015/02/01
- [elpa] master b19ea52 12/45: js2-parse-named-prop: Highlight externals in abbreviated props, Dmitry Gutov, 2015/02/01
- [elpa] master 31c49e0 14/45: js2-imenu-extension-styles: Turn into defvar, Dmitry Gutov, 2015/02/01
- [elpa] master 9b77d64 15/45: Implement template strings, ignoring substitutions, Dmitry Gutov, 2015/02/01
- [elpa] master 7b3d590 17/45: No need to explicitly apply face to string nodes, Dmitry Gutov, 2015/02/01
- [elpa] master 3f09ff3 09/45: Fix string literals in parser.el test harness., Dmitry Gutov, 2015/02/01
- [elpa] master d0c6bf3 18/45: Comment the new token types, Dmitry Gutov, 2015/02/01
- [elpa] master 9525eb3 10/45: Merge branch 'master' of github.com:shicks/js2-mode into shicks-master, Dmitry Gutov, 2015/02/01
- [elpa] master c910d1b 19/45: Parse tagged templates,
Dmitry Gutov <=
- [elpa] master 8df508e 16/45: Handle template substitutions, Dmitry Gutov, 2015/02/01
- [elpa] master 92063fe 22/45: Fix js2-visit-tagged-template, Dmitry Gutov, 2015/02/01
- [elpa] master 94c2ec9 20/45: Drop `js2-allow-keywords-as-property-names' and `js2-valid-prop-name-token', Dmitry Gutov, 2015/02/01
- [elpa] master ab82544 23/45: Do not declare fn expression's name in the enclosing scope, Dmitry Gutov, 2015/02/01
- [elpa] master e6dc986 24/45: Limit the scope of `with-silent-modifications', Dmitry Gutov, 2015/02/01
- [elpa] master 6a4e84b 26/45: js2-parse-for: Reword docstring and comments, Dmitry Gutov, 2015/02/01
- [elpa] master 8088e75 21/45: Add js2-language-version check for template literals, Dmitry Gutov, 2015/02/01
- [elpa] master 395d4ae 29/45: Highlight methods with font-lock-function-name-face, Dmitry Gutov, 2015/02/01
- [elpa] master a117465 32/45: Handle "arguments" specially, Dmitry Gutov, 2015/02/01
- [elpa] master 3c4367f 25/45: Make catch-node inherit from scope, Dmitry Gutov, 2015/02/01