n1.truedivision = None # unknown
return n1
-#expression: or_test [if or_test else test] | lambda_form
-
-# actually:
-#test: or_test ['if' or_test 'else' test] | lambdef
-
-def p_simple_expr(s):
- if s.sy == 'lambda':
- return p_lambdef(s)
- pos = s.position()
- expr = p_or_test(s)
- if s.sy == 'if':
- s.next()
- test = p_or_test(s)
- s.expect('else')
- other = p_test(s)
- return ExprNodes.CondExprNode(pos, test=test, true_val=expr, false_val=other)
- else:
- return expr
-
#lambdef: 'lambda' [varargslist] ':' test
def p_lambdef(s, allow_conditional=True):
s, terminator=':', annotated=False)
s.expect(':')
if allow_conditional:
- expr = p_simple_expr(s)
+ expr = p_test(s)
else:
expr = p_test_nocond(s)
return ExprNodes.LambdaNode(
def p_lambdef_nocond(s):
return p_lambdef(s, allow_conditional=False)
-#test: or_test | lambdef
+#test: or_test ['if' or_test 'else' test] | lambdef
def p_test(s):
- return p_or_test(s)
+ if s.sy == 'lambda':
+ return p_lambdef(s)
+ pos = s.position()
+ expr = p_or_test(s)
+ if s.sy == 'if':
+ s.next()
+ test = p_or_test(s)
+ s.expect('else')
+ other = p_test(s)
+ return ExprNodes.CondExprNode(pos, test=test, true_val=expr, false_val=other)
+ else:
+ return expr
#test_nocond: or_test | lambdef_nocond
# If it is actually a type, but parsable as an expression,
# we treat it as an expression here.
if looking_at_expr(s):
- operand = p_simple_expr(s)
+ operand = p_test(s)
node = ExprNodes.SizeofVarNode(pos, operand = operand)
else:
base_type = p_c_base_type(s)
s.error("only one star-arg parameter allowed",
pos = s.position())
s.next()
- star_arg = p_simple_expr(s)
+ star_arg = p_test(s)
else:
- arg = p_simple_expr(s)
+ arg = p_test(s)
if s.sy == '=':
s.next()
if not arg.is_name:
pos = arg.pos)
encoded_name = EncodedString(arg.name)
keyword = ExprNodes.IdentifierStringNode(arg.pos, value = encoded_name)
- arg = p_simple_expr(s)
+ arg = p_test(s)
keyword_args.append((keyword, arg))
else:
if keyword_args:
positional_args = [ p_genexp(s, positional_args[0]) ]
elif s.sy == '**':
s.next()
- starstar_arg = p_simple_expr(s)
+ starstar_arg = p_test(s)
if s.sy == ',':
s.next()
s.expect(')')
# Simple expression which may be missing iff
# it is followed by something in follow_set.
if s.sy not in follow_set:
- return p_simple_expr(s)
+ return p_test(s)
else:
return None
if s.sy == ']':
s.expect(']')
return ExprNodes.ListNode(pos, args = [])
- expr = p_simple_expr(s)
+ expr = p_test(s)
if s.sy == 'for':
target = ExprNodes.ListNode(pos, args = [])
append = ExprNodes.ComprehensionAppendNode(
if s.sy == '}':
s.next()
return ExprNodes.DictNode(pos, key_value_pairs = [])
- item = p_simple_expr(s)
+ item = p_test(s)
if s.sy == ',' or s.sy == '}':
# set literal
values = [item]
s.next()
if s.sy == '}':
break
- values.append( p_simple_expr(s) )
+ values.append( p_test(s) )
s.expect('}')
return ExprNodes.SetNode(pos, args=values)
elif s.sy == 'for':
# dict literal or comprehension
key = item
s.next()
- value = p_simple_expr(s)
+ value = p_test(s)
if s.sy == 'for':
# dict comprehension
target = ExprNodes.DictNode(pos, key_value_pairs = [])
s.next()
if s.sy == '}':
break
- key = p_simple_expr(s)
+ key = p_test(s)
s.expect(':')
- value = p_simple_expr(s)
+ value = p_test(s)
items.append(
ExprNodes.DictItemNode(key.pos, key=key, value=value))
s.expect('}')
def p_simple_expr_list(s):
exprs = []
while s.sy not in expr_terminators:
- expr = p_simple_expr(s)
+ expr = p_test(s)
exprs.append(expr)
if s.sy != ',':
break
def p_expr(s):
pos = s.position()
- expr = p_simple_expr(s)
+ expr = p_test(s)
if s.sy == ',':
s.next()
exprs = [expr] + p_simple_expr_list(s)
s.next()
if s.sy == '>>':
s.next()
- stream = p_simple_expr(s)
+ stream = p_test(s)
if s.sy == ',':
s.next()
ends_with_comma = s.sy in ('NEWLINE', 'EOF')
stream = None
args = []
if s.sy not in ('NEWLINE', 'EOF'):
- args.append(p_simple_expr(s))
+ args.append(p_test(s))
while s.sy == ',':
s.next()
if s.sy in ('NEWLINE', 'EOF'):
ends_with_comma = 1
break
- args.append(p_simple_expr(s))
+ args.append(p_test(s))
arg_tuple = ExprNodes.TupleNode(pos, args = args)
return Nodes.PrintStatNode(pos,
arg_tuple = arg_tuple, stream = stream,
args = [ p_bit_expr(s) ]
if s.sy == 'in':
s.next()
- args.append(p_simple_expr(s))
+ args.append(p_test(s))
if s.sy == ',':
s.next()
- args.append(p_simple_expr(s))
+ args.append(p_test(s))
else:
error(pos, "'exec' currently requires a target mapping (globals/locals)")
return Nodes.ExecStatNode(pos, args = args)
# s.sy == 'del'
pos = s.position()
s.next()
+ # FIXME: 'exprlist' in Python
args = p_simple_expr_list(s)
return Nodes.DelStatNode(pos, args = args)
exc_value = None
exc_tb = None
if s.sy not in statement_terminators:
- exc_type = p_simple_expr(s)
+ exc_type = p_test(s)
if s.sy == ',':
s.next()
- exc_value = p_simple_expr(s)
+ exc_value = p_test(s)
if s.sy == ',':
s.next()
- exc_tb = p_simple_expr(s)
+ exc_tb = p_test(s)
if exc_type or exc_value or exc_tb:
return Nodes.RaiseStatNode(pos,
exc_type = exc_type,
# s.sy == 'assert'
pos = s.position()
s.next()
- cond = p_simple_expr(s)
+ cond = p_test(s)
if s.sy == ',':
s.next()
- value = p_simple_expr(s)
+ value = p_test(s)
else:
value = None
return Nodes.AssertStatNode(pos, cond = cond, value = value)
def p_if_clause(s):
pos = s.position()
- test = p_simple_expr(s)
+ test = p_test(s)
body = p_suite(s)
return Nodes.IfClauseNode(pos,
condition = test, body = body)
# s.sy == 'while'
pos = s.position()
s.next()
- test = p_simple_expr(s)
+ test = p_test(s)
body = p_suite(s)
else_clause = p_else_clause(s)
return Nodes.WhileStatNode(pos,
exc_type = None
exc_value = None
if s.sy != ':':
- exc_type = p_simple_expr(s)
+ exc_type = p_test(s)
if s.sy == ',' or (s.sy == 'IDENT' and s.systring == 'as'):
s.next()
- exc_value = p_simple_expr(s)
+ exc_value = p_test(s)
elif s.sy == 'IDENT' and s.systring == 'as':
# Py3 syntax requires a name here
s.next()
s.next() # s.sy is '='
s.next()
if looking_at_expr(s):
- arg = p_simple_expr(s)
+ arg = p_test(s)
else:
base_type = p_c_base_type(s, templates = templates)
declarator = p_c_declarator(s, empty = 1)
else:
if looking_at_expr(s):
- arg = p_simple_expr(s)
+ arg = p_test(s)
else:
base_type = p_c_base_type(s, templates = templates)
declarator = p_c_declarator(s, empty = 1)
cname = p_opt_cname(s)
if name != "operator" and s.sy == '=' and assignable:
s.next()
- rhs = p_simple_expr(s)
+ rhs = p_test(s)
else:
if nonempty:
error(s.position(), "Empty declarator")
if s.sy == '?':
exc_check = 1
s.next()
- exc_val = p_simple_expr(s)
+ exc_val = p_test(s)
return exc_val, exc_check
c_arg_list_terminators = ('*', '**', '.', ')')
not_none = kind == 'not'
if annotated and s.sy == ':':
s.next()
- annotation = p_simple_expr(s)
+ annotation = p_test(s)
if s.sy == '=':
s.next()
if 'pxd' in s.level:
default = ExprNodes.BoolNode(1)
s.next()
else:
- default = p_simple_expr(s)
+ default = p_test(s)
return Nodes.CArgDeclNode(pos,
base_type = base_type,
declarator = declarator,
value = None
if s.sy == '=':
s.next()
- value = p_simple_expr(s)
+ value = p_test(s)
items.append(Nodes.CEnumDefItemNode(pos,
name = name, cname = cname, value = value))
return_type_annotation = None
if s.sy == '->':
s.next()
- return_type_annotation = p_simple_expr(s)
+ return_type_annotation = p_test(s)
doc, body = p_suite(s, Ctx(level = 'function'), with_doc = 1)
return Nodes.DefNode(pos, name = name, args = args,
star_arg = star_arg, starstar_arg = starstar_arg,
annotation = None
if s.sy == ':':
s.next()
- annotation = p_simple_expr(s)
+ annotation = p_test(s)
return Nodes.PyArgDeclNode(pos, name = name, annotation = annotation)
def p_class_statement(s, decorators):