-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.cc
executable file
·88 lines (71 loc) · 2.37 KB
/
errors.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* File: errors.cc
* ---------------
* Implementation for error-reporting class.
*/
#include "errors.h"
#include <iostream>
#include <sstream>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
#include "scanner.h" // for GetLineNumbered
int ReportError::numErrors = 0;
void ReportError::UnderlineErrorInLine(const char *line, yyltype *pos) {
if (!line) return;
cerr << line << endl;
for (int i = 1; i <= pos->last_column; i++)
cerr << (i >= pos->first_column ? '^' : ' ');
cerr << endl;
}
void ReportError::OutputError(yyltype *loc, string msg) {
numErrors++;
fflush(stdout); // make sure any buffered text has been output
if (loc) {
cerr << endl << "*** Error line " << loc->first_line << "." << endl;
UnderlineErrorInLine(GetLineNumbered(loc->first_line), loc);
} else
cerr << endl << "*** Error." << endl;
cerr << "*** " << msg << endl << endl;
}
void ReportError::Formatted(yyltype *loc, const char *format, ...) {
va_list args;
char errbuf[2048];
va_start(args, format);
vsprintf(errbuf,format, args);
va_end(args);
OutputError(loc, errbuf);
}
void ReportError::UntermComment() {
OutputError(NULL, "Input ends with unterminated comment");
}
void ReportError::InvalidDirective(int linenum) {
yyltype ll = {0, linenum, 0, 0};
OutputError(&ll, "Invalid # directive");
}
void ReportError::LongIdentifier(yyltype *loc, const char *ident) {
stringstream s;
s << "Identifier too long: \"" << ident << "\"" << '\0';
OutputError(loc, s.str());
}
void ReportError::UntermString(yyltype *loc, const char *str) {
stringstream s;
s << "Unterminated string constant: " << str << '\0';
OutputError(loc, s.str());
}
void ReportError::UnrecogChar(yyltype *loc, char ch) {
stringstream s;
s << "Unrecognized char: '" << ch << "'" << '\0';
OutputError(loc, s.str());
}
/* Function: yyerror()
* -------------------
* Standard error-reporting function expected by yacc. Our version merely
* just calls into the error reporter above, passing the location of
* the last token read. If you want to suppress the ordinary "parse error"
* message from yacc, you can implement yyerror to do nothing and
* then call ReportError::Formatted yourself with a more descriptive
* message.
*/
void yyerror(char *msg) {
ReportError::Formatted(&yylloc, "%s", msg);
}