Skip to content
Snippets Groups Projects
Commit 1703b536 authored by Andre Maroneze's avatar Andre Maroneze
Browse files

[Kernel] logic_lexer: refactor and update absolute position to fix pos_cnum

parent b93824e6
No related branches found
No related tags found
No related merge requests found
...@@ -283,7 +283,7 @@ ...@@ -283,7 +283,7 @@
end end
(* Update lexer buffer. *) (* Update lexer buffer. *)
let update_line_loc lexbuf line = let update_line_pos lexbuf line =
let pos = lexbuf.Lexing.lex_curr_p in let pos = lexbuf.Lexing.lex_curr_p in
lexbuf.Lexing.lex_curr_p <- lexbuf.Lexing.lex_curr_p <-
{ pos with { pos with
...@@ -291,10 +291,7 @@ ...@@ -291,10 +291,7 @@
Lexing.pos_bol = pos.Lexing.pos_cnum; Lexing.pos_bol = pos.Lexing.pos_cnum;
} }
let update_newline_loc lexbuf = let update_file_pos lexbuf file =
update_line_loc lexbuf (lexbuf.Lexing.lex_curr_p.Lexing.pos_lnum + 1)
let update_file_loc lexbuf file =
let pos = lexbuf.Lexing.lex_curr_p in let pos = lexbuf.Lexing.lex_curr_p in
lexbuf.Lexing.lex_curr_p <- { pos with Lexing.pos_fname = file } lexbuf.Lexing.lex_curr_p <- { pos with Lexing.pos_fname = file }
...@@ -349,8 +346,8 @@ let utf8_char = ['\128'-'\254']+ ...@@ -349,8 +346,8 @@ let utf8_char = ['\128'-'\254']+
rule token = parse rule token = parse
| space+ { token lexbuf } | space+ { token lexbuf }
| '\n' { update_newline_loc lexbuf; token lexbuf } | '\n' { Lexing.new_line lexbuf; token lexbuf }
| comment_line '\n' { update_newline_loc lexbuf; token lexbuf } | comment_line '\n' { Lexing.new_line lexbuf; token lexbuf }
| comment_line eof { token lexbuf } | comment_line eof { token lexbuf }
| "*/" { lex_error lexbuf "unexpected block-comment closing" } | "*/" { lex_error lexbuf "unexpected block-comment closing" }
| "/*" { if !accept_c_comments_into_acsl_spec | "/*" { if !accept_c_comments_into_acsl_spec
...@@ -502,7 +499,7 @@ and chr buffer = parse ...@@ -502,7 +499,7 @@ and chr buffer = parse
| _ { Buffer.add_string buffer (lexeme lexbuf); chr buffer lexbuf } | _ { Buffer.add_string buffer (lexeme lexbuf); chr buffer lexbuf }
and hash = parse and hash = parse
'\n' { update_newline_loc lexbuf; token lexbuf} '\n' { Lexing.new_line lexbuf; token lexbuf}
| [' ''\t'] { hash lexbuf} | [' ''\t'] { hash lexbuf}
| rD+ { (* We are seeing a line number. This is the number for the | rD+ { (* We are seeing a line number. This is the number for the
* next line *) * next line *)
...@@ -517,14 +514,14 @@ and hash = parse ...@@ -517,14 +514,14 @@ and hash = parse
"Bad line number in preprocessed file: %s" s; "Bad line number in preprocessed file: %s" s;
(-1) (-1)
in in
update_line_loc lexbuf (lineno - 1); update_line_pos lexbuf (lineno - 1);
(* A file name may follow *) (* A file name may follow *)
file lexbuf } file lexbuf }
| "line" { hash lexbuf } (* MSVC line number info *) | "line" { hash lexbuf } (* MSVC line number info *)
| _ { endline lexbuf} | _ { endline lexbuf}
and file = parse and file = parse
'\n' { update_newline_loc lexbuf; token lexbuf} '\n' { Lexing.new_line lexbuf; token lexbuf}
| [' ''\t''\r'] { file lexbuf} | [' ''\t''\r'] { file lexbuf}
| '"' ([^ '\012' '\t' '"']|"\\\"")* '"' { | '"' ([^ '\012' '\t' '"']|"\\\"")* '"' {
let n = Lexing.lexeme lexbuf in let n = Lexing.lexeme lexbuf in
...@@ -532,38 +529,36 @@ and file = parse ...@@ -532,38 +529,36 @@ and file = parse
((String.length n) - 2) in ((String.length n) - 2) in
let unescape = Str.regexp_string "\\\"" in let unescape = Str.regexp_string "\\\"" in
let n1 = Str.global_replace unescape "\"" n1 in let n1 = Str.global_replace unescape "\"" n1 in
update_file_loc lexbuf n1; update_file_pos lexbuf n1;
endline lexbuf endline lexbuf
} }
| _ { endline lexbuf} | _ { endline lexbuf}
and endline = parse and endline = parse
'\n' { update_newline_loc lexbuf; token lexbuf} '\n' { Lexing.new_line lexbuf; token lexbuf}
| eof { EOF } | eof { EOF }
| _ { endline lexbuf} | _ { endline lexbuf}
and comment = parse and comment = parse
'\n' { update_newline_loc lexbuf; comment lexbuf} '\n' { Lexing.new_line lexbuf; comment lexbuf}
| "*/" { token lexbuf} | "*/" { token lexbuf}
| eof { lex_error lexbuf "non-terminating block-comment" } | eof { lex_error lexbuf "non-terminating block-comment" }
| _ { comment lexbuf} | _ { comment lexbuf}
{ {
let set_initial_location dest_lexbuf src_loc = (* When Ocaml 4.11+ becomes mandatory, we can probably replace this with
Lexing.( Lexing.set_position. *)
dest_lexbuf.lex_curr_p <- let set_initial_position dest_lexbuf src_pos =
{ src_loc with dest_lexbuf.Lexing.lex_curr_p <- src_pos;
pos_bol = src_loc.pos_bol - src_loc.pos_cnum; dest_lexbuf.lex_abs_pos <- src_pos.pos_cnum
pos_cnum = 0; };
) let parse_from_position f (pos, s : Filepath.position * string) =
let parse_from_location f (loc, s : Filepath.position * string) =
let finally _ = Logic_utils.exit_kw_c_mode () in let finally _ = Logic_utils.exit_kw_c_mode () in
let output = Kernel.logwith finally ~wkey:Kernel.wkey_annot_error let output = Kernel.logwith finally ~wkey:Kernel.wkey_annot_error
in in
let lb = from_string s in let lb = from_string s in
set_initial_location lb (Cil_datatype.Position.to_lexing_pos loc); set_initial_position lb (Cil_datatype.Position.to_lexing_pos pos);
try try
let res = f token lb in let res = f token lb in
Some (Cil_datatype.Position.of_lexing_pos lb.Lexing.lex_curr_p, res) Some (Cil_datatype.Position.of_lexing_pos lb.Lexing.lex_curr_p, res)
...@@ -584,11 +579,11 @@ and comment = parse ...@@ -584,11 +579,11 @@ and comment = parse
Kernel.fatal ~source:(Cil_datatype.Position.of_lexing_pos lb.lex_curr_p) "Unknown error (%s)" Kernel.fatal ~source:(Cil_datatype.Position.of_lexing_pos lb.lex_curr_p) "Unknown error (%s)"
(Printexc.to_string exn) (Printexc.to_string exn)
let lexpr = parse_from_location Logic_parser.lexpr_eof let lexpr = parse_from_position Logic_parser.lexpr_eof
let annot = parse_from_location Logic_parser.annot let annot = parse_from_position Logic_parser.annot
let spec = parse_from_location Logic_parser.spec let spec = parse_from_position Logic_parser.spec
let ext_spec lexbuf = try let ext_spec lexbuf = try
accept_c_comments_into_acsl_spec := true ; accept_c_comments_into_acsl_spec := true ;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment