Commit 85b19a53 authored by POTTIER Francois's avatar POTTIER Francois
Browse files

Added [Segment.segment].

parent 41a5fc79
(* This lexer is used to cut an input into segments, delimited by a
blank line. (More precisely, by a run of at least one blank lines
and zero or more comment lines.) It produces a list of segments,
where each segment is a pair of positions. *)
where each segment is represented as a pair of positions. It is
stand-alone and cannot fail. *)
{
......@@ -63,3 +64,29 @@ and busy segments opening just_saw_a_newline = parse
| _
{ busy segments opening false lexbuf }
{
(* This wrapper function reads a file, cuts it into segments, and
creates a fresh lexbuf for each segment, taking care to adjust
its start position. *)
let segment filename : (string * lexbuf) list =
let content = IO.read_whole_file filename in
let lexbuf = from_string content in
lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename };
let segments = idle [] lexbuf in
List.map (fun (startp, endp) ->
let start = startp.pos_cnum in
let length = endp.pos_cnum - start in
let content = String.sub content start length in
let lexbuf = from_string content in
lexbuf.lex_start_p <- startp;
lexbuf.lex_curr_p <- startp;
lexbuf.lex_abs_pos <- startp.pos_cnum;
(* That was tricky to find out. See [Lexing.engine]. [pos_cnum] is
updated based on [buf.lex_abs_pos + buf.lex_curr_pos]. *)
content, lexbuf
) segments
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment