Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions parsing/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,24 @@ let check_label_name ?(raw_escape=false) lexbuf name =

(* Update the current location with file name and line number. *)

let update_loc lexbuf file line absolute chars =
let update_loc_ lexbuf ~absolute ~file ~lines ~chars =
let pos = lexbuf.lex_curr_p in
let new_file = match file with
| None -> pos.pos_fname
| Some s -> s
in
lexbuf.lex_curr_p <- { pos with
pos_fname = new_file;
pos_lnum = if absolute then line else pos.pos_lnum + line;
pos_lnum = if absolute then lines else pos.pos_lnum + lines;
pos_bol = pos.pos_cnum - chars;
}

let set_loc lexbuf ~file ~line =
update_loc_ lexbuf ~absolute:true ~file:(Some file) ~lines:line ~chars:0

let update_loc lexbuf ~lines ~chars =
update_loc_ lexbuf ~absolute:false ~file:None ~lines ~chars

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could define directly the two functions rather than goes through the C-style update_loc_ function.

let update_loc lexbuf ~lines ~chars =
  let pos = lexbuf.lex_curr_p in
  lexbuf.lex_curr_p <- { pos with
    pos_lnum = pos.pos_lnum + lines;
    pos_bol = pos.pos_cnum - chars;
  }

let set_loc lexbuf ~file ~line =
  let pos = lexbuf.lex_curr_p in
  lexbuf.lex_curr_p <- { pos with
    pos_fname = file;
    pos_lnum = line;
    pos_bol = pos.pos_cnum;
  }

let preprocessor = ref None

let escaped_newlines = ref false
Expand Down Expand Up @@ -503,10 +509,10 @@ let raw_ident_escape = "\\#"
rule token = parse
| ('\\' as bs) newline {
if not !escaped_newlines then error lexbuf (Illegal_character bs);
update_loc lexbuf None 1 false 0;
update_loc lexbuf ~lines:1 ~chars:0;
token lexbuf }
| newline
{ update_loc lexbuf None 1 false 0;
{ update_loc lexbuf ~lines:1 ~chars:0;
EOL }
| blank +
{ token lexbuf }
Expand Down Expand Up @@ -593,7 +599,7 @@ rule token = parse
let idloc = compute_quoted_string_idloc orig_loc 3 id in
QUOTED_STRING_ITEM (id, idloc, s, loc, Some delim) }
| "\'" newline "\'"
{ update_loc lexbuf None 1 false 1;
{ update_loc lexbuf ~lines:1 ~chars:1;
(* newline is ('\013'* '\010') *)
CHAR '\n' }
| "\'" ([^ '\\' '\'' '\010' '\013'] as c) "\'"
Expand Down Expand Up @@ -745,7 +751,7 @@ and lex_directive = parse
(* Documentation says that the line number should be
positive, but we have never guarded against this and it
might have useful hackish uses. *)
update_loc lexbuf (Some name) (line_num - 1) true 0;
set_loc lexbuf ~file:name ~line:(line_num - 1);
true
}
| ""
Expand Down Expand Up @@ -811,7 +817,7 @@ and comment = parse
| "\'\'"
{ store_lexeme lexbuf; comment lexbuf }
| "\'" (newline as nl) "\'"
{ update_loc lexbuf None 1 false 1;
{ update_loc lexbuf ~lines:1 ~chars:1;
store_string_char '\'';
store_normalized_newline nl;
store_string_char '\'';
Expand All @@ -836,7 +842,7 @@ and comment = parse
error_loc loc (Unterminated_comment start)
}
| newline as nl
{ update_loc lexbuf None 1 false 0;
{ update_loc lexbuf ~lines:1 ~chars:0;
store_normalized_newline nl;
comment lexbuf
}
Expand All @@ -849,7 +855,7 @@ and string = parse
'\"'
{ lexbuf.lex_start_p }
| '\\' (newline as nl) ([' ' '\t'] * as space)
{ update_loc lexbuf None 1 false (String.length space);
{ update_loc lexbuf ~lines:1 ~chars:(String.length space);
if in_comment () then begin
store_string_char '\\';
store_normalized_newline nl;
Expand Down Expand Up @@ -884,7 +890,7 @@ and string = parse
string lexbuf
}
| newline as nl
{ update_loc lexbuf None 1 false 0;
{ update_loc lexbuf ~lines:1 ~chars:0;
store_normalized_newline nl;
string lexbuf
}
Expand All @@ -897,7 +903,7 @@ and string = parse

and quoted_string delim = parse
| newline as nl
{ update_loc lexbuf None 1 false 0;
{ update_loc lexbuf ~lines:1 ~chars:0;
store_normalized_newline nl;
quoted_string delim lexbuf
}
Expand All @@ -916,9 +922,9 @@ and quoted_string delim = parse

and skip_hash_bang = parse
| "#!" [^ '\n']* '\n' [^ '\n']* "\n!#\n"
{ update_loc lexbuf None 3 false 0 }
{ update_loc lexbuf ~lines:3 ~chars:0 }
| "#!" [^ '\n']* '\n'
{ update_loc lexbuf None 1 false 0 }
{ update_loc lexbuf ~lines:1 ~chars:0 }
| "" { () }

{
Expand Down
Loading