Skip to content

Commit

Permalink
Avoid folding over source lines that only denote the end-of-input/file
Browse files Browse the repository at this point in the history
  • Loading branch information
nberth committed Oct 6, 2023
1 parent 4864506 commit f3585eb
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 355 deletions.
4 changes: 3 additions & 1 deletion src/lsp/cobol_preproc/preproc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ let fold_source_lines pl ~f acc =
spit_chunk suffix (acc, new_lnum, [])
in
let acc, last_lnum, tail = fold_source_chunks pl spit_chunk (acc, 1, []) in
f last_lnum tail acc (* fold on the last line upon exit *)
match tail with (* fold on the last line upon exit... *)
| [] | { payload = Eof; _ } :: _ -> acc (* ... if non-empty *)
| _ -> f last_lnum tail acc

(* --- *)

Expand Down
16 changes: 8 additions & 8 deletions src/lsp/cobol_preproc/src_lexing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,15 @@ let newline_cnums { newline_cnums; _ } = List.rev newline_cnums
let source_format { config = { source_format; _ }; _ } = source_format
let allow_debug { config = { debug; _ }; _ } = debug

(* Just check there is no text that requires continuation. *)
let flushed = function
| { continued = CNone; pseudotext = None; _ } -> true
| _ -> false

(** 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,
or upon end-of-input/file.
*)
or the last token is a lonesome period. *)
let flush ({ lex_prods; _ } as state) : _ state * text =
match lex_prods with
| h :: prods when not state.cdir_seen && ~&h <> Eof ->
| { payload = TextWord w; _ } as h :: prods
when not state.cdir_seen && w <> "." ->
{ state with lex_prods = [h] }, List.rev prods
| prods ->
{ state with lex_prods = []; cdir_seen = false }, List.rev prods
Expand All @@ -110,6 +105,11 @@ let lex_diag ~severity state =
let lex_error state = lex_diag ~severity:DIAGS.Error state

let change_source_format ({ config; _ } as state) { payload = sf; loc } =
(* Just check there is no text that requires continuation. *)
let flushed = function
| { continued = CNone; pseudotext = None; _ } -> true
| _ -> false
in
if flushed state
then Ok { state with config = { config with source_format = sf } }
else Error (lex_error state ~loc "Forbidden@ change@ of@ source@ format")
Expand Down
Loading

0 comments on commit f3585eb

Please sign in to comment.