diff --git a/bin/print.ml b/bin/print.ml index 0f2bf3b..c889b85 100644 --- a/bin/print.ml +++ b/bin/print.ml @@ -15,7 +15,7 @@ let get_files_with_ext ~ext dir = loop [] [ dir ] ;; -let get_declarations_from ~directory () = +let get_sources_from ~directory () = directory |> get_files_with_ext ~ext:".pi" |> List.map Source.of_file ;; @@ -24,10 +24,11 @@ let main = let directory = Sys.argv.(1) in let root = Sys.argv.(2) in - let declarations = get_declarations_from ~directory () in + let sources = get_sources_from ~directory () in try - declarations - |> Interpreter.eval_sources ~root ~tag_data_provider:Helpers.noop_data_provider + sources + |> Parser.get_ast + |> Interpreter.eval_declarations ~root ~tag_data_provider:Helpers.noop_data_provider |> fst |> print_endline with Diagnostics.Pinc_error -> exit 1 diff --git a/bin/print_debug.ml b/bin/print_debug.ml index 537bd34..771e097 100644 --- a/bin/print_debug.ml +++ b/bin/print_debug.ml @@ -8,7 +8,8 @@ let main = let end_time_parser = Unix.gettimeofday () in let result = [ src ] - |> Interpreter.eval_sources ~tag_data_provider:Helpers.noop_data_provider ~root + |> Parser.get_ast + |> Interpreter.eval_declarations ~tag_data_provider:Helpers.noop_data_provider ~root in let end_time = Unix.gettimeofday () in Printf.printf "Lexer & Parser: %fms\n" ((end_time_parser -. start_time) *. 1000.); diff --git a/lib/02_parsing/Ast.ml b/lib/00_types/Ast.ml similarity index 100% rename from lib/02_parsing/Ast.ml rename to lib/00_types/Ast.ml diff --git a/lib/02_parsing/Operators.ml b/lib/00_types/Operators.ml similarity index 91% rename from lib/02_parsing/Operators.ml rename to lib/00_types/Operators.ml index 316293f..9678361 100644 --- a/lib/02_parsing/Operators.ml +++ b/lib/00_types/Operators.ml @@ -49,12 +49,6 @@ module Binary = struct | _ -> Assoc_Left ;; - let get_closing_token = function - | FUNCTION_CALL -> Some Token.RIGHT_PAREN - | BRACKET_ACCESS -> Some Token.RIGHT_BRACK - | _ -> None - ;; - let to_string = function | DOT_ACCESS -> "." | POW -> "**" diff --git a/lib/00_types/dune b/lib/00_types/dune new file mode 100644 index 0000000..f2f28a3 --- /dev/null +++ b/lib/00_types/dune @@ -0,0 +1,5 @@ +(library + (name Pinc_Types) + (public_name pinc-lang.types) + (flags :standard -open Pinc_Core) + (libraries Pinc_Core Pinc_Source Pinc_Diagnostics)) diff --git a/lib/02_parsing/Parser.ml b/lib/02_parsing/Parser.ml index 1818f40..5704032 100644 --- a/lib/02_parsing/Parser.ml +++ b/lib/02_parsing/Parser.ml @@ -1,5 +1,6 @@ module Parsetree = Parsetree module Diagnostics = Pinc_Diagnostics +module Operators = Pinc_Types.Operators open Diagnostics type t = { @@ -826,7 +827,12 @@ module Rules = struct | Some right -> Parsetree.P_BinaryExpression (left, operator, right)) in let expect_close token = expect token t in - Operators.Binary.get_closing_token operator |> Option.iter expect_close; + let () = + match operator with + | Operators.Binary.FUNCTION_CALL -> expect_close Token.RIGHT_PAREN + | Operators.Binary.BRACKET_ACCESS -> expect_close Token.RIGHT_BRACK + | _ -> () + in let expression_end = t.token.location in let expression_loc = Location.merge ~s:expression_start ~e:expression_end () diff --git a/lib/02_parsing/Parsetree.ml b/lib/02_parsing/Parsetree.ml index 7782b25..247efd5 100644 --- a/lib/02_parsing/Parsetree.ml +++ b/lib/02_parsing/Parsetree.ml @@ -1,4 +1,4 @@ -module Operators = Operators +module Operators = Pinc_Types.Operators type annotation = | P_Comment_Annotation of string diff --git a/lib/02_parsing/Pinc_Parser.ml b/lib/02_parsing/Pinc_Parser.ml index c3bb824..43f15fa 100644 --- a/lib/02_parsing/Pinc_Parser.ml +++ b/lib/02_parsing/Pinc_Parser.ml @@ -1,6 +1,5 @@ module Parsetree = Parsetree module Transformer = Transformer -module Ast = Ast type t = Parser.t type parsetree = Parsetree.t diff --git a/lib/02_parsing/Pinc_Parser.mli b/lib/02_parsing/Pinc_Parser.mli index f8532c4..9882a64 100644 --- a/lib/02_parsing/Pinc_Parser.mli +++ b/lib/02_parsing/Pinc_Parser.mli @@ -1,11 +1,10 @@ module Parsetree = Parsetree module Transformer = Transformer -module Ast = Ast type t type parsetree = Parsetree.t type public_tag = Transformer.public_tag val parse : ?include_stdlib:bool -> Pinc_Source.t list -> Parsetree.t -val get_ast : ?include_stdlib:bool -> Pinc_Source.t list -> Ast.t +val get_ast : ?include_stdlib:bool -> Pinc_Source.t list -> Pinc_Types.Ast.t val all_tags : ?include_stdlib:bool -> Pinc_Source.t list -> string -> public_tag list diff --git a/lib/02_parsing/Transformer.ml b/lib/02_parsing/Transformer.ml index 654194f..a90058e 100644 --- a/lib/02_parsing/Transformer.ml +++ b/lib/02_parsing/Transformer.ml @@ -1,4 +1,4 @@ -open Ast +open Pinc_Types.Ast type primitive_value = [ `String of string @@ -559,7 +559,7 @@ and transform_declarations env declarations = |> StringMap.fold_map ~init:env ~f:transform_declaration ;; -let transform (declarations : Parsetree.t) : Ast.t = +let transform (declarations : Parsetree.t) : Pinc_Types.Ast.t = let env = Env.empty in let _env, ast = transform_declarations env declarations in ast diff --git a/lib/02_parsing/Transformer.mli b/lib/02_parsing/Transformer.mli index 8fca6ca..28d3bf8 100644 --- a/lib/02_parsing/Transformer.mli +++ b/lib/02_parsing/Transformer.mli @@ -24,5 +24,5 @@ type public_tag = { public_tag_initial_value : primitive_value option; } -val transform : Parsetree.t -> Ast.t +val transform : Parsetree.t -> Pinc_Types.Ast.t val all_tags : string -> Parsetree.t -> public_tag list diff --git a/lib/02_parsing/dune b/lib/02_parsing/dune index b089fbd..40939df 100644 --- a/lib/02_parsing/dune +++ b/lib/02_parsing/dune @@ -2,7 +2,7 @@ (name Pinc_Parser) (public_name pinc-lang.parser) (flags :standard -open Pinc_Core) - (libraries Pinc_Core Pinc_Source Pinc_Diagnostics)) + (libraries Pinc_Core Pinc_Types Pinc_Source Pinc_Diagnostics)) (rule (target pinc_stdlib.ml) diff --git a/lib/Pinc_lang.ml b/lib/Pinc_lang.ml index 20df97c..17c96f5 100644 --- a/lib/Pinc_lang.ml +++ b/lib/Pinc_lang.ml @@ -1,6 +1,6 @@ module Diagnostics = Pinc_Diagnostics module Source = Pinc_Source -module Ast = Pinc_Parser.Ast +module Ast = Pinc_Types.Ast module Parser = Pinc_Parser module Formatter = Pinc_Format diff --git a/lib/dune b/lib/dune index dea83ee..3985004 100644 --- a/lib/dune +++ b/lib/dune @@ -3,6 +3,7 @@ (public_name pinc-lang) (libraries Pinc_Core + Pinc_Types Pinc_Source Pinc_Diagnostics Pinc_Parser diff --git a/lib/pinc_backend/DeclarationEvaluator.ml b/lib/pinc_backend/DeclarationEvaluator.ml index 3ed072c..fb2449d 100644 --- a/lib/pinc_backend/DeclarationEvaluator.ml +++ b/lib/pinc_backend/DeclarationEvaluator.ml @@ -1,7 +1,6 @@ let eval ~eval_expression ~state declaration = state.Types.Type_State.declarations |> StringMap.find_opt declaration |> function - | Some { Pinc_Parser.Ast.declaration_body; _ } -> - eval_expression ~state declaration_body + | Some { Pinc_Types.Ast.declaration_body; _ } -> eval_expression ~state declaration_body | None -> Pinc_Diagnostics.raise_error Pinc_Diagnostics.Location.none diff --git a/lib/pinc_backend/Helpers.ml b/lib/pinc_backend/Helpers.ml index 5729a84..66e7483 100644 --- a/lib/pinc_backend/Helpers.ml +++ b/lib/pinc_backend/Helpers.ml @@ -153,7 +153,7 @@ module Expect = struct let declarations = StringMap.fold (fun id decl acc -> - match (typ, decl.Pinc_Parser.Ast.declaration_kind) with + match (typ, decl.Pinc_Types.Ast.declaration_kind) with | (`All | `Component), Declaration_Component -> id :: acc | (`All | `Library), Declaration_Library -> id :: acc | (`All | `Page), Declaration_Page -> id :: acc diff --git a/lib/pinc_backend/Interpreter.ml b/lib/pinc_backend/Interpreter.ml index 6fd1076..ad67658 100644 --- a/lib/pinc_backend/Interpreter.ml +++ b/lib/pinc_backend/Interpreter.ml @@ -1,8 +1,7 @@ open Types module Diagnostics = Pinc_Diagnostics module Types = Types -module Ast = Pinc_Parser.Ast -module Parser = Pinc_Parser +module Ast = Pinc_Types.Ast module Location = Diagnostics.Location module Source = Pinc_Source @@ -1278,8 +1277,7 @@ and eval_template ~state template = } ;; -let eval_meta sources = - let declarations = Parser.get_ast sources in +let eval_meta declarations = let state = State.make ~mode:`Portal_Collection @@ -1353,9 +1351,3 @@ let eval_declarations (html, meta_tree) ;; - -let eval_sources ?tag_meta_provider ~tag_data_provider ~root sources = - sources - |> Parser.get_ast - |> eval_declarations ?tag_meta_provider ~tag_data_provider ~root -;; diff --git a/lib/pinc_backend/Interpreter.mli b/lib/pinc_backend/Interpreter.mli index c000e19..b6aa3fc 100644 --- a/lib/pinc_backend/Interpreter.mli +++ b/lib/pinc_backend/Interpreter.mli @@ -1,10 +1,10 @@ module Diagnostics = Pinc_Diagnostics module Types = Types -module Ast = Pinc_Parser.Ast +module Ast = Pinc_Types.Ast open Types val eval_meta : - Pinc_Source.t list -> + Ast.t -> [> `Component of Diagnostics.Location.t * value StringMap.t | `Library of Diagnostics.Location.t * value StringMap.t | `Page of Diagnostics.Location.t * value StringMap.t @@ -12,17 +12,6 @@ val eval_meta : ] StringMap.t -(** [eval_sources ?tag_meta_provider ~tag_data_provider ~root sources] evaluates - definition {!root} found in {!sources}, getting its data from the - {!tag_data_provider}. - @raise Invalid_argument if the given root can't be evaluated (store, library). *) -val eval_sources : - ?tag_meta_provider:Types.Type_Tag.meta_provider -> - tag_data_provider:Types.Type_Tag.data_provider -> - root:string -> - Pinc_Source.t list -> - string * (string * Types.Type_Tag.meta) list - (** [eval ?tag_meta_provider ~tag_data_provider ~root sources] evaluates definition {!root} found in {!sources}, getting its data from the {!tag_data_provider}. @raise Invalid_argument if the given root can't be evaluated (store, library). *) diff --git a/lib/pinc_backend/Tag.ml b/lib/pinc_backend/Tag.ml index a3d5734..f6b5169 100644 --- a/lib/pinc_backend/Tag.ml +++ b/lib/pinc_backend/Tag.ml @@ -1,7 +1,7 @@ open State -open Pinc_Parser.Ast +open Pinc_Types.Ast open Types.Type_Value -module Ast = Pinc_Parser.Ast +module Ast = Pinc_Types.Ast let find_path path value = let rec aux path value = @@ -672,8 +672,8 @@ module Tag_Array = struct let output, child_meta = data |> Option.map (function - | { value_desc = Array _; value_loc } as x -> begin - match Helpers.Expect.(required (array (required string))) x with + | { value_desc = Array _; value_loc } as x -> + begin match Helpers.Expect.(required (array (required string))) x with | Ok v -> v | Error (`UnexpectedType value_loc) -> Pinc_Diagnostics.raise_error @@ -688,7 +688,7 @@ module Tag_Array = struct "Expected attribute %s to be an array of keys. Did not recieve the \ array at all." (key |> List.rev |> List.hd)) - end + end | { value_desc = _; value_loc } -> Pinc_Diagnostics.raise_error value_loc diff --git a/lib/pinc_backend/Types.ml b/lib/pinc_backend/Types.ml index 724040b..47e22a9 100644 --- a/lib/pinc_backend/Types.ml +++ b/lib/pinc_backend/Types.ml @@ -1,4 +1,4 @@ -module Ast = Pinc_Parser.Ast +module Ast = Pinc_Types.Ast module rec Type_Value : sig type value = { diff --git a/lib/pinc_backend/dune b/lib/pinc_backend/dune index 2fc2b20..4928e9e 100644 --- a/lib/pinc_backend/dune +++ b/lib/pinc_backend/dune @@ -2,7 +2,7 @@ (name Pinc_Backend) (public_name pinc-lang.backend) (flags :standard -open Pinc_Core) - (libraries Pinc_Core Pinc_Source Pinc_Diagnostics Pinc_Parser) + (libraries Pinc_Core Pinc_Types Pinc_Source Pinc_Diagnostics) (preprocess (pps ppx_deriving.show)) (instrumentation