Fix some missing spots to use markup stack, move code lines, add a syntax error
authorNick Downing <nick@ndcode.org>
Sun, 20 Jan 2019 00:29:12 +0000 (11:29 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 20 Jan 2019 00:33:53 +0000 (11:33 +1100)
bootstrap/scan.l

index 56278b7..b1cf64e 100644 (file)
@@ -231,13 +231,13 @@ M4QEND      "]""]"
 <CODEBLOCK>{
   ^"%}".*{NL} {
     state.linenum += 1
-    piece_insert(markup_stack.pop(), '<AST_Text>')
-    piece_append('</AST_Text>')
-    piece_flush(len(yytext))
     yy_pop_state()
     #add_action(']' ']')
     #if not indented_code:
     #  line_directive_out(None, 0)
+    piece_insert(markup_stack.pop(), '<AST_Text>')
+    piece_append('</AST_Text>')
+    piece_flush(len(yytext))
     piece_insert(markup_stack.pop(), '<AST_Section1Or2_CodeBlock>')
     piece_append('</AST_Section1Or2_CodeBlock>')
   }
@@ -247,13 +247,13 @@ M4QEND      "]""]"
     state.linenum += 1
     #add_action(yytext)
     if indented_code:
-      piece_flush(len(yytext))
-      piece_insert(markup_stack.pop(), '<AST_Text>')
-      piece_append('</AST_Text>')
       yy_pop_state()
       #add_action(']' ']')
       #if not indented_code:
       #  line_directive_out(None, 0)
+      piece_flush(len(yytext))
+      piece_insert(markup_stack.pop(), '<AST_Text>')
+      piece_append('</AST_Text>')
       piece_insert(markup_stack.pop(), '<AST_Section1Or2_CodeBlock>')
       piece_append('</AST_Section1Or2_CodeBlock>')
   }
@@ -1364,11 +1364,12 @@ M4QEND      "]""]"
 
   {WS}"%{" {
     global bracelevel, doing_rule_action
+    if not state.in_rule:
+      state.synerr('action outside rule')
     bracelevel = 1
     BEGIN(PERCENT_BRACE_ACTION)
     piece_flush(len(yytext) - 2)
     markup_stack.append(len(piece)) # <AST_Section2_Rule_Action>
-    assert state.in_rule # should do error message here
     doing_rule_action = True
     state.in_rule = False
     piece_flush(2)
@@ -1376,8 +1377,7 @@ M4QEND      "]""]"
   }
   {WS}"|".*{NL} {
     if state._sf_stk[-1] & 4:
-      amt = int(strchr(yytext, ord('|')) - yytext)
-      yyless(amt)
+      yyless(yytext.index('|'))
     else:
       #add_action(']' ']')
       continued_action = True
@@ -1415,7 +1415,8 @@ M4QEND      "]""]"
         doing_rule_action = True
         state.in_rule = False
         piece_flush(len(yytext))
-        piece_append('<AST_Section2_Rule_Action><AST_Text>')
+        markup_stack.append(len(piece)) # <AST_Section2_Rule_Action>
+        markup_stack.append(len(piece)) # <AST_Text>
   }
 
   {OPTWS}{NL} {
@@ -1431,7 +1432,8 @@ M4QEND      "]""]"
         doing_rule_action = True
         state.in_rule = False
         piece_flush(len(yytext))
-        piece_append('<AST_Section2_Rule_Action><AST_Text>')
+        markup_stack.append(len(piece)) # <AST_Section2_Rule_Action>
+        markup_stack.append(len(piece)) # <AST_Text>
   }
 
   ^{OPTWS}"<<EOF>>" |
@@ -1833,9 +1835,9 @@ M4QEND      "]""]"
     if bracelevel <= 0 or doing_codeblock and indented_code:
       #if doing_rule_action:
       #  add_action('\tYY_BREAK]' ']\n')
-      piece_flush(len(yytext))
       doing_rule_action = False
       BEGIN(SECT2)
+      piece_flush(len(yytext))
       if doing_codeblock:
         piece_insert(markup_stack.pop(), '<AST_Section1Or2_CodeBlock>')
         piece_append('</AST_Section1Or2_CodeBlock>')
@@ -1878,10 +1880,13 @@ M4QEND      "]""]"
     if bracelevel <= 0:
       BEGIN(SECT2)
       if doing_rule_action:
+        doing_rule_action = False
         #add_action('\tYY_BREAK]' ']\n')
         piece_flush(len(yytext))
-        piece_append('</AST_Text></AST_Section2_Rule_Action>')
-        doing_rule_action = False
+        piece_insert(markup_stack.pop(), '<AST_Text>')
+        piece_append('</AST_Text>')
+        piece_insert(markup_stack.pop(), '<AST_Section2_Rule_Action>')
+        piece_append('</AST_Section2_Rule_Action>')
         return ord('\n')
   }
   .                            #add_action(yytext)
@@ -1911,8 +1916,11 @@ M4QEND      "]""]"
       BEGIN(SECT2)
       piece_flush(len(yytext))
       if doing_rule_action:
-        piece_append('</AST_Text></AST_Section2_Rule_Action>')
         doing_rule_action = False # Nick added, error in the original?
+        piece_insert(markup_stack.pop(), '<AST_Text>')
+        piece_append('</AST_Text>')
+        piece_insert(markup_stack.pop(), '<AST_Section2_Rule_Action>')
+        piece_append('</AST_Section2_Rule_Action>')
         return '\n'
     else:
       BEGIN(ACTION)