diff --git a/lib/02_parsing/Ast.ml b/lib/02_parsing/Ast.ml index 561a212..3d9b6c1 100644 --- a/lib/02_parsing/Ast.ml +++ b/lib/02_parsing/Ast.ml @@ -20,9 +20,9 @@ and template_node_desc = component_tag_attributes : expression StringMap.t; component_tag_children : template_node list; } - | FragmentTemplateNode of { fragement_children : template_node list } - | ExpressionTemplateNode of { template_expression_node_expression : expression } - | TextTemplateNode of { text_template_node_text : string } + | FragmentTemplateNode of template_node list + | ExpressionTemplateNode of expression + | TextTemplateNode of string and tag = { tag_loc : Pinc_Diagnostics.Location.t; @@ -68,7 +68,7 @@ and expression = { } and expression_desc = - | Comment of string + | Void | String of string_template list | Char of Uchar.t | Int of int @@ -153,7 +153,7 @@ end let show_expression expr = match expr with - | { expression_loc = _; expression_desc = Comment _ } -> "Comment" + | { expression_loc = _; expression_desc = Void } -> "Void" | { expression_loc = _; expression_desc = String _ } -> "String" | { expression_loc = _; expression_desc = Char _ } -> "Char" | { expression_loc = _; expression_desc = Int _ } -> "Int" diff --git a/lib/02_parsing/Parser.ml b/lib/02_parsing/Parser.ml index 5a29590..fd5262f 100644 --- a/lib/02_parsing/Parser.ml +++ b/lib/02_parsing/Parser.ml @@ -308,41 +308,42 @@ module Rules = struct match t.token.typ with | Token.HTML_TEXT text_template_node_text -> next t; - Some (Parsetree.P_TextTemplateNode { text_template_node_text }) + Some (Parsetree.P_TextTemplateNode text_template_node_text) | Token.LEFT_BRACE -> let start_token = t.token in next t; - let has_comment = + let comment = match t.token.typ with - | Token.COMMENT _ -> true - | _ -> false + | Token.COMMENT s -> Some s + | _ -> None in - let template_expression_node_expression = + let template_node = match parse_expression t with - | Some e -> e - | None -> + | Some template_expression_node_expression -> + Parsetree.P_ExpressionTemplateNode template_expression_node_expression + | None -> ( let location = Location.merge ~s:start_token.location ~e:t.token.location () in - if not has_comment then - Diagnostics.warn - location - "Expected to see an expression between these braces. \n\ - This is currently not doing anything, so you can safely remove it.\n\ - If you wanted to have an empty record here, you need to write `{{}}`"; - - Parsetree. - { - expression_loc = location; - expression_desc = Parsetree.P_BlockExpression []; - expression_annotations = []; - } - in - let expression = - Parsetree.P_ExpressionTemplateNode { template_expression_node_expression } + match comment with + | None -> + Diagnostics.warn + location + "Expected to see an expression between these braces. \n\ + This is currently not doing anything, so you can safely remove it.\n\ + If you wanted to have an empty record here, you need to write \ + `{{}}`"; + Parsetree.P_ExpressionTemplateNode + Parsetree. + { + expression_loc = location; + expression_desc = Parsetree.P_Record []; + expression_annotations = []; + } + | Some comment -> Parsetree.P_TemplateComment comment) in t |> expect Token.RIGHT_BRACE; - Some expression + Some template_node | Token.HTML_OPEN_TAG html_tag_identifier -> next t; let html_tag_attributes = @@ -372,7 +373,7 @@ module Rules = struct next t; let fragement_children = t |> Helpers.list ~fn:parse_template_node in t |> expect Token.HTML_CLOSE_FRAGMENT; - Some (Parsetree.P_FragmentTemplateNode { fragement_children }) + Some (Parsetree.P_FragmentTemplateNode fragement_children) | Token.COMPONENT_OPEN_TAG identifier -> let component_tag_identifier = Parsetree.P_Uppercase_Id (identifier, t.token.location) @@ -402,7 +403,7 @@ module Rules = struct }) | Token.HTML_DOCTYPE text_template_node_text -> next t; - Some (Parsetree.P_TextTemplateNode { text_template_node_text }) + Some (Parsetree.P_TextTemplateNode text_template_node_text) | _ -> None in let node_end = t.prev_token in @@ -536,11 +537,6 @@ module Rules = struct let expr_start = t.token.location in let* expression_desc, expr_end = match t.token.typ with - (* PARSING COMMENT EXPRESSION *) - | Token.COMMENT s -> - let end_location = t.token.location in - next t; - Some (Parsetree.P_Comment s, end_location) (* PARSING PARENTHESIZED EXPRESSION *) | Token.LEFT_PAREN -> next t; diff --git a/lib/02_parsing/Parsetree.ml b/lib/02_parsing/Parsetree.ml index 6879c84..6c16f7e 100644 --- a/lib/02_parsing/Parsetree.ml +++ b/lib/02_parsing/Parsetree.ml @@ -25,9 +25,10 @@ and template_node_desc = component_tag_attributes : (string * expression) list; component_tag_children : template_node list; } - | P_FragmentTemplateNode of { fragement_children : template_node list } - | P_ExpressionTemplateNode of { template_expression_node_expression : expression } - | P_TextTemplateNode of { text_template_node_text : string } + | P_FragmentTemplateNode of template_node list + | P_ExpressionTemplateNode of expression + | P_TemplateComment of string + | P_TextTemplateNode of string and tag = { tag_loc : Pinc_Diagnostics.Location.t; @@ -71,7 +72,7 @@ and expression = { } and expression_desc = - | P_Comment of string + | P_Void | P_String of string_template list | P_Char of Uchar.t | P_Int of int diff --git a/lib/02_parsing/Token.ml b/lib/02_parsing/Token.ml index 5d81055..f2bcc43 100644 --- a/lib/02_parsing/Token.ml +++ b/lib/02_parsing/Token.ml @@ -65,6 +65,7 @@ type token_type = | KEYWORD_LIBRARY | KEYWORD_PAGE | KEYWORD_STORE + | TEMPLATE_COMMENT of string | HTML_DOCTYPE of string | HTML_TEXT of string | HTML_OPEN_FRAGMENT @@ -152,6 +153,7 @@ let to_string = function | KEYWORD_PAGE -> "page" | KEYWORD_STORE -> "store" | COMMENT s -> "/* " ^ s ^ " */" + | TEMPLATE_COMMENT s -> "/* " ^ s ^ " */" | HTML_DOCTYPE s -> s | HTML_TEXT s -> s | HTML_OPEN_FRAGMENT -> "<>" @@ -225,6 +227,7 @@ let is_keyword = function | LESS_EQUAL | PLUSPLUS | KEYWORD_REVERSE + | TEMPLATE_COMMENT _ | HTML_DOCTYPE _ | HTML_TEXT _ | HTML_OPEN_FRAGMENT diff --git a/lib/02_parsing/Token.mli b/lib/02_parsing/Token.mli index 36018a8..d0c214e 100644 --- a/lib/02_parsing/Token.mli +++ b/lib/02_parsing/Token.mli @@ -65,6 +65,7 @@ type token_type = | KEYWORD_LIBRARY | KEYWORD_PAGE | KEYWORD_STORE + | TEMPLATE_COMMENT of string | HTML_DOCTYPE of string | HTML_TEXT of string | HTML_OPEN_FRAGMENT diff --git a/lib/02_parsing/Transformer.ml b/lib/02_parsing/Transformer.ml index 82abd26..d1d369b 100644 --- a/lib/02_parsing/Transformer.ml +++ b/lib/02_parsing/Transformer.ml @@ -42,7 +42,7 @@ let rec transform_lowercase_id env = function and transform_uppercase_id env = function | Parsetree.P_Uppercase_Id (id, loc) -> (env, Uppercase_Id (id, loc)) -and transform_comment env comment = (env, Comment comment) +and transform_void env = (env, Void) and transform_string env templates = let transform_string_template env (template : Parsetree.string_template) = @@ -396,7 +396,7 @@ and transform_fragment_template_node env ~fragement_children = let env, fragement_children = List.fold_map ~init:env ~f:transform_template_node fragement_children in - (env, FragmentTemplateNode { fragement_children }) + (env, FragmentTemplateNode fragement_children) and transform_component_template_node env @@ -422,14 +422,14 @@ and transform_expression_template_node env ~template_expression_node_expression let env, template_expression_node_expression = transform_expression env template_expression_node_expression in - (env, ExpressionTemplateNode { template_expression_node_expression }) + (env, ExpressionTemplateNode template_expression_node_expression) -and transform_text_template_node env ~text_template_node_text = - (env, TextTemplateNode { text_template_node_text }) +and transform_text_template_node env s = (env, TextTemplateNode s) and transform_template_node env (node : Parsetree.template_node) = let env, desc = match node.template_node_desc with + | P_TemplateComment _ -> (env, TextTemplateNode "") | P_HtmlTemplateNode { html_tag_identifier; @@ -450,12 +450,12 @@ and transform_template_node env (node : Parsetree.template_node) = ~component_tag_identifier ~component_tag_attributes ~component_tag_children - | P_FragmentTemplateNode { fragement_children } -> + | P_FragmentTemplateNode fragement_children -> transform_fragment_template_node env ~fragement_children - | P_ExpressionTemplateNode { template_expression_node_expression } -> + | P_ExpressionTemplateNode template_expression_node_expression -> transform_expression_template_node env ~template_expression_node_expression - | P_TextTemplateNode { text_template_node_text } -> - transform_text_template_node env ~text_template_node_text + | P_TextTemplateNode text_template_node_text -> + transform_text_template_node env text_template_node_text in (env, { template_node_loc = node.template_node_loc; template_node_desc = desc }) @@ -466,7 +466,7 @@ and transform_template env node = and transform_expression env (exression : Parsetree.expression) = let env, desc = match exression.expression_desc with - | P_Comment comment -> transform_comment env comment + | P_Void -> transform_void env | P_String templates -> transform_string env templates | P_Char c -> transform_char env c | P_Int i -> transform_int env i diff --git a/lib/pinc_backend/Interpreter.ml b/lib/pinc_backend/Interpreter.ml index 93a551b..4ace001 100644 --- a/lib/pinc_backend/Interpreter.ml +++ b/lib/pinc_backend/Interpreter.ml @@ -92,7 +92,7 @@ and eval_statement ~state statement = and eval_expression ~state expression = match expression.expression_desc with - | Ast.Comment _ -> state + | Ast.Void -> state | Ast.Char c -> state |> State.add_output ~output:(Helpers.Value.char ~loc:expression.expression_loc c) @@ -1222,7 +1222,7 @@ and eval_block ~state statements = and eval_template ~state template = match template.template_node_desc with - | Ast.TextTemplateNode { text_template_node_text } -> + | Ast.TextTemplateNode text_template_node_text -> state |> State.add_output ~output: @@ -1256,7 +1256,7 @@ and eval_template ~state template = html_tag_children, html_tag_self_closing ); } - | Ast.FragmentTemplateNode { fragement_children } -> + | Ast.FragmentTemplateNode fragement_children -> let children = fragement_children |> List.map (fun child -> eval_template ~state child |> State.get_output) @@ -1269,7 +1269,7 @@ and eval_template ~state template = value_loc = template.template_node_loc; value_desc = FragmentTemplateNode children; } - | Ast.ExpressionTemplateNode { template_expression_node_expression } -> + | Ast.ExpressionTemplateNode template_expression_node_expression -> eval_expression ~state template_expression_node_expression | Ast.ComponentTemplateNode { diff --git a/lib/pinc_format/Formatter.ml b/lib/pinc_format/Formatter.ml index bda2f28..5a41c55 100644 --- a/lib/pinc_format/Formatter.ml +++ b/lib/pinc_format/Formatter.ml @@ -42,8 +42,33 @@ and format_uppercase_id = function | Parsetree.P_Uppercase_Id (id, _loc) -> string id and format_comment comment = - let comment = nest 2 (ifflat (blank 1) (break 1) ^^ arbitrary_string comment) in - string "/*" ^^ group (comment ^^ ifflat (blank 1) (break 1)) ^^ string "*/" + let starts_with_blank = + String.starts_with ~prefix:" " comment || String.starts_with ~prefix:"\t" comment + in + let ends_with_blank = + String.ends_with ~suffix:" " comment || String.ends_with ~suffix:"\t" comment + in + let comment = + nest + 2 + (ifflat + (if starts_with_blank then + empty + else + blank 1) + (break 1) + ^^ arbitrary_string comment) + in + string "/*" + ^^ group + (comment + ^^ ifflat + (if ends_with_blank then + empty + else + blank 1) + (break 1)) + ^^ string "*/" and format_string templates = let templates = @@ -310,6 +335,7 @@ and format_template_node (node : Parsetree.template_node) = let annotations = format_annotations node.template_node_annotations in let desc = match node.template_node_desc with + | P_TemplateComment s -> braces (format_comment s) | P_HtmlTemplateNode { html_tag_identifier; @@ -328,11 +354,11 @@ and format_template_node (node : Parsetree.template_node) = ~component_tag_identifier ~component_tag_attributes ~component_tag_children - | P_FragmentTemplateNode { fragement_children } -> + | P_FragmentTemplateNode fragement_children -> format_fragment_template_node ~fragement_children - | P_ExpressionTemplateNode { template_expression_node_expression } -> + | P_ExpressionTemplateNode template_expression_node_expression -> format_expression_template_node ~template_expression_node_expression - | P_TextTemplateNode { text_template_node_text } -> + | P_TextTemplateNode text_template_node_text -> format_text_template_node ~text_template_node_text in annotations ^^ desc @@ -341,7 +367,7 @@ and format_expression (exression : Parsetree.expression) = let annotations = format_annotations exression.expression_annotations in let desc = match exression.expression_desc with - | P_Comment comment -> format_comment comment + | P_Void -> empty | P_String templates -> format_string templates | P_Char c -> format_char c | P_Int i -> format_int i diff --git a/test/format/data.pi b/test/format/data.pi index ba45f29..bc67fc2 100644 --- a/test/format/data.pi +++ b/test/format/data.pi @@ -61,6 +61,7 @@ component Component(label: "Long Name of $(c)", icon: "/images/icons/page-docs.s }}

+ {/* A Template Comment */} Aliqua in elit sunt in anim culpa nostrud elit. Elit voluptate nisi laborum est eu esse non exercitation minim. Nisi ea enim minim quis. diff --git a/test/format/run.t b/test/format/run.t index c7aeff3..105bb8f 100644 --- a/test/format/run.t +++ b/test/format/run.t @@ -84,6 +84,7 @@ }}

+ {/* A Template Comment */} Aliqua in elit sunt in anim culpa nostrud elit. Elit voluptate nisi laborum est eu esse non exercitation minim. Nisi ea enim minim quis.