diff --git a/src/dune_lexer.mli b/src/dune_lexer.mli index eac90852..7b3c5eaf 100644 --- a/src/dune_lexer.mli +++ b/src/dune_lexer.mli @@ -6,5 +6,9 @@ type first_line = ; 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 + +(** 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 diff --git a/src/dune_lexer.mll b/src/dune_lexer.mll index 3f26c611..b7ac50b8 100644 --- a/src/dune_lexer.mll +++ b/src/dune_lexer.mll @@ -23,18 +23,16 @@ rule is_script = parse | "(* -*- tuareg -*- *)" { true } | "" { false } -and first_line = parse +and maybe_first_line = parse | '(' blank* "lang" { let start = Lexing.lexeme_start_p lexbuf in let lang = atom start lexbuf in let version = atom start lexbuf in first_line_end start lexbuf; - { lang; version } + Some { lang; version } } | "" - { let start = Lexing.lexeme_start_p lexbuf in - to_eol lexbuf; - invalid_lang_line start lexbuf + { None } and atom start = parse @@ -62,3 +60,13 @@ and to_eol = parse | [^'\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 +}