bjpop / language-python

A parser for Python 2.x and 3.x written in Haskell
157 stars 46 forks source link

Boolean constants are parsed as Vars #4

Closed spockz closed 13 years ago

spockz commented 13 years ago

When we parse the following piece of Python code:

a = 1.0
a = 3
b = True
c = False

We get the following output:

*Main> (\f -> (readFile f >>= \x -> print $ parseModule x f)) "../test/assign.py" 
Right (Module [Assign {assign_to = [Var {var_ident = Ident {ident_string = "a", ident_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 1, span_column = 1}}, expr_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 1, span_column = 1}}], assign_expr = Float {float_value = 1.0, expr_literal = "1.0", expr_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 1, span_start_column = 5, span_end_column = 7}}, stmt_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 1, span_start_column = 1, span_end_column = 7}},Assign {assign_to = [Var {var_ident = Ident {ident_string = "a", ident_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 2, span_column = 1}}, expr_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 2, span_column = 1}}], assign_expr = Int {int_value = 3, expr_literal = "3", expr_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 2, span_column = 5}}, stmt_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 2, span_start_column = 1, span_end_column = 5}},Assign {assign_to = [Var {var_ident = Ident {ident_string = "b", ident_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 3, span_column = 1}}, expr_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 3, span_column = 1}}], assign_expr = Var {var_ident = Ident {ident_string = "True", ident_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 3, span_start_column = 5, span_end_column = 8}}, expr_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 3, span_start_column = 5, span_end_column = 8}}, stmt_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 3, span_start_column = 1, span_end_column = 8}},Assign {assign_to = [Var {var_ident = Ident {ident_string = "c", ident_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 4, span_column = 1}}, expr_annot = SpanPoint {span_filename = "../test/assign.py", span_row = 4, span_column = 1}}], assign_expr = Var {var_ident = Ident {ident_string = "False", ident_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 4, span_start_column = 5, span_end_column = 9}}, expr_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 4, span_start_column = 5, span_end_column = 9}}, stmt_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 4, span_start_column = 1, span_end_column = 9}}],[])

And the relative bit:

assign_expr = Var {var_ident = Ident {ident_string = "True", ident_annot = SpanCoLinear {span_filename = "../test/assign.py", span_row = 3, span_start_column = 5, span_end_column = 8}},

Is this a bug or is it supposed to work in this way?

bjpop commented 13 years ago

Hi spockz,

That is the intended behaviour for Python 2.x, but it is not the intended behaviour for Python 3.x. I assume you are using the Version 2 parser?

See the official Python grammars: http://docs.python.org/release/2.6/reference/grammar.html http://docs.python.org/release/3.1/reference/grammar.html

Compare the definition of atom in both. In 3.1 the symbols True and False are atom literals, but they are not in 2.6.

Cheers, Bernie.

spockz commented 13 years ago

Yes we are using the 2.x parser. Sorry I didn't know the grammer of 2.6 was like this. :-) I'm glad they fixed that in 3.x

Thanks