-module(prequel@error@parse_error). -compile([no_auto_import, nowarn_unused_vars]). -export([to_report/3]). -export_type([parse_error/0]). -type parse_error() :: {wrong_entity_name, gleam@option:option(binary()), gleam@option:option(prequel@span:span()), prequel@span:span(), binary(), prequel@span:span(), binary()} | {more_than_one_hierarchy, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), prequel@span:span()} | {possible_circle_lollipop_typo, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {possible_star_lollipop_typo, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {possible_arrow_lollipop_typo, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {wrong_order_of_hierarchy_qualifiers, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary(), binary()} | {unqualified_hierarchy, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {unexpected_token_in_entity_body, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {wrong_attribute_name, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary(), prequel@span:span()} | {wrong_cardinality_annotation, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary(), prequel@span:span()} | {wrong_key_name, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary(), prequel@span:span()} | {type_annotation_on_composed_key, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), prequel@span:span()} | {missing_cardinality_annotation, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {wrong_relationship_name, gleam@option:option(binary()), gleam@option:option(prequel@span:span()), prequel@span:span(), binary(), prequel@span:span()} | {key_inside_relationship, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {unexpected_token_in_binary_relationship, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {wrong_hierarchy_overlapping, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary(), prequel@span:span()} | {missing_hierarchy_keyword, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {empty_hierarchy, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {unexpected_token_in_hierarchy_body, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {relationship_body_with_no_entities, gleam@option:option(binary()), prequel@span:span()} | {relationship_body_with_just_one_entity, gleam@option:option(binary()), prequel@span:span(), binary(), prequel@span:span()} | {unexpected_token_in_relationship_body, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {unexpected_token_in_top_level, gleam@option:option(binary()), prequel@span:span()} | {wrong_letter_in_unbounded_cardinality, gleam@option:option(binary()), prequel@span:span(), prequel@span:span()} | {incomplete_cardinality, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary()} | {unexpected_end_of_file, gleam@option:option(binary()), gleam@option:option(prequel@span:span()), prequel@span:span(), binary()} | {internal_error, gleam@option:option(binary()), gleam@option:option(prequel@span:span()), prequel@span:span(), binary()} | {incomplete_composed_key, gleam@option:option(binary()), prequel@span:span(), prequel@span:span(), binary(), prequel@span:span()}. -spec code(parse_error()) -> binary(). code(Error) -> case Error of {wrong_entity_name, _, _, _, _, _, _} -> <<"PE001"/utf8>>; {more_than_one_hierarchy, _, _, _, _} -> <<"PE002"/utf8>>; {possible_circle_lollipop_typo, _, _, _} -> <<"PE003"/utf8>>; {possible_star_lollipop_typo, _, _, _} -> <<"PE004"/utf8>>; {possible_arrow_lollipop_typo, _, _, _} -> <<"PE005"/utf8>>; {wrong_order_of_hierarchy_qualifiers, _, _, _, _, _} -> <<"PE006"/utf8>>; {unqualified_hierarchy, _, _, _} -> <<"PE007"/utf8>>; {unexpected_token_in_entity_body, _, _, _} -> <<"PE008"/utf8>>; {wrong_attribute_name, _, _, _, _, _} -> <<"PE009"/utf8>>; {wrong_cardinality_annotation, _, _, _, _, _} -> <<"PE010"/utf8>>; {wrong_key_name, _, _, _, _, _} -> <<"PE011"/utf8>>; {type_annotation_on_composed_key, _, _, _, _} -> <<"PE012"/utf8>>; {missing_cardinality_annotation, _, _, _} -> <<"PE013"/utf8>>; {wrong_relationship_name, _, _, _, _, _} -> <<"PE014"/utf8>>; {key_inside_relationship, _, _, _} -> <<"PE015"/utf8>>; {unexpected_token_in_binary_relationship, _, _, _} -> <<"PE016"/utf8>>; {wrong_hierarchy_overlapping, _, _, _, _, _} -> <<"PE017"/utf8>>; {missing_hierarchy_keyword, _, _, _} -> <<"PE018"/utf8>>; {empty_hierarchy, _, _, _} -> <<"PE019"/utf8>>; {unexpected_token_in_hierarchy_body, _, _, _} -> <<"PE020"/utf8>>; {relationship_body_with_no_entities, _, _} -> <<"PE021"/utf8>>; {relationship_body_with_just_one_entity, _, _, _, _} -> <<"PE022"/utf8>>; {unexpected_token_in_relationship_body, _, _, _} -> <<"PE023"/utf8>>; {wrong_letter_in_unbounded_cardinality, _, _, _} -> <<"PE024"/utf8>>; {incomplete_cardinality, _, _, _, _} -> <<"PE025"/utf8>>; {unexpected_end_of_file, _, _, _, _} -> <<"PE026"/utf8>>; {unexpected_token_in_top_level, _, _} -> <<"PE027"/utf8>>; {internal_error, _, _, _, _} -> <<"PE028"/utf8>>; {incomplete_composed_key, _, _, _, _, _} -> <<"PE029"/utf8>> end. -spec name(parse_error()) -> binary(). name(Error) -> case Error of {wrong_entity_name, _, _, _, _, _, _} -> <<"Wrong entity name"/utf8>>; {more_than_one_hierarchy, _, _, _, _} -> <<"More than one hierarchy"/utf8>>; {possible_circle_lollipop_typo, _, _, _} -> <<"Circle lollipop typo"/utf8>>; {possible_star_lollipop_typo, _, _, _} -> <<"Star lollipop typo"/utf8>>; {possible_arrow_lollipop_typo, _, _, _} -> <<"Arrow lollipop typo"/utf8>>; {wrong_order_of_hierarchy_qualifiers, _, _, _, _, _} -> <<"Wrong order of hierarchy qualifiers"/utf8>>; {unqualified_hierarchy, _, _, _} -> <<"Unqualified hierarchy"/utf8>>; {unexpected_token_in_entity_body, _, _, _} -> <<"Unexpected token in entity body"/utf8>>; {wrong_attribute_name, _, _, _, _, _} -> <<"Wrong attribute name"/utf8>>; {wrong_cardinality_annotation, _, _, _, _, _} -> <<"Wrong cardinality annotation"/utf8>>; {wrong_key_name, _, _, _, _, _} -> <<"Wrong key name"/utf8>>; {type_annotation_on_composed_key, _, _, _, _} -> <<"Type annotation on composed key"/utf8>>; {missing_cardinality_annotation, _, _, _} -> <<"Missing cardinality annotation"/utf8>>; {wrong_relationship_name, _, _, _, _, _} -> <<"Wrong relationship name"/utf8>>; {key_inside_relationship, _, _, _} -> <<"Key inside relationship"/utf8>>; {unexpected_token_in_binary_relationship, _, _, _} -> <<"Unexpected token in binary relationship"/utf8>>; {wrong_hierarchy_overlapping, _, _, _, _, _} -> <<"Wrong hierarchy overlapping"/utf8>>; {missing_hierarchy_keyword, _, _, _} -> <<"Missing hierarchy keyword"/utf8>>; {empty_hierarchy, _, _, _} -> <<"Empty hierarchy"/utf8>>; {unexpected_token_in_hierarchy_body, _, _, _} -> <<"Unexpected token in hierarchy body"/utf8>>; {relationship_body_with_no_entities, _, _} -> <<"Relationship body with no entities"/utf8>>; {relationship_body_with_just_one_entity, _, _, _, _} -> <<"Relatinoship body with just one entity"/utf8>>; {unexpected_token_in_relationship_body, _, _, _} -> <<"Unexpected token in relationship body"/utf8>>; {wrong_letter_in_unbounded_cardinality, _, _, _} -> <<"Wrong letter in unbounded cardinality"/utf8>>; {incomplete_cardinality, _, _, _, _} -> <<"Incomplete cardinality"/utf8>>; {unexpected_end_of_file, _, _, _, _} -> <<"Unexpected end of file"/utf8>>; {unexpected_token_in_top_level, _, _} -> <<"Unexpected token"/utf8>>; {internal_error, _, _, _, _} -> <<"Internal error"/utf8>>; {incomplete_composed_key, _, _, _, _, _} -> <<"Incomplete composed key"/utf8>> end. -spec main_span(parse_error()) -> prequel@span:span(). main_span(Error) -> case Error of {wrong_entity_name, _, _, _, _, Span, _} -> Span; {more_than_one_hierarchy, _, _, _, Span@1} -> Span@1; {possible_circle_lollipop_typo, _, _, Span@2} -> Span@2; {possible_star_lollipop_typo, _, _, Span@3} -> Span@3; {possible_arrow_lollipop_typo, _, _, Span@4} -> Span@4; {wrong_order_of_hierarchy_qualifiers, _, _, Span@5, _, _} -> Span@5; {unqualified_hierarchy, _, _, Span@6} -> Span@6; {unexpected_token_in_entity_body, _, _, Span@7} -> Span@7; {wrong_attribute_name, _, _, _, _, Span@8} -> Span@8; {wrong_cardinality_annotation, _, _, _, _, Span@9} -> Span@9; {wrong_key_name, _, _, _, _, Span@10} -> Span@10; {type_annotation_on_composed_key, _, _, _, Span@11} -> Span@11; {missing_cardinality_annotation, _, _, Span@12} -> Span@12; {wrong_relationship_name, _, _, _, _, Span@13} -> Span@13; {key_inside_relationship, _, _, Span@14} -> Span@14; {unexpected_token_in_binary_relationship, _, _, Span@15} -> Span@15; {wrong_hierarchy_overlapping, _, _, _, _, Span@16} -> Span@16; {missing_hierarchy_keyword, _, _, Span@17} -> Span@17; {empty_hierarchy, _, _, Span@18} -> Span@18; {unexpected_token_in_hierarchy_body, _, _, Span@19} -> Span@19; {relationship_body_with_no_entities, _, Span@20} -> Span@20; {relationship_body_with_just_one_entity, _, Span@21, _, _} -> Span@21; {unexpected_token_in_relationship_body, _, _, Span@22} -> Span@22; {wrong_letter_in_unbounded_cardinality, _, _, Span@23} -> Span@23; {incomplete_cardinality, _, _, Span@24, _} -> Span@24; {unexpected_end_of_file, _, _, Span@25, _} -> Span@25; {unexpected_token_in_top_level, _, Span@26} -> Span@26; {internal_error, _, _, Span@27, _} -> Span@27; {incomplete_composed_key, _, _, _, _, Span@28} -> Span@28 end. -spec message(parse_error()) -> binary(). message(Error) -> case Error of {wrong_entity_name, _, _, _, Wrong_name, _, After_what} -> <<<<<<<<"I was expecting to find an entity name after "/utf8, After_what/binary>>/binary, " but I ran into `"/utf8>>/binary, Wrong_name/binary>>/binary, "`, which is not a valid name"/utf8>>; {more_than_one_hierarchy, _, _, _, _} -> <<"An entity can only be the root of one hierarchy. Here is the first one..."/utf8>>; {possible_circle_lollipop_typo, _, _, _} -> <<"Did you mean to write `-o` here?"/utf8>>; {possible_star_lollipop_typo, _, _, _} -> <<"Did you mean to write `-*` here?"/utf8>>; {possible_arrow_lollipop_typo, _, _, _} -> <<"Did you mean to write `->` here?"/utf8>>; {wrong_order_of_hierarchy_qualifiers, _, _, _, First, Second} -> <<<<<<<<"Did you mean to write `"/utf8, Second/binary>>/binary, " "/utf8>>/binary, First/binary>>/binary, "` here?"/utf8>>; {unqualified_hierarchy, _, _, _} -> <<"This hierarchy is missing its totality and overlapping qualifiers"/utf8>>; {unexpected_token_in_entity_body, _, _, _} -> <<"I didn't expect to find this token inside an entity's body"/utf8>>; {wrong_attribute_name, _, _, _, Name, _} -> <<<<"I was expecting to find an attribute name but I ran into `"/utf8, Name/binary>>/binary, "`, which is not a valid name"/utf8>>; {wrong_cardinality_annotation, _, _, _, Wrong_cardinality, _} -> <<<<"I was expecting a cardinality annotation but I ran into `"/utf8, Wrong_cardinality/binary>>/binary, "`, which is not a valid cardinality"/utf8>>; {wrong_key_name, _, _, _, Name@1, _} -> <<<<"I was expecting to find a key name but I ran into `"/utf8, Name@1/binary>>/binary, "`, which is not a valid name"/utf8>>; {type_annotation_on_composed_key, _, _, _, _} -> <<"A key composed of multiple items cannot have a type or cardinality annotation"/utf8>>; {missing_cardinality_annotation, _, _, _} -> <<"I was expecting to find a cardinality annotation after this"/utf8>>; {wrong_relationship_name, _, _, _, Name@2, _} -> <<<<"I was expecting to find a relationship name but I ran into `"/utf8, Name@2/binary>>/binary, "`, which is not a valid name"/utf8>>; {key_inside_relationship, _, _, _} -> <<"A relationship cannot contain any keys, did you mean to write `-o` instead?"/utf8>>; {unexpected_token_in_binary_relationship, _, _, _} -> <<"I didn't expect to find this token inside a relationship's body"/utf8>>; {wrong_hierarchy_overlapping, _, _, _, Name@3, _} -> <<<<"I was expecting to find an overlapping qualifier (either `overlapped` or `disjoint`), but I ran into `"/utf8, Name@3/binary>>/binary, "`, which is not a valid qualifier"/utf8>>; {missing_hierarchy_keyword, _, _, _} -> <<"I was expecting to find the `hierarchy` keyword after these qualifiers"/utf8>>; {empty_hierarchy, _, _, _} -> <<"This hierarchy has an empty body"/utf8>>; {unexpected_token_in_hierarchy_body, _, _, _} -> <<"I didn't expect to find this token inside a hierarchy's body"/utf8>>; {relationship_body_with_no_entities, _, _} -> <<"This relationship has no entities in its body"/utf8>>; {relationship_body_with_just_one_entity, _, _, Name@4, _} -> <<<<"This is the only entity taking part into the relationship `"/utf8, Name@4/binary>>/binary, "`"/utf8>>; {unexpected_token_in_relationship_body, _, _, _} -> <<"I didn't expect to find this token inside a relationship's body"/utf8>>; {wrong_letter_in_unbounded_cardinality, _, _, _} -> <<"Did you mean to write `N` as the upper bound of this cardinality?"/utf8>>; {incomplete_cardinality, _, _, _, What} -> <<"This looks like a cardinality annotation but it is missing "/utf8, What/binary>>; {unexpected_end_of_file, _, _, _, What@1} -> <<"I ran into the end of file halfway through parsing "/utf8, What@1/binary>>; {unexpected_token_in_top_level, _, _} -> <<"I didn't expect to find this token here"/utf8>>; {internal_error, _, _, _, What@2} -> What@2; {incomplete_composed_key, _, _, _, Wrong_key, _} -> <<"foo "/utf8, Wrong_key/binary>> end. -spec blocks(parse_error()) -> non_empty_list:non_empty_list(prequel@internals@report:report_block()). blocks(Error) -> case Error of {wrong_entity_name, _, {some, Context}, Underlined, _, Pointed, _} -> non_empty_list:new( {context_block, Context}, [{error_block, Pointed, {some, Underlined}, message(Error)}] ); {wrong_entity_name, _, none, Underlined@1, _, Pointed@1, _} -> non_empty_list:single( {error_block, Pointed@1, {some, Underlined@1}, message(Error)} ); {more_than_one_hierarchy, _, Context@1, First, Second} -> non_empty_list:new( {context_block, Context@1}, [{error_block, First, none, message(Error)}, {error_block, Second, none, <<"...and here's another one. Maybe you could merge those together?"/utf8>>}] ); {possible_circle_lollipop_typo, _, Context@2, Span} -> non_empty_list:new( {context_block, Context@2}, [{error_block, Span, none, message(Error)}] ); {possible_star_lollipop_typo, _, Context@3, Span@1} -> non_empty_list:new( {context_block, Context@3}, [{error_block, Span@1, none, message(Error)}] ); {possible_arrow_lollipop_typo, _, Context@4, Span@2} -> non_empty_list:new( {context_block, Context@4}, [{error_block, Span@2, none, message(Error)}] ); {wrong_order_of_hierarchy_qualifiers, _, Context@5, Span@3, _, _} -> non_empty_list:new( {context_block, Context@5}, [{error_block, Span@3, none, message(Error)}] ); {unqualified_hierarchy, _, Context@6, Span@4} -> non_empty_list:new( {context_block, Context@6}, [{error_block, Span@4, none, message(Error)}] ); {unexpected_token_in_entity_body, _, Context@7, Span@5} -> non_empty_list:new( {context_block, Context@7}, [{error_block, Span@5, none, message(Error)}] ); {wrong_attribute_name, _, Context@8, Underlined@2, _, Span@6} -> non_empty_list:new( {context_block, Context@8}, [{error_block, Span@6, {some, Underlined@2}, message(Error)}] ); {wrong_cardinality_annotation, _, Context@9, Underlined@3, _, Span@7} -> non_empty_list:new( {context_block, Context@9}, [{error_block, Span@7, {some, Underlined@3}, message(Error)}] ); {wrong_key_name, _, Context@10, Underlined@4, _, Span@8} -> non_empty_list:new( {context_block, Context@10}, [{error_block, Span@8, {some, Underlined@4}, message(Error)}] ); {type_annotation_on_composed_key, _, Context@11, Underlined@5, Span@9} -> non_empty_list:new( {context_block, Context@11}, [{error_block, Span@9, {some, Underlined@5}, message(Error)}] ); {missing_cardinality_annotation, _, Context@12, Span@10} -> non_empty_list:new( {context_block, Context@12}, [{error_block, Span@10, none, message(Error)}] ); {wrong_relationship_name, _, {some, Context@13}, Underlined@6, _, Span@11} -> non_empty_list:new( {context_block, Context@13}, [{error_block, Span@11, {some, Underlined@6}, message(Error)}] ); {wrong_relationship_name, _, none, Underlined@7, _, Span@12} -> non_empty_list:single( {error_block, Span@12, {some, Underlined@7}, message(Error)} ); {key_inside_relationship, _, Context@14, Span@13} -> non_empty_list:new( {context_block, Context@14}, [{error_block, Span@13, none, message(Error)}] ); {unexpected_token_in_binary_relationship, _, Context@15, Span@14} -> non_empty_list:new( {context_block, Context@15}, [{error_block, Span@14, none, message(Error)}] ); {wrong_hierarchy_overlapping, _, Context@16, Underlined@8, _, Span@15} -> non_empty_list:new( {context_block, Context@16}, [{error_block, Span@15, {some, Underlined@8}, message(Error)}] ); {missing_hierarchy_keyword, _, Context@17, Span@16} -> non_empty_list:new( {context_block, Context@17}, [{error_block, Span@16, none, message(Error)}] ); {empty_hierarchy, _, Context@18, Span@17} -> non_empty_list:new( {context_block, Context@18}, [{error_block, Span@17, none, message(Error)}] ); {unexpected_token_in_hierarchy_body, _, Context@19, Span@18} -> non_empty_list:new( {context_block, Context@19}, [{error_block, Span@18, none, message(Error)}] ); {relationship_body_with_no_entities, _, Span@19} -> non_empty_list:single({error_block, Span@19, none, message(Error)}); {relationship_body_with_just_one_entity, _, Context@20, _, Span@20} -> non_empty_list:new( {context_block, Context@20}, [{error_block, Span@20, none, message(Error)}] ); {unexpected_token_in_relationship_body, _, Context@21, Span@21} -> non_empty_list:new( {context_block, Context@21}, [{error_block, Span@21, none, message(Error)}] ); {wrong_letter_in_unbounded_cardinality, _, Context@22, Span@22} -> non_empty_list:new( {context_block, Context@22}, [{error_block, Span@22, none, message(Error)}] ); {incomplete_cardinality, _, Context@23, Span@23, _} -> non_empty_list:new( {context_block, Context@23}, [{error_block, Span@23, none, message(Error)}] ); {unexpected_end_of_file, _, {some, Context@24}, Span@24, _} -> non_empty_list:new( {context_block, Context@24}, [{error_block, Span@24, none, message(Error)}] ); {unexpected_end_of_file, _, none, Span@25, _} -> non_empty_list:single({error_block, Span@25, none, message(Error)}); {unexpected_token_in_top_level, _, Span@26} -> non_empty_list:single({error_block, Span@26, none, message(Error)}); {internal_error, _, {some, Context@25}, Span@27, _} -> non_empty_list:new( {context_block, Context@25}, [{error_block, Span@27, none, message(Error)}] ); {internal_error, _, none, Span@28, _} -> non_empty_list:single({error_block, Span@28, none, message(Error)}); {incomplete_composed_key, _, Composed_key_span, Enclosing_entity, _, Wrong_key_span} -> non_empty_list:new( {context_block, Enclosing_entity}, [{error_block, Wrong_key_span, {some, Composed_key_span}, message(Error)}] ) end. -spec to_report(parse_error(), binary(), binary()) -> prequel@internals@report:report(). to_report(Error, File_name, Source_code) -> Main_span = main_span(Error), Line = erlang:element(2, Main_span), Column = erlang:element(4, Main_span), Name = name(Error), Code = code(Error), Blocks = blocks(Error), {report, File_name, Source_code, Name, Code, Line, Column, Blocks, erlang:element(2, Error)}.