From f1d23fd977eace4b38a78bf9267c0e2278fce22a Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 29 Apr 2011 17:40:52 -0400 Subject: [PATCH] parser: display colored lines with error messages Lines are located during the lexical analysis then printed on error. A --no-color option is added to remove the line display. Signed-off-by: Jean Privat --- src/location.nit | 4 ++++ src/mmloader.nit | 58 +++++++++++++++++++++++++++++++++++++++++++--- src/parser/lexer.nit | 3 +++ src/parser/xss/lexer.xss | 3 +++ tests/sav/nitc.sav | 1 + tests/sav/nitdoc.sav | 1 + tests/tests.sh | 4 ++-- 7 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/location.nit b/src/location.nit index 65cbbbc..0603050 100644 --- a/src/location.nit +++ b/src/location.nit @@ -30,7 +30,11 @@ class SourceFile do self.filename = filename string = stream.read_all + line_starts[0] = 0 end + + # Position of each line start + var line_starts: Array[Int] = new Array[Int] end # A location inside a source file diff --git a/src/mmloader.nit b/src/mmloader.nit index 5133747..678ecc7 100644 --- a/src/mmloader.nit +++ b/src/mmloader.nit @@ -37,7 +37,8 @@ class Message return location.as(not null) < other.location.as(not null) end - redef fun to_s: String do + redef fun to_s: String + do var l = location if l == null then return text @@ -45,6 +46,50 @@ class Message return "{l}: {text}" end end + + fun to_color_string: String + do + var esc = 27.ascii + var red = "{esc}[0;31m" + var bred = "{esc}[1;31m" + var green = "{esc}[0;32m" + var yellow = "{esc}[0;33m" + var def = "{esc}[0m" + + var l = location + if l == null then + return text + else if l.file == null then + return "{yellow}{l}{def}: {text}" + else + var i = location.line_start + var line_start = l.file.line_starts[i-1] + var line_end = line_start + var string = l.file.string + while line_end+1 < string.length and string[line_end+1] != '\n' and string[line_end+1] != '\r' do + line_end += 1 + end + print "{i-1}: {line_start} && {line_end}" + var lstart = string.substring(line_start, location.column_start - 1) + var cend + if i != location.line_end then + cend = line_end - line_start + 1 + else + cend = location.column_end + end + var lmid = string.substring(line_start + location.column_start - 1, cend - location.column_start + 1) + var lend = string.substring(line_start + cend, line_end - line_start - cend + 1) + var indent = new Buffer + for j in [line_start..line_start+location.column_start-1[ do + if string[j] == '\t' then + indent.add '\t' + else + indent.add ' ' + end + end + return "{yellow}{l}{def}: {text}\n\t{lstart}{bred}{lmid}{def}{lend}\n\t{indent}^" + end + end end # Global context for tools @@ -69,7 +114,11 @@ class ToolContext _message_sorter.sort(_messages) for m in _messages do - stderr.write("{m}\n") + if opt_no_color.value then + stderr.write("{m}\n") + else + stderr.write("{m.to_color_string}\n") + end end _messages.clear @@ -153,13 +202,16 @@ class ToolContext # Option --stop-on-first-error readable var _opt_stop_on_first_error: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error") + # Option --no-color + readable var _opt_no_color: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color") + # Verbose level readable var _verbose_level: Int = 0 init do super - option_context.add_option(opt_warn, opt_stop_on_first_error, opt_path, opt_log, opt_log_dir, opt_only_parse, opt_only_metamodel, opt_help, opt_version, opt_verbose) + option_context.add_option(opt_warn, opt_stop_on_first_error, opt_no_color, opt_path, opt_log, opt_log_dir, opt_only_parse, opt_only_metamodel, opt_help, opt_version, opt_verbose) end # Parse and process the options given on the command line diff --git a/src/parser/lexer.nit b/src/parser/lexer.nit index a6a56e2..5b830d1 100644 --- a/src/parser/lexer.nit +++ b/src/parser/lexer.nit @@ -1282,14 +1282,17 @@ class Lexer if c == 10 then if cr then cr = false + _file.line_starts[line] = sp else line = line + 1 pos = 0 + _file.line_starts[line] = sp end else if c == 13 then line = line + 1 pos = 0 cr = true + _file.line_starts[line] = sp else pos = pos + 1 cr = false diff --git a/src/parser/xss/lexer.xss b/src/parser/xss/lexer.xss index 070295d..1e2dda9 100644 --- a/src/parser/xss/lexer.xss +++ b/src/parser/xss/lexer.xss @@ -104,14 +104,17 @@ $ end foreach if c == 10 then if cr then cr = false + _file.line_starts[line] = sp else line = line + 1 pos = 0 + _file.line_starts[line] = sp end else if c == 13 then line = line + 1 pos = 0 cr = true + _file.line_starts[line] = sp else pos = pos + 1 cr = false diff --git a/tests/sav/nitc.sav b/tests/sav/nitc.sav index 8bbdff9..b8426ea 100644 --- a/tests/sav/nitc.sav +++ b/tests/sav/nitc.sav @@ -1,6 +1,7 @@ usage: nitc [options] file... -W, --warn Show warnings --stop-on-first-error Stop on first error + --no-color Do not use color to display errors and warnings -I, --path Set include path for loaders (may be used more than once) --log Generate various log files --log-dir Directory where to generate log files diff --git a/tests/sav/nitdoc.sav b/tests/sav/nitdoc.sav index a9c25e7..6cc3f82 100644 --- a/tests/sav/nitdoc.sav +++ b/tests/sav/nitdoc.sav @@ -1,6 +1,7 @@ usage: nitdoc [options] file... -W, --warn Show warnings --stop-on-first-error Stop on first error + --no-color Do not use color to display errors and warnings -I, --path Set include path for loaders (may be used more than once) --log Generate various log files --log-dir Directory where to generate log files diff --git a/tests/tests.sh b/tests/tests.sh index 4710af6..02ae121 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -187,9 +187,9 @@ for ii in "$@"; do # Compile if [ "x$verbose" = "xtrue" ]; then echo "" - echo $NITC $OPT -o "$ff.bin" "$i" "$includes" + echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes" fi - $NITC $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log" + $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log" ERR=$? if [ "x$verbose" = "xtrue" ]; then cat "$ff.compile.log" -- 1.7.9.5