Compiler icon indicating copy to clipboard operation
Compiler copied to clipboard

Fix the incorrect display of the column and line in errors.

Open SerafimArts opened this issue 8 years ago • 17 comments

Fixes "Incorrect calculation of line and position in utf-8 files" issue

Unrecognized token:

image

Unexpected token:

image

:smile_cat:

SerafimArts avatar Nov 01 '17 18:11 SerafimArts

Thanks for the PR!

I would use a custom exception constructor (that can use a trait or a parent class) to compute the correct line, column, marker/arrow position etc. rather than a trait in the Lexer and the Parser classes. It makes more sense for me.

Can you update your patch this way please? If you don't have time, I can do it for you.

Hywan avatar Nov 01 '17 20:11 Hywan

@Hywan With such a signature?

$error = SomeErr::createFromOffest(string $message, array $args = [], string $sources, int $offset)

SerafimArts avatar Nov 01 '17 21:11 SerafimArts

Or maybe:

$message = \sprintf('...', ...$args);

throw SomeErr::fromOffset($message, string $sources, int $offset);

SerafimArts avatar Nov 01 '17 21:11 SerafimArts

What about:

class UnrecognizedToken extends Exception
{
    …

    public function __construct($code, $text, $byteOffset)
    {
        // …
        parent::__construct($message, $code, $args);
    }
}

Notice that $message and $args are no longer part of the signature, but they are computed and passed to the parent constructor.

Thoughts?

Hywan avatar Nov 06 '17 10:11 Hywan

This will make it impossible to reuse the exception with arguments that are different from those specified in the constructor. And this is a violation of backward compatibility.

Is such an improvement of such changes worthwhile?

SerafimArts avatar Nov 06 '17 15:11 SerafimArts

Or add a static method on UnrecognizedToken to build a valid UnrecognizedToken exception with a pre-defined message? Something like:

class UnrecognizedToken extends Exception
{
    public static function blabla(…)
    {
        // $message
        return new self($message, …);
    }
}

Hywan avatar Nov 06 '17 15:11 Hywan

This I suggested above. Just now there is no time to realize =\

I think I'll fix it this week.

SerafimArts avatar Nov 06 '17 16:11 SerafimArts

Heh, sorry :-).

Hywan avatar Nov 07 '17 09:11 Hywan

@Hywan ping?

SerafimArts avatar Nov 11 '17 07:11 SerafimArts

@SerafimArts PR was closed for a reason?

Pierozi avatar Jan 10 '18 21:01 Pierozi

@Pierozi for me it became not relevant. I had to fork the project and slightly rewrite it: https://github.com/railt/compiler/network

SerafimArts avatar Jan 11 '18 07:01 SerafimArts

We are still interested by the PR though… Can you reopen it please?

Hywan avatar Jan 11 '18 08:01 Hywan

nope =( image

SerafimArts avatar Jan 11 '18 09:01 SerafimArts

you can still get the code of the PR, in order to recreate your branch

$ git clone [email protected]:hoaproject/Compiler.git
$ cd Compiler
$ git fetch origin pull/79/head:issue/78
$ git remote add my-fork ....
$ git push my-fork issue/78

Pierozi avatar Jan 11 '18 09:01 Pierozi

Done

SerafimArts avatar Jan 11 '18 09:01 SerafimArts

In any case, in this PR I did not take into account the behavior of %pragma lexer.unicode, so it is not entirely correct. More precisely, exceptions always take utf into account, regardless of whether there are instructions to use it.

SerafimArts avatar Jan 11 '18 09:01 SerafimArts

Thanks for reopening!

Hywan avatar Jan 22 '18 10:01 Hywan