Streamline markup_push()/markup_pop() to call factory earlier and avoid copying
authorNick Downing <nick@ndcode.org>
Mon, 21 Jan 2019 00:08:23 +0000 (11:08 +1100)
committerNick Downing <nick@ndcode.org>
Mon, 21 Jan 2019 00:08:23 +0000 (11:08 +1100)
scan.l

diff --git a/scan.l b/scan.l
index 4c9411b..690d987 100644 (file)
--- a/scan.l
+++ b/scan.l
@@ -81,8 +81,8 @@ M4QEND      "]""]"
     yy_push_state(CODEBLOCK)
     indented_code = True
     #add_action(yytext)
-    markup_push() # ast.AST.Section1Or2.CodeBlock
-    markup_push() # ast.AST.Text
+    markup_push(ast.AST.Section1Or2.CodeBlock)
+    markup_push(ast.AST.Text)
   }
   ^"/*" {
     #add_action('/*[' '[')
@@ -99,9 +99,9 @@ M4QEND      "]""]"
     #add_action('[' '[')
     yy_push_state(CODEBLOCK)
     indented_code = False
-    markup_push() # ast.AST.Section1Or2.CodeBlock
+    markup_push(ast.AST.Section1Or2.CodeBlock)
     markup_flush(len(yytext))
-    markup_push() # ast.AST.Text
+    markup_push(ast.AST.Text)
   }
   ^"%top"[[:blank:]]*"{"[[:blank:]]*{NL} {
     global brace_start_line, brace_depth
@@ -224,9 +224,9 @@ M4QEND      "]""]"
     #add_action(']' ']')
     #if not indented_code:
     #  line_directive_out(None, 0)
-    markup_pop(ast.AST.Text)
+    markup_pop() # ast.AST.Text
     markup_flush(len(yytext))
-    markup_pop(ast.AST.Section1Or2.CodeBlock)
+    markup_pop() # ast.AST.Section1Or2.CodeBlock
   }
   [^\n%\[\]]*                  #add_action(yytext)
   .                            #add_action(yytext)
@@ -239,8 +239,8 @@ M4QEND      "]""]"
       #if not indented_code:
       #  line_directive_out(None, 0)
       markup_flush(len(yytext))
-      markup_pop(ast.AST.Text)
-      markup_pop(ast.AST.Section1Or2.CodeBlock)
+      markup_pop() # ast.AST.Text
+      markup_pop() # ast.AST.Section1Or2.CodeBlock
   }
 }
 
@@ -870,8 +870,8 @@ M4QEND      "]""]"
     yy_push_state(CODEBLOCK)
     indented_code = True
     #add_action(yytext)
-    markup_push() # ast.AST.Section1Or2.CodeBlock
-    markup_push() # ast.AST.Text
+    markup_push(ast.AST.Section1Or2.CodeBlock)
+    markup_push(ast.AST.Text)
   }
 
   ^{NOT_WS}.* {
@@ -889,8 +889,8 @@ M4QEND      "]""]"
       yy_push_state(CODEBLOCK)
       indented_code = True
       #add_action(yytext)
-      markup_push() # ast.AST.Section1Or2.CodeBlock
-      markup_push() # ast.AST.Text
+      markup_push(ast.AST.Section1Or2.CodeBlock)
+      markup_push(ast.AST.Text)
   }
 
   .                            #add_action(yytext)
@@ -916,9 +916,9 @@ M4QEND      "]""]"
     bracelevel = 1
     BEGIN(PERCENT_BRACE_ACTION)
     markup_flush(len(yytext) - 2)
-    markup_push() # ast.AST.Section1Or2.CodeBlock
+    markup_push(ast.AST.Section1Or2.CodeBlock)
     markup_flush(2)
-    markup_push() # ast.AST.Text
+    markup_push(ast.AST.Text)
   }
 
   ^{OPTWS}"<" {
@@ -951,11 +951,11 @@ M4QEND      "]""]"
     bracelevel = 1
     BEGIN(PERCENT_BRACE_ACTION)
     markup_flush(len(yytext) - 2)
-    markup_push() # ast.AST.Section2.Rule.Action
+    markup_push(ast.AST.Section2.Rule.Action)
     doing_rule_action = True
     state.in_rule = False
     markup_flush(2)
-    markup_push() # ast.AST.Text
+    markup_push(ast.AST.Text)
   }
   {WS}(?E{ast.AST.Section2.Rule.Action, continued = True}"|".*{NL}) {
     if state._sf_stk[-1] & 4:
@@ -990,8 +990,8 @@ M4QEND      "]""]"
         doing_rule_action = True
         state.in_rule = False
         markup_flush(len(yytext))
-        markup_push() # ast.AST.Section2.Rule.Action
-        markup_push() # ast.AST.Text
+        markup_push(ast.AST.Section2.Rule.Action)
+        markup_push(ast.AST.Text)
   }
 
   {OPTWS}{NL} {
@@ -1007,8 +1007,8 @@ M4QEND      "]""]"
         doing_rule_action = True
         state.in_rule = False
         markup_flush(len(yytext))
-        markup_push() # ast.AST.Section2.Rule.Action
-        markup_push() # ast.AST.Text
+        markup_push(ast.AST.Section2.Rule.Action)
+        markup_push(ast.AST.Text)
   }
 
   ^{OPTWS}"<<EOF>>" |
@@ -1170,7 +1170,7 @@ M4QEND      "]""]"
     yy_push_state(ACTION_GROUP)
     bracelevel = 1
     markup_flush(len(yytext))
-    markup_push() # regex.RegexGroupAction.Text
+    markup_push(regex.RegexGroupAction.Text)
   }
   "E{" {
     global bracelevel
@@ -1178,7 +1178,7 @@ M4QEND      "]""]"
     yy_push_state(ELEMENT_GROUP)
     bracelevel = 1
     markup_flush(len(yytext))
-    markup_push() # RegexGroupElement.Text
+    markup_push(regex.RegexGroupElement.Text)
   }
   ":" {
     BEGIN(SECT2)
@@ -1368,7 +1368,7 @@ M4QEND      "]""]"
   {OPTWS}"%}".* {
     global bracelevel
     bracelevel = 0
-    markup_pop(ast.AST.Text)
+    markup_pop() # ast.AST.Text
   }
 
   <ACTION>"/*" {
@@ -1400,11 +1400,12 @@ M4QEND      "]""]"
       doing_rule_action = False
       BEGIN(SECT2)
       markup_flush(len(yytext))
+      markup_pop() # as below
       if doing_codeblock:
-        markup_pop(ast.AST.Section1Or2.CodeBlock)
+        # ast.AST.Section1Or2.CodeBlock
         doing_codeblock = False
       else:
-        markup_pop(ast.AST.Section2.Rule.Action)
+        # ast.AST.Section2.Rule.Action
         return ord('\n')
   }
 }
@@ -1443,8 +1444,8 @@ M4QEND      "]""]"
         doing_rule_action = False
         #add_action('\tYY_BREAK]' ']\n')
         markup_flush(len(yytext))
-        markup_pop(ast.AST.Text)
-        markup_pop(ast.AST.Section2.Rule.Action)
+        markup_pop() # ast.AST.Text
+        markup_pop() # ast.AST.Section2.Rule.Action
         return ord('\n')
   }
   .                            #add_action(yytext)
@@ -1475,8 +1476,8 @@ M4QEND      "]""]"
       markup_flush(len(yytext))
       if doing_rule_action:
         doing_rule_action = False # Nick added, error in the original?
-        markup_pop(ast.AST.Text)
-        markup_pop(ast.AST.Section2.Rule.Action)
+        markup_pop() # ast.AST.Text
+        markup_pop() # ast.AST.Section2.Rule.Action
         return '\n'
     else:
       BEGIN(ACTION)
@@ -1537,7 +1538,7 @@ M4QEND      "]""]"
     bracelevel -= 1
     if bracelevel == 0:
       yy_pop_state()
-      markup_pop(regex.RegexGroupAction.Text)
+      markup_pop() # regex.RegexGroupAction.Text
       return y_tab.TOK_ACTION_GROUP
   }
 }
@@ -1547,7 +1548,7 @@ M4QEND      "]""]"
     bracelevel -= 1
     if bracelevel == 0:
       yy_pop_state()
-      markup_pop(regex.RegexGroupElement.Text)
+      markup_pop() # regex.RegexGroupElement.Text
       return y_tab.TOK_ELEMENT_GROUP
   }
 }
@@ -1616,20 +1617,14 @@ def markup_yyless(n):
     element.get_text(yy_element_token, 0)[:n]
   )
 
-def markup_push():
+def markup_push(factory, *args, **kwargs):
   global yy_element_space
   markup_stack.append(yy_element_space)
-  yy_element_space = element.Element('root')
+  yy_element_space = factory(*args, **kwargs)
 
-def markup_pop(factory, *args, **kwargs):
+def markup_pop():
   global yy_element_space
   _element = markup_stack.pop()
-  _element.append(
-    factory(
-      text = element.get_text(yy_element_space, 0),
-      children = yy_element_space[:],
-      *args,
-      **kwargs
-    )
-  )
+  _element.append(yy_element_space)
+  #element.set_text(_element, len(_element), '')
   yy_element_space = _element