Add YY_USER_ACTION() hook for location tracking in Python generator/skeletons
authorNick Downing <nick@ndcode.org>
Wed, 30 Jan 2019 12:14:59 +0000 (23:14 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 30 Jan 2019 12:31:09 +0000 (23:31 +1100)
skel/skel_py.py
skel/skel_py_element.py

index 2a68499..6b228b6 100644 (file)
 import bisect
 import sys
 
+# this can be redefined in SECTION1
+def YY_AT_BOL():
+  return yy_buffer_stack[-1].at_bol
+
+def yy_set_bol(at_bol):
+  yy_buffer_stack[-1].at_bol = at_bol
+
+def YY_USER_ACTION():
+  pass
+
+def YY_RULE_START():
+  # note that this should also be done after yyless() and REJECT(),
+  # and state should be saved in case they result in a null string,
+  # however, it doesn't seem to be in flex, maintain compatibility:
+  if len(yytext):
+    yy_set_bol(yytext[-1] == '\n')
+  YY_USER_ACTION()
+
 # GENERATE SECTION1
 
 # GENERATE STARTCONDDECL
@@ -113,11 +131,7 @@ def yy_rule_start():
   yytext = yy_group_text[:yy_group_stack[-1]]
   yytext_len = yy_group_stack[-1]
   del yy_group_stack[-2:]
-  # note that this should also be done after yyless() and REJECT(),
-  # and state should be saved in case they result in a null string,
-  # however, it doesn't seem to be in flex, maintain compatibility:
-  if len(yytext):
-    yy_buffer_stack[-1].at_bol = yytext[-1] == '\n'
+  YY_RULE_START()
 
 def yy_group_end():
   pass
@@ -138,12 +152,6 @@ def yy_pop_state():
   global yystart
   yystart = yystart_stack.pop()
 
-def YY_AT_BOL():
-  return yy_buffer_stack[-1].at_bol
-
-def yy_set_bol(at_bol):
-  yy_buffer_stack[-1].at_bol = at_bol
-
 # GENERATE SECTION2
 
 def yylex():
index 688fc95..acfde43 100644 (file)
@@ -28,6 +28,24 @@ import bisect
 import element
 import sys
 
+# this can be redefined in SECTION1
+def YY_AT_BOL():
+  return yy_buffer_stack[-1].at_bol
+
+def yy_set_bol(at_bol):
+  yy_buffer_stack[-1].at_bol = at_bol
+
+def YY_USER_ACTION():
+  pass
+
+def YY_RULE_START():
+  # note that this should also be done after yyless() and REJECT(),
+  # and state should be saved in case they result in a null string,
+  # however, it doesn't seem to be in flex, maintain compatibility:
+  if len(yytext):
+    yy_set_bol(yytext[-1] == '\n')
+  YY_USER_ACTION()
+
 # GENERATE SECTION1
 
 # GENERATE STARTCONDDECL
@@ -118,11 +136,7 @@ def yy_rule_start():
   yytext = yy_group_text[:yy_group_stack[-1]]
   yytext_len = yy_group_stack[-1]
   del yy_group_stack[-2:]
-  # note that this should also be done after yyless() and REJECT(),
-  # and state should be saved in case they result in a null string,
-  # however, it doesn't seem to be in flex, maintain compatibility:
-  if len(yytext):
-    yy_buffer_stack[-1].at_bol = yytext[-1] == '\n'
+  YY_RULE_START()
   yy_element_stack.append([])
 
 def yy_group_end():
@@ -157,12 +171,6 @@ def yy_pop_state():
   global yystart
   yystart = yystart_stack.pop()
 
-def YY_AT_BOL():
-  return yy_buffer_stack[-1].at_bol
-
-def yy_set_bol(at_bol):
-  yy_buffer_stack[-1].at_bol = at_bol
-
 # GENERATE SECTION2
 
 def yylex(_class = element.Element, *args, **kwargs):