From: Jean Privat Date: Fri, 18 Oct 2013 17:14:03 +0000 (-0400) Subject: nitcc: lexer do not crash on empty input stream X-Git-Tag: v0.6.2~1^2~50 X-Git-Url: http://nitlanguage.org nitcc: lexer do not crash on empty input stream Signed-off-by: Jean Privat --- diff --git a/contrib/nitcc/tests/sav/empty.input2.res b/contrib/nitcc/tests/sav/empty.input2.res index f22973a..b0ab3c5 100644 --- a/contrib/nitcc/tests/sav/empty.input2.res +++ b/contrib/nitcc/tests/sav/empty.input2.res @@ -1,2 +1,2 @@ -NLexerError@(1:1-1:1)='' +NParserError@(1:1-1:1)='' Nodes diff --git a/lib/nitcc_runtime.nit b/lib/nitcc_runtime.nit index ce591b9..20342e6 100644 --- a/lib/nitcc_runtime.nit +++ b/lib/nitcc_runtime.nit @@ -192,20 +192,22 @@ abstract class Lexer end var next = state.trans(c) if next == null then - if last_state == null then - var token = new NLexerError - var position = new Position(pos_start, pos, line_start, line, col_start, col) - token.position = position - token.text = text.substring(pos_start, pos-pos_start+1) - res.add token - break + if pos_start < length then + if last_state == null then + var token = new NLexerError + var position = new Position(pos_start, pos, line_start, line, col_start, col) + token.position = position + token.text = text.substring(pos_start, pos-pos_start+1) + res.add token + break + end + var position = new Position(pos_start, pos_end, line_start, line_end, col_start, col_end) + var token = last_state.make_token(position, text.substring(pos_start, pos_end-pos_start+1)) + if token != null then res.add(token) end - var position = new Position(pos_start, pos_end, line_start, line_end, col_start, col_end) - var token = last_state.make_token(position, text.substring(pos_start, pos_end-pos_start+1)) - if token != null then res.add(token) if pos >= length then - token = new NEof - position = new Position(pos, pos, line, line, col, col) + var token = new NEof + var position = new Position(pos, pos, line, line, col, col) token.position = position token.text = "" res.add token