literal comparasins

This commit is contained in:
Anthony Scopatz 2015-01-24 23:02:15 -06:00
parent bb9e3e4742
commit 7650ace31f
3 changed files with 60 additions and 4 deletions

View file

@ -157,6 +157,49 @@ def test_plus_group_times():
def test_group():
yield check_ast, '(42)'
def test_lt():
yield check_ast, '42 < 65'
def test_lt():
yield check_ast, '42 < 65'
def test_gt():
yield check_ast, '42 > 65'
def test_eq():
yield check_ast, '42 == 65'
def test_lt():
yield check_ast, '42 < 65'
def test_le():
yield check_ast, '42 <= 65'
def test_ge():
yield check_ast, '42 >= 65'
def test_ne():
yield check_ast, '42 != 65'
def test_in():
yield check_ast, '"4" in "65"'
def test_is():
yield check_ast, '42 is 65'
def test_not_in():
yield check_ast, '"4" not in "65"'
def test_is_not():
yield check_ast, '42 is not 65'
def test_lt_lt():
yield check_ast, '42 < 65 < 105'
def test_lt_lt_lt():
yield check_ast, '42 < 65 < 105 < 77'

View file

@ -1,4 +1,5 @@
"""The xonsh abstract syntax tree node."""
from __future__ import unicode_literals, print_function
from ast import Module, Num, Expr, Str, Bytes, UnaryOp, UAdd, USub, Invert, \
BinOp, Add, Sub, Mult, Div, FloorDiv, Mod, Pow
BinOp, Add, Sub, Mult, Div, FloorDiv, Mod, Pow, Compare, Lt, Gt, LtE, \
GtE, Eq, NotEq, In, NotIn, Is, IsNot

View file

@ -732,11 +732,22 @@ class Parser(object):
def p_comparison(self, p):
"""comparison : expr comp_op_expr_list_opt"""
p[0] = p[1] if p[2] is None else p[1] + p[2]
p2 = p[2]
if p2 is None:
p0 = p[1]
else:
p0 = ast.Compare(left=p[1], ops=p2[::2], comparators=p2[1::2],
lineno=self.lineno, col_offset=self.col)
p[0] = p0
def p_comp_op_expr(self, p):
"""comp_op_expr : comp_op expr"""
p[0] = p[1] + p[2]
p[0] = [p[1], p[2]]
_comp_ops = {'<': ast.Lt, '>': ast.Gt, '==': ast.Eq, '>=': ast.GtE,
'<=': ast.LtE, '!=': ast.NotEq, 'in': ast.In,
('not', 'in'): ast.NotIn, 'is': ast.Is,
('is', 'not'): ast.IsNot}
def p_comp_op(self, p):
"""comp_op : LT
@ -750,7 +761,8 @@ class Parser(object):
| IS
| IS NOT
"""
p[0] = p[1] if len(p) == 2 else p[1] + p[2]
key = p[1] if len(p) == 2 else (p[1], p[2])
p[0] = self._comp_ops[key]()
def p_star_expr(self, p):
"""star_expr : TIMES expr"""