Allow value and stack references in action text of (?{...}...) element groups, add...
authorNick Downing <nick@ndcode.org>
Sun, 20 Jan 2019 09:58:05 +0000 (20:58 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 20 Jan 2019 09:59:54 +0000 (20:59 +1100)
ast.py
bootstrap/n.sh
bootstrap/parse-gram.y
generate_py.py

diff --git a/ast.py b/ast.py
index 044b7f5..e2e5e6e 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -467,7 +467,6 @@ class AST(element.Element):
               (False, [], True, 0, last_action)
             )
           )
-        assert isinstance(self[0], AST.Text) # temporary
         for i in self[0]:
           if isinstance(i, AST.Text.StackLocation):
             i.offset = -len(symbols)
@@ -884,6 +883,17 @@ class AST(element.Element):
         symbols,
         tag_names
       ):
+        for i in self[0]:
+          if isinstance(i, AST.Text.StackLocation):
+            i.offset = -production.n_symbols
+          elif isinstance(i, AST.Text.StackReference):
+            if (
+              len(i.tag_name) == 0 and
+              i.index >= 1 and
+              i.index <= len(tag_names)
+            ):
+              i.tag_name = tag_names[i.index - 1]
+            i.offset = -production.n_symbols
         for i in self[1:]:
           last_action = i.add_to_symbols(
             _ast,
index 36871bd..9aac315 100755 (executable)
@@ -3,12 +3,15 @@ if ! test -d out
 then
   mkdir out
   ../../bootstrap_bison.git/src/bison -o /dev/null ../tests/cal.y 2>out/cal.y.xml.ok
+  ../../bootstrap_bison.git/src/bison -o /dev/null ../tests_ast/cal_py.y 2>out/cal_py.y.xml.ok
   ../../bootstrap_bison.git/src/bison -o /dev/null ../../bootstrap_flex.git/src/parse.y 2>out/parse.y.xml.ok
   ../../bootstrap_bison.git/src/bison ../../bootstrap_bison.git/src/parse-gram.y 2>out/parse-gram.y.xml.ok
   rm parse-gram.output parse-gram.tab.c parse-gram.tab.h
 fi
 ./markup.py <../tests/cal.y >out/cal.y.xml
 diff --unified out/cal.y.xml.ok out/cal.y.xml
+./markup.py <../tests_ast/cal_py.y >out/cal_py.y.xml
+diff --unified out/cal_py.y.xml.ok out/cal_py.y.xml
 ./markup.py <../../bootstrap_flex.git/src/parse.y >out/parse.y.xml
 diff --unified out/parse.y.xml.ok out/parse.y.xml
 ./markup.py <../../bootstrap_bison.git/src/parse-gram.y >out/parse-gram.y.xml
index 8782db7..514eb4f 100644 (file)
@@ -39,7 +39,7 @@
   #current_type = None
   gram_piece2 = 0
   gram_piece3 = 0
-  nested_rhs = 0
+  #nested_rhs = 0
 }
 
 /* Nick %define api.prefix {gram_} */
@@ -902,7 +902,6 @@ rhs
     }
   | rhs "{...}" named_ref.opt
     {
-      lex_yy_code.trap = True
       lex_yy_code.code_piece_append('<AST_Text>')
       #grammar_current_rule_action_append($2, @2, $3, False)
       lex_yy_code.yyin = None
@@ -948,17 +947,27 @@ rhs
       insert_before(1, '<AST_Production_Merge>')
     }
   /* Nick extra rules for element groups */
-| rhs '('
-    {
+  | rhs '('
+    /*{
       #nested_rhs += 1
-    }
+    }*/
     rhs ')'
     {
+      lex_yy_code.code_piece_append('(?E{<AST_Text>')
+      lex_yy_code.yyin = None
+      lex_yy_code.yy_buffer_stack = [lex_yy_code.YYBufferState()]
+      lex_yy_code.yytext = '' # fool unput()
+      lex_yy_code.unput($2[4:-1])
+      lex_yy_code.sc_context = lex_yy_code.SC_RULE_ACTION # CODE_PROPS_RULE_ACTION
+      lex_yy_code.yylex()
+      lex_yy_code.code_piece_append('</AST_Text>}')
+      lex_yy_code.code_piece_pack()
+      lex_yy.gram_piece[gram_piece2 + 2] = lex_yy_code.code_piece[lex_yy_code.code_piece0 - 1]
       insert_after(3, '</AST_Production_GroupElement>')
       insert_before(1, '<AST_Production_GroupElement>')
     }
   /* Nick added %space */
-| rhs "%space"
+  | rhs "%space"
     {
       insert_after(1, '</AST_Production_Space>')
       insert_before(1, '<AST_Production_Space>')
index b900f30..e4f37e8 100644 (file)
@@ -133,15 +133,15 @@ YYERROR_VERBOSE = {1:s}
 {2:s}{3:s}{4:s}{5:s}'''.format(
                     i,
                     (
-                      'yytoken, yylval, yylloc, '
+                      'yychar, yylval, yylloc, '
                     if actions[i][0] else # take_space_right
                       ''
                     ),
                     (
-                      '''  if yytoken == -1:
+                      '''  if yychar == -1:
     yylval = None
     yylloc = None
-    yytoken = yylex()
+    yychar = lex_yy.yylex()
   yy_element_stack.append(lex_yy.yy_element_space)
   lex_yy.yy_element_space = element.Element('root')
 '''