[Proposition] Column number in which the error ocurred

141 views
Skip to first unread message

Arian Sameni

unread,
Nov 18, 2009, 9:30:59 AM11/18/09
to ply-hack
Hi everyone,

Just joined the group. I'm using PLY, and wanted to make a proposition
(actually a very little but useful one).

I have modified ply/lex.py to be able to know in which column the
lexer is working, so, in case of error, we can display a more specific
message, like "An error ocurred on line X, column Y".

I added an attribute to class Lexer (ply/lex.py). Line 139 now looks
like:

self.current = -1

Then, in the "new line" rule (lexer implementation file, something
like my_lexer.py), we update the value of lexer.current:

t.lexer.lineno += len(t.value) # Just like before
t.lexer.current = t.lexer.lexpos - 1 # Added new

And when error (error rule):

print 'ERROR: line ' + str(t.lexer.lineno) + \
', column: ' + str(t.lexer.lexpos - t.lexer.current) +
\
'. Character not recognised: ' + t.value[0]
t.lexer.skip(1)

It's quite simple, but I didn't find any column track in the lexer, so
I just wanted to share my trick with you. I hope its clear.

Best regards :D and nice job!!
Reply all
Reply to author
Forward
0 new messages