diff options
author | Daniil Baturin <daniil@baturin.org> | 2019-06-30 02:17:45 +0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2019-06-30 04:03:03 +0700 |
commit | b280bab4edd89733d364e87adfb8a1e7e5f1b302 (patch) | |
tree | bfd9fed1a8e68b55ce514471419002a388ad7509 | |
parent | aa3d09c3fff12e379fd189ceaf55644574ff5c43 (diff) | |
download | vyos1x-config-b280bab4edd89733d364e87adfb8a1e7e5f1b302.tar.gz vyos1x-config-b280bab4edd89733d364e87adfb8a1e7e5f1b302.zip |
T1478: Improved lexing error reporting, including proper support for single quoted strings.
-rw-r--r-- | src/util.ml | 8 | ||||
-rw-r--r-- | src/util.mli | 4 | ||||
-rw-r--r-- | src/vyos1x_lexer.mll | 61 |
3 files changed, 44 insertions, 29 deletions
diff --git a/src/util.ml b/src/util.ml index c2ec4c4..9ce9387 100644 --- a/src/util.ml +++ b/src/util.ml @@ -1,3 +1,11 @@ +exception Syntax_error of ((int * int) option * string) + +let get_lexing_position lexbuf = + let p = Lexing.lexeme_start_p lexbuf in + let line_number = p.Lexing.pos_lnum in + let column = p.Lexing.pos_cnum - p.Lexing.pos_bol + 1 in + (line_number, column) + let default default_value opt = match opt with | None -> default_value diff --git a/src/util.mli b/src/util.mli index 5316e90..119ae01 100644 --- a/src/util.mli +++ b/src/util.mli @@ -1 +1,5 @@ +exception Syntax_error of ((int * int) option * string) + +val get_lexing_position : Lexing.lexbuf -> int * int + val default : 'a -> 'a option -> 'a diff --git a/src/vyos1x_lexer.mll b/src/vyos1x_lexer.mll index a996642..f5d17af 100644 --- a/src/vyos1x_lexer.mll +++ b/src/vyos1x_lexer.mll @@ -1,9 +1,14 @@ { +open Util open Vyos1x_parser exception Error of string +let lexing_error lexbuf msg = + let line, column = Util.get_lexing_position lexbuf in + raise (Syntax_error (Some (line, column), msg)) + (* The language of the VyOS 1.x config file has multiple ambiguities that @@ -57,7 +62,7 @@ rule token = parse | '\n' { Lexing.new_line lexbuf; if !vy_inside_node then (vy_inside_node := false; NEWLINE) else token lexbuf } | '"' - { vy_inside_node := true; read_string (Buffer.create 16) lexbuf } + { vy_inside_node := true; read_double_quoted_string (Buffer.create 16) lexbuf } | ''' { vy_inside_node := true; read_single_quoted_string (Buffer.create 16) lexbuf } | "/*" @@ -72,48 +77,46 @@ rule token = parse { vy_inside_node := true; IDENTIFIER s} | eof { EOF } -| _ -{ raise (Error (Printf.sprintf "At offset %d: unexpected character.\n" (Lexing.lexeme_start lexbuf))) } +| _ as bad_char +{ lexing_error lexbuf (Printf.sprintf "unexpected character \'%c\'" bad_char) } -and read_string buf = +and read_double_quoted_string buf = parse | '"' { STRING (Buffer.contents buf) } - | '\\' '/' { Buffer.add_char buf '/'; read_string buf lexbuf } - | '\\' '\\' { Buffer.add_char buf '\\'; read_string buf lexbuf } - | '\\' 'b' { Buffer.add_char buf '\b'; read_string buf lexbuf } - | '\\' 'f' { Buffer.add_char buf '\012'; read_string buf lexbuf } - | '\\' 'n' { Buffer.add_char buf '\n'; read_string buf lexbuf } - | '\\' 'r' { Buffer.add_char buf '\r'; read_string buf lexbuf } - | '\\' 't' { Buffer.add_char buf '\t'; read_string buf lexbuf } - | '\\' '\'' { Buffer.add_char buf '\''; read_string buf lexbuf } - | '\\' '"' { Buffer.add_char buf '"'; read_string buf lexbuf } - | '\n' { Lexing.new_line lexbuf; Buffer.add_char buf '\n'; read_string buf lexbuf } + | '\\' '/' { Buffer.add_char buf '/'; read_double_quoted_string buf lexbuf } + | '\\' '\\' { Buffer.add_char buf '\\'; read_double_quoted_string buf lexbuf } + | '\\' 'b' { Buffer.add_char buf '\b'; read_double_quoted_string buf lexbuf } + | '\\' 'f' { Buffer.add_char buf '\012'; read_double_quoted_string buf lexbuf } + | '\\' 'n' { Buffer.add_char buf '\n'; read_double_quoted_string buf lexbuf } + | '\\' 'r' { Buffer.add_char buf '\r'; read_double_quoted_string buf lexbuf } + | '\\' 't' { Buffer.add_char buf '\t'; read_double_quoted_string buf lexbuf } + | '\\' '\'' { Buffer.add_char buf '\''; read_double_quoted_string buf lexbuf } + | '\\' '"' { Buffer.add_char buf '"'; read_double_quoted_string buf lexbuf } + | '\n' { Lexing.new_line lexbuf; Buffer.add_char buf '\n'; read_double_quoted_string buf lexbuf } | [^ '"' '\\']+ { Buffer.add_string buf (Lexing.lexeme lexbuf); - read_string buf lexbuf + read_double_quoted_string buf lexbuf } - | _ { raise (Error (Printf.sprintf "Illegal string character: %s" (Lexing.lexeme lexbuf))) } - | eof { raise (Error ("String is not terminated")) } + | eof { lexing_error lexbuf "Quoted string is missing the closing double quote" } and read_single_quoted_string buf = parse | ''' { STRING (Buffer.contents buf) } - | '\\' '/' { Buffer.add_char buf '/'; read_string buf lexbuf } - | '\\' '\\' { Buffer.add_char buf '\\'; read_string buf lexbuf } - | '\\' 'b' { Buffer.add_char buf '\b'; read_string buf lexbuf } - | '\\' 'f' { Buffer.add_char buf '\012'; read_string buf lexbuf } - | '\\' 'n' { Buffer.add_char buf '\n'; read_string buf lexbuf } - | '\\' 'r' { Buffer.add_char buf '\r'; read_string buf lexbuf } - | '\\' 't' { Buffer.add_char buf '\t'; read_string buf lexbuf } - | '\\' '\'' { Buffer.add_char buf '\''; read_string buf lexbuf } - | '\\' '"' { Buffer.add_char buf '"'; read_string buf lexbuf } - | '\n' { Lexing.new_line lexbuf; Buffer.add_char buf '\n'; read_string buf lexbuf } + | '\\' '/' { Buffer.add_char buf '/'; read_single_quoted_string buf lexbuf } + | '\\' '\\' { Buffer.add_char buf '\\'; read_single_quoted_string buf lexbuf } + | '\\' 'b' { Buffer.add_char buf '\b'; read_single_quoted_string buf lexbuf } + | '\\' 'f' { Buffer.add_char buf '\012'; read_single_quoted_string buf lexbuf } + | '\\' 'n' { Buffer.add_char buf '\n'; read_single_quoted_string buf lexbuf } + | '\\' 'r' { Buffer.add_char buf '\r'; read_single_quoted_string buf lexbuf } + | '\\' 't' { Buffer.add_char buf '\t'; read_single_quoted_string buf lexbuf } + | '\\' '\'' { Buffer.add_char buf '\''; read_single_quoted_string buf lexbuf } + | '\\' '"' { Buffer.add_char buf '"'; read_single_quoted_string buf lexbuf } + | '\n' { Lexing.new_line lexbuf; Buffer.add_char buf '\n'; read_single_quoted_string buf lexbuf } | [^ ''' '\\']+ { Buffer.add_string buf (Lexing.lexeme lexbuf); read_single_quoted_string buf lexbuf } - | _ { raise (Error (Printf.sprintf "Illegal string character: %s" (Lexing.lexeme lexbuf))) } - | eof { raise (Error ("String is not terminated")) } + | eof { lexing_error lexbuf "Quoted string is missing the closing single quote" } and read_comment buf = parse |