Move Rule.post_process() code into EOFRule, FLexRule to avoid isinstance() test
authorNick Downing <downing.nick@gmail.com>
Sat, 22 Sep 2018 01:29:41 +0000 (11:29 +1000)
committerNick Downing <downing.nick@gmail.com>
Sat, 22 Sep 2018 01:29:41 +0000 (11:29 +1000)
ast.py

diff --git a/ast.py b/ast.py
index 3e6f5b4..5e02610 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -3961,7 +3961,35 @@ class AST(element.Element):
           self.repr_serialize(params)
           return 'ast.AST.Section2.Rule.EOFRule({0:s})'.format(', '.join(params))
         # GENERATE END
-
+        def post_process(
+          self,
+          _ast,
+          section,
+          name_to_start_condition,
+          all_start_conditions,
+          inclusive_start_conditions,
+          parent_start_conditions,
+          continued_action,
+          start_conditions,
+          action
+        ):
+          assert not continued_action
+          if len(start_conditions) == 0:
+            for i in all_start_conditions:
+              if _ast.start_conditions[i].eof_action == 0:
+                _ast.start_conditions[i].eof_action = (
+                  len(_ast.eof_actions_text)
+                )
+          else:
+            for i in start_conditions:
+              assert _ast.start_conditions[i].eof_action == 0
+              _ast.start_conditions[i].eof_action = (
+                len(_ast.eof_actions_text)
+              )
+          assert not action.continued
+          _ast.eof_actions_text.append(action[0])
+          return False # continued_action
       class FLexRule(element.Element):
         # GENERATE ELEMENT(bool bol, int action) BEGIN
         def __init__(
@@ -4021,6 +4049,57 @@ class AST(element.Element):
           self.repr_serialize(params)
           return 'ast.AST.Section2.Rule.FLexRule({0:s})'.format(', '.join(params))
         # GENERATE END
+        def post_process(
+          self,
+          _ast,
+          section,
+          name_to_start_condition,
+          all_start_conditions,
+          inclusive_start_conditions,
+          parent_start_conditions,
+          continued_action,
+          start_conditions,
+          action
+        ):
+          for i in (
+            start_conditions
+          if len(start_conditions) else
+            inclusive_start_conditions
+          ):
+            if not self.bol:
+              _ast.start_conditions[i].rules.append(self)
+            _ast.start_conditions[i].bol_rules.append(self)
+          self[0].post_process(
+            caseless = _ast[0].caseless
+          ) # regex
+          self[1].post_process(
+            caseless = _ast[0].caseless
+          ) # trailing context regex
+          self.action = len(_ast.actions_text)
+          if action.continued:
+            continued_action = True
+          else:
+            _ast.actions_text.append(action[0])
+            continued_action = False
+            #def to_text(node):
+            #  return ''.join(
+            #    [
+            #      j
+            #      for i in range(len(node))
+            #      for j in [element.get_text(node, i), to_text(node[i])]
+            #    ] +
+            #    [element.get_text(node, len(node))]
+            #  )
+            #text = '{0:s}/{1:s}'.format(to_text(self[0]), to_text(self[1]))
+            #element.set_text(
+            #  _ast.actions_text[-1],
+            #  0,
+            #  'fprintf(stderr, "%d >>>%s<<< {0:s}\\n", yy_start, yytext);\n{1:s}'.format(
+            #    text.replace('\\', '\\\\').replace('"', '\\"').replace('%', '%%'),
+            #    element.get_text(_ast.actions_text[-1], 0)
+            #  )
+            #)
+          return continued_action
  
       # GENERATE ELEMENT() BEGIN
       def __init__(
@@ -4066,65 +4145,18 @@ class AST(element.Element):
             start_conditions.add(
               name_to_start_condition[i.get_text()]
             )
-        if isinstance(self[1], AST.Section2.Rule.EOFRule):
-          assert not continued_action
-          if len(start_conditions) == 0:
-            for i in all_start_conditions:
-              if _ast.start_conditions[i].eof_action == 0:
-                _ast.start_conditions[i].eof_action = (
-                  len(_ast.eof_actions_text)
-                )
-          else:
-            for i in start_conditions:
-              assert _ast.start_conditions[i].eof_action == 0
-              _ast.start_conditions[i].eof_action = (
-                len(_ast.eof_actions_text)
-              )
-          assert not self[2].continued
-          _ast.eof_actions_text.append(self[2][0])
-        elif isinstance(self[1], AST.Section2.Rule.FLexRule):
-          for i in (
-            start_conditions
-          if len(start_conditions) else
-            inclusive_start_conditions
-          ):
-            if not self[1].bol:
-              _ast.start_conditions[i].rules.append(self[1])
-            _ast.start_conditions[i].bol_rules.append(self[1])
-          self[1][0].post_process(
-            caseless = _ast[0].caseless
-          ) # regex
-          self[1][1].post_process(
-            caseless = _ast[0].caseless
-          ) # trailing context regex
-          self[1].action = len(_ast.actions_text)
-          if self[2].continued:
-            continued_action = True
-          else:
-            _ast.actions_text.append(self[2][0])
-            continued_action = False
-            #def to_text(node):
-            #  return ''.join(
-            #    [
-            #      j
-            #      for i in range(len(node))
-            #      for j in [element.get_text(node, i), to_text(node[i])]
-            #    ] +
-            #    [element.get_text(node, len(node))]
-            #  )
-            #text = '{0:s}/{1:s}'.format(to_text(self[1][0]), to_text(self[1][1]))
-            #element.set_text(
-            #  _ast.actions_text[-1],
-            #  0,
-            #  'fprintf(stderr, "%d >>>%s<<< {0:s}\\n", yy_start, yytext);\n{1:s}'.format(
-            #    text.replace('\\', '\\\\').replace('"', '\\"').replace('%', '%%'),
-            #    element.get_text(_ast.actions_text[-1], 0)
-            #  )
-            #)
-        else:
-          assert False
-        return continued_action
+        return self[1].post_process(
+          _ast,
+          section,
+          name_to_start_condition,
+          all_start_conditions,
+          inclusive_start_conditions,
+          parent_start_conditions,
+          continued_action,
+          start_conditions,
+          self[2] # action
+        )
+
     class StartConditions(element.Element):
       # GENERATE ELEMENT(bool wildcard) BEGIN
       def __init__(