Skip to content

Commit 12716ae

Browse files
committed
Simplify code-block tag types and parser
1 parent 6583b29 commit 12716ae

File tree

6 files changed

+101
-171
lines changed

6 files changed

+101
-171
lines changed

src/markdown/doc_of_md.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,8 @@ let code_block_to_nestable_block_element ~locator cb m (bs, warns) =
454454
split_info_string_locs ~left_count ~right_count im
455455
in
456456
let env =
457-
if env = "" then None
458-
else
459-
Some
460-
(Loc.at
461-
(textloc_to_loc ~locator env_loc)
462-
[ Loc.at (textloc_to_loc ~locator env_loc) (`Tag env) ])
457+
if env = "" then []
458+
else [ `Tag (Loc.at (textloc_to_loc ~locator env_loc) env) ]
463459
in
464460
let lang = Loc.at (textloc_to_loc ~locator lang_loc) lang in
465461
let metadata = Some { Ast.language = lang; tags = env } in

src/odoc/extract_code.cppo.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ open Odoc_parser
55
let tags_included_in_names names tags =
66
List.exists
77
(function
8-
| {
9-
Loc.value = `Binding ({ Loc.value = "name"; _ }, { Loc.value = n; _ }); _
10-
}
8+
| `Binding ({ Loc.value = "name"; _ }, { Loc.value = n; _ })
119
when List.exists (String.equal n) names ->
1210
true
1311
| _ -> false)
@@ -21,8 +19,8 @@ let needs_extraction names meta =
2119
in
2220
let check_name () =
2321
match meta with
24-
| Some { Ast.tags = Some tags; _ } ->
25-
tags_included_in_names names tags.Loc.value
22+
| Some { Ast.tags; _ } ->
23+
tags_included_in_names names tags
2624
| _ -> false
2725
in
2826
match names with [] -> check_language () | _ :: _ -> check_name ()

src/parser/ast.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ type 'a grid = 'a row list
3737
type 'a abstract_table = 'a grid * alignment option list option
3838

3939
type code_block_tag =
40-
[ `Tag of string | `Binding of string with_location * string with_location ]
40+
[ `Tag of string with_location
41+
| `Binding of string with_location * string with_location ]
4142

42-
type code_block_tags = code_block_tag with_location list
43+
type code_block_tags = code_block_tag list
4344

4445
type code_block_meta = {
4546
language : string with_location;
46-
tags : code_block_tags with_location option;
47+
tags : code_block_tags;
4748
}
4849

4950
type media = Token.media

src/parser/lexer.mll

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ let warning_loc =
8383
let warning =
8484
with_location_adjustments warning_loc
8585

86-
let unescape_tag s start_loc input =
86+
let unescape_tag s offset input =
87+
let start_loc = Lexing.lexeme_start input.lexbuf + String.length offset in
8788
let warn n c =
8889
let start = input.offset_to_location @@ start_loc + n in
8990
let end_ = input.offset_to_location @@ start_loc + n + 2 in
@@ -389,7 +390,7 @@ and token input = parse
389390
in
390391
let emit_truncated_code_block () =
391392
let empty_content = with_location_adjustments (fun _ -> Loc.at) input "" in
392-
emit ~start_offset input (`Code_block (Some (lang_tag, None), delim, empty_content, false))
393+
emit ~start_offset input (`Code_block (Some (lang_tag, []), delim, empty_content, false))
393394
in
394395
(* Disallow result block sections for code blocks without a delimiter.
395396
This avoids the surprising parsing of '][' ending the code block. *)
@@ -401,15 +402,15 @@ and token input = parse
401402
code_block allow_result_block start_offset content_offset metadata
402403
prefix delim input lexbuf
403404
in
404-
match code_block_metadata_tail None input [] lexbuf with
405+
match code_block_metadata_tail input [] lexbuf with
405406
| `Ok metadata -> code_block_with_metadata metadata
406407
| `Eof ->
407408
warning input ~start_offset Parse_error.truncated_code_block_meta;
408409
emit_truncated_code_block ()
409410
| `Invalid_char c ->
410411
warning input ~start_offset
411412
(Parse_error.language_tag_invalid_char lang_tag_ c);
412-
code_block_with_metadata None
413+
code_block_with_metadata []
413414
}
414415

415416
| "{@" horizontal_space* '['
@@ -735,79 +736,41 @@ and bad_markup_recovery start_offset input = parse
735736
(Parse_error.bad_markup ("{" ^ rest) ~suggestion);
736737
emit input (`Code_span text) ~start_offset}
737738

738-
and code_block_metadata_tail start_offset input acc = parse
739-
| (space_char* '[') {
740-
match acc with [] -> `Ok None | _ ->
741-
let start_offset = match start_offset with None -> Lexing.lexeme_start lexbuf | Some s -> s in
742-
let res =
743-
let res = List.rev acc in
744-
with_location_adjustments ~start_offset ~adjust_end_by:"[" (fun _ -> Loc.at) input res
745-
in
746-
`Ok (Some res)
747-
}
748-
| (space_char+ as prefix)
749-
((('"' as quote) (tag_quoted_atom as value) '"')
750-
| (tag_unquoted_atom as value))
739+
and code_block_metadata_atom input = parse
740+
| '"' (tag_quoted_atom as value) '"'
751741
{
752-
let start_offset =
753-
match start_offset with
754-
| None -> Some (Lexing.lexeme_start lexbuf)
755-
| Some _ -> start_offset
756-
in
757-
let adjust_start_by = prefix in
758-
let start_loc =
759-
Lexing.lexeme_start input.lexbuf +
760-
(String.length adjust_start_by) +
761-
(match quote with Some _ -> 1 | _ -> 0)
762-
in
763-
let value = `Tag (unescape_tag value start_loc input) in
764-
let tag =
765-
with_location_adjustments ~adjust_start_by (fun _ -> Loc.at) input value
766-
in
767-
let acc = tag :: acc in
768-
code_block_metadata_tail start_offset input acc lexbuf
769-
}
742+
let adjust_start_by = "\"" in
743+
with_location_adjustments ~adjust_start_by ~adjust_end_by:"\"" (fun _ -> Loc.at) input (unescape_tag value adjust_start_by input) }
744+
| (tag_unquoted_atom as value)
745+
{ with_location_adjustments (fun _ -> Loc.at) input value }
746+
747+
and code_block_metadata_tail input acc = parse
748+
| (space_char* '[') { `Ok (List.rev acc) }
770749
| (space_char+ as prefix)
771-
(((('"' as key_quote) (tag_quoted_atom as key) '"' as full_key) |
772-
((tag_unquoted_atom as key) as full_key))
773-
'='
774-
(((('"' as tag_quote) (tag_quoted_atom as value) '"') as full_value) |
775-
((tag_unquoted_atom as value) as full_value)))
750+
'"' (tag_quoted_atom as key) '"' '='
751+
{
752+
let adjust_start_by = prefix ^ "\"" in
753+
let key = with_location_adjustments ~adjust_start_by ~adjust_end_by:"\"="
754+
(fun _ -> Loc.at) input (unescape_tag key adjust_start_by input) in
755+
let value = code_block_metadata_atom input lexbuf in
756+
code_block_metadata_tail input (`Binding (key, value) :: acc) lexbuf }
757+
| (space_char+ as prefix)
758+
'"' (tag_quoted_atom as tag) '"' {
759+
let adjust_start_by = prefix ^ "\"" in
760+
let tag = with_location_adjustments ~adjust_start_by ~adjust_end_by:"\""
761+
(fun _ -> Loc.at) input (unescape_tag tag adjust_start_by input) in
762+
code_block_metadata_tail input (`Tag tag :: acc) lexbuf }
763+
| (space_char+ as _prefix)
764+
(tag_unquoted_atom as key) '='
776765
{
777-
let start_offset = match start_offset with None -> Some (Lexing.lexeme_start lexbuf) | Some _ -> start_offset in
778-
let key =
779-
let adjust_start_by = prefix in
780-
let adjust_end_by = "=" ^ full_value in
781-
let start_loc =
782-
Lexing.lexeme_start input.lexbuf +
783-
(String.length adjust_start_by) +
784-
(match key_quote with Some _ -> 1 | _ -> 0)
785-
in
786-
let key = unescape_tag key start_loc input in
787-
with_location_adjustments
788-
~adjust_start_by ~adjust_end_by
789-
(fun _ -> Loc.at) input key
790-
in
791-
let value =
792-
let adjust_start_by = prefix ^ full_key ^ "=" in
793-
let start_loc =
794-
Lexing.lexeme_start input.lexbuf +
795-
(String.length adjust_start_by) +
796-
(match key_quote with Some _ -> 2 | _ -> 0) +
797-
(match tag_quote with Some _ -> 1 | _ -> 0)
798-
in
799-
let value = unescape_tag value start_loc input in
800-
with_location_adjustments ~adjust_start_by
801-
(fun _ -> Loc.at) input value
802-
in
803-
let binding = `Binding (key, value) in
804-
let binding =
805-
let adjust_start_by = prefix in
806-
with_location_adjustments ~adjust_start_by (fun _ -> Loc.at) input binding
807-
in
808-
let acc = binding :: acc in
809-
code_block_metadata_tail start_offset input acc lexbuf
810-
}
766+
let key = with_location_adjustments (fun _ -> Loc.at) input key in
767+
let value = code_block_metadata_atom input lexbuf in
768+
code_block_metadata_tail input (`Binding (key, value) :: acc) lexbuf }
769+
| (space_char+ as _prefix)
770+
(tag_unquoted_atom as tag)
771+
{
772+
let tag = with_location_adjustments (fun _ -> Loc.at) input tag in
773+
code_block_metadata_tail input (`Tag tag :: acc) lexbuf }
811774
| _ as c
812775
{ `Invalid_char c }
813776
| eof

0 commit comments

Comments
 (0)