hoaproject / Compiler

The Hoa\Compiler library.
https://hoa-project.net/
453 stars 48 forks source link

Update Parser.php #72

Closed SerafimArts closed 7 years ago

SerafimArts commented 7 years ago

Fix "strrpos()" bug when offset is greater than the length of haystack string. Fix https://github.com/hoaproject/Compiler/issues/71

SerafimArts commented 7 years ago

It would be good to show (in UnexpectedToken exception at line 0) in this case not the previous token (null).

Maybe some other message, defined in *.pp file?

grammar.pp:

%undefined T_UNDEFINED

UnexpectedTokenException:

Unexpected token (T_UNDEFINED) at line 0 and column 0:
some any
↑
Hywan commented 7 years ago

Thank @Pierozi for handling this PR and for the review!

SerafimArts commented 7 years ago

@Hywan The problem can be reproduced if the first token and/or character does not correspond to any rule (rule == defined token).

Hywan commented 7 years ago

@SerafimArts I took the liberty to implement another approach, please see https://github.com/hoaproject/Compiler/commit/23cc8e191bdd9dd3aec73c7b450200ebccc0deb7. The patch is very minimal too.

You are right, an error token is not always found when backtracking, especially when it happens at the very first position of the text being parsed. In this particular edge case, the current token is used as the error token. So with your example, it produces this error:

Hoa\Compiler\Llk\Parser::parse(): (0) Unexpected token "type" (T_NAME) at line 1 and column 1:
type Test { }
↑

instead of:

Hoa\Compiler\Llk\Parser::parse(): (0) Unexpected token "(null)" ((null)) at line 1 and column 1:
type Test { }
↑

as your patch suggested.

Thank you very much for this excellent bug report and your patience!

Hywan commented 7 years ago

3.17.08.08 has been released because this is an important bug fix.

SerafimArts commented 7 years ago

@Hywan i think this commit https://github.com/hoaproject/Compiler/commit/23cc8e191bdd9dd3aec73c7b450200ebccc0deb7 are better. I also thought about it, but decided to limit myself to simple bugfix =)

Hywan commented 7 years ago

:+1: The harder part was to find the bug, and what you did was excellent. Thank you!