vinzenz / libpypa

libpypa is a Python parser implemented in pure C++
Apache License 2.0
189 stars 48 forks source link

support no newline at end of file #13

Closed tjhance closed 9 years ago

tjhance commented 9 years ago

Currently pypa fails when the last statement doesn't have a trailing newline.

I think that the Official Python Grammar (https://docs.python.org/2/reference/grammar.html) doesn't technically require that the programs missing a trailing newline be accepted - so pypa really isn't in err here.

However, I found that CPython actually accepts programs missing a newline (2.7.7 does, in any case). It might be a good idea for pypa to do the same?

vinzenz commented 9 years ago

I am sorry but I cannot reproduce this, do you have a concrete example, as a test case please?

tjhance commented 9 years ago

Yeah, take this file for example: https://www.dropbox.com/s/orxshzq09uel4p6/newline_example.py?dl=0

It's just one line, but there is no newline.

$ python newline_example.py
hi
$ ./pyston_dbg -x newline_example.py # uses libpypa parser
Traceback (most recent call last):
  File "newline_example.py", line 1, in :
    print "hi"
SyntaxError: Expected new line after statement
tjhance commented 9 years ago

So, I realized this issue is a lot more relevant when we have eval and exec since people often write stuff like

exec "print 'hi'"

where that string needs to be parsed but it has no newline at the end.

Anyway, it seems like this is partially fixed (the example I gave above seems to work now), but now it doesn't seem to work when the contents end in a space, for example the following string doesn't seem to work:

"print 'hi'\n "