I’m working on a DSL compiler and want to provide more friendly information for user.
So I remember that those mature compilers like GCC or GHC all can point out what semantics error happened in which lines, and they even can strip them from generated codes.
The first idea is to add linenumbers in my AST while parsing. But I’m not sure if this is a standard solution, or there’re other better ways to do that ? I doubt it because this solution ask every nodes in the AST to bring extra data, and they may become burden.
Well, as you know, the lexer is who knows the line number and then, for unknown tokens and for syntactic errors you have your line number with no problem. However, for debugging purposes you have to generate debugging info and store it in somewhere (for example, in .Net you have two different files: the compiled file and the debug info file. In Java, debug info is in the same .class file in a special table which maps instructions with code line number). That info is generated by the debug info pass which uses, among others, the AST.
Then, the answer is YES, your AST nodes could have a lineNumber property to be used for the debug info generator/code generator. This is important in both compilers and interpreters.
I´ve did it in this way with good results.