diff --git a/lib/02_parsing/Parser.ml b/lib/02_parsing/Parser.ml index df238c0..ab230f5 100644 --- a/lib/02_parsing/Parser.ml +++ b/lib/02_parsing/Parser.ml @@ -248,16 +248,11 @@ module Rules = struct let start_token = t.token in let attributes = if t |> optional Token.LEFT_PAREN then ( - let res = - t - |> Helpers.separated_list ~fn:parse_attribute ~sep:Token.COMMA - |> List.to_seq - |> StringMap.of_seq - in + let res = t |> Helpers.separated_list ~fn:parse_attribute ~sep:Token.COMMA in t |> expect Token.RIGHT_PAREN; res) else - StringMap.empty + [] in let transformer = let start_token = t.token in @@ -334,10 +329,7 @@ module Rules = struct | Token.HTML_OPEN_TAG html_tag_identifier -> next t; let html_tag_attributes = - t - |> Helpers.list ~fn:(parse_attribute ~sep:Token.EQUAL) - |> List.to_seq - |> StringMap.of_seq + t |> Helpers.list ~fn:(parse_attribute ~sep:Token.EQUAL) in let html_tag_self_closing = t |> optional Token.HTML_OR_COMPONENT_TAG_SELF_CLOSING @@ -370,10 +362,7 @@ module Rules = struct in next t; let component_tag_attributes = - t - |> Helpers.list ~fn:(parse_attribute ~sep:Token.EQUAL) - |> List.to_seq - |> StringMap.of_seq + t |> Helpers.list ~fn:(parse_attribute ~sep:Token.EQUAL) in let component_tag_self_closing = t |> optional Token.HTML_OR_COMPONENT_TAG_SELF_CLOSING @@ -554,10 +543,7 @@ module Rules = struct if is_record then ( next t; let attrs = - t - |> Helpers.separated_list ~sep:Token.COMMA ~fn:parse_record_field - |> List.to_seq - |> StringMap.of_seq + t |> Helpers.separated_list ~sep:Token.COMMA ~fn:parse_record_field in t |> expect Token.RIGHT_BRACE; let end_location = t.token.location in @@ -718,7 +704,6 @@ module Rules = struct next t; let expressions = Helpers.separated_list ~sep:Token.COMMA ~fn:parse_expression t - |> Array.of_list in let end_location = t.token.location in expect Token.RIGHT_BRACK t; @@ -819,13 +804,11 @@ module Rules = struct if optional Token.LEFT_PAREN t then ( let attributes = Helpers.separated_list ~sep:Token.COMMA ~fn:parse_attribute t - |> List.to_seq - |> StringMap.of_seq in t |> expect Token.RIGHT_PAREN; attributes) else - StringMap.empty + [] in let declaration_body = t |> parse_expression in match declaration_body with @@ -878,11 +861,11 @@ let parse ?(include_stdlib = true) sources : Parsetree.t = [] in stdlib @ sources - |> ListLabels.fold_left ~init:StringMap.empty ~f:(fun acc source -> + |> ListLabels.fold_left ~init:[] ~f:(fun acc source -> let decls = parse_source source in ListLabels.fold_left decls ~init:acc ~f:(fun acc (key, decl) -> - match StringMap.find_opt key acc with - | None -> StringMap.add key decl acc + match List.assoc_opt key acc with + | None -> (key, decl) :: acc | Some _ -> let message = Printf.sprintf @@ -892,4 +875,5 @@ let parse ?(include_stdlib = true) sources : Parsetree.t = in Pinc_Diagnostics.raise_error decl.Parsetree.declaration_loc message)) + |> List.rev ;; diff --git a/lib/02_parsing/Parsetree.ml b/lib/02_parsing/Parsetree.ml index d13cd53..706fab6 100644 --- a/lib/02_parsing/Parsetree.ml +++ b/lib/02_parsing/Parsetree.ml @@ -9,13 +9,13 @@ and template_node = { and template_node_desc = | P_HtmlTemplateNode of { html_tag_identifier : string; - html_tag_attributes : expression StringMap.t; + html_tag_attributes : (string * expression) list; html_tag_children : template_node list; html_tag_self_closing : bool; } | P_ComponentTemplateNode of { component_tag_identifier : uppercase_identifier; - component_tag_attributes : expression StringMap.t; + component_tag_attributes : (string * expression) list; component_tag_children : template_node list; } | P_FragmentTemplateNode of { fragement_children : template_node list } @@ -44,7 +44,7 @@ and tag_kind = and tag_desc = { tag : tag_kind; - attributes : expression StringMap.t; + attributes : (string * expression) list; transformer : expression option; } @@ -69,8 +69,8 @@ and expression_desc = | P_Int of int | P_Float of float | P_Bool of bool - | P_Array of expression array - | P_Record of ([ `Required | `Optional ] * expression) StringMap.t + | P_Array of expression list + | P_Record of (string * ([ `Required | `Optional ] * expression)) list | P_ExternalFunction of { parameters : lowercase_identifier list; name : string; @@ -133,11 +133,11 @@ and declaration_kind = | P_Declaration_Store of declaration_desc and declaration_desc = { - declaration_attributes : expression StringMap.t; + declaration_attributes : (string * expression) list; declaration_body : expression; } -and t = declaration StringMap.t +and t = (string * declaration) list module Declaration = struct let marshal (d : declaration) = Marshal.to_string d [] diff --git a/lib/02_parsing/Transformer.ml b/lib/02_parsing/Transformer.ml index bb1a3c5..f6ade42 100644 --- a/lib/02_parsing/Transformer.ml +++ b/lib/02_parsing/Transformer.ml @@ -66,12 +66,14 @@ and transform_float env f = (env, Float f) and transform_bool env b = (env, Bool b) and transform_array env a = - let env, array = Array.fold_map ~init:env ~f:transform_expression a in - (env, Array array) + let env, array = List.fold_map ~init:env ~f:transform_expression a in + (env, Array (Array.of_list array)) and transform_record env r = let env, r = - StringMap.fold_mapi r ~init:env ~f:(fun key env (requirement, expr) -> + r + |> StringMap.of_list + |> StringMap.fold_mapi ~init:env ~f:(fun key env (requirement, expr) -> let env = { env with @@ -127,6 +129,7 @@ and transform_tag env (tag : Parsetree.tag) = let current_identifier = env.Env.current_identifier in let child_env, children = desc.attributes + |> StringMap.of_list |> StringMap.find_opt "of" |> Option.fold_map ~init: @@ -144,6 +147,7 @@ and transform_tag env (tag : Parsetree.tag) = in let env, attributes = desc.attributes + |> StringMap.of_list |> StringMap.remove "of" |> StringMap.fold_map ~init:env ~f:transform_expression in @@ -371,7 +375,9 @@ and transform_html_template_node ~html_tag_self_closing = let html_tag_identifier = html_tag_identifier in let env, html_tag_attributes = - StringMap.fold_map ~init:env ~f:transform_expression html_tag_attributes + html_tag_attributes + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_expression in let env, html_tag_children = List.fold_map ~init:env ~f:transform_template_node html_tag_children @@ -401,7 +407,9 @@ and transform_component_template_node transform_uppercase_id env component_tag_identifier in let env, component_tag_attributes = - StringMap.fold_map ~init:env ~f:transform_expression component_tag_attributes + component_tag_attributes + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_expression in let env, component_tag_children = List.fold_map ~init:env ~f:transform_template_node component_tag_children @@ -553,40 +561,36 @@ and transform_statement env (statement : Parsetree.statement) = and transform_component_declaration env (declaration : Parsetree.declaration_desc) = let env, declaration_attributes = - StringMap.fold_map - ~init:env - ~f:transform_expression - declaration.declaration_attributes + declaration.declaration_attributes + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_expression in let env, declaration_body = transform_expression env declaration.declaration_body in (env, Declaration_Component { declaration_attributes; declaration_body }) and transform_library_declaration env (declaration : Parsetree.declaration_desc) = let env, declaration_attributes = - StringMap.fold_map - ~init:env - ~f:transform_expression - declaration.declaration_attributes + declaration.declaration_attributes + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_expression in let env, declaration_body = transform_expression env declaration.declaration_body in (env, Declaration_Library { declaration_attributes; declaration_body }) and transform_page_declaration env (declaration : Parsetree.declaration_desc) = let env, declaration_attributes = - StringMap.fold_map - ~init:env - ~f:transform_expression - declaration.declaration_attributes + declaration.declaration_attributes + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_expression in let env, declaration_body = transform_expression env declaration.declaration_body in (env, Declaration_Page { declaration_attributes; declaration_body }) and transform_store_declaration env (declaration : Parsetree.declaration_desc) = let env, declaration_attributes = - StringMap.fold_map - ~init:env - ~f:transform_expression - declaration.declaration_attributes + declaration.declaration_attributes + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_expression in let env, declaration_body = transform_expression env declaration.declaration_body in (env, Declaration_Store { declaration_attributes; declaration_body }) @@ -602,7 +606,9 @@ and transform_declaration env (declaration : Parsetree.declaration) = (env, { declaration_loc = declaration.declaration_loc; declaration_kind }) and transform_declarations env declarations = - StringMap.fold_map ~init:env ~f:transform_declaration declarations + declarations + |> StringMap.of_list + |> StringMap.fold_map ~init:env ~f:transform_declaration ;; let transform (declarations : Parsetree.t) : Ast.t = @@ -613,7 +619,7 @@ let transform (declarations : Parsetree.t) : Ast.t = let all_tags key declarations = let env = Env.empty in - let declaration = StringMap.find_opt key declarations in + let declaration = List.assoc_opt key declarations in match declaration with | None -> env.tags | Some declaration ->