This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author alonho
Recipients alonho
Date 2013-09-23.20:42:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1379968925.72.0.669299760684.issue19080@psf.upfronthosting.co.za>
In-reply-to
Content
Some context for this feature request:
I'm using the wonderful ast module for a library that translates python code to MongoDB queries (https://github.com/alonho/pql). I also did the same for SQL queries using sqlalchemy as a part of another project (https://github.com/alonho/pytrace).

One of the things I find lacking in python's parser is additional information about SyntaxErrors. This could help users of the 'ast' module, IDE and developers.

Here are some examples of what I'd like to see
1. ast.parse('* 2') -> SyntaxError('Unexpected operator at start of an expression')
2. ast.parse('2 *') -> SyntaxError('Missing right hand side operand')
3. ast.parse('if a = 1: pass') -> SyntaxError('Cannot assign inside an expression')

There are several challenges here:
1. Does the parser have this information and doesn't surface it?
2. Can such messages be automatically generated without filling the code with error handling code? 3. Which part of the code could be responsible for this kind of a task? I've looked at the BNF and it contains more than just syntax legality but operator precedence and such. Perhaps there's another (simpler) grammar definition somewhere?

I was curious to see what Ruby does, and it uses a simple solution of providing raw information along with the exception:
>> a == * 1
SyntaxError: compile error
(irb):17: syntax error, unexpected tSTAR
a == * 1
      ^
	from (irb):17
History
Date User Action Args
2013-09-23 20:42:05alonhosetrecipients: + alonho
2013-09-23 20:42:05alonhosetmessageid: <1379968925.72.0.669299760684.issue19080@psf.upfronthosting.co.za>
2013-09-23 20:42:05alonholinkissue19080 messages
2013-09-23 20:42:05alonhocreate