On 09/22/2013 02:33 AM, Walter Bright wrote:
On 9/21/2013 5:11 PM, Sean Kelly wrote:
Tracking the column number is certainly doable, but it comes at a
cost of
memory consumption and some compile speed, since it has to be tracked in
every token. I used to do it in the Digital Mars C compiler, but it
was of
only marginal utility and I dropped it.

Can't you just hold a pointer to the beginning of the line and
subtract to
find the column?  I agree that it's generally of marginal utility though.

Holding the pointer has a cost of memory consumption and compile speed
:-)

(The current scheme is not optimal either.)

struct Loc
{
    const char *filename; // <--
    unsigned linnum;

Replace this pointer by a pointer or (global) offset to the first character in the source file buffer corresponding to the AST node. The file name can be recovered from this using an appropriate data structure.

In order to find the first column index, decode backwards until you hit a newline character or the beginning of the source file.

In order to find the last column index if that is wanted, re-invoke the parser. (Personally, I just store a slice into the source buffer.)

This allows the compiler to underline the location where an error occurred:

tt.d:7:14: error: need 'this' to access field 'S.member'
    auto x = S.member;
             ^~~~~~~~
tt.d:3:5: note: field was declared here
    int member;
    ^~~~~~~~~~~

Editors understand this format and can quickly jump to the location.

You could also get rid of linnum by splitting the source file buffer into lines on demand and using binary search.

as well as having to have the source file buffer stay around
throughout the compile  ...

Which, as far as I understand, DMD does anyway? If you do not want to keep it around, release it and reload it on demand if an error actually occurs in that buffer. (Time taken to spit out an error message does not need to be optimized that much.)


Reply via email to