Add function to parse version header from sexp files

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Jérémie Dimino 2018-07-01 01:34:33 +07:00 committed by Rudi Grinberg
parent d7796156fd
commit e706421893
2 changed files with 18 additions and 6 deletions

View File

@ -6,5 +6,9 @@ type first_line =
; version : Loc.t * string ; version : Loc.t * string
} }
(** Parse the first line of a dune-project file. *) (** Parse the first line of a versioned file. *)
val first_line : Lexing.lexbuf -> first_line val first_line : Lexing.lexbuf -> first_line
(** Parse the first line of a versioned file but do not fail if it
doesn't start with [(lang ...)]. *)
val maybe_first_line : Lexing.lexbuf -> first_line option

View File

@ -23,18 +23,16 @@ rule is_script = parse
| "(* -*- tuareg -*- *)" { true } | "(* -*- tuareg -*- *)" { true }
| "" { false } | "" { false }
and first_line = parse and maybe_first_line = parse
| '(' blank* "lang" | '(' blank* "lang"
{ let start = Lexing.lexeme_start_p lexbuf in { let start = Lexing.lexeme_start_p lexbuf in
let lang = atom start lexbuf in let lang = atom start lexbuf in
let version = atom start lexbuf in let version = atom start lexbuf in
first_line_end start lexbuf; first_line_end start lexbuf;
{ lang; version } Some { lang; version }
} }
| "" | ""
{ let start = Lexing.lexeme_start_p lexbuf in { None
to_eol lexbuf;
invalid_lang_line start lexbuf
} }
and atom start = parse and atom start = parse
@ -62,3 +60,13 @@ and to_eol = parse
| [^'\r' '\n']* | [^'\r' '\n']*
{ () { ()
} }
{
let first_line lb =
match maybe_first_line lb with
| Some x -> x
| None ->
let start = Lexing.lexeme_start_p lb in
to_eol lb;
invalid_lang_line start lb
}