[Pkg-javascript-commits] [node-acorn-jsx] 102/484: [util/walk] Add findNodeAfter

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:11 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-acorn-jsx.

commit b1845b51b17a31f8e0073291e60a5df3c44eae19
Author: Marijn Haverbeke <marijnh at gmail.com>
Date:   Sun Feb 24 21:25:25 2013 +0100

    [util/walk] Add findNodeAfter
---
 index.html   | 34 ++++++++++++++++------------------
 util/walk.js | 18 ++++++++++++++++++
 2 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/index.html b/index.html
index 5418270..fe54262 100644
--- a/index.html
+++ b/index.html
@@ -555,17 +555,15 @@ will return <code>null</code> unless the integer has exactly <code>len</code> di
     <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="sr">/[89]/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="o">||</span> <span class="nx">strict</span><span class="p">)</span> <span class="nx">raise</span><span class="p">(</span><span class="nx">start</span><span class="p">,</span> <span class="s2">"Invalid number"</span><span class="p [...]
     <span class="k">else</span> <span class="nx">val</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="mi">8</span><span class="p">);</span>
     <span class="k">return</span> <span class="nx">finishToken</span><span class="p">(</span><span class="nx">_num</span><span class="p">,</span> <span class="nx">val</span><span class="p">);</span>
-  <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-69">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-69">¶</a>               </div>               <p>Read a string value, interpreting backslash-escapes.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">var</span> <span class="nx">rs_st [...]
-
-  <span class="kd">function</span> <span class="nx">readString</span><span class="p">(</span><span class="nx">quote</span><span class="p">)</span> <span class="p">{</span>
+  <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-69">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-69">¶</a>               </div>               <p>Read a string value, interpreting backslash-escapes.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx"> [...]
     <span class="nx">tokPos</span><span class="o">++</span><span class="p">;</span>
-    <span class="nx">rs_str</span><span class="p">.</span><span class="nx">length</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
+    <span class="kd">var</span> <span class="nx">out</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span>
     <span class="k">for</span> <span class="p">(;;)</span> <span class="p">{</span>
       <span class="k">if</span> <span class="p">(</span><span class="nx">tokPos</span> <span class="o">>=</span> <span class="nx">inputLen</span><span class="p">)</span> <span class="nx">raise</span><span class="p">(</span><span class="nx">tokStart</span><span class="p">,</span> <span class="s2">"Unterminated string constant"</span><span class="p">);</span>
       <span class="kd">var</span> <span class="nx">ch</span> <span class="o">=</span> <span class="nx">input</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="nx">tokPos</span><span class="p">);</span>
       <span class="k">if</span> <span class="p">(</span><span class="nx">ch</span> <span class="o">===</span> <span class="nx">quote</span><span class="p">)</span> <span class="p">{</span>
         <span class="o">++</span><span class="nx">tokPos</span><span class="p">;</span>
-        <span class="k">return</span> <span class="nx">finishToken</span><span class="p">(</span><span class="nx">_string</span><span class="p">,</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">rs_str</span><span class="p">));</span>
+        <span class="k">return</span> <span class="nx">finishToken</span><span class="p">(</span><span class="nx">_string</span><span class="p">,</span> <span class="nx">out</span><span class="p">);</span>
       <span class="p">}</span>
       <span class="k">if</span> <span class="p">(</span><span class="nx">ch</span> <span class="o">===</span> <span class="mi">92</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// '\'</span>
         <span class="nx">ch</span> <span class="o">=</span> <span class="nx">input</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="o">++</span><span class="nx">tokPos</span><span class="p">);</span>
@@ -576,30 +574,30 @@ will return <code>null</code> unless the integer has exactly <code>len</code> di
         <span class="o">++</span><span class="nx">tokPos</span><span class="p">;</span>
         <span class="k">if</span> <span class="p">(</span><span class="nx">octal</span><span class="p">)</span> <span class="p">{</span>
           <span class="k">if</span> <span class="p">(</span><span class="nx">strict</span><span class="p">)</span> <span class="nx">raise</span><span class="p">(</span><span class="nx">tokPos</span> <span class="o">-</span> <span class="mi">2</span><span class="p">,</span> <span class="s2">"Octal literal in strict mode"</span><span class="p">);</span>
-          <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">octal</span><span class="p">,</span> <span class="mi">8</span><span class="p">));</span>
+          <span class="nx">out</span> <span class="o">+=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">octal</span><span class="p">,</span> <span class="mi">8</span><span class="p">));</span>
           <span class="nx">tokPos</span> <span class="o">+=</span> <span class="nx">octal</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
           <span class="k">switch</span> <span class="p">(</span><span class="nx">ch</span><span class="p">)</span> <span class="p">{</span>
-          <span class="k">case</span> <span class="mi">110</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'n' -> '\n'</span>
-          <span class="k">case</span> <span class="mi">114</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">13</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'r' -> '\r'</span>
-          <span class="k">case</span> <span class="mi">120</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">readHexChar</span><span class="p">(</span><span class="mi">2</span><span class="p">));</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'x'</span>
-          <span class="k">case</span> <span class="mi">117</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">readHexChar</span><span class="p">(</span><span class="mi">4</span><span class="p">));</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'u'</span>
-          <span class="k">case</span> <span class="mi">85</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">readHexChar</span><span class="p">(</span><span class="mi">8</span><span class="p">));</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'U'</span>
-          <span class="k">case</span> <span class="mi">116</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">9</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 't' -> '\t'</span>
-          <span class="k">case</span> <span class="mi">98</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">8</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'b' -> '\b'</span>
-          <span class="k">case</span> <span class="mi">118</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">11</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'v' -> '\u000b'</span>
-          <span class="k">case</span> <span class="mi">102</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">12</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'f' -> '\f'</span>
-          <span class="k">case</span> <span class="mi">48</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 0 -> '\0'</span>
+          <span class="k">case</span> <span class="mi">110</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\n"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'n' -> '\n'</span>
+          <span class="k">case</span> <span class="mi">114</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\r"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'r' -> '\r'</span>
+          <span class="k">case</span> <span class="mi">120</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">readHexChar</span><span class="p">(</span><span class="mi">2</span><span class="p">));</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'x'</span>
+          <span class="k">case</span> <span class="mi">117</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">readHexChar</span><span class="p">(</span><span class="mi">4</span><span class="p">));</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'u'</span>
+          <span class="k">case</span> <span class="mi">85</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">readHexChar</span><span class="p">(</span><span class="mi">8</span><span class="p">));</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'U'</span>
+          <span class="k">case</span> <span class="mi">116</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\t"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 't' -> '\t'</span>
+          <span class="k">case</span> <span class="mi">98</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\b"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'b' -> '\b'</span>
+          <span class="k">case</span> <span class="mi">118</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\v"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'v' -> '\u000b'</span>
+          <span class="k">case</span> <span class="mi">102</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\f"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 'f' -> '\f'</span>
+          <span class="k">case</span> <span class="mi">48</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="s2">"\0"</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span> <span class="c1">// 0 -> '\0'</span>
           <span class="k">case</span> <span class="mi">13</span><span class="o">:</span> <span class="k">if</span> <span class="p">(</span><span class="nx">input</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="nx">tokPos</span><span class="p">)</span> <span class="o">===</span> <span class="mi">10</span><span class="p">)</span> <span class="o">++</span><span class="nx">tokPos</span><span class="p">;</span> <span class="c1">// '\r\n [...]
           <span class="k">case</span> <span class="mi">10</span><span class="o">:</span> <span class="c1">// ' \n'</span>
             <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">locations</span><span class="p">)</span> <span class="p">{</span> <span class="nx">tokLineStart</span> <span class="o">=</span> <span class="nx">tokPos</span><span class="p">;</span> <span class="o">++</span><span class="nx">tokCurLine</span><span class="p">;</span> <span class="p">}</span>
             <span class="k">break</span><span class="p">;</span>
-          <span class="k">default</span><span class="o">:</span> <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">ch</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span>
+          <span class="k">default</span><span class="o">:</span> <span class="nx">out</span> <span class="o">+=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">ch</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span>
           <span class="p">}</span>
         <span class="p">}</span>
       <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
         <span class="k">if</span> <span class="p">(</span><span class="nx">ch</span> <span class="o">===</span> <span class="mi">13</span> <span class="o">||</span> <span class="nx">ch</span> <span class="o">===</span> <span class="mi">10</span> <span class="o">||</span> <span class="nx">ch</span> <span class="o">===</span> <span class="mi">8232</span> <span class="o">||</span> <span class="nx">ch</span> <span class="o">===</span> <span class="mi">8329</span><span class="p">)</span> <spa [...]
-        <span class="nx">rs_str</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">ch</span><span class="p">);</span> <span class="c1">// '\'</span>
+        <span class="nx">out</span> <span class="o">+=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">ch</span><span class="p">);</span> <span class="c1">// '\'</span>
         <span class="o">++</span><span class="nx">tokPos</span><span class="p">;</span>
       <span class="p">}</span>
     <span class="p">}</span>
diff --git a/util/walk.js b/util/walk.js
index cfa7eb0..d6f2074 100644
--- a/util/walk.js
+++ b/util/walk.js
@@ -95,6 +95,24 @@
     }
   };
 
+  // Find the outermost matching node after a given position.
+  exports.findNodeAfter = function(node, pos, test, base, state) {
+    test = makeTest(test);
+    try {
+      if (!base) base = exports.base;
+      var c = function(node, st, override) {
+        if (node.end < pos) return;
+        var type = override || node.type;
+        if (node.start >= pos && test(node, type)) throw new Found(node, st);
+        base[type](node, st, c);
+      };
+      c(node, state);
+    } catch (e) {
+      if (e instanceof Found) return e;
+      throw e;
+    }
+  };
+
   // Used to create a custom walker. Will fill in all missing node
   // type properties with the defaults.
   exports.make = function(funcs, base) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-jsx.git



More information about the Pkg-javascript-commits mailing list