3 ms·
A 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,
by sterlind 7y ago
A 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)