From: Jean Privat Date: Thu, 18 Jul 2013 08:47:42 +0000 (-0400) Subject: parser: specialize error tokens X-Git-Tag: v0.6~25 X-Git-Url: http://nitlanguage.org parser: specialize error tokens Signed-off-by: Jean Privat --- diff --git a/src/parser/lexer.nit b/src/parser/lexer.nit index 3c975a6..4867685 100644 --- a/src/parser/lexer.nit +++ b/src/parser/lexer.nit @@ -1148,6 +1148,25 @@ redef class AError end end +redef class ALexerError + readable var _string: String + + init init_lexer_error(message: String, loc: Location, string: String) + do + init_error(message, loc) + _string = string + end +end + +redef class AParserError + readable var _token: Token + + init init_parser_error(message: String, loc: Location, token: Token) + do + init_error(message, loc) + _token = token + end +end # The lexer extract NIT tokens from an input stream. # It is better user with the Parser @@ -1583,7 +1602,7 @@ class Lexer var location = new Location(_file, start_line + 1, start_line + 1, start_pos + 1, start_pos + 1) if sp > start_stream_pos then var text = string.substring(start_stream_pos, sp-start_stream_pos) - var token = new AError.init_error("Syntax error: unknown token {text}.", location) + var token = new ALexerError.init_lexer_error("Syntax error: unknown token {text}.", location, text) return token else var token = new EOF(location) diff --git a/src/parser/parser.nit b/src/parser/parser.nit index f709698..30087de 100644 --- a/src/parser/parser.nit +++ b/src/parser/parser.nit @@ -141,7 +141,7 @@ class Parser (new ComputeProdLocationVisitor).enter_visit(node) return node else if action_type == 3 then # ERROR - var node2 = new AError.init_error("Syntax error: unexpected {token}.", token.location) + var node2 = new AParserError.init_parser_error("Syntax error: unexpected {token}.", token.location, token) var node = new Start(null, node2) return node end diff --git a/src/parser/parser_abs.nit b/src/parser/parser_abs.nit index 9e9e7a2..8c41b98 100644 --- a/src/parser/parser_abs.nit +++ b/src/parser/parser_abs.nit @@ -307,6 +307,14 @@ class AError super EOF private init noinit do end end +class ALexerError + super AError +private init noinit do end +end +class AParserError + super AError +private init noinit do end +end class AModule super Prod end class AModuledecl super Prod end diff --git a/src/parser/parser_nodes.nit b/src/parser/parser_nodes.nit index 775c120..17f8558 100644 --- a/src/parser/parser_nodes.nit +++ b/src/parser/parser_nodes.nit @@ -386,6 +386,14 @@ class AError super EOF private init noinit do end end +class ALexerError + super AError +private init noinit do end +end +class AParserError + super AError +private init noinit do end +end class AModule super Prod diff --git a/src/parser/xss/lexer.xss b/src/parser/xss/lexer.xss index e4342ae..577c592 100644 --- a/src/parser/xss/lexer.xss +++ b/src/parser/xss/lexer.xss @@ -191,7 +191,7 @@ $ end foreach var location = new Location(_file, start_line + 1, start_line + 1, start_pos + 1, start_pos + 1) if sp > start_stream_pos then var text = string.substring(start_stream_pos, sp-start_stream_pos) - var token = new PError.init_error("Syntax error: unknown token {text}.", location) + var token = new PLexerError.init_lexer_error("Syntax error: unknown token {text}.", location, text) return token else var token = new EOF(location) diff --git a/src/parser/xss/parser.xss b/src/parser/xss/parser.xss index 45d4590..a461563 100644 --- a/src/parser/xss/parser.xss +++ b/src/parser/xss/parser.xss @@ -153,7 +153,7 @@ class Parser (new ComputeProdLocationVisitor).enter_visit(node) return node else if action_type == 3 then # ERROR - var node2 = new PError.init_error("Syntax error: unexpected {token}.", token.location) + var node2 = new PParserError.init_parser_error("Syntax error: unexpected {token}.", token.location, token) var node = new Start(null, node2) return node end diff --git a/src/parser/xss/tokens.xss b/src/parser/xss/tokens.xss index 7a883f7..e1a4e33 100644 --- a/src/parser/xss/tokens.xss +++ b/src/parser/xss/tokens.xss @@ -31,6 +31,14 @@ class PError super EOF private init noinit do end end +class PLexerError + super PError +private init noinit do end +end +class PParserError + super PError +private init noinit do end +end $ end template $ template make_tokens() @@ -90,4 +98,23 @@ redef class PError end end +redef class PLexerError + readable var _string: String + + init init_lexer_error(message: String, loc: Location, string: String) + do + init_error(message, loc) + _string = string + end +end + +redef class PParserError + readable var _token: Token + + init init_parser_error(message: String, loc: Location, token: Token) + do + init_error(message, loc) + _token = token + end +end $ end template