Implement element groups with support from pilex (not via user code) origin/master
authorNick Downing <downing.nick@gmail.com>
Wed, 26 Sep 2018 07:27:28 +0000 (17:27 +1000)
committerNick Downing <downing.nick@gmail.com>
Wed, 26 Sep 2018 07:28:37 +0000 (17:28 +1000)
generate_py.py
regex.py
tests_ast/cal_py.l

index 0085154..231c36c 100644 (file)
@@ -47,7 +47,11 @@ def generate_py(_ast, _element, home_dir, skel_file, out_file):
       [
         (
           'yy_group{0:d}'.format(len(group_actions_text) + j),
-          'yy_group_end'
+          (
+            'yy_group_end_element'
+          if isinstance(i.groups0[j], regex.RegexGroupElement) else
+            'yy_group_end'
+          )
         )
         for j in range(len(i.groups0))
       ] +
@@ -60,7 +64,11 @@ def generate_py(_ast, _element, home_dir, skel_file, out_file):
       [
         (
           'yy_group{0:d}'.format(len(group_actions_text) + j),
-          'yy_group_end'
+          (
+            'yy_group_end_element'
+          if isinstance(i.groups1[j], regex.RegexGroupElement) else
+            'yy_group_end'
+          )
         )
         for j in range(len(i.groups1))
       ]
@@ -83,6 +91,22 @@ yy_action = yy_action{1:d}
         (
           groups[j][0]
         if isinstance(groups[j], regex.RegexGroupAction) else
+          regex.Text(
+            text = '''_element = yy_group_element(
+  yy_group_stack[-1],
+  yy_group_stack[-2],
+  yy_element_stack.pop(),
+  {0:s}
+)
+yy_element_stack[-1].append(
+  (yy_group_stack[-1], yy_group_stack[-2], _element)
+)
+del yy_group_stack[-2:]
+'''.format(
+              groups[j][0].get_text()
+            )
+          )
+        if isinstance(groups[j], regex.RegexGroupElement) else
           regex.Text(
             text = '''if yy_groups[{0:d}] is None:
   yy_groups[{1:d}] = yy_group_text[yy_group_stack[-1]:yy_group_stack[-2]]
index 1989a7b..9594231 100644 (file)
--- a/regex.py
+++ b/regex.py
@@ -884,6 +884,64 @@ class RegexGroupAction(RegexGroup):
     self.repr_serialize(params)
     return 'regex.RegexGroupAction({0:s})'.format(', '.join(params))
   # GENERATE END
+
+class RegexGroupElement(RegexGroup):
+  class Text(Text):
+    # GENERATE ELEMENT() BEGIN
+    def __init__(
+      self,
+      tag = 'RegexGroupElement_Text',
+      attrib = {},
+      text = '',
+      children = []
+    ):
+      Text.__init__(
+        self,
+        tag,
+        attrib,
+        text,
+        children
+      )
+    def copy(self, factory = None):
+      result = Text.copy(
+        self,
+        Text if factory is None else factory
+      )
+      return result
+    def __repr__(self):
+      params = []
+      self.repr_serialize(params)
+      return 'regex.RegexGroupElement.Text({0:s})'.format(', '.join(params))
+    # GENERATE END
+
+  # GENERATE ELEMENT() BEGIN
+  def __init__(
+    self,
+    tag = 'RegexGroupElement',
+    attrib = {},
+    text = '',
+    children = [],
+    n_groups = -1
+  ):
+    RegexGroup.__init__(
+      self,
+      tag,
+      attrib,
+      text,
+      children,
+      n_groups
+    )
+  def copy(self, factory = None):
+    result = RegexGroup.copy(
+      self,
+      RegexGroupElement if factory is None else factory
+    )
+    return result
+  def __repr__(self):
+    params = []
+    self.repr_serialize(params)
+    return 'regex.RegexGroupElement({0:s})'.format(', '.join(params))
+  # GENERATE END
  
 # GENERATE FACTORY(element.Element) BEGIN
 tag_to_class = {
@@ -905,7 +963,9 @@ tag_to_class = {
   'RegexGroupName': RegexGroupName,
   'RegexGroupName_Text': RegexGroupName.Text,
   'RegexGroupAction': RegexGroupAction,
-  'RegexGroupAction_Text': RegexGroupAction.Text
+  'RegexGroupAction_Text': RegexGroupAction.Text,
+  'RegexGroupElement': RegexGroupElement,
+  'RegexGroupElement_Text': RegexGroupElement.Text
 }
 def factory(tag, attrib = {}, *args, **kwargs):
   return tag_to_class.get(tag, element.Element)(tag, attrib, *args, **kwargs)
index 10c22b9..f0dd02a 100644 (file)
@@ -5,68 +5,14 @@ NUM = 0x100
 yylval = None
 %}
 
+DIGIT (?E{ast.AST.Num.Mantissa}[0-9]+)\.?|(?E{ast.AST.Num.Mantissa}[0-9]*)\.(?E{ast.AST.Num.Fraction}[0-9]+)
+
 %option noecs nometa-ecs noyywrap reject yymore
 
 %%
 
 [ ]
-(?{
-  _element = yy_group_element(
-    yy_group_stack[-1],
-    yy_group_stack[-2],
-    yy_element_stack.pop(),
-    ast.AST.Num
-  )
-  yy_element_stack[-1].append(
-    (yy_group_stack[-1], yy_group_stack[-2], _element)
-  )
-  del yy_group_stack[-2:]
-}(?{
-  _element = yy_group_element(
-    yy_group_stack[-1],
-    yy_group_stack[-2],
-    yy_element_stack.pop(),
-    ast.AST.Num.Mantissa
-  )
-  yy_element_stack[-1].append(
-    (yy_group_stack[-1], yy_group_stack[-2], _element)
-  )
-  del yy_group_stack[-2:]
-}[0-9]+)(?{
-  yy_element_stack.append([])
-  del yy_group_stack[-2:]
-}"")\.?|(?{
-  _element = yy_group_element(
-    yy_group_stack[-1],
-    yy_group_stack[-2],
-    yy_element_stack.pop(),
-    ast.AST.Num.Mantissa
-  )
-  yy_element_stack[-1].append(
-    (yy_group_stack[-1], yy_group_stack[-2], _element)
-  )
-  del yy_group_stack[-2:]
-}[0-9]*)(?{
-  yy_element_stack.append([])
-  del yy_group_stack[-2:]
-}"")\.(?{
-  _element = yy_group_element(
-    yy_group_stack[-1],
-    yy_group_stack[-2],
-    yy_element_stack.pop(),
-    ast.AST.Num.Fraction
-  )
-  yy_element_stack[-1].append(
-    (yy_group_stack[-1], yy_group_stack[-2], _element)
-  )
-  del yy_group_stack[-2:]
-}[0-9]+)(?{
-  yy_element_stack.append([])
-  del yy_group_stack[-2:]
-}""))(?{
-  yy_element_stack.append([])
-  del yy_group_stack[-2:]
-}"")   {
+(?E{ast.AST.Num}{DIGIT})       {
   global yylval
   yylval = float(yytext)
   return NUM