I was looking at how to better highlight context sensitive errors such as
"Octave must be between 0 and 8", by passing a SourceSpan
to new LogMessage(...) instead of a SourceLocation.
The result of this was that my highlighting routine could do the following to display the entire erroneous token instead of just it's first character:
err.Location is SourceSpan ? ((SourceSpan)err.Location).Length : 1);
The simplest way I found to achieve this was:
- Convert SourceLocation and SourceSpan to classes, sub-classing
SourceSpan from SourceLocation.
- Adding empty and copy constructors to SourceLocation.
- Changing readonly field Location in SourceSpan
to a get-only property.
- Changing line 1 of Parser.ParseTree to initialize loc
Is there a bettwer way to get this result?
Of course, the same result is possible by:
- Renaming struct SourceLocation to ConcreteSourceLocation;
- Defining an interface SourceLocation to be implemented by both ConcreteSourceLocation and
SourceSpan; and finally
- Fighting one's way through all the consequent code changes.
but I didn't want to tackle that unless you felt it worthwhile.