Skip to content

Commit

Permalink
Merge pull request #70 from nberth/fold-lines-with-init-source-format
Browse files Browse the repository at this point in the history
Provide access to auto-detected format in `Cobol_preproc.fold_source_lines`
  • Loading branch information
nberth authored Oct 25, 2023
2 parents cd5c401 + 0ba7015 commit eee6ee9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/lsp/cobol_preproc/preproc_engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,18 @@ let lex_lib ~dialect ~source_format ~libpath ?(ppf = default_oppf) libname =
| Error lnf ->
DIAGS.error_result () "%a" Cobol_common.Copybook.pp_lookup_error lnf

let fold_source_lines ~dialect ~source_format
let fold_source_lines ~dialect ~source_format ?on_initial_source_format
?skip_compiler_directives_text ?on_compiler_directive
~f input acc =
let reader =
Src_reader.from input ?source_format:(source_format_config source_format)
in
let acc = match on_initial_source_format with
| Some f -> f (Src_reader.source_format reader) acc
| None -> acc
in
DIAGS.result @@
Src_reader.fold_lines ~dialect ~f
(Src_reader.from input ?source_format:(source_format_config source_format))
Src_reader.fold_lines ~dialect ~f reader
?skip_compiler_directives_text ?on_compiler_directive acc

let text_of_input ?options input =
Expand Down
14 changes: 9 additions & 5 deletions src/lsp/cobol_preproc/preproc_engine.mli
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ val lex_lib
-> unit Cobol_common.Diagnostics.with_diags

(** [fold_source_lines ~dialect ~source_format ~skip_compiler_directives_text
~on_compiler_directive ~f input acc] applies [f line_number line acc] for
each successive source line [line] of [input]. [line_number] gives the line
number for [line] (starting at [1]). [line] is given empty to [f] if it
corresponds to an empty line in the input, or was a line continuation in the
case of fixed-width reference format.
~on_compiler_directive ~on_initial_source_format ~f input acc] applies [f
line_number line acc] for each successive source line [line] of [input].
[line_number] gives the line number for [line] (starting at [1]). [line] is
given empty to [f] if it corresponds to an empty line in the input, or was a
line continuation in the case of fixed-width reference format.
When given, [on_compiler_directive] is called {e after} [f] has been fed
with the text of a compiler directive, with the same line number.
When given, [on_initial_source_format] is called {e before} either [f] or
[on_compiler_directive] has been called.
When set, [skip_compiler_directives_text] ([false] by default) prevents the
text of compiler directives from being fed to [f]. If given,
[on_compiler_directive] is called as if the text had been fed to [f].
Expand All @@ -82,6 +85,7 @@ val lex_lib
val fold_source_lines
: dialect: Cobol_config.dialect
-> source_format: Cobol_config.source_format_spec
-> ?on_initial_source_format: (Src_format.any -> 'a -> 'a)
-> ?skip_compiler_directives_text: bool
-> ?on_compiler_directive
: (int -> Preproc_directives.compiler_directive with_loc -> 'a -> 'a)
Expand Down

0 comments on commit eee6ee9

Please sign in to comment.