Merge branch 'wrywerytwreywery-imag'

This commit is contained in:
Anthony Scopatz 2015-03-21 19:59:05 -05:00
commit cf286e0b7a
3 changed files with 19 additions and 2 deletions

View file

@ -99,6 +99,15 @@ def test_int_literal():
def test_float_literal():
yield check_ast, '42.0'
def test_imag_literal():
yield check_ast, '42j'
def test_float_imag_literal():
yield check_ast, '42.0j'
def test_complex():
yield check_ast, '42+84j'
def test_str_literal():
yield check_ast, '"hello"'

View file

@ -109,8 +109,8 @@ class Lexer(object):
# literals
'INT_LITERAL', 'HEX_LITERAL', 'OCT_LITERAL', 'BIN_LITERAL',
'FLOAT_LITERAL', 'STRING_LITERAL', 'RAW_STRING_LITERAL',
'BYTES_LITERAL', 'UNICODE_LITERAL',
'FLOAT_LITERAL', 'IMAG_LITERAL', 'STRING_LITERAL',
'RAW_STRING_LITERAL', 'BYTES_LITERAL', 'UNICODE_LITERAL',
# Basic Operators
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'DOUBLEDIV', 'MOD', 'POW',
@ -176,6 +176,7 @@ class Lexer(object):
float_mantissa = r"(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.)"
float_literal = ('((((' + float_mantissa + ')' + float_exponent +
'?)|([0-9]+' + float_exponent + ')))')
imag_literal = '(' + r'[0-9]+[jJ]' + '|' + float_literal + r'[jJ]' + ')'
#
# Rules
@ -360,6 +361,12 @@ class Lexer(object):
# float literal must come before int literals
@TOKEN(imag_literal)
def t_IMAG_LITERAL(self, t):
if self.in_py_mode[-1]:
t.value = eval(t.value)
return t
@TOKEN(float_literal)
def t_FLOAT_LITERAL(self, t):
if self.in_py_mode[-1]:

View file

@ -1580,6 +1580,7 @@ class Parser(object):
| OCT_LITERAL
| BIN_LITERAL
| FLOAT_LITERAL
| IMAG_LITERAL
"""
p[0] = ast.Num(n=p[1], lineno=self.lineno, col_offset=self.col)