[Zpt] CVS: Packages/TAL - HTMLTALParser.py:1.11 TALGenerator.py:1.9 TALParser.py:1.11

guido@digicool.com guido@digicool.com
Fri, 16 Mar 2001 07:57:03 -0500 (EST)


Update of /cvs-repository/Packages/TAL
In directory korak:/tmp/cvs-serv20441

Modified Files:
	HTMLTALParser.py TALGenerator.py TALParser.py 
Log Message:
Prepare for a separate optimization pass:

- add getCode() interface to TALGenerator

- remove premature optimization from emitRawText()

- rework unEmitNewlineWhitespace() so it works with unoptimized code

- use getCode() in HTMLTALParser and TALParser




--- Updated File HTMLTALParser.py in package Packages/TAL --
--- HTMLTALParser.py	2001/03/15 22:56:48	1.10
+++ HTMLTALParser.py	2001/03/16 12:57:03	1.11
@@ -86,7 +86,7 @@
         assert self.nsdict == {}, self.nsdict
 
     def getCode(self):
-        return self.gen.program, self.gen.macros
+        return self.gen.getCode()
 
     # Overriding SGMLParser methods
 

--- Updated File TALGenerator.py in package Packages/TAL --
--- TALGenerator.py	2001/03/15 12:59:52	1.8
+++ TALGenerator.py	2001/03/16 12:57:03	1.9
@@ -86,6 +86,7 @@
 Code generator for TALInterpreter intermediate code.
 """
 
+import string
 import re
 import cgi
 
@@ -109,6 +110,12 @@
         self.slots = {}
         self.slotStack = []
 
+    def getCode(self):
+        return self.optimize(self.program), self.macros
+
+    def optimize(self, program):
+        return program # XXX later
+
     def todoPush(self, todo):
         self.todoStack.append(todo)
 
@@ -125,7 +132,7 @@
     def popProgram(self):
         program = self.program
         self.program = self.stack.pop()
-        return program
+        return self.optimize(program)
 
     def pushSlots(self):
         self.slotStack.append(self.slots)
@@ -150,10 +157,6 @@
             self.program.append(("endTag", name))
 
     def emitRawText(self, text):
-        if self.program and self.program[-1][0] == "rawtext":
-            # Concatenate text
-            self.program[-1] = ("rawtext", self.program[-1][1] + text)
-            return
         self.program.append(("rawtext", text))
 
     def emitText(self, text):
@@ -223,13 +226,27 @@
         self.emit("fillSlot", slotName, program)
 
     def unEmitNewlineWhitespace(self):
-        if self.program and self.program[-1][0] == "rawtext":
-            text = self.program[-1][1]
-            m = re.match(r"(?s)^(.*)(\n[ \t]*)$", text)
-            if m:
-                text, rest = m.group(1, 2)
-                self.program[-1] = ("rawtext", text)
-                return rest
+        collect = []
+        i = len(self.program)
+        while i > 0:
+            i = i-1
+            item = self.program[i]
+            if item[0] != "rawtext":
+                break
+            text = item[1]
+            if re.match(r"\A[ \t]*\Z", text):
+                collect.append(text)
+                continue
+            m = re.match(r"(?s)^(.*)(\n[ \t]*)\Z", text)
+            if not m:
+                break
+            text, rest = m.group(1, 2)
+            collect.reverse()
+            rest = rest + string.join(collect, "")
+            del self.program[i:]
+            if text:
+                self.program.append(("rawtext", text))
+            return rest
         return None
 
     def replaceAttrs(self, attrlist, repldict):

--- Updated File TALParser.py in package Packages/TAL --
--- TALParser.py	2001/03/15 12:59:52	1.10
+++ TALParser.py	2001/03/16 12:57:03	1.11
@@ -105,7 +105,7 @@
         self.nsNew = []
 
     def getCode(self):
-        return self.gen.program, self.gen.macros
+        return self.gen.getCode()
 
     def StartNamespaceDeclHandler(self, prefix, uri):
         self.nsStack.append(self.nsDict.copy())