More doc improvements. Parser production improvements.
This commit is contained in:
parent
05a709aaea
commit
41ee4aff04
1561
docs/sly.rst
1561
docs/sly.rst
File diff suppressed because it is too large
Load Diff
37
sly/yacc.py
37
sly/yacc.py
@ -105,8 +105,8 @@ class YaccSymbol:
|
|||||||
class YaccProduction:
|
class YaccProduction:
|
||||||
def __init__(self, s, stack=None):
|
def __init__(self, s, stack=None):
|
||||||
self._slice = s
|
self._slice = s
|
||||||
self._stack = stack
|
|
||||||
self._namemap = { }
|
self._namemap = { }
|
||||||
|
self._stack = stack
|
||||||
|
|
||||||
def __getitem__(self, n):
|
def __getitem__(self, n):
|
||||||
if n >= 0:
|
if n >= 0:
|
||||||
@ -115,19 +115,29 @@ class YaccProduction:
|
|||||||
return self._stack[n].value
|
return self._stack[n].value
|
||||||
|
|
||||||
def __setitem__(self, n, v):
|
def __setitem__(self, n, v):
|
||||||
|
if n >= 0:
|
||||||
self._slice[n].value = v
|
self._slice[n].value = v
|
||||||
|
else:
|
||||||
|
self._stack[n].value = v
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self._slice)
|
return len(self._slice)
|
||||||
|
|
||||||
def lineno(self, n):
|
@property
|
||||||
return getattr(self._slice[n], 'lineno', 0)
|
def lineno(self):
|
||||||
|
for tok in self._slice:
|
||||||
|
lineno = getattr(tok, 'lineno', None)
|
||||||
|
if lineno:
|
||||||
|
return lineno
|
||||||
|
raise AttributeError('No line number found')
|
||||||
|
|
||||||
def set_lineno(self, n, lineno):
|
@property
|
||||||
self._slice[n].lineno = lineno
|
def index(self):
|
||||||
|
for tok in self._slice:
|
||||||
def index(self, n):
|
index = getattr(tok, 'index', None)
|
||||||
return getattr(self._slice[n], 'index', 0)
|
if index:
|
||||||
|
return index
|
||||||
|
raise AttributeError('No index attribute found')
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
return self._slice[self._namemap[name]].value
|
return self._slice[self._namemap[name]].value
|
||||||
@ -1821,9 +1831,10 @@ class Parser(metaclass=ParserMeta):
|
|||||||
errorcount = 0 # Used during error recovery
|
errorcount = 0 # Used during error recovery
|
||||||
|
|
||||||
# Set up the state and symbol stacks
|
# Set up the state and symbol stacks
|
||||||
|
self.tokens = tokens
|
||||||
self.statestack = statestack = [] # Stack of parsing states
|
self.statestack = statestack = [] # Stack of parsing states
|
||||||
self.symstack = symstack = [] # Stack of grammar symbols
|
self.symstack = symstack = [] # Stack of grammar symbols
|
||||||
pslice.stack = symstack # Put in the production
|
pslice._stack = symstack # Associate the stack with the production
|
||||||
self.restart()
|
self.restart()
|
||||||
|
|
||||||
errtoken = None # Err token
|
errtoken = None # Err token
|
||||||
@ -1870,15 +1881,15 @@ class Parser(metaclass=ParserMeta):
|
|||||||
|
|
||||||
# Call the production function
|
# Call the production function
|
||||||
pslice._slice = symstack[-plen:] if plen else []
|
pslice._slice = symstack[-plen:] if plen else []
|
||||||
if plen:
|
|
||||||
del symstack[-plen:]
|
|
||||||
del statestack[-plen:]
|
|
||||||
|
|
||||||
sym = YaccSymbol()
|
sym = YaccSymbol()
|
||||||
sym.type = pname
|
sym.type = pname
|
||||||
sym.value = p.func(self, pslice)
|
sym.value = p.func(self, pslice)
|
||||||
symstack.append(sym)
|
if plen:
|
||||||
|
del symstack[-plen:]
|
||||||
|
del statestack[-plen:]
|
||||||
|
|
||||||
|
symstack.append(sym)
|
||||||
self.state = goto[statestack[-1]][pname]
|
self.state = goto[statestack[-1]][pname]
|
||||||
statestack.append(self.state)
|
statestack.append(self.state)
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user