Skip to content

Commit

Permalink
Remove cobol_preproc.ml
Browse files Browse the repository at this point in the history
  • Loading branch information
lefessan committed Feb 1, 2024
1 parent 5160068 commit 733155c
Show file tree
Hide file tree
Showing 24 changed files with 109 additions and 141 deletions.
2 changes: 1 addition & 1 deletion src/lsp/cobol_indent/indent_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let indent_range ~dialect ~source_format ~indent_config ~range ~filename ~conten
Cobol_preproc.Src_format.from_config SFFixed
in
let state =
Cobol_preproc.fold_source_lines ~dialect ~source_format
Cobol_preproc.Preprocess.fold_source_lines ~dialect ~source_format
~on_initial_source_format:(fun src_format st -> { st with src_format })
~on_compiler_directive:(fun _ { payload = cd; _} st ->
match cd with
Expand Down
4 changes: 2 additions & 2 deletions src/lsp/cobol_lsp/lsp_document.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let rewindable_parse ({ project; textdoc; _ } as doc) =
recovery = EnableRecovery { silence_benign_recoveries = true };
config = project.config.cobol_config;
} @@
Cobol_preproc.preprocessor
Cobol_preproc.Preprocess.preprocessor
~options:Cobol_preproc.Options.{
default with
libpath = Lsp_project.libpath_for ~uri:(uri doc) project;
Expand Down Expand Up @@ -116,7 +116,7 @@ let reparse_and_analyze ?position ({ copybook; rewinder; textdoc; _ } as doc) =
| Some position, Some rewinder ->
check doc @@
Cobol_parser.rewind_and_parse rewinder ~position @@
Cobol_preproc.reset_preprocessor_for_string @@
Cobol_preproc.Preprocess.reset_preprocessor_for_string @@
Lsp.Text_document.text textdoc

(** Creates a record for a document that is not yet parsed or analyzed. *)
Expand Down
36 changes: 18 additions & 18 deletions src/lsp/cobol_parser/parser_engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ type 'x rewinder =
('x * ('x rewinder)) with_diags;
}
and preprocessor_rewind =
?new_position: Lexing.position -> (Cobol_preproc.preprocessor as 'r) -> 'r
?new_position: Lexing.position -> (Cobol_preproc.Preprocess.preprocessor as 'r) -> 'r
and position =
| Lexing of Lexing.position
| Indexed of { line: int; char: int } (* all starting at 0 *)

type 'm simple_parsing
= ?options:parser_options
-> Cobol_preproc.preprocessor
-> Cobol_preproc.Preprocess.preprocessor
-> (Cobol_ptree.compilation_group option, 'm) output DIAGS.with_diags

type 'm rewindable_parsing
= ?options:parser_options
-> Cobol_preproc.preprocessor
-> Cobol_preproc.Preprocess.preprocessor
-> (((Cobol_ptree.compilation_group option, 'm) output as 'x) *
'x rewinder) DIAGS.with_diags

Expand Down Expand Up @@ -78,7 +78,7 @@ type 'm state =
basis (mostly the pre-processor and tokenizer's states). *)
and 'm preproc =
{
pp: Cobol_preproc.preprocessor; (* also holds diagnostics *)
pp: Cobol_preproc.Preprocess.t; (* also holds diagnostics *)
tokzr: 'm Tokzr.state;
persist: 'm persist;
}
Expand Down Expand Up @@ -130,21 +130,21 @@ and update_tokzr ps tokzr =
else { ps with preproc = { ps.preproc with tokzr } }

let add_diag diag ({ preproc = { pp; _ }; _ } as ps) =
update_pp ps (Cobol_preproc.add_diag pp diag)
update_pp ps (Cobol_preproc.Preprocess.add_diag pp diag)
let add_diags diags ({ preproc = { pp; _ }; _ } as ps) =
update_pp ps (Cobol_preproc.add_diags pp diags)
update_pp ps (Cobol_preproc.Preprocess.add_diags pp diags)

let all_diags { preproc = { pp; tokzr; _ }; _ } =
DIAGS.Set.union (Cobol_preproc.diags pp) @@ Tokzr.diagnostics tokzr
DIAGS.Set.union (Cobol_preproc.Preprocess.diags pp) @@ Tokzr.diagnostics tokzr

(* --- *)

let rec produce_tokens (ps: _ state as 's) : 's * Text_tokenizer.tokens =
let text, pp = Cobol_preproc.next_chunk ps.preproc.pp in
let text, pp = Cobol_preproc.Preprocess.next_chunk ps.preproc.pp in
let { preproc = { pp; tokzr; _ }; _ } as ps = update_pp ps pp in
assert (text <> []);
(* Note: this is the source format in use at the end of the sentence. *)
let source_format = Cobol_preproc.source_format pp in
let source_format = Cobol_preproc.Preprocess.source_format pp in
match Tokzr.tokenize_text ~source_format tokzr text with
| Error `MissingInputs, tokzr ->
produce_tokens (update_tokzr ps tokzr)
Expand Down Expand Up @@ -396,7 +396,7 @@ let on_exn ps e =
let first_stage (ps: 'm state) ~make_checkpoint : ('a, 'm) stage =
let ps, tokens = produce_tokens ps in
let first_pos = match tokens with
| [] -> Cobol_preproc.position ps.preproc.pp
| [] -> Cobol_preproc.Preprocess.position ps.preproc.pp
| t :: _ -> Cobol_common.Srcloc.start_pos ~@t
in
normal ps tokens (make_checkpoint first_pos)
Expand All @@ -419,9 +419,9 @@ let aggregate_output (type m) (ps: m state) res
| Eidetic ->
let artifacts =
{ tokens = Tokzr.parsed_tokens ps.preproc.tokzr;
pplog = Cobol_preproc.rev_log ps.preproc.pp;
rev_comments = Cobol_preproc.rev_comments ps.preproc.pp;
rev_ignored = Cobol_preproc.rev_ignored ps.preproc.pp } in
pplog = Cobol_preproc.Preprocess.rev_log ps.preproc.pp;
rev_comments = Cobol_preproc.Preprocess.rev_comments ps.preproc.pp;
rev_ignored = Cobol_preproc.Preprocess.rev_ignored ps.preproc.pp } in
WithArtifacts (res, artifacts)

(** Simple parsing *)
Expand Down Expand Up @@ -469,7 +469,7 @@ let init_rewindable_parse ps ~make_checkpoint =

(** Stores a stage as part of the memorized rewindable history events. *)
let save_interim_stage (ps, _, env) (store: _ rewindable_history) =
let preproc_position = Cobol_preproc.position ps.preproc.pp in
let preproc_position = Cobol_preproc.Preprocess.position ps.preproc.pp in
match store with
| store'
when preproc_position.pos_cnum <> preproc_position.pos_bol ->
Expand Down Expand Up @@ -516,7 +516,7 @@ let find_history_event_preceding ~position ({ store; _ } as rwps) =
pos
| Indexed { line; char } ->
let ps = rewindable_parser_state rwps in
Cobol_preproc.position_at ~line ~char ps.preproc.pp
Cobol_preproc.Preprocess.position_at ~line ~char ps.preproc.pp
in
let rec aux = function
| [] ->
Expand Down Expand Up @@ -560,7 +560,7 @@ let rec rewind_n_parse

let rewindable_parse
: options:_ -> memory:'m memory -> make_checkpoint:_
-> Cobol_preproc.preprocessor
-> Cobol_preproc.Preprocess.preprocessor
-> ((('a option, 'm) output as 'x) * 'x rewinder) with_diags =
fun ~options ~memory ~make_checkpoint pp ->
let res, rwps =
Expand All @@ -581,7 +581,7 @@ let parse
(type m)
~(memory: m memory)
?(options = Parser_options.default)
: Cobol_preproc.preprocessor ->
: Cobol_preproc.Preprocess.t ->
(Cobol_ptree.compilation_group option, m) output with_diags =
parse_once ~options ~memory
~make_checkpoint:Grammar.Incremental.compilation_group
Expand All @@ -593,7 +593,7 @@ let rewindable_parse
(type m)
~(memory: m memory)
?(options = Parser_options.default)
: Cobol_preproc.preprocessor ->
: Cobol_preproc.Preprocess.t ->
(((Cobol_ptree.compilation_group option, m) output as 'x) * 'x rewinder)
with_diags =
rewindable_parse ~options ~memory
Expand Down
7 changes: 4 additions & 3 deletions src/lsp/cobol_parser/parser_engine.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ open Parser_outputs
(** Simple parsing functions traverse the inputs once to produce a result. *)
type 'm simple_parsing
= ?options:Parser_options.parser_options
-> Cobol_preproc.preprocessor
-> Cobol_preproc.Preprocess.t
-> (Cobol_ptree.compilation_group option, 'm) output
Cobol_common.Diagnostics.with_diags

Expand All @@ -60,7 +60,7 @@ val parse_with_artifacts
{!rewinder} that may then be given to {!rewind_and_parse}. *)
type 'm rewindable_parsing
= ?options:parser_options
-> Cobol_preproc.preprocessor
-> Cobol_preproc.Preprocess.preprocessor
-> (((Cobol_ptree.compilation_group option, 'm) output as 'x) * 'x rewinder)
Cobol_common.Diagnostics.with_diags

Expand All @@ -73,7 +73,8 @@ and 'x rewinder
[new_position] is not given, the text should be read from the very begining
of the input. *)
and preprocessor_rewind =
?new_position:Lexing.position -> (Cobol_preproc.preprocessor as 'r) -> 'r
?new_position:Lexing.position ->
(Cobol_preproc.Preprocess.t as 'r) -> 'r

(* val rewindable_parse *)
(* : memory:'m memory -> 'm rewindable_parsing *)
Expand Down
37 changes: 0 additions & 37 deletions src/lsp/cobol_preproc/cobol_preproc.ml

This file was deleted.

File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/lsp/cobol_preproc/preproc_grammar.mly
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

(* Entry points *)

%start <Preproc_directives.copy_statement
%start <Directives.copy_statement
Cobol_common.Diagnostics.with_diags
Cobol_common.Srcloc.with_loc> copy_statement
%start <Preproc_directives.replace_statement
%start <Directives.replace_statement
Cobol_common.Diagnostics.with_diags
Cobol_common.Srcloc.with_loc> replace_statement

Expand Down Expand Up @@ -104,8 +104,8 @@ let copy_replacing_text_identifier :=
c @ [lpar] @ cl @ [rpar] }

let leading_or_trailing ==
| LEADING; { Preproc_directives.Leading }
| TRAILING; { Preproc_directives.Trailing }
| LEADING; { Directives.Leading }
| TRAILING; { Directives.Trailing }

(* --- REPLACE -------------------------------------------------------------- *)

Expand All @@ -117,7 +117,7 @@ let replace_statement_ :=
{ result = CDirReplace { also; replacing }; diags } }
| REPLACE; last = ibo(LAST); OFF; ".";
{ Cobol_common.Diagnostics.simple_result @@
Preproc_directives.CDirReplaceOff { last } }
Directives.CDirReplaceOff { last } }

(* ISO/IEC 1989:2014 only allows the following clauses in "REPLACE"; however we
allow the same clauses as GnuCOBOL. *)
Expand Down
8 changes: 4 additions & 4 deletions src/lsp/cobol_preproc/preproc_grammar_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ module type S = sig

(* The monolithic API. *)

val replace_statement: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> (Preproc_directives.replace_statement Cobol_common.Srcloc.with_loc)
val replace_statement: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> (Directives.replace_statement Cobol_common.Srcloc.with_loc)

val copy_statement: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> (Preproc_directives.copy_statement Cobol_common.Srcloc.with_loc)
val copy_statement: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> (Directives.copy_statement Cobol_common.Srcloc.with_loc)

val _unused_symbols: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> (unit)

Expand All @@ -50,9 +50,9 @@ module type S = sig

module Incremental : sig

val replace_statement: Lexing.position -> (Preproc_directives.replace_statement Cobol_common.Srcloc.with_loc) MenhirInterpreter.checkpoint
val replace_statement: Lexing.position -> (Directives.replace_statement Cobol_common.Srcloc.with_loc) MenhirInterpreter.checkpoint

val copy_statement: Lexing.position -> (Preproc_directives.copy_statement Cobol_common.Srcloc.with_loc) MenhirInterpreter.checkpoint
val copy_statement: Lexing.position -> (Directives.copy_statement Cobol_common.Srcloc.with_loc) MenhirInterpreter.checkpoint

val _unused_symbols: Lexing.position -> (unit) MenhirInterpreter.checkpoint

Expand Down
4 changes: 2 additions & 2 deletions src/lsp/cobol_preproc/preproc_utils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ open Cobol_common.Diagnostics.TYPES
module Make (Config: Cobol_config.Types.T) : sig

val replacing'
: ?repl_dir:Preproc_directives.replacing_direction
: ?repl_dir:Directives.replacing_direction
-> [< `Alphanum of Text.pseudotext
| `PseudoText of Text.pseudotext ] Cobol_common.Srcloc.with_loc
-> Text.pseudotext Cobol_common.Srcloc.with_loc
-> Preproc_directives.replacing option Cobol_common.Diagnostics.with_diags
-> Directives.replacing option Cobol_common.Diagnostics.with_diags
val filter_map_4_list_with_diags'
: 'a option with_diags with_loc list -> 'a with_loc list with_diags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
open Cobol_common.Srcloc.TYPES
open Cobol_common.Srcloc.INFIX
open Cobol_common.Diagnostics.TYPES
open Preproc_options
open Options

module DIAGS = Cobol_common.Diagnostics

Expand All @@ -25,7 +25,7 @@ type preprocessor =
buff: Text.t;
reader: Src_reader.t;
ppstate: Preproc_state.t;
pplog: Preproc_trace.log;
pplog: Trace.log;
diags: DIAGS.diagnostics;
persist: preprocessor_persist;
}
Expand All @@ -35,7 +35,7 @@ and preprocessor_persist =
{
pparser: (module Text_processor.PPPARSER);
overlay_manager: (module Src_overlay.MANAGER);
replacing: Preproc_directives.replacing with_loc list list;
replacing: Directives.replacing with_loc list list;
copybooks: Cobol_common.Srcloc.copylocs; (* opened copybooks *)
dialect: Cobol_config.Types.dialect;
source_format: Src_format.any option; (* to keep auto-detecting on reset *)
Expand All @@ -44,6 +44,8 @@ and preprocessor_persist =
show_if_verbose: [`Txt | `Src] list;
}

type t = preprocessor

let diags { diags; reader; _ } = DIAGS.Set.union diags @@ Src_reader.diags reader
let add_diag lp d = { lp with diags = DIAGS.Set.cons d lp.diags }
let add_diags lp d = { lp with diags = DIAGS.Set.union d lp.diags }
Expand Down Expand Up @@ -110,7 +112,7 @@ let preprocessor input = function
buff = [];
reader = Src_reader.from input ?source_format;
ppstate = Preproc_state.initial;
pplog = Preproc_trace.empty;
pplog = Trace.empty;
diags = DIAGS.Set.none;
persist =
{
Expand Down Expand Up @@ -182,8 +184,8 @@ let rec next_chunk ({ reader; buff; persist = { dialect; _ }; _ } as lp) =

and apply_compiler_directive
({ reader; pplog; _ } as lp) { payload = compdir; loc } =
let lp = with_pplog lp @@ Preproc_trace.new_compdir ~loc ~compdir pplog in
match (compdir : Preproc_directives.compiler_directive) with
let lp = with_pplog lp @@ Trace.new_compdir ~loc ~compdir pplog in
match (compdir : Directives.compiler_directive) with
| CDirSource sf ->
(match Src_reader.with_source_format sf reader with
| Ok reader -> with_reader lp reader
Expand Down Expand Up @@ -285,7 +287,7 @@ and do_replace lp rev_prefix repl suffix =
replacing phrase. *)
apply_active_replacing_full lp @@ List.rev rev_prefix
in
let lp = with_pplog lp @@ Preproc_trace.new_replace ~loc pplog in
let lp = with_pplog lp @@ Trace.new_replace ~loc pplog in
let lp = match repl, lp.persist.replacing with
| CDirReplace { replacing = repl; _ }, ([] as replacing)
| CDirReplace { replacing = repl; also = false }, replacing ->
Expand All @@ -310,7 +312,7 @@ and read_lib ({ persist = { libpath; copybooks; verbose; _ }; _ } as lp)
(* TODO: `note addendum *)
[],
DIAGS.Acc.error lp.diags ~loc "@[Cyclic@ COPY@ of@ `%s'@]" filename,
Preproc_trace.cyclic_copy ~loc ~filename lp.pplog
Trace.cyclic_copy ~loc ~filename lp.pplog
| Ok filename ->
if verbose then
Pretty.error "Reading library `%s'@." filename;
Expand All @@ -321,12 +323,12 @@ and read_lib ({ persist = { libpath; copybooks; verbose; _ }; _ } as lp)
~postproc:(Cobol_common.Srcloc.copy_from ~filename ~copyloc:loc)
end
in
text, lp.diags, Preproc_trace.copy_done ~loc ~filename lp.pplog
text, lp.diags, Trace.copy_done ~loc ~filename lp.pplog
| Error lnf ->
[],
DIAGS.Acc.error lp.diags ~loc "%a"
Cobol_common.Copybook.pp_lookup_error lnf,
Preproc_trace.missing_copy ~loc ~info:lnf lp.pplog
Trace.missing_copy ~loc ~info:lnf lp.pplog
in
text, with_diags_n_pplog lp diags pplog

Expand Down Expand Up @@ -401,7 +403,7 @@ let reset_preprocessor_for_string string ?new_position pp =

(* --- *)

let preprocessor ?(options = Preproc_options.default) input =
let preprocessor ?(options = Options.default) input =
preprocessor input (`WithOptions options)

(** Default pretty-printing formatter for {!lex_file}, {!lex_lib}, and
Expand Down
Loading

0 comments on commit 733155c

Please sign in to comment.