diff --git a/lib/pinc_backend/Pinc_Interpreter.ml b/lib/pinc_backend/Pinc_Interpreter.ml index 4d42eb8..5a31464 100644 --- a/lib/pinc_backend/Pinc_Interpreter.ml +++ b/lib/pinc_backend/Pinc_Interpreter.ml @@ -1041,7 +1041,9 @@ and eval_binary_merge ~state left_expression right_expression = let result = fn attributes in { left with value_desc = ComponentTemplateNode (fn, tag, attributes, result) } | ComponentTemplateNode _, _ -> - failwith "Trying to merge a non record value onto tag attributes." + Pinc_Diagnostics.error + right_expression.expression_loc + "Trying to merge a non record value onto component attributes." | Array _, _ -> Pinc_Diagnostics.error right_expression.expression_loc @@ -1416,7 +1418,7 @@ and eval_slot ~tag ~attributes ~slotted_elements key = in let check_instance_restriction tag = match constraints with - | None -> () + | None -> Result.ok () | Some restrictions -> let is_in_list = ref false in let allowed, disallowed = @@ -1436,7 +1438,7 @@ and eval_slot ~tag ~attributes ~slotted_elements key = | allowed, _disallowed -> List.mem tag allowed in if is_allowed then - () + Result.ok () else ( let contraints = constraints @@ -1448,7 +1450,7 @@ and eval_slot ~tag ~attributes ~slotted_elements key = name) |> String.concat "," in - failwith + Result.error (Printf.sprintf "Child with tag `%s` may not be used inside this #Slot. The following \ restrictions are set: [ %s ]" @@ -1457,10 +1459,12 @@ and eval_slot ~tag ~attributes ~slotted_elements key = in slotted_elements |> List.map (function - | ( { value_desc = HtmlTemplateNode (tag_name, _, _, _); _ } - | { value_desc = ComponentTemplateNode (_, tag_name, _, _); _ } ) as v -> - let () = check_instance_restriction tag_name in - v + | ( { value_desc = HtmlTemplateNode (tag_name, _, _, _); value_loc } + | { value_desc = ComponentTemplateNode (_, tag_name, _, _); value_loc } ) as v + -> ( + match check_instance_restriction tag_name with + | Ok () -> v + | Error e -> Pinc_Diagnostics.error value_loc e) | { value_desc = _; value_loc } -> Pinc_Diagnostics.error value_loc @@ -1647,7 +1651,7 @@ and call_tag_listener ~state ~tag ~value_bag t = | None -> Result.ok (Value.null ~value_loc:tag.tag_loc ())) |> function | Ok v -> v |> transformer - | Error e -> failwith e + | Error e -> Pinc_Diagnostics.error tag.tag_loc e and eval_tag ~state tag = let Ast.{ tag = tag_identifier; attributes; transformer } = tag.tag_desc in diff --git a/lib/pinc_backend/Pinc_Typer.ml b/lib/pinc_backend/Pinc_Typer.ml index f032b4b..7bdb7a6 100644 --- a/lib/pinc_backend/Pinc_Typer.ml +++ b/lib/pinc_backend/Pinc_Typer.ml @@ -3,7 +3,7 @@ open Pinc_Interpreter_Types module Expect = struct let required fn value = match fn value with - | None -> failwith "required a value, but got null" + | None -> Pinc_Diagnostics.(error Location.none "required a value, but got null") | Some v -> v ;; @@ -19,133 +19,174 @@ module Expect = struct let string = function | Null -> None - | Portal _ -> failwith "expected string, got portal value" + | Portal _ -> + Pinc_Diagnostics.(error Location.none "expected string, got portal value") | String s -> Some s - | Int _ -> failwith "expected string, got int" - | Float _ -> failwith "expected string, got float" - | Bool _ -> failwith "expected string, got bool" - | Array _ -> failwith "expected string, got array" - | Record _ -> failwith "expected string, got record" - | Function _ -> failwith "expected string, got function definition" - | DefinitionInfo _ -> failwith "expected string, got definition info" - | TagInfo _ -> failwith "expected string, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected string, got HTML template node" + | Int _ -> Pinc_Diagnostics.(error Location.none "expected string, got int") + | Float _ -> Pinc_Diagnostics.(error Location.none "expected string, got float") + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected string, got bool") + | Array _ -> Pinc_Diagnostics.(error Location.none "expected string, got array") + | Record _ -> Pinc_Diagnostics.(error Location.none "expected string, got record") + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected string, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected string, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected string, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected string, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected string, got component template node" + Pinc_Diagnostics.( + error Location.none "expected string, got component template node") ;; let int = function | Null -> None - | Portal _ -> failwith "expected int, got portal value" - | String _ -> failwith "expected int, got string" + | Portal _ -> Pinc_Diagnostics.(error Location.none "expected int, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected int, got string") | Int i -> Some i - | Float _ -> failwith "expected int, got float" - | Bool _ -> failwith "expected int, got bool" - | Array _ -> failwith "expected int, got array" - | Record _ -> failwith "expected int, got record" - | Function _ -> failwith "expected int, got function definition" - | DefinitionInfo _ -> failwith "expected int, got definition info" - | TagInfo _ -> failwith "expected int, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected int, got HTML template node" + | Float _ -> Pinc_Diagnostics.(error Location.none "expected int, got float") + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected int, got bool") + | Array _ -> Pinc_Diagnostics.(error Location.none "expected int, got array") + | Record _ -> Pinc_Diagnostics.(error Location.none "expected int, got record") + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected int, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected int, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected int, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected int, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected int, got component template node" + Pinc_Diagnostics.(error Location.none "expected int, got component template node") ;; let float = function | Null -> None - | Portal _ -> failwith "expected float, got portal value" - | String _ -> failwith "expected float, got string" - | Int _ -> failwith "expected float, got int" + | Portal _ -> + Pinc_Diagnostics.(error Location.none "expected float, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected float, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected float, got int") | Float f -> Some f - | Bool _ -> failwith "expected float, got bool" - | Array _ -> failwith "expected float, got array" - | Record _ -> failwith "expected float, got record" - | Function _ -> failwith "expected float, got function definition" - | DefinitionInfo _ -> failwith "expected float, got definition info" - | TagInfo _ -> failwith "expected float, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected float, got HTML template node" + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected float, got bool") + | Array _ -> Pinc_Diagnostics.(error Location.none "expected float, got array") + | Record _ -> Pinc_Diagnostics.(error Location.none "expected float, got record") + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected float, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected float, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected float, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected float, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected float, got component template node" + Pinc_Diagnostics.( + error Location.none "expected float, got component template node") ;; let bool = function | Null -> None - | Portal _ -> failwith "expected bool, got portal value" - | String _ -> failwith "expected bool, got string" - | Int _ -> failwith "expected bool, got int" - | Float _ -> failwith "expected bool, got float" + | Portal _ -> Pinc_Diagnostics.(error Location.none "expected bool, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected bool, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected bool, got int") + | Float _ -> Pinc_Diagnostics.(error Location.none "expected bool, got float") | Bool b -> Some b - | Array _ -> failwith "expected bool, got array" - | Record _ -> failwith "expected bool, got record" - | Function _ -> failwith "expected bool, got function definition" - | DefinitionInfo _ -> failwith "expected bool, got definition info" - | TagInfo _ -> failwith "expected bool, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected bool, got HTML template node" + | Array _ -> Pinc_Diagnostics.(error Location.none "expected bool, got array") + | Record _ -> Pinc_Diagnostics.(error Location.none "expected bool, got record") + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected bool, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected bool, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected bool, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected bool, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected bool, got component template node" + Pinc_Diagnostics.( + error Location.none "expected bool, got component template node") ;; let array fn = function | Null -> None - | Portal _ -> failwith "expected array, got portal value" - | String _ -> failwith "expected array, got string" - | Int _ -> failwith "expected array, got int" - | Float _ -> failwith "expected array, got float" - | Bool _ -> failwith "expected array, bool" + | Portal _ -> + Pinc_Diagnostics.(error Location.none "expected array, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected array, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected array, got int") + | Float _ -> Pinc_Diagnostics.(error Location.none "expected array, got float") + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected array, bool") | Array a -> Some (a |> Array.to_list |> List.map fn) - | Record _ -> failwith "expected array, got record" - | Function _ -> failwith "expected array, got function definition" - | DefinitionInfo _ -> failwith "expected array, got definition info" - | TagInfo _ -> failwith "expected array, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected array, got HTML template node" + | Record _ -> Pinc_Diagnostics.(error Location.none "expected array, got record") + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected array, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected array, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected array, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected array, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected array, got component template node" + Pinc_Diagnostics.( + error Location.none "expected array, got component template node") ;; let record = function | Null -> None - | Portal _ -> failwith "expected record, got portal value" - | String _ -> failwith "expected record, got string" - | Int _ -> failwith "expected record, got int" - | Float _ -> failwith "expected record, got float" - | Bool _ -> failwith "expected record, got bool" - | Array _ -> failwith "expected record, got array" + | Portal _ -> + Pinc_Diagnostics.(error Location.none "expected record, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected record, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected record, got int") + | Float _ -> Pinc_Diagnostics.(error Location.none "expected record, got float") + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected record, got bool") + | Array _ -> Pinc_Diagnostics.(error Location.none "expected record, got array") | Record r -> Some (r |> StringMap.map snd) - | Function _ -> failwith "expected record, got function definition" - | DefinitionInfo _ -> failwith "expected record, got definition info" - | TagInfo _ -> failwith "expected record, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected record, got HTML template node" + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected record, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected record, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected record, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected record, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected record, got component template node" + Pinc_Diagnostics.( + error Location.none "expected record, got component template node") ;; let record_with_order = function | Null -> None - | Portal _ -> failwith "expected record, got portal value" - | String _ -> failwith "expected record, got string" - | Int _ -> failwith "expected record, got int" - | Float _ -> failwith "expected record, got float" - | Bool _ -> failwith "expected record, got bool" - | Array _ -> failwith "expected record, got array" + | Portal _ -> + Pinc_Diagnostics.(error Location.none "expected record, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected record, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected record, got int") + | Float _ -> Pinc_Diagnostics.(error Location.none "expected record, got float") + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected record, got bool") + | Array _ -> Pinc_Diagnostics.(error Location.none "expected record, got array") | Record r -> Some r - | Function _ -> failwith "expected record, got function definition" - | DefinitionInfo _ -> failwith "expected record, got definition info" - | TagInfo _ -> failwith "expected record, got tag" - | HtmlTemplateNode (_, _, _, _) -> failwith "expected record, got HTML template node" + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected record, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected record, got definition info") + | TagInfo _ -> Pinc_Diagnostics.(error Location.none "expected record, got tag") + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected record, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected record, got component template node" + Pinc_Diagnostics.( + error Location.none "expected record, got component template node") ;; let definition_info ?(typ = `All) = function | Null -> None - | Portal _ -> failwith "expected definition info, got portal value" - | String _ -> failwith "expected definition info, got string" - | Int _ -> failwith "expected definition info, got int" - | Float _ -> failwith "expected definition info, got float" - | Bool _ -> failwith "expected definition info, got bool" - | Array _ -> failwith "expected definition info, got array" - | Record _ -> failwith "expected definition info, got record" - | Function _ -> failwith "expected definition info, got function definition" + | Portal _ -> + Pinc_Diagnostics.( + error Location.none "expected definition info, got portal value") + | String _ -> + Pinc_Diagnostics.(error Location.none "expected definition info, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected definition info, got int") + | Float _ -> + Pinc_Diagnostics.(error Location.none "expected definition info, got float") + | Bool _ -> + Pinc_Diagnostics.(error Location.none "expected definition info, got bool") + | Array _ -> + Pinc_Diagnostics.(error Location.none "expected definition info, got array") + | Record _ -> + Pinc_Diagnostics.(error Location.none "expected definition info, got record") + | Function _ -> + Pinc_Diagnostics.( + error Location.none "expected definition info, got function definition") | DefinitionInfo (name, def_typ, negated) -> let typ = match (typ, def_typ) with @@ -154,35 +195,48 @@ module Expect = struct | (`Site | `All), Some `Site -> `Site | (`Library | `All), Some (`Library _) -> `Library | (`Store | `All), Some `Store -> `Store - | _, None -> failwith ("definition \"" ^ name ^ "\" does not exist") - | `Component, _ -> failwith "expected a component definition" - | `Page, _ -> failwith "expected a page definition" - | `Site, _ -> failwith "expected a site definition" - | `Library, _ -> failwith "expected a library definition" - | `Store, _ -> failwith "expected a store definition" + | _, None -> + Pinc_Diagnostics.( + error Location.none ("definition \"" ^ name ^ "\" does not exist")) + | `Component, _ -> + Pinc_Diagnostics.(error Location.none "expected a component definition") + | `Page, _ -> + Pinc_Diagnostics.(error Location.none "expected a page definition") + | `Site, _ -> + Pinc_Diagnostics.(error Location.none "expected a site definition") + | `Library, _ -> + Pinc_Diagnostics.(error Location.none "expected a library definition") + | `Store, _ -> + Pinc_Diagnostics.(error Location.none "expected a store definition") in Some (typ, name, negated = `Negated) - | TagInfo _ -> failwith "expected definition info, got tag" + | TagInfo _ -> + Pinc_Diagnostics.(error Location.none "expected definition info, got tag") | HtmlTemplateNode (_, _, _, _) -> - failwith "expected definition info, got HTML template node" + Pinc_Diagnostics.( + error Location.none "expected definition info, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected definition info, got component template node" + Pinc_Diagnostics.( + error Location.none "expected definition info, got component template node") ;; let tag_info = function | Null -> None - | Portal _ -> failwith "expected tag, got portal value" - | String _ -> failwith "expected tag, got string" - | Int _ -> failwith "expected tag, got int" - | Float _ -> failwith "expected tag, got float" - | Bool _ -> failwith "expected tag, got bool" - | Array _ -> failwith "expected tag, got array" - | Record _ -> failwith "expected tag, got record" - | Function _ -> failwith "expected tag, got function definition" - | DefinitionInfo _ -> failwith "expected tag, got definition info" + | Portal _ -> Pinc_Diagnostics.(error Location.none "expected tag, got portal value") + | String _ -> Pinc_Diagnostics.(error Location.none "expected tag, got string") + | Int _ -> Pinc_Diagnostics.(error Location.none "expected tag, got int") + | Float _ -> Pinc_Diagnostics.(error Location.none "expected tag, got float") + | Bool _ -> Pinc_Diagnostics.(error Location.none "expected tag, got bool") + | Array _ -> Pinc_Diagnostics.(error Location.none "expected tag, got array") + | Record _ -> Pinc_Diagnostics.(error Location.none "expected tag, got record") + | Function _ -> + Pinc_Diagnostics.(error Location.none "expected tag, got function definition") + | DefinitionInfo _ -> + Pinc_Diagnostics.(error Location.none "expected tag, got definition info") | TagInfo i -> Some i - | HtmlTemplateNode (_, _, _, _) -> failwith "expected tag, got HTML template node" + | HtmlTemplateNode (_, _, _, _) -> + Pinc_Diagnostics.(error Location.none "expected tag, got HTML template node") | ComponentTemplateNode (_, _, _, _) -> - failwith "expected tag, got component template node" + Pinc_Diagnostics.(error Location.none "expected tag, got component template node") ;; end