Rearrange so that group actions don't need any arguments, implement group names
authorNick Downing <downing.nick@gmail.com>
Tue, 25 Sep 2018 23:51:19 +0000 (09:51 +1000)
committerNick Downing <downing.nick@gmail.com>
Tue, 25 Sep 2018 23:51:19 +0000 (09:51 +1000)
generate_py.py
skel/skel_py.py
tests/cal_py.l

index bc36709..1b83417 100644 (file)
@@ -163,7 +163,7 @@ yy_action = yy_action{0:d}
               ),
               ''.join(
                 [
-                  '''def yy_rule{0:d}(match, pos):
+                  '''def yy_rule{0:d}():
 {1:s}'''.format(
                     i,
                     regex_text_to_python(group_rules_text[i], '  ')
@@ -173,7 +173,7 @@ yy_action = yy_action{0:d}
               ),
               ''.join(
                 [
-                  '''def yy_group{0:d}(match, pos):
+                  '''def yy_group{0:d}():
 {1:s}'''.format(
                     i,
                     regex_text_to_python(group_actions_text[i], '  ')
index c1cfd46..9e3a911 100644 (file)
@@ -38,12 +38,12 @@ yy_threads0 = [None]
 yy_threads1 = [None]
 yy_prefix_slop = 1
 
+yy_group_text = None
+yy_group_stack = None
+yy_action = None
 yytext = ''
 yytext_len = 0
 
-yy_action = None
-yy_group_stack = None
-
 def REJECT():
   raise YYReject()
 
@@ -73,13 +73,13 @@ def unput(text):
     yytext_len -= i
   yy_buffer_stack[-1].next = YYBufferBlock(yy_buffer_stack[-1].next, 0, text)
 
-def yy_rule_start(match, pos):
+def yy_rule_start():
   global yytext, yytext_len
-  yytext = match[:pos]
-  yytext_len = pos
+  yytext_len = yy_group_stack.pop()
+  yytext = yy_group_text[:yytext_len]
 
-def yy_group_end(match, pos):
-  yy_group_stack.append(pos)
+def yy_group_end():
+  pass
 
 # GENERATE SECTION2
 
@@ -89,10 +89,11 @@ def yylex():
     yy_threads0, \
     yy_threads1, \
     yy_prefix_slop, \
-    yytext, \
-    yytext_len, \
+    yy_group_text, \
+    yy_group_stack, \
     yy_action, \
-    yy_group_stack
+    yytext, \
+    yytext_len
 
   # GENERATE SECTION2INITIAL
 
@@ -213,21 +214,22 @@ def yylex():
       match += block.text[block_pos - i:]
 
     for i in yy_dfa_states[state][2]:
+      yy_group_text = match
+      yy_group_stack = []
+      yy_action = None
       yytext = None
       yytext_len = None
-      yy_action = None
-      yy_group_stack = []
 
       thread = yy_threads0[yy_prefix_slop + i]
       #print('thread', thread)
       while thread is not None:
         pos, ref_data, thread = thread
-        ref_data(match, pos)
+        yy_group_stack.append(pos)
+        ref_data()
 
       #print('yytext', yytext)
       #print('yytext_len', yytext_len)
       #print('yy_action', yy_action)
-      #print('yy_group_stack', yy_group_stack)
 
       try:
         return yy_action()
index 624b71a..1429385 100644 (file)
@@ -2,32 +2,49 @@
 NUM = 0x100 
 yylval = None
 groups = None
+groups_by_name = {}
 def gc(n):
-  global groups
+  global groups, groups_by_name
   groups = [None for i in range(n)]
-  yy_group_stack.pop()
-def g(n, match, pos):
-  groups[n] = match[pos:yy_group_stack.pop()]
+  groups_by_name = {}
+  del yy_group_stack[-2:]
+def g(n, name = None):
+  if groups[n] is None:
+    groups[n] = yy_group_text[yy_group_stack[-1]:yy_group_stack[-2]]
+    if name is not None and name not in groups_by_name:
+      groups_by_name[name] = groups[n]
+  del yy_group_stack[-2:]
 %}
 
-DIGIT (?{g(1, match, pos)}[0-9]+)\.?|(?{g(2, match, pos)}[0-9]*)\.(?{g(3, match, pos)}[0-9]+)
+DIGIT (?{g(1, 'mantissa')}[0-9]+)\.?|(?{g(2, 'mantissa')}[0-9]*)\.(?{g(3, 'fraction')}[0-9]+)
 
 %option noecs nometa-ecs noyywrap reject yymore
 
 %%
 
 [ ]
-(?{g(0, match, pos)}{DIGIT})(?{gc(4)}"")       {
+(?{g(0)}{DIGIT})(?{gc(4)}"")   {
   global yylval
-  #print('groups', groups)
+  print('groups', groups)
+  print('groups_by_name', groups_by_name)
+
+  # by Python parser:
   #yylval = float(yytext)
-  if groups[1] is not None:
-    mantissa = groups[1]
-    exponent = 0
-  else:
-    mantissa = groups[2] + groups[3]
-    exponent = -len(groups[3])
-  yylval = int(mantissa) * 10 ** exponent
+
+  # by group numbers:
+  #if groups[1] is not None:
+  #  mantissa = groups[1]
+  #  fraction = ''
+  #else:
+  #  mantissa = groups[2]
+  #  fraction = groups[3]
+  #yylval = int(mantissa + fraction) * 10 ** -len(fraction)
+
+  # by group names:
+  mantissa = groups_by_name['mantissa']
+  fraction = groups_by_name.get('fraction', '')
+  yylval = int(mantissa + fraction) * 10 ** -len(fraction)
+
   return NUM
 }
 \n|.   {