Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch: pyparser-improvements-3
Changeset: r94727:159368e940b5
Date: 2018-06-05 23:26 +0200
http://bitbucket.org/pypy/pypy/changeset/159368e940b5/
Log: slightly less silly code in the parser module
diff --git a/pypy/module/parser/pyparser.py b/pypy/module/parser/pyparser.py
--- a/pypy/module/parser/pyparser.py
+++ b/pypy/module/parser/pyparser.py
@@ -133,10 +133,9 @@
space.newtext(message))
-def get_node_type(space, w_tuple):
+def get_node_type(space, tup_w, w_tuple):
try:
- w_type = space.getitem(w_tuple, space.newint(0))
- return space.int_w(w_type)
+ return space.int_w(tup_w[0])
except OperationError:
raise parser_error(space, w_tuple, "Illegal component tuple.")
@@ -145,44 +144,47 @@
self.lineno = 0
def build_node_tree(space, w_tuple):
- type = get_node_type(space, w_tuple)
+ tup_w = space.unpackiterable(w_tuple)
+ if len(tup_w) == 0:
+ raise parser_error(space, w_tuple, "tuple too short")
+
+ type = get_node_type(space, tup_w, w_tuple)
node_state = NodeState()
if 0 <= type < 256:
# The tuple is simple, but it doesn't start with a start symbol.
# Raise an exception now and be done with it.
raise parser_error(space, w_tuple,
"Illegal syntax-tree; cannot start with terminal
symbol.")
+ return build_node_children(space, type, tup_w, node_state)
+
+def build_node_children(space, type, tup_w, node_state):
node = pyparse.parser.Nonterminal(type)
- build_node_children(space, w_tuple, node, node_state)
- return node
-
-def build_node_children(space, w_tuple, node, node_state):
- for w_elem in space.unpackiterable(w_tuple)[1:]:
- type = get_node_type(space, w_elem)
+ for i in range(1, len(tup_w)):
+ w_elem = tup_w[i]
+ subtup_w = space.unpackiterable(w_elem)
+ type = get_node_type(space, subtup_w, w_elem)
if type < 256: # Terminal node
- length = space.len_w(w_elem)
+ length = len(subtup_w)
if length == 2:
- _, w_obj = space.unpackiterable(w_elem, 2)
+ _, w_obj = subtup_w
elif length == 3:
- _, w_obj, w_lineno = space.unpackiterable(w_elem, 3)
+ _, w_obj, w_lineno = subtup_w
else:
raise parse_error(
space, "terminal nodes must have 2 or 3 entries")
strn = space.text_w(w_obj)
child = pyparse.parser.Terminal(type, strn, node_state.lineno, 0)
else:
- child = pyparse.parser.Nonterminal(type)
+ child = build_node_children(space, type, subtup_w, node_state)
node.append_child(child)
- if type >= 256: # Nonterminal node
- build_node_children(space, w_elem, child, node_state)
- elif type == pyparse.pygram.tokens.NEWLINE:
+ if type == pyparse.pygram.tokens.NEWLINE:
node_state.lineno += 1
+ return node
-def validate_node(space, tree):
+def validate_node(space, tree, parser):
assert tree.type >= 256
type = tree.type - 256
- parser = pyparse.PythonParser(space)
if type >= len(parser.grammar.dfas):
raise parse_error(space, "Unrecognized node type %d." % type)
dfa = parser.grammar.dfas[type]
@@ -195,7 +197,7 @@
if label == ch.type:
# The child is acceptable; validate it recursively
if ch.type >= 256:
- validate_node(space, ch)
+ validate_node(space, ch, parser)
# Update the state, and move on to the next child.
arcs, is_accepting = dfa.states[next_state]
break
@@ -209,5 +211,6 @@
def tuple2st(space, w_sequence):
# Convert the tree to the internal form before checking it
tree = build_node_tree(space, w_sequence)
- validate_node(space, tree)
+ parser = pyparse.PythonParser(space)
+ validate_node(space, tree, parser)
return W_STType(tree, 'eval')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit