diff --git a/src/lsp/cobol_preproc/preproc_engine.ml b/src/lsp/cobol_preproc/preproc_engine.ml index 42165fe73..29995e756 100644 --- a/src/lsp/cobol_preproc/preproc_engine.ml +++ b/src/lsp/cobol_preproc/preproc_engine.ml @@ -188,7 +188,7 @@ let rec next_chunk ({ srclex; buff; _ } as lp) = | Error `NotCDir -> preprocess_line { lp with srclex; buff = [] } (buff @ text) | Ok ([], lexdir_text) -> - next_chunk @@ on_lexing_directive lp lexdir_text + next_chunk @@ on_lexing_directive { lp with srclex } lexdir_text | Ok (text, lexdir_text) -> let lp = { lp with srclex; buff = [] } in preprocess_line (on_lexing_directive lp lexdir_text) (buff @ text) @@ -228,7 +228,7 @@ and preprocess_line lp srctext = be a compiler directive. *) preprocess_line lp srctext | Ok (`ReplaceDone (lp, text, srctext)) -> - text, with_buff lp srctext + text, with_buff lp @@ Text.strip_eof srctext | Error (`MissingPeriod | `MissingText) -> next_chunk (with_buff lp srctext) diff --git a/src/lsp/cobol_preproc/src_lexing.ml b/src/lsp/cobol_preproc/src_lexing.ml index 235807d9a..d55d16d29 100644 --- a/src/lsp/cobol_preproc/src_lexing.ml +++ b/src/lsp/cobol_preproc/src_lexing.ml @@ -90,11 +90,12 @@ let flushed = function (** Flush buffered lexing productions, possibly holding onto one that may be subject to continuation on the following line. - Always flushes completely whenever a compiler-directive word has been seen. + Always flushes completely whenever a compiler-directive word has been seen, + or upon end-of-input/file. *) let flush ({ lex_prods; _ } as state) : _ state * text = match lex_prods with - | h :: prods when not state.cdir_seen -> + | h :: prods when not state.cdir_seen && ~&h <> Eof -> { state with lex_prods = [h] }, List.rev prods | prods -> { state with lex_prods = []; cdir_seen = false }, List.rev prods diff --git a/src/lsp/cobol_preproc/text.ml b/src/lsp/cobol_preproc/text.ml index cb1ca1944..fecbff6b4 100644 --- a/src/lsp/cobol_preproc/text.ml +++ b/src/lsp/cobol_preproc/text.ml @@ -132,3 +132,8 @@ let pseudotext_of_string = pseudotext_ pseudo_string let pseudotext_of_alphanum = pseudotext_ pseudo_alphanum let pseudotext_of_integer = pseudotext_ pseudo_integer let alphanum_as_pseudotext a = pseudotext_ alphanum_as_pseudo a + +(** Strips any end-of-input/file item from the beginning of the given text. *) +let rec strip_eof = function + | { payload = Eof; _ } :: text -> strip_eof text + | text -> text diff --git a/src/lsp/cobol_preproc/text.mli b/src/lsp/cobol_preproc/text.mli index a7cbcc732..2ff0905e5 100644 --- a/src/lsp/cobol_preproc/text.mli +++ b/src/lsp/cobol_preproc/text.mli @@ -61,3 +61,4 @@ val pp_alphanum: alphanum Pretty.printer val prefix_of_literal_kind: literal_kind -> string val char_of_quotation: quotation -> char +val strip_eof: text -> text