emacs-elpa-diffs
[Top][All Lists]
Advanced

[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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]