Skip to content

Commit f4a0094

Browse files
panglesdjonludlam
authored andcommitted
Parser: Use implicit ending for tags
They are not necessarily at the end anymore, and the end similarly as light syntax list: on new lines or heading. This is a breaking change, which according to ocaml#1138 is fixing more things than breaking them. It will allow using custom tags and tags in mld files.
1 parent 4beb07a commit f4a0094

File tree

3 files changed

+71
-140
lines changed

3 files changed

+71
-140
lines changed

src/parser/syntax.ml

+43-72
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ type ('block, 'stops_at_which_tokens) context =
580580
| In_explicit_list : (Ast.nestable_block_element, stops_at_delimiters) context
581581
| In_table_cell : (Ast.nestable_block_element, stops_at_delimiters) context
582582
| In_code_results : (Ast.nestable_block_element, code_stop) context
583-
| In_tag : (Ast.nestable_block_element, Token.t) context
583+
| In_tag : (Ast.nestable_block_element, stopped_implicitly) context
584584

585585
(* This is a no-op. It is needed to prove to the type system that nestable block
586586
elements are acceptable block elements in all contexts. *)
@@ -638,13 +638,12 @@ let rec block_element_list :
638638
* where_in_line =
639639
fun context ~parent_markup input ->
640640
let rec consume_block_elements :
641-
parsed_a_tag:bool ->
642641
where_in_line ->
643642
block with_location list ->
644643
block with_location list
645644
* stops_at_which_tokens with_location
646645
* where_in_line =
647-
fun ~parsed_a_tag where_in_line acc ->
646+
fun where_in_line acc ->
648647
let describe token =
649648
match token with
650649
| #token_that_always_begins_an_inline_element -> "paragraph"
@@ -657,16 +656,6 @@ let rec block_element_list :
657656
|> add_warning input
658657
in
659658

660-
let warn_if_after_tags { Loc.location; value = token } =
661-
if parsed_a_tag then
662-
let suggestion =
663-
Printf.sprintf "move %s before any tags." (Token.describe token)
664-
in
665-
Parse_error.not_allowed ~what:(describe token)
666-
~in_what:"the tags section" ~suggestion location
667-
|> add_warning input
668-
in
669-
670659
let warn_because_not_at_top_level { Loc.location; value = token } =
671660
let suggestion =
672661
Printf.sprintf "move %s outside of any other markup."
@@ -700,31 +689,33 @@ let rec block_element_list :
700689
| In_tag -> (List.rev acc, next_token, where_in_line)
701690
| In_code_results ->
702691
junk input;
703-
consume_block_elements ~parsed_a_tag where_in_line acc)
692+
consume_block_elements where_in_line acc)
704693
| { value = `Right_code_delimiter; _ } as next_token -> (
705694
match context with
706695
| In_code_results -> (List.rev acc, next_token, where_in_line)
707696
| _ ->
708697
junk input;
709-
consume_block_elements ~parsed_a_tag where_in_line acc)
698+
consume_block_elements where_in_line acc)
710699
(* Whitespace. This can terminate some kinds of block elements. It is also
711700
necessary to track it to interpret [`Minus] and [`Plus] correctly, as
712701
well as to ensure that all block elements begin on their own line. *)
713702
| { value = `Space _; _ } ->
714703
junk input;
715-
consume_block_elements ~parsed_a_tag where_in_line acc
704+
consume_block_elements where_in_line acc
716705
| { value = `Single_newline _; _ } ->
717706
junk input;
718-
consume_block_elements ~parsed_a_tag `At_start_of_line acc
707+
consume_block_elements `At_start_of_line acc
719708
| { value = `Blank_line _; _ } as next_token -> (
720709
match context with
721-
(* Blank lines terminate shorthand lists ([- foo]). They also terminate
722-
paragraphs, but the paragraph parser is aware of that internally. *)
710+
(* Blank lines terminate shorthand lists ([- foo]) and tags. They also
711+
terminate paragraphs, but the paragraph parser is aware of that
712+
internally. *)
723713
| In_shorthand_list -> (List.rev acc, next_token, where_in_line)
714+
| In_tag -> (List.rev acc, next_token, where_in_line)
724715
(* Otherwise, blank lines are pretty much like single newlines. *)
725716
| _ ->
726717
junk input;
727-
consume_block_elements ~parsed_a_tag `At_start_of_line acc)
718+
consume_block_elements `At_start_of_line acc)
728719
(* Explicit list items ([{li ...}] and [{- ...}]) can never appear directly
729720
in block content. They can only appear inside [{ul ...}] and [{ol ...}].
730721
So, catch those. *)
@@ -740,7 +731,7 @@ let rec block_element_list :
740731
|> add_warning input;
741732

742733
junk input;
743-
consume_block_elements ~parsed_a_tag where_in_line acc
734+
consume_block_elements where_in_line acc
744735
(* Table rows ([{tr ...}]) can never appear directly
745736
in block content. They can only appear inside [{table ...}]. *)
746737
| { value = `Begin_table_row as token; location } ->
@@ -753,7 +744,7 @@ let rec block_element_list :
753744
~suggestion location
754745
|> add_warning input;
755746
junk input;
756-
consume_block_elements ~parsed_a_tag where_in_line acc
747+
consume_block_elements where_in_line acc
757748
(* Table cells ([{th ...}] and [{td ...}]) can never appear directly
758749
in block content. They can only appear inside [{tr ...}]. *)
759750
| { value = `Begin_table_cell _ as token; location } ->
@@ -766,9 +757,8 @@ let rec block_element_list :
766757
~suggestion location
767758
|> add_warning input;
768759
junk input;
769-
consume_block_elements ~parsed_a_tag where_in_line acc
770-
(* Tags. These can appear at the top level only. Also, once one tag is seen,
771-
the only top-level elements allowed are more tags. *)
760+
consume_block_elements where_in_line acc
761+
(* Tags. These can appear at the top level only. *)
772762
| { value = `Tag tag as token; location } as next_token -> (
773763
let recover_when_not_at_top_level context =
774764
warn_because_not_at_top_level next_token;
@@ -779,8 +769,7 @@ let rec block_element_list :
779769
|> accepted_in_all_contexts context
780770
|> Loc.at location
781771
in
782-
consume_block_elements ~parsed_a_tag `At_start_of_line
783-
(paragraph :: acc)
772+
consume_block_elements `At_start_of_line (paragraph :: acc)
784773
in
785774

786775
match context with
@@ -831,8 +820,7 @@ let rec block_element_list :
831820
in
832821

833822
let tag = Loc.at location (`Tag tag) in
834-
consume_block_elements ~parsed_a_tag:true `After_text
835-
(tag :: acc)
823+
consume_block_elements `After_text (tag :: acc)
836824
| (`Deprecated | `Return) as tag ->
837825
let content, _stream_head, where_in_line =
838826
block_element_list In_tag ~parent_markup:token input
@@ -846,8 +834,7 @@ let rec block_element_list :
846834
location :: List.map Loc.location content |> Loc.span
847835
in
848836
let tag = Loc.at location (`Tag tag) in
849-
consume_block_elements ~parsed_a_tag:true where_in_line
850-
(tag :: acc)
837+
consume_block_elements where_in_line (tag :: acc)
851838
| (`Param _ | `Raise _ | `Before _) as tag ->
852839
let content, _stream_head, where_in_line =
853840
block_element_list In_tag ~parent_markup:token input
@@ -862,8 +849,7 @@ let rec block_element_list :
862849
location :: List.map Loc.location content |> Loc.span
863850
in
864851
let tag = Loc.at location (`Tag tag) in
865-
consume_block_elements ~parsed_a_tag:true where_in_line
866-
(tag :: acc)
852+
consume_block_elements where_in_line (tag :: acc)
867853
| `See (kind, target) ->
868854
let content, _next_token, where_in_line =
869855
block_element_list In_tag ~parent_markup:token input
@@ -873,23 +859,19 @@ let rec block_element_list :
873859
in
874860
let tag = `Tag (`See (kind, target, content)) in
875861
let tag = Loc.at location tag in
876-
consume_block_elements ~parsed_a_tag:true where_in_line
877-
(tag :: acc)
862+
consume_block_elements where_in_line (tag :: acc)
878863
| (`Inline | `Open | `Closed | `Hidden) as tag ->
879864
let tag = Loc.at location (`Tag tag) in
880-
consume_block_elements ~parsed_a_tag:true `After_text
881-
(tag :: acc)))
865+
consume_block_elements `After_text (tag :: acc)))
882866
| ( { value = #token_that_always_begins_an_inline_element; _ }
883867
| { value = `Bar; _ } ) as next_token ->
884-
warn_if_after_tags next_token;
885868
warn_if_after_text next_token;
886869

887870
let block = paragraph input in
888871
let block = Loc.map (accepted_in_all_contexts context) block in
889872
let acc = block :: acc in
890-
consume_block_elements ~parsed_a_tag `After_text acc
873+
consume_block_elements `After_text acc
891874
| { value = `Verbatim s as token; location } as next_token ->
892-
warn_if_after_tags next_token;
893875
warn_if_after_text next_token;
894876
if s = "" then
895877
Parse_error.should_not_be_empty ~what:(Token.describe token) location
@@ -899,9 +881,8 @@ let rec block_element_list :
899881
let block = accepted_in_all_contexts context token in
900882
let block = Loc.at location block in
901883
let acc = block :: acc in
902-
consume_block_elements ~parsed_a_tag `After_text acc
884+
consume_block_elements `After_text acc
903885
| { value = `Math_block s as token; location } as next_token ->
904-
warn_if_after_tags next_token;
905886
warn_if_after_text next_token;
906887
if s = "" then
907888
Parse_error.should_not_be_empty ~what:(Token.describe token) location
@@ -911,14 +892,13 @@ let rec block_element_list :
911892
let block = accepted_in_all_contexts context token in
912893
let block = Loc.at location block in
913894
let acc = block :: acc in
914-
consume_block_elements ~parsed_a_tag `After_text acc
895+
consume_block_elements `After_text acc
915896
| {
916897
value =
917898
`Code_block (meta, delim, { value = s; location = v_loc }, has_outputs)
918899
as token;
919900
location;
920901
} as next_token ->
921-
warn_if_after_tags next_token;
922902
warn_if_after_text next_token;
923903
junk input;
924904
let delimiter = if delim = "" then None else Some delim in
@@ -958,9 +938,8 @@ let rec block_element_list :
958938
in
959939
let block = Loc.at location block in
960940
let acc = block :: acc in
961-
consume_block_elements ~parsed_a_tag `After_text acc
941+
consume_block_elements `After_text acc
962942
| { value = `Modules s as token; location } as next_token ->
963-
warn_if_after_tags next_token;
964943
warn_if_after_text next_token;
965944

966945
junk input;
@@ -999,9 +978,8 @@ let rec block_element_list :
999978
let block = accepted_in_all_contexts context (`Modules modules) in
1000979
let block = Loc.at location block in
1001980
let acc = block :: acc in
1002-
consume_block_elements ~parsed_a_tag `After_text acc
981+
consume_block_elements `After_text acc
1003982
| { value = `Begin_list kind as token; location } as next_token ->
1004-
warn_if_after_tags next_token;
1005983
warn_if_after_text next_token;
1006984

1007985
junk input;
@@ -1018,10 +996,9 @@ let rec block_element_list :
1018996
let block = accepted_in_all_contexts context block in
1019997
let block = Loc.at location block in
1020998
let acc = block :: acc in
1021-
consume_block_elements ~parsed_a_tag `After_text acc
999+
consume_block_elements `After_text acc
10221000
| { value = (`Begin_table_light | `Begin_table_heavy) as token; location }
10231001
as next_token ->
1024-
warn_if_after_tags next_token;
10251002
warn_if_after_text next_token;
10261003
junk input;
10271004
let block, brace_location =
@@ -1037,7 +1014,7 @@ let rec block_element_list :
10371014
let block = accepted_in_all_contexts context (`Table block) in
10381015
let block = Loc.at location block in
10391016
let acc = block :: acc in
1040-
consume_block_elements ~parsed_a_tag `After_text acc
1017+
consume_block_elements `After_text acc
10411018
| { value = (`Minus | `Plus) as token; location } as next_token -> (
10421019
(match where_in_line with
10431020
| `After_text | `After_shorthand_bullet ->
@@ -1046,8 +1023,6 @@ let rec block_element_list :
10461023
|> add_warning input
10471024
| _ -> ());
10481025

1049-
warn_if_after_tags next_token;
1050-
10511026
match context with
10521027
| In_shorthand_list -> (List.rev acc, next_token, where_in_line)
10531028
| _ ->
@@ -1064,11 +1039,9 @@ let rec block_element_list :
10641039
let block = accepted_in_all_contexts context block in
10651040
let block = Loc.at location block in
10661041
let acc = block :: acc in
1067-
consume_block_elements ~parsed_a_tag where_in_line acc)
1042+
consume_block_elements where_in_line acc)
10681043
| { value = `Begin_section_heading (level, label) as token; location } as
10691044
next_token -> (
1070-
warn_if_after_tags next_token;
1071-
10721045
let recover_when_not_at_top_level context =
10731046
warn_because_not_at_top_level next_token;
10741047
junk input;
@@ -1083,8 +1056,7 @@ let rec block_element_list :
10831056
|> accepted_in_all_contexts context
10841057
|> Loc.at location
10851058
in
1086-
consume_block_elements ~parsed_a_tag `At_start_of_line
1087-
(paragraph :: acc)
1059+
consume_block_elements `At_start_of_line (paragraph :: acc)
10881060
in
10891061

10901062
match context with
@@ -1094,7 +1066,10 @@ let rec block_element_list :
10941066
else recover_when_not_at_top_level context
10951067
| In_explicit_list -> recover_when_not_at_top_level context
10961068
| In_table_cell -> recover_when_not_at_top_level context
1097-
| In_tag -> recover_when_not_at_top_level context
1069+
| In_tag ->
1070+
if where_in_line = `At_start_of_line then
1071+
(List.rev acc, next_token, where_in_line)
1072+
else recover_when_not_at_top_level context
10981073
| In_code_results -> recover_when_not_at_top_level context
10991074
| Top_level ->
11001075
if where_in_line <> `At_start_of_line then
@@ -1127,7 +1102,7 @@ let rec block_element_list :
11271102
let heading = `Heading (level, label, content) in
11281103
let heading = Loc.at location heading in
11291104
let acc = heading :: acc in
1130-
consume_block_elements ~parsed_a_tag `After_text acc)
1105+
consume_block_elements `After_text acc)
11311106
| { value = `Begin_paragraph_style _ as token; location } ->
11321107
junk input;
11331108
let content, brace_location =
@@ -1146,13 +1121,11 @@ let rec block_element_list :
11461121
|> accepted_in_all_contexts context
11471122
|> Loc.at location
11481123
in
1149-
consume_block_elements ~parsed_a_tag `At_start_of_line (paragraph :: acc)
1124+
consume_block_elements `At_start_of_line (paragraph :: acc)
11501125
| {
1151-
location;
1152-
value = `Media_with_replacement_text (href, media, content) as token;
1153-
} as next_token ->
1154-
warn_if_after_tags next_token;
1155-
1126+
location;
1127+
value = `Media_with_replacement_text (href, media, content) as token;
1128+
} ->
11561129
junk input;
11571130

11581131
let r_location =
@@ -1181,10 +1154,8 @@ let rec block_element_list :
11811154
let block = accepted_in_all_contexts context block in
11821155
let block = Loc.at location block in
11831156
let acc = block :: acc in
1184-
consume_block_elements ~parsed_a_tag `After_text acc
1185-
| { location; value = `Simple_media (href, media) } as next_token ->
1186-
warn_if_after_tags next_token;
1187-
1157+
consume_block_elements `After_text acc
1158+
| { location; value = `Simple_media (href, media) } ->
11881159
junk input;
11891160

11901161
let r_location =
@@ -1198,7 +1169,7 @@ let rec block_element_list :
11981169
let block = accepted_in_all_contexts context block in
11991170
let block = Loc.at location block in
12001171
let acc = block :: acc in
1201-
consume_block_elements ~parsed_a_tag `After_text acc
1172+
consume_block_elements `After_text acc
12021173
in
12031174

12041175
let where_in_line =
@@ -1211,7 +1182,7 @@ let rec block_element_list :
12111182
| In_tag -> `After_tag
12121183
in
12131184

1214-
consume_block_elements ~parsed_a_tag:false where_in_line []
1185+
consume_block_elements where_in_line []
12151186

12161187
(* {3 Lists} *)
12171188

0 commit comments

Comments
 (0)