It's not practical to write a class that detects compiler errors (that would be your entire compiler). What I think you should be writing is a class that is responsible for formatting errors detected by your compiler. This will give all the errors reported by your compiler a uniform appearance and behavior.
Some things your error class should know:
- The current source file (if it supports includes)
- The current line number in the source file
- The current column number in the source file. This advances as you parse the current line.
- The contents of the current line
- Whether or not the current line has already been printed. i.e. If you detect multiple errors in a line, you don't want to print that line multiple times.
- Whether or not the source listing is enabled.
- Maximum number of errors allowed in a compile
- Count of errors encountered so far
- Depending on the nature of your compiler, you might want to differentiate errors and warnings.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class ErrorStream
{ std::string source_file;
std::string source_line;
int line_number;
int column_number;
bool line_printed;
bool listing_enabled;
int max_errors;
int error_count;
public:
ErrorStream();
void ReportError(int errnum, const std::string & errtext);
};
| |