# special exception.
import bisect
-#import element
import sys
+from ndcode.piyacc import element
+
+# 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
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():
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):
# License without this special exception.
import bisect
-#import element
-#import lex_yy
#import xml.etree.ElementTree
+from ndcode.piyacc import element
+from ndcode.piyacc import lex_yy
# this can be redefined in SECTION1
class YYLTYPE:
def __init__(
self,
- first_line = 0,
- first_column = 0,
- last_line = 0,
- last_column = 0
+ first_line = 1,
+ first_column = 1,
+ last_line = 1,
+ last_column = 1
):
self.first_line = first_line
self.first_column = first_column
self.last_line = last_line
self.last_column = last_column
-
+ def copy(self):
+ return YYLTYPE(
+ self.first_line,
+ self.first_column,
+ self.last_line,
+ self.last_column
+ )
+
+def YYLLOC_DEFAULT(current, stack, n):
+ if n:
+ current.first_line = stack[-n][2].first_line
+ current.first_column = stack[-n][2].first_column
+ current.last_line = stack[-1][2].last_line
+ current.last_column = stack[-1][2].last_column
+ else:
+ current.first_line = current.last_line = stack[-1][2].last_line
+ current.first_column = current.last_column = stack[-1][2].last_column
+
# GENERATE SECTION1
# GENERATE TOKENS
YYEMPTY = -1
yyval = None
-yyloc = None
+yyloc = YYLTYPE()
yylval = None
yylloc = YYLTYPE()
# GENERATE INITIALACTION
state = 0
- yystack = []
- yylval = None
+ yystack = [(-1, yylval, yylloc.copy())] # kludge for bison compatibility
yychar = -1
yy_element_stack = []
while True:
#print('state', state, 'yystack', yystack)
- assert len(yy_element_stack) == len(yystack) * 2
reduce = yy_lr1dfa_states[state][4]
if reduce == -1:
if yychar == -1:
- yylval = None
- yylloc = YYLTYPE() # temporary until lex_yy updated, should be None
yychar = lex_yy.yylex()
#print('yychar', yychar, 'yylval', yylval, 'yylloc', yylloc, 'lex_yy.yytext', lex_yy.yytext)
#print('lex_yy.yy_element_space')
bisect.bisect_right(yy_lr1dfa_states[state][0], yychar)
]
if action == -1:
- raise Exception('syntax error')
+ yyerror(yylloc, 'syntax error')
+ assert False # error recovery is not implemented yet
if (action & 1) == 0:
- yystack.append((state, yylval, yylloc))
+ yystack.append((state, yylval, yylloc.copy()))
# push space then AST element contiguously onto yy_element_stack
# even numbered elements are spaces, odd numbered elements are AST
reduce = action >> 1
#print('reduce', reduce)
len_symbols, ref_data = yy_lr1dfa_productions[reduce]
+ # GENERATE YYLLOC_DEFAULT BEGIN
+ YYLLOC_DEFAULT(yyloc, yystack, len_symbols)
+ # GENERATE END
base = len(yystack) - len_symbols
- yystack.append((state, None, None))
- state, yyval, yyloc = yystack[base]
+ yystack.append((state, None, None)) # only has effect if len_symbols == 0
+ state, yyval, _ = yystack[base]
ref_data()
del yystack[base:]
if reduce == 0:
- assert base == 0
+ assert base == 1
break
- yystack.append((state, yyval, yyloc))
+ yystack.append((state, yyval, yyloc.copy()))
# action creates empty space in yy_element_stack[base * 2] if needed
- assert len(yy_element_stack) > base * 2
+ assert len(yy_element_stack) >= base * 2 - 1
- # concatenate yy_element_stack[base * 2 + 1:] to a single AST element
- yy_element_stack[base * 2 + 1:] = [
+ # concatenate yy_element_stack[base * 2 - 1:] to a single AST element
+ yy_element_stack[base * 2 - 1:] = [
element.concatenate(
- yy_element_stack[base * 2 + 1:],
+ yy_element_stack[base * 2 - 1:],
element.Element
)
]