Introduce PLex.Section2.Rule.FLexRule which is always a pair of (expr, trailing conte...
authorNick Downing <downing.nick@gmail.com>
Sun, 22 Jul 2018 04:29:32 +0000 (14:29 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 22 Jul 2018 04:29:32 +0000 (14:29 +1000)
ast.py

diff --git a/ast.py b/ast.py
index 6a260d2..6a3a755 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -836,11 +836,11 @@ class PLex(element.Element):
           )
 
     class Rule(Item):
-      class BOLRule(element.Element):
+      class Action(element.Element):
         # GENERATE ELEMENT() BEGIN
         def __init__(
           self,
-          tag = 'PLex_Section2_Rule_BOLRule',
+          tag = 'PLex_Section2_Rule_Action',
           attrib = {},
           text = '',
           children = []
@@ -855,13 +855,13 @@ class PLex(element.Element):
         def copy(self, factory = None):
           result = element.Element.copy(
             self,
-            BOLRule if factory is None else factory
+            Action if factory is None else factory
           )
           return result
         def __repr__(self):
           params = []
           self.repr_serialize(params)
-          return 'ast.PLex.Section2.Rule.BOLRule({0:s})'.format(', '.join(params))
+          return 'ast.PLex.Section2.Rule.Action({0:s})'.format(', '.join(params))
         # GENERATE END
 
       class EOFRule(element.Element):
@@ -892,14 +892,16 @@ class PLex(element.Element):
           return 'ast.PLex.Section2.Rule.EOFRule({0:s})'.format(', '.join(params))
         # GENERATE END
 
-      class Action(element.Element):
-        # GENERATE ELEMENT() BEGIN
+      class FLexRule(element.Element):
+        # GENERATE ELEMENT(bool bol, int action) BEGIN
         def __init__(
           self,
-          tag = 'PLex_Section2_Rule_Action',
+          tag = 'PLex_Section2_Rule_FLexRule',
           attrib = {},
           text = '',
-          children = []
+          children = [],
+          bol = False,
+          action = -1
         ):
           element.Element.__init__(
             self,
@@ -908,26 +910,55 @@ class PLex(element.Element):
             text,
             children
           )
+          self.bol = (
+            element.deserialize_bool(bol)
+          if isinstance(bol, str) else
+            bol
+          )
+          self.action = (
+            element.deserialize_int(action)
+          if isinstance(action, str) else
+            action
+          )
+        def serialize(self, ref_list):
+          element.Element.serialize(self, ref_list)
+          self.set('bol', element.serialize_bool(self.bol))
+          self.set('action', element.serialize_int(self.action))
+        def deserialize(self, ref_list):
+          element.Element.deserialize(self, ref_list)
+          self.bol = element.deserialize_bool(self.get('bol', 'false'))
+          self.action = element.deserialize_int(self.get('action', '-1'))
         def copy(self, factory = None):
           result = element.Element.copy(
             self,
-            Action if factory is None else factory
+            FLexRule if factory is None else factory
           )
+          result.bol = self.bol
+          result.action = self.action
           return result
+        def repr_serialize(self, params):
+          element.Element.repr_serialize(self, params)
+          if self.bol != False:
+            params.append(
+              'bol = {0:s}'.format(repr(self.bol))
+            )
+          if self.action != -1:
+            params.append(
+              'action = {0:s}'.format(repr(self.action))
+            )
         def __repr__(self):
           params = []
           self.repr_serialize(params)
-          return 'ast.PLex.Section2.Rule.Action({0:s})'.format(', '.join(params))
+          return 'ast.PLex.Section2.Rule.FLexRule({0:s})'.format(', '.join(params))
         # GENERATE END
-
-      # GENERATE ELEMENT(int action) BEGIN
+      # GENERATE ELEMENT() BEGIN
       def __init__(
         self,
         tag = 'PLex_Section2_Rule',
         attrib = {},
         text = '',
-        children = [],
-        action = -1
+        children = []
       ):
         Item.__init__(
           self,
@@ -936,30 +967,12 @@ class PLex(element.Element):
           text,
           children
         )
-        self.action = (
-          element.deserialize_int(action)
-        if isinstance(action, str) else
-          action
-        )
-      def serialize(self, ref_list):
-        Item.serialize(self, ref_list)
-        self.set('action', element.serialize_int(self.action))
-      def deserialize(self, ref_list):
-        Item.deserialize(self, ref_list)
-        self.action = element.deserialize_int(self.get('action', '-1'))
       def copy(self, factory = None):
         result = Item.copy(
           self,
           Rule if factory is None else factory
         )
-        result.action = self.action
         return result
-      def repr_serialize(self, params):
-        Item.repr_serialize(self, params)
-        if self.action != -1:
-          params.append(
-            'action = {0:s}'.format(repr(self.action))
-          )
       def __repr__(self):
         params = []
         self.repr_serialize(params)
@@ -983,31 +996,36 @@ class PLex(element.Element):
               name_to_start_condition[i.get_text()]
             )
         if isinstance(self[1], PLex.Section2.Rule.EOFRule):
-          assert isinstance(self[2], regex.RegexNone)
           if len(start_conditions) == 0:
-            for i in inclusive_start_conditions: # should be all?
+            for i in all_start_conditions:
               if plex.start_conditions[i].eof_action == 0:
-                plex.start_conditions[i].eof_action = len(plex.eof_actions_text)
+                plex.start_conditions[i].eof_action = (
+                  len(plex.eof_actions_text)
+                )
           else:
             for i in start_conditions:
               assert plex.start_conditions[i].eof_action == 0
-              plex.start_conditions[i].eof_action = len(plex.eof_actions_text)
-          plex.eof_actions_text.append(self[3][0])
-        else:
+              plex.start_conditions[i].eof_action = (
+                len(plex.eof_actions_text)
+              )
+          plex.eof_actions_text.append(self[2][0] if len(self) > 2 else PLex.Text()) # fix this later
+        elif isinstance(self[1], PLex.Section2.Rule.FLexRule):
           if len(start_conditions) == 0:
             start_conditions = inclusive_start_conditions
-          if isinstance(self[1], PLex.Section2.Rule.BOLRule):
-            for i in start_conditions:
-              plex.start_conditions[i].bol_rules.append(self)
-            self[1][0].post_process()
-          else:
-            for i in start_conditions:
-              plex.start_conditions[i].rules.append(self)
-              plex.start_conditions[i].bol_rules.append(self)
-            self[1].post_process()
-          self[2].post_process()
-          self.action = len(plex.actions_text)
-          plex.actions_text.append(self[3][0])
+            for i in (
+              start_conditions
+            if len(start_conditions) else
+              inclusive_start_conditions
+            ):
+              if not self[1].bol:
+                plex.start_conditions[i].rules.append(self[1])
+              plex.start_conditions[i].bol_rules.append(self[1])
+          self[1][0].post_process() # expr
+          self[1][1].post_process() # trailing context
+          self[1].action = len(plex.actions_text)
+          plex.actions_text.append(self[2][0] if len(self) > 2 else PLex.Text()) # fix this later
+        else:
+          assert False
  
     class StartConditions(element.Element):
       # GENERATE ELEMENT(bool wildcard) BEGIN
@@ -1261,14 +1279,10 @@ class PLex(element.Element):
               expr,
               regex.RegexSequence(
                 children = [
-                  (
-                    k[1][0]
-                  if isinstance(k[1], PLex.Section2.Rule.BOLRule) else
-                    k[1]
-                  ),
+                  k[0],
                   regex.RegexGroup(
                     children = [
-                      k[2]
+                      k[1]
                     ],
                     group_index = k.action
                   )
@@ -1331,9 +1345,9 @@ tag_to_class = {
   'PLex_Section2': PLex.Section2,
   'PLex_Section2_CompoundRule': PLex.Section2.CompoundRule,
   'PLex_Section2_Rule': PLex.Section2.Rule,
-  'PLex_Section2_Rule_BOLRule': PLex.Section2.Rule.BOLRule,
-  'PLex_Section2_Rule_EOFRule': PLex.Section2.Rule.EOFRule,
   'PLex_Section2_Rule_Action': PLex.Section2.Rule.Action,
+  'PLex_Section2_Rule_EOFRule': PLex.Section2.Rule.EOFRule,
+  'PLex_Section2_Rule_FLexRule': PLex.Section2.Rule.FLexRule,
   'PLex_Section2_StartConditions': PLex.Section2.StartConditions,
   'PLex_Section3': PLex.Section3
 }