diff options
| author | Alasdair | 2020-08-13 11:27:31 +0100 |
|---|---|---|
| committer | Alasdair | 2020-08-13 15:47:21 +0100 |
| commit | 40625c16f7573398252ccf040ef49d398d64d5bd (patch) | |
| tree | 378aba617a045c866c53d8938ce13f92562e06cf /src/initial_check.ml | |
| parent | 2736af39811331502c7f5bc7e2bd8f590f1f9b2a (diff) | |
Preserve file structure through initial check
Insert $file_start and $file_end pragmas in the AST, as well as
$include_start and $include_end pragmas so we can reconstruct the
original file structure later if needed, provided nothing like
topological sorting has been done.
Have the Lexer produce a list of comments whenever it parses a file,
which can the be attached to the nearest nodes in the abstract syntax
tree.
Diffstat (limited to 'src/initial_check.ml')
| -rw-r--r-- | src/initial_check.ml | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/initial_check.ml b/src/initial_check.ml index f808af49..828d1b5e 100644 --- a/src/initial_check.ml +++ b/src/initial_check.ml @@ -828,12 +828,25 @@ let rec remove_mutrec = function | def :: defs -> def :: remove_mutrec defs -let to_ast ctx (P.Defs(defs)) = - let defs = remove_mutrec defs in +let to_ast ctx (P.Defs files) = + let to_ast_defs ctx (_, defs) = + let defs = remove_mutrec defs in + let defs, ctx = + List.fold_left (fun (defs, ctx) def -> let def, ctx = to_ast_def ctx def in (def @ defs, ctx)) ([], ctx) defs + in + List.rev defs, ctx + in + let wrap_file file defs = + [DEF_pragma ("file_start", file, P.Unknown)] + @ defs + @ [DEF_pragma ("file_end", file, P.Unknown)] + in let defs, ctx = - List.fold_left (fun (defs, ctx) def -> let def, ctx = to_ast_def ctx def in (def @ defs, ctx)) ([], ctx) defs + List.fold_left (fun (defs, ctx) file -> + let defs', ctx = to_ast_defs ctx file in (defs @ wrap_file (fst file) defs', ctx) + ) ([], ctx) files in - Defs (List.rev defs), ctx + Defs defs, ctx let initial_ctx = { type_constructors = @@ -1107,8 +1120,8 @@ let process_ast ?generate:(generate=true) defs = let ast_of_def_string_with f str = let def = Parser.def_eof Lexer.token (Lexing.from_string str) in - process_ast (f (P.Defs [def])) + process_ast (P.Defs [("", f [def])]) let ast_of_def_string str = let def = Parser.def_eof Lexer.token (Lexing.from_string str) in - process_ast (P.Defs [def]) + process_ast (P.Defs [("", [def])]) |
