Skip to content

Commit 2367bf4

Browse files
committed
Show error position using ascii art in the cross-compiler.
1 parent ed26abe commit 2367bf4

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

src/compiler/compile.cc

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ static int show_error(std::string msg)
129129
return 1;
130130
}
131131

132+
static char printable(char c)
133+
{
134+
if(c < 32)
135+
return '.';
136+
else if(c < 127)
137+
return c;
138+
else
139+
return '.';
140+
}
141+
132142
compiler::compiler()
133143
{
134144
optimize = true;
@@ -171,18 +181,33 @@ int compiler::compile_file(std::string iname, std::string output_filename,
171181
}
172182
catch(parse_error &e)
173183
{
174-
std::cerr << iname << ":" << ln << ":" << e.pos << ": " << e.what() << "\n";
184+
// Get start/end of current line, removing last EOL
175185
size_t min = 0, max = s.str.length();
176-
if(e.pos > 40)
177-
min = e.pos - 40;
178-
if(e.pos + 40 < max)
179-
max = e.pos + 40;
186+
if(max && s.str[max - 1] == '\n')
187+
max--;
188+
// Adjust error position to be inside the line
189+
if(e.pos > max)
190+
e.pos = max;
191+
// Only show up to 76 characters total
192+
if(max > 76)
193+
{
194+
if(e.pos > 50)
195+
min = e.pos - 50;
196+
if(max - min > 76)
197+
max = min + 76;
198+
}
199+
// Show error position, line and marker
200+
std::cerr << iname << ":" << ln << ":" << e.pos << ": " << e.what() << "\n ";
180201
for(auto i = min; i < e.pos; i++)
181-
std::cerr << s.str[i];
182-
std::cerr << "<--- HERE -->";
202+
std::cerr << printable(s.str[i]);
203+
std::cerr << " ";
183204
for(auto i = e.pos; i < max; i++)
184-
std::cerr << s.str[i];
185-
std::cerr << "\n";
205+
std::cerr << printable(s.str[i]);
206+
std::cerr << "\n ";
207+
for(auto i = min; i < e.pos; i++)
208+
std::cerr << "-";
209+
std::cerr << "^\n";
210+
186211
return 1;
187212
}
188213
}

0 commit comments

Comments
 (0)