3 ms·
How does this approach play with error handling? If there is an error in my code and it's only caught at step 10, won't the error message be completely inscruta
by cosarara 7y ago
How does this approach play with error handling? If there is an error in my code and it's only caught at step 10, won't the error message be completely inscrutable? Will the line numbers make sense?
- mojuba 7y agoLine number information is usually passed down the chain because it's required for generating debug info anyway. However, as you descend into deeper optimization layers of the compiler, errors are less and less likely to occur. Optimizations are either possible or not, though sometimes these layers can warn you about possible inefficiencies at the source code level (unused local, etc.)
- sterlind 7y agoA simple approach could be to mark up spans of characters in layer N with what spans were used to generate them in layer N-1. Then when parser N hits an error, put red squiggly lines under the source characters responsible. For instance, in C#: using (<span decl>var x = 3</span>) { <span inner>Console.WriteLine(x);</span>} Parser 0 transforms the using: <span stmt1 parent=decl>var x = 3;</span> try { <span stmt2 parent=inner> Console.WriteLine(x);</span> } finally { <span stmt3=generated>x.Dispose();</span> } Parser 1 (or a later parser, inductively) notices x doesn't have Dispose() defined, and throws MissingMethod(Dispose, span=generated). Parser 0 catches this error, knows that this is because x isn't disosable, and emits its own error: NotDisposable(span=decl). If a parser doesn't know to catch the error, it still passes it up the chain, transforming spans where appropriate. When it hits the IDE, the error is printed and the red squiggly lines go where they should. (Disclaimer: I've never written a compiler)
- thedufer 7y agoIn the only compiler I'm somewhat familiar with (OCaml), a decent amount of the work is done in AST->AST transformations. In the AST, every node is annotated with a location, which is a tuple of filename, line number, char start, char end. As long as these are propagated in the right way, you get good location information in errors.
- deleted 7y ago[deleted]