[pypy-svn] r40457 - in pypy/dist/pypy/lang/js: . js

santagada at codespeak.net santagada at codespeak.net
Tue Mar 13 22:17:19 CET 2007


Author: santagada
Date: Tue Mar 13 22:17:16 2007
New Revision: 40457

Modified:
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/js/jsparse.js
   pypy/dist/pypy/lang/js/jsparser.py
   pypy/dist/pypy/lang/js/operations.py
Log:
new quoting system, now working better, just need to fix a bug in narcissus


Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Tue Mar 13 22:17:16 2007
@@ -55,7 +55,8 @@
         functioncode = "__anon__ = function (%s) {%s}"%(fargs, fbody)
     else:
         functioncode = "__anon__ = function () {}"
-    print functioncode
+    if DEBUG:
+        print functioncode
     return evaljs(ctx, [W_String(functioncode),], this)
 
 def printjs(ctx, args, this):

Modified: pypy/dist/pypy/lang/js/js/jsparse.js
==============================================================================
--- pypy/dist/pypy/lang/js/js/jsparse.js	(original)
+++ pypy/dist/pypy/lang/js/js/jsparse.js	Tue Mar 13 22:17:16 2007
@@ -159,7 +159,7 @@
             token.value = id;
         } else if ((match = /^"(?:\\.|[^"])*"|^'(?:[^']|\\.)*'/(input))) { //"){
             token.type = STRING;
-            token.value = eval(match[0]);
+            token.value = match[0];
         } else if (this.scanOperand &&
                    (match = /^\/((?:\\.|[^\/])+)\/([gi]*)/(input))) {
             token.type = REGEXP;
@@ -297,7 +297,7 @@
         }
         
         if(typeof a[i].value == 'string'){
-            val = a[i].value.replace(/\\/g, "\\\\").replace(/'/g, "\\'")
+	  val = a[i].value.replace(/\'/g,"\\'")
         } else {
             val = a[i].value+ "";
         }

Modified: pypy/dist/pypy/lang/js/jsparser.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsparser.py	(original)
+++ pypy/dist/pypy/lang/js/jsparser.py	Tue Mar 13 22:17:16 2007
@@ -27,15 +27,15 @@
     tmp = []
     last = ""
     for c in code_string:
-        if c == "'" and last != SLASH:
+        if c == "'":
             tmp.append("\\'")
+        elif c == SLASH:
+            tmp.append(SLASH*2)
+        elif c == "\n":
+            tmp.append("\\n")
         else:
-            if c == SLASH:
-                tmp.append(SLASH*2)
-            elif c == "\n":
-                tmp.append("\\n")
-            else:
-                tmp.append(c)
+            tmp.append(c)
+        last = c
     stripped_code = "".join(tmp)
     if DEBUG:
         print "------ got:"
@@ -71,19 +71,9 @@
     if isinstance(t, Symbol):
         if t.symbol == "QUOTED_STRING":
             stop = len(t.additional_info)-1
-            if stop < 0:
-                stop = 0
+            if stop < 1:
+                raise JsSyntaxError()
             t.additional_info = t.additional_info[1:stop]
-            temp = []
-            last = ""
-            for char in t.additional_info:
-                if last == SLASH:
-                    if char == SLASH:
-                        temp.append(SLASH)
-                if char != SLASH:        
-                    temp.append(char)
-                last = char
-            t.additional_info = ''.join(temp)
     else:
         for i in t.children:
             unquote(i)
@@ -102,7 +92,7 @@
     return tree
 
 regexs, rules, ToAST = parse_ebnf(r"""
-    QUOTED_STRING: "'([^\\\']|\\[\\\'])*'";"""+"""
+    QUOTED_STRING: "'([^\']|\\')*'";"""+"""
     IGNORE: " |\n";
     data: <dict> | <QUOTED_STRING> | <list>;
     dict: ["{"] (dictentry [","])* dictentry ["}"];

Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py	(original)
+++ pypy/dist/pypy/lang/js/operations.py	Tue Mar 13 22:17:16 2007
@@ -8,6 +8,8 @@
 from pypy.rlib.parsing.ebnfparse import Symbol, Nonterminal
 from pypy.rlib.rarithmetic import r_uint, intmask
 
+SLASH = "\\"
+
 class Node(object):
     """
     Node is the base class for all the other nodes, the opcode parameter
@@ -763,11 +765,13 @@
     def eval(self, ctx):
         return W_Number(self.num)
 
+
+
 class String(Expression):
     opcode = 'STRING'
     
     def from_tree(self, t):
-        self.strval = get_string(t, 'value')
+        self.strval = self.string_unquote(get_string(t, 'value'))
 
     def eval(self, ctx):
         return W_String(self.strval)
@@ -775,6 +779,45 @@
     def get_literal(self):
         return W_String(self.strval).ToString()
 
+    escapedict = { 
+        r'\n' : '\n',
+        r'\t' : '\t',
+	r"\'" : "'",
+        r'\"' : '"',
+        r'\\' : '\\'
+    }
+    def string_unquote(self, string):
+        temp = []
+        stop = len(string)-1
+        last = ""
+    
+        #removing the begining quotes (" or \')
+        if string.startswith('"'):
+            singlequote = False
+            if stop < 0:
+                print stop
+                raise JsSyntaxError()
+            internalstring = string[1:stop]
+        else:
+            singlequote = True
+            stop -= 1
+            if stop < 0:
+                print stop
+                raise JsSyntaxError()
+            internalstring = string[2:stop]
+    
+        for c in internalstring:
+            if last == SLASH:
+                escapeseq = self.escapedict[last+c]
+                temp.append(escapeseq)
+                last = escapeseq
+                continue
+            if c != SLASH:        
+                temp.append(c)
+            last = c
+        return ''.join(temp)
+
+
 class ObjectInit(ListOp):
     opcode = 'OBJECT_INIT'
 



More information about the Pypy-commit mailing list