[New-bugs-announce] [issue19080] Enrich SyntaxError with additional information

alon horev report at bugs.python.org
Mon Sep 23 22:42:05 CEST 2013


New submission from alon horev:

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

----------
components: Interpreter Core
messages: 198341
nosy: alonho
priority: normal
severity: normal
status: open
title: Enrich SyntaxError with additional information
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19080>
_______________________________________


More information about the New-bugs-announce mailing list