123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239(* This module is a recursive descent parser for the ocamldoc syntax. The parser
consumes a token stream of type [Token.t Stream.t], provided by the lexer,
and produces a comment AST of the type defined in [Parser_.Ast].
The AST has two main levels: inline elements, which can appear inside
paragraphs, and are spaced horizontally when presented, and block elements,
such as paragraphs and lists, which are spaced vertically when presented.
Block elements contain inline elements, but not vice versa.
Corresponding to this, the parser has three "main" functions:
- [delimited_inline_element_list] parses a run of inline elements that is
delimited by curly brace markup ([{...}]).
- [paragraph] parses a run of inline elements that make up a paragraph, and
is not explicitly delimited with curly braces.
- [block_element_list] parses a sequence of block elements. A comment is a
sequence of block elements, so [block_element_list] is the top-level
parser. It is also used for list item and tag content. *)moduleLocation=Odoc_model.Location_moduleError=Odoc_model.ErrormoduleComment=Odoc_model.Commenttype'awith_location='aLocation.with_location(* {2 Input} *)typeinput={tokens:(Token.tLocation.with_location)Stream.t;warnings:Error.warning_accumulator;}letjunkinput=Stream.junkinput.tokensletpeekinput=matchStream.peekinput.tokenswith|Sometoken->token|None->assertfalse(* The last token in the stream is always [`End], and it is never consumed by
the parser, so the [None] case is impossible. *)letnpeekninput=Stream.npeekninput.tokens(* {2 Non-link inline elements} *)(* Convenient abbreviation for use in patterns. *)typetoken_that_always_begins_an_inline_element=[|`Wordofstring|`Code_spanofstring|`Raw_markupofstringoption*string|`Begin_styleofComment.style|`Simple_referenceofstring|`Begin_reference_with_replacement_textofstring|`Simple_linkofstring|`Begin_link_with_replacement_textofstring](* Check that the token constructors above actually are all in [Token.t]. *)let_check_subset:token_that_always_begins_an_inline_element->Token.t=funt->(t:>Token.t)(* Consumes tokens that make up a single non-link inline element:
- a horizontal space ([`Space], significant in inline elements),
- a word (see [word]),
- a code span ([...], [`Code_span _]), or
- styled text ({e ...}).
The latter requires a recursive call to [delimited_inline_element_list],
defined below.
This should be part of [delimited_inline_element_list]; however, it is also
called by function [paragraph]. As a result, it is factored out, and made
mutually-recursive with [delimited_inline_element_list].
This is called only when it is known that the first token in the list is the
beginning of an inline element. In the case of [`Minus] and [`Plus], that
means the caller has determined that they are not a list bullet (i.e., not
the first non-whitespace tokens on their line).
This function consumes exactly the tokens that make up the element. *)letrecinline_element:input->Location.span->_->Ast.inline_elementwith_location=funinputlocationnext_token->matchnext_tokenwith|`Space_astoken->junkinput;Location.atlocationtoken|`Word_astoken->junkinput;Location.atlocationtoken(* This is actually the same memory representation as the token, complete
with location, and is probably the most common case. Perhaps the token
can be reused somehow. The same is true of [`Space], [`Code_span]. *)|`Minus->junkinput;Location.atlocation(`Word"-")|`Plus->junkinput;Location.atlocation(`Word"+")|`Code_spanc->junkinput;Location.atlocation(`Code_spanc)|`Raw_markup(raw_markup_target,s)->junkinput;Location.atlocation(`Raw_markup(raw_markup_target,s))|`Begin_stylesasparent_markup->junkinput;letrequires_leading_whitespace=matchswith|`Bold|`Italic|`Emphasis->true|`Superscript|`Subscript->falseinletcontent,brace_location=delimited_inline_element_list~parent_markup~parent_markup_location:location~requires_leading_whitespaceinputinletlocation=Location.span[location;brace_location]inifcontent=[]thenParse_error.should_not_be_empty~what:(Token.describeparent_markup)location|>Error.warninginput.warnings;Location.atlocation(`Styled(s,content))|`Simple_referencer->junkinput;letr_location=Location.nudge_start(String.length"{!")locationinletr=Location.atr_locationrinLocation.atlocation(`Reference(`Simple,r,[]))|`Begin_reference_with_replacement_textrasparent_markup->junkinput;letr_location=Location.nudge_start(String.length"{{!")locationinletr=Location.atr_locationrinletcontent,brace_location=delimited_inline_element_list~parent_markup~parent_markup_location:location~requires_leading_whitespace:falseinputinletlocation=Location.span[location;brace_location]inifcontent=[]thenParse_error.should_not_be_empty~what:(Token.describeparent_markup)location|>Error.warninginput.warnings;Location.atlocation(`Reference(`With_text,r,content))|`Simple_linku->junkinput;letu=String.trimuinifu=""thenParse_error.should_not_be_empty~what:(Token.describenext_token)location|>Error.warninginput.warnings;Location.atlocation(`Link(u,[]))|`Begin_link_with_replacement_textuasparent_markup->junkinput;letu=String.trimuinifu=""thenParse_error.should_not_be_empty~what:(Token.describeparent_markup)location|>Error.warninginput.warnings;letcontent,brace_location=delimited_inline_element_list~parent_markup~parent_markup_location:location~requires_leading_whitespace:falseinputin`Link(u,content)|>Location.at(Location.span[location;brace_location])(* Consumes tokens that make up a sequence of inline elements that is ended by
a '}', a [`Right_brace] token. The brace token is also consumed.
The sequences are also preceded by some markup like '{b'. Some of these
markup tokens require whitespace immediately after the token, and others not.
The caller indicates which way that is through the
[~requires_leading_whitespace] argument.
Whitespace is significant in inline element lists. In particular, "foo [bar]"
is represented as [`Word "foo"; `Space; `Code_span "bar"], while "foo[bar]"
is [`Word "foo"; `Code_span "bar"]. It doesn't matter how much whitespace is
there, just whether it is present or not. Single newlines and horizontal
space in any amount are allowed. Blank lines are not, as these are separators
for {e block} elements.
In correct input, the first and last elements emitted will not be [`Space],
i.e. [`Space] appears only between other non-link inline elements. In
incorrect input, there might be [`Space] followed immediately by something
like an @author tag.
The [~parent_markup] and [~parent_markup_location] arguments are used for
generating error messages. *)anddelimited_inline_element_list:parent_markup:[<Token.t]->parent_markup_location:Location.span->requires_leading_whitespace:bool->input->(Ast.inline_elementwith_location)list*Location.span=fun~parent_markup~parent_markup_location~requires_leading_whitespaceinput->(* [~at_start_of_line] is used to interpret [`Minus] and [`Plus]. These are
word tokens if not the first non-whitespace tokens on their line. Then,
they are allowed in a non-link element list. *)letrecconsume_elements:at_start_of_line:bool->(Ast.inline_elementwith_location)list->(Ast.inline_elementwith_location)list*Location.span=fun~at_start_of_lineacc->letnext_token=peekinputinmatchnext_token.valuewith|`Right_brace->junkinput;List.revacc,next_token.location(* The [`Space] token is not space at the beginning or end of line, because
that is combined into [`Single_newline] or [`Blank_line] tokens. It is
also not at the beginning of markup (after e.g. '{b'), because that is
handled separately before calling
[consume_non_link_inline_elements], and not immediately before '}',
because that is combined into the [`Right_brace] token by the lexer. So,
it is an internal space, and we want to add it to the non-link inline
element list. *)|`Space_|#token_that_always_begins_an_inline_elementastoken->letacc=(inline_elementinputnext_token.locationtoken)::accinconsume_elements~at_start_of_line:falseacc|`Single_newlinews->junkinput;letelement=Location.samenext_token(`Spacews)inconsume_elements~at_start_of_line:true(element::acc)|`Blank_linewsasblank->Parse_error.not_allowed~what:(Token.describeblank)~in_what:(Token.describeparent_markup)next_token.location|>Error.warninginput.warnings;junkinput;letelement=Location.samenext_token(`Spacews)inconsume_elements~at_start_of_line:true(element::acc)|`Minus|`Plusasbullet->ifat_start_of_linethenbeginletsuggestion=Printf.sprintf"move %s so it isn't the first thing on the line."(Token.printbullet)inParse_error.not_allowed~what:(Token.describebullet)~in_what:(Token.describeparent_markup)~suggestionnext_token.location|>Error.warninginput.warningsend;letacc=(inline_elementinputnext_token.locationbullet)::accinconsume_elements~at_start_of_line:falseacc|other_token->Parse_error.not_allowed~what:(Token.describeother_token)~in_what:(Token.describeparent_markup)next_token.location|>Error.warninginput.warnings;letlast_location=matchaccwith|last_token::_->last_token.location|[]->parent_markup_locationinList.revacc,last_locationinletfirst_token=peekinputinmatchfirst_token.valuewith|`Space_->junkinput;consume_elements~at_start_of_line:false[](* [~at_start_of_line] is [false] here because the preceding token was some
some markup like '{b', and we didn't move to the next line, so the next
token will not be the first non-whitespace token on its line. *)|`Single_newline_->junkinput;consume_elements~at_start_of_line:true[]|`Blank_line_asblank->(* In case the markup is immediately followed by a blank line, the error
message printed by the catch-all case below can be confusing, as it will
suggest that the markup must be followed by a newline (which it is). It
just must not be followed by two newlines. To explain that clearly,
handle that case specifically. *)Parse_error.not_allowed~what:(Token.describeblank)~in_what:(Token.describeparent_markup)first_token.location|>Error.warninginput.warnings;junkinput;consume_elements~at_start_of_line:true[]|`Right_brace->junkinput;[],first_token.location|_->ifrequires_leading_whitespacethenbeginParse_error.should_be_followed_by_whitespace~what:(Token.printparent_markup)parent_markup_location|>Error.warninginput.warningsend;consume_elements~at_start_of_line:false[](* {2 Paragraphs} *)(* Consumes tokens that make up a paragraph.
A paragraph is a sequence of inline elements that ends on a blank line, or
explicit block markup such as a verbatim block on a new line.
Because of the significance of newlines, paragraphs are parsed line-by-line.
The function [paragraph] is called only when the current token is the first
non-whitespace token on its line, and begins an inline element. [paragraph]
then parses a line of inline elements. Afterwards, it looks ahead to the next
line. If that line also begins with an inline element, it parses that line,
and so on. *)letparagraph:input->Ast.nestable_block_elementwith_location=funinput->(* Parses a single line of a paragraph, consisting of inline elements. The
only valid ways to end a paragraph line are with [`End], [`Single_newline],
[`Blank_line], and [`Right_brace]. Everything else either belongs in the
paragraph, or signifies an attempt to begin a block element inside a
paragraph line, which is an error. These errors are caught elsewhere; the
paragraph parser just stops. *)letrecparagraph_line:(Ast.inline_elementwith_location)list->(Ast.inline_elementwith_location)list=funacc->letnext_token=peekinputinmatchnext_token.valuewith|`Space_|`Minus|`Plus|#token_that_always_begins_an_inline_elementastoken->letelement=inline_elementinputnext_token.locationtokeninparagraph_line(element::acc)|_->accin(* After each line is parsed, decides whether to parse more lines. *)letrecadditional_lines:(Ast.inline_elementwith_location)list->(Ast.inline_elementwith_location)list=funacc->matchnpeek2inputwith|{value=`Single_newlinews;location}::{value=#token_that_always_begins_an_inline_element;_}::_->junkinput;letacc=(Location.atlocation(`Spacews))::accinletacc=paragraph_lineaccinadditional_linesacc|_->List.revaccinletelements=paragraph_line[]|>additional_linesin`Paragraphelements|>Location.at(Location.span(List.mapLocation.locationelements))(* {2 Block elements} *)(* {3 Helper types} *)(* The interpretation of tokens in the block parser depends on where on a line
each token appears. The five possible "locations" are:
- [`At_start_of_line], when only whitespace has been read on the current
line.
- [`After_tag], when a valid tag token, such as [@deprecated], has been read,
and only whitespace has been read since.
- [`After_shorthand_bullet], when a valid shorthand list item bullet, such as
[-], has been read, and only whitespace has been read since.
- [`After_explicit_list_bullet], when a valid explicit bullet, such as [{li],
has been read, and only whitespace has been read since.
- [`After_text], when any other valid non-whitespace token has already been
read on the current line.
Here are some examples of how this affects the interpretation of tokens:
- A paragraph can start anywhere except [`After_text] (two paragraphs cannot
be on the same line, but paragraphs can be nested in just about anything).
- [`Minus] is interpreted as a list item bullet [`At_start_of_line],
[`After_tag], and [`After_explicit_list_bullet].
- Tags are only allowed [`At_start_of_line].
To track the location accurately, the functions that make up the block parser
pass explicit [where_in_line] values around and return them.
In a few cases, [where_in_line] can be inferred from what helper was called.
For example, the [paragraph] parser always stops on the same line as the last
significant token that is in the paragraph it consumed, so the location must
be [`After_text]. *)typewhere_in_line=[|`At_start_of_line|`After_tag|`After_shorthand_bullet|`After_explicit_list_bullet|`After_text](* The block parsing loop, function [block_element_list], stops when it
encounters certain tokens.
When it is called for the whole comment, or for in explicit list item
([{li foo}]), it can only stop on end of input or a right brace.
When it is called inside a shorthand list item ([- foo]), it stops on end of
input, right brace, a blank line (indicating end of shorthand list), plus or
minus (indicating the start of the next liste item), or a section heading or
tag, which cannot be nested in list markup.
The block parser [block_element_list] explicitly returns the token that
stopped it, with a type more precise than [Token.t stream_head]: if it was
called for the whole comment or an explicit list item, the stop token will
have type [stops_at_delimiters stream_head], and if it was called for a
shorthand list item, the stop token will have type
[implicit_stop stream_head]. This allows the calling parsers to write precise
cases for exactly the tokens that might be at the front of the stream after
the block parser returns. *)typestops_at_delimiters=[|`End|`Right_brace]typestopped_implicitly=[|`End|`Blank_lineofstring|`Right_brace|`Minus|`Plus|Token.section_heading|Token.tag](* Ensure that the above two types are really subsets of [Token.t]. *)let_check_subset:stops_at_delimiters->Token.t=funt->(t:>Token.t)let_check_subset:stopped_implicitly->Token.t=funt->(t:>Token.t)(* The different contexts in which the block parser [block_element_list] can be
called. The block parser's behavior depends somewhat on the context. For
example, while paragraphs are allowed anywhere, shorthand lists are not
allowed immediately inside other shorthand lists, while tags are not allowed
anywhere except at the comment top level.
Besides telling the block parser how to behave, each context also carries two
types, which determine the return type of the block parser:
- The type of blocks the parser returns. Note that [nestable_block_element]
is included in [block_element]. However, the extra block kinds in
[block_element] are only allowed at the comment top level.
- The type of token that the block parser stops at. See discussion above. *)type('block,'stops_at_which_tokens)context=|Top_level:(Ast.block_element,stops_at_delimiters)context|In_shorthand_list:(Ast.nestable_block_element,stopped_implicitly)context|In_explicit_list:(Ast.nestable_block_element,stops_at_delimiters)context|In_tag:(Ast.nestable_block_element,Token.t)context(* This is a no-op. It is needed to prove to the type system that nestable block
elements are acceptable block elements in all contexts. *)letaccepted_in_all_contexts:typeblockstops_at_which_tokens.(block,stops_at_which_tokens)context->Ast.nestable_block_element->block=funcontextblock->matchcontextwith|Top_level->(block:>Ast.block_element)|In_shorthand_list->block|In_explicit_list->block|In_tag->block(* Converts a tag to a series of words. This is used in error recovery, when a
tag cannot be generated. *)lettag_to_words=function|`Authors->[`Word"@author";`Space" ";`Words]|`Befores->[`Word"@before";`Space" ";`Words]|`Canonicals->[`Word"@canonical";`Space" ";`Words]|`Deprecated->[`Word"@deprecated"]|`Inline->[`Word"@inline"]|`Open->[`Word"@open"]|`Closed->[`Word"@closed"]|`Params->[`Word"@param";`Space" ";`Words]|`Raises->[`Word"@raise";`Space" ";`Words]|`Return->[`Word"@return"]|`See(`Document,s)->[`Word"@see";`Space" ";`Word("\""^s^"\"")]|`See(`File,s)->[`Word"@see";`Space" ";`Word("'"^s^"'")]|`See(`Url,s)->[`Word"@see";`Space" ";`Word("<"^s^">")]|`Sinces->[`Word"@since";`Space" ";`Words]|`Versions->[`Word"@version";`Space" ";`Words](* {3 Block element lists} *)(* Consumes tokens making up a sequence of block elements. These are:
- paragraphs,
- code blocks,
- verbatim text blocks,
- lists, and
- section headings. *)letrecblock_element_list:typeblockstops_at_which_tokens.(block,stops_at_which_tokens)context->parent_markup:[<Token.t|`Comment]->input->(blockwith_location)list*stops_at_which_tokenswith_location*where_in_line=funcontext~parent_markupinput->letrecconsume_block_elements:parsed_a_tag:bool->where_in_line->(blockwith_location)list->(blockwith_location)list*stops_at_which_tokenswith_location*where_in_line=fun~parsed_a_tagwhere_in_lineacc->letdescribetoken=matchtokenwith|#token_that_always_begins_an_inline_element->"paragraph"|_->Token.describetokeninletwarn_if_after_text{Location.location;value=token}=ifwhere_in_line=`After_textthenParse_error.should_begin_on_its_own_line~what:(describetoken)location|>Error.warninginput.warningsinletwarn_if_after_tags{Location.location;value=token}=ifparsed_a_tagthenletsuggestion=Printf.sprintf"move %s before any tags."(Token.describetoken)inParse_error.not_allowed~what:(describetoken)~in_what:"the tags section"~suggestionlocation|>Error.warninginput.warningsinletwarn_because_not_at_top_level{Location.location;value=token}=letsuggestion=Printf.sprintf"move %s outside of any other markup."(Token.printtoken)inParse_error.not_allowed~what:(Token.describetoken)~in_what:(Token.describeparent_markup)~suggestionlocation|>Error.warninginput.warningsinmatchpeekinputwith(* Terminators: the two tokens that terminate anything. *)|{value=`End;_}|{value=`Right_brace;_}asnext_token->(* This little absurdity is needed to satisfy the type system. Without it,
OCaml is unable to prove that [stream_head] has the right type for all
possible values of [context]. *)beginmatchcontextwith|Top_level->List.revacc,next_token,where_in_line|In_shorthand_list->List.revacc,next_token,where_in_line|In_explicit_list->List.revacc,next_token,where_in_line|In_tag->List.revacc,next_token,where_in_lineend(* Whitespace. This can terminate some kinds of block elements. It is also
necessary to track it to interpret [`Minus] and [`Plus] correctly, as
well as to ensure that all block elements begin on their own line. *)|{value=`Space_;_}->junkinput;consume_block_elements~parsed_a_tagwhere_in_lineacc|{value=`Single_newline_;_}->junkinput;consume_block_elements~parsed_a_tag`At_start_of_lineacc|{value=`Blank_line_;_}asnext_token->beginmatchcontextwith(* Blank lines terminate shorthand lists ([- foo]). They also terminate
paragraphs, but the paragraph parser is aware of that internally. *)|In_shorthand_list->List.revacc,next_token,where_in_line(* Otherwise, blank lines are pretty much like single newlines. *)|_->junkinput;consume_block_elements~parsed_a_tag`At_start_of_lineaccend(* Explicit list items ([{li ...}] and [{- ...}]) can never appear directly
in block content. They can only appear inside [{ul ...}] and [{ol ...}].
So, catch those. *)|{value=`Begin_list_item_astoken;location}->letsuggestion=Printf.sprintf"move %s into %s, or use %s."(Token.printtoken)(Token.describe(`Begin_list`Unordered))(Token.describe(`Minus))inParse_error.not_allowed~what:(Token.describetoken)~in_what:(Token.describeparent_markup)~suggestionlocation|>Error.warninginput.warnings;junkinput;consume_block_elements~parsed_a_tagwhere_in_lineacc(* Tags. These can appear at the top level only. Also, once one tag is seen,
the only top-level elements allowed are more tags. *)|{value=`Tagtagastoken;location}asnext_token->letrecover_when_not_at_top_levelcontext=warn_because_not_at_top_levelnext_token;junkinput;letwords=List.map(Location.atlocation)(tag_to_wordstag)inletparagraph=`Paragraphwords|>accepted_in_all_contextscontext|>Location.atlocationinconsume_block_elements~parsed_a_tag`At_start_of_line(paragraph::acc)inbeginmatchcontextwith(* Tags cannot make sense in an explicit list ([{ul {li ...}}]). *)|In_explicit_list->recover_when_not_at_top_levelcontext(* If a tag starts at the beginning of a line, it terminates the preceding
tag and/or the current shorthand list. In this case, return to the
caller, and let the caller decide how to interpret the tag token. *)|In_shorthand_list->ifwhere_in_line=`At_start_of_linethenList.revacc,next_token,where_in_lineelserecover_when_not_at_top_levelcontext|In_tag->ifwhere_in_line=`At_start_of_linethenList.revacc,next_token,where_in_lineelserecover_when_not_at_top_levelcontext(* If this is the top-level call to [block_element_list], parse the
tag. *)|Top_level->ifwhere_in_line<>`At_start_of_linethenParse_error.should_begin_on_its_own_line~what:(Token.describetoken)location|>Error.warninginput.warnings;junkinput;beginmatchtagwith|`Authors|`Sinces|`Versions|`Canonicalsastag->lets=String.trimsinifs=""thenParse_error.should_not_be_empty~what:(Token.describetoken)location|>Error.warninginput.warnings;lettag=matchtagwith|`Author_->`Authors|`Since_->`Sinces|`Version_->`Versions|`Canonical_->(* TODO The location is only approximate, as we need lexer
cooperation to get the real location. *)letr_location=Location.nudge_start(String.length"@canonical ")locationin`Canonical(Location.atr_locations)inlettag=Location.atlocation(`Tagtag)inconsume_block_elements~parsed_a_tag:true`After_text(tag::acc)|`Deprecated|`Returnastag->letcontent,_stream_head,where_in_line=block_element_listIn_tag~parent_markup:tokeninputinlettag=matchtagwith|`Deprecated->`Deprecatedcontent|`Return->`Returncontentinletlocation=location::(List.mapLocation.locationcontent)|>Location.spaninlettag=Location.atlocation(`Tagtag)inconsume_block_elements~parsed_a_tag:truewhere_in_line(tag::acc)|`Param_|`Raise_|`Before_astag->letcontent,_stream_head,where_in_line=block_element_listIn_tag~parent_markup:tokeninputinlettag=matchtagwith|`Params->`Param(s,content)|`Raises->`Raise(s,content)|`Befores->`Before(s,content)inletlocation=location::(List.mapLocation.locationcontent)|>Location.spaninlettag=Location.atlocation(`Tagtag)inconsume_block_elements~parsed_a_tag:truewhere_in_line(tag::acc)|`See(kind,target)->letcontent,_next_token,where_in_line=block_element_listIn_tag~parent_markup:tokeninputinletlocation=location::(List.mapLocation.locationcontent)|>Location.spaninlettag=`Tag(`See(kind,target,content))inlettag=Location.atlocationtaginconsume_block_elements~parsed_a_tag:truewhere_in_line(tag::acc)|`Inline|`Open|`Closedastag->lettag=Location.atlocation(`Tagtag)inconsume_block_elements~parsed_a_tag:true`After_text(tag::acc)endend|{value=#token_that_always_begins_an_inline_element;_}asnext_token->warn_if_after_tagsnext_token;warn_if_after_textnext_token;letblock=paragraphinputinletblock=Odoc_model.Location_.map(accepted_in_all_contextscontext)blockinletacc=block::accinconsume_block_elements~parsed_a_tag`After_textacc|{value=`Code_blocks|`Verbatimsastoken;location}asnext_token->warn_if_after_tagsnext_token;warn_if_after_textnext_token;ifs=""thenParse_error.should_not_be_empty~what:(Token.describetoken)location|>Error.warninginput.warnings;junkinput;letblock=matchtokenwith|`Code_block_->`Code_blocks|`Verbatim_->`Verbatimsinletblock=accepted_in_all_contextscontextblockinletblock=Location.atlocationblockinletacc=block::accinconsume_block_elements~parsed_a_tag`After_textacc|{value=`Modulessastoken;location}asnext_token->warn_if_after_tagsnext_token;warn_if_after_textnext_token;junkinput;(* TODO Use some library for a splitting function, or move this out into a
Util module. *)letsplit_stringdelimiterss=letrecscan_delimitersaccindex=ifindex>=String.lengthsthenList.revaccelseifString.containsdelimiterss.[index]thenscan_delimitersacc(index+1)elsescan_wordaccindex(index+1)andscan_wordaccstart_indexindex=ifindex>=String.lengthsthenletword=String.subsstart_index(index-start_index)inList.rev(word::acc)elseifString.containsdelimiterss.[index]thenletword=String.subsstart_index(index-start_index)inscan_delimiters(word::acc)(index+1)elsescan_wordaccstart_index(index+1)inscan_delimiters[]0in(* TODO Correct locations await a full implementation of {!modules}
parsing. *)letmodules=split_string" \t\r\n"s|>List.map(funr->Location.atlocationr)inifmodules=[]thenParse_error.should_not_be_empty~what:(Token.describetoken)location|>Error.warninginput.warnings;letblock=accepted_in_all_contextscontext(`Modulesmodules)inletblock=Location.atlocationblockinletacc=block::accinconsume_block_elements~parsed_a_tag`After_textacc|{value=`Begin_listkindastoken;location}asnext_token->warn_if_after_tagsnext_token;warn_if_after_textnext_token;junkinput;letitems,brace_location=explicit_list_items~parent_markup:tokeninputinifitems=[]thenParse_error.should_not_be_empty~what:(Token.describetoken)location|>Error.warninginput.warnings;letlocation=Location.span[location;brace_location]inletblock=`List(kind,`Heavy,items)inletblock=accepted_in_all_contextscontextblockinletblock=Location.atlocationblockinletacc=block::accinconsume_block_elements~parsed_a_tag`After_textacc|{value=`Minus|`Plusastoken;location}asnext_token->beginmatchwhere_in_linewith|`After_text|`After_shorthand_bullet->Parse_error.should_begin_on_its_own_line~what:(Token.describetoken)location|>Error.warninginput.warnings|_->()end;warn_if_after_tagsnext_token;beginmatchcontextwith|In_shorthand_list->List.revacc,next_token,where_in_line|_->letitems,where_in_line=shorthand_list_itemsnext_tokenwhere_in_lineinputinletkind=matchtokenwith|`Minus->`Unordered|`Plus->`Orderedinletlocation=location::(List.mapLocation.location(List.flattenitems))|>Location.spaninletblock=`List(kind,`Light,items)inletblock=accepted_in_all_contextscontextblockinletblock=Location.atlocationblockinletacc=block::accinconsume_block_elements~parsed_a_tagwhere_in_lineaccend|{value=`Begin_section_heading(level,label)astoken;location}asnext_token->warn_if_after_tagsnext_token;letrecover_when_not_at_top_levelcontext=warn_because_not_at_top_levelnext_token;junkinput;letcontent,brace_location=delimited_inline_element_list~parent_markup:token~parent_markup_location:location~requires_leading_whitespace:trueinputinletlocation=Location.span[location;brace_location]inletparagraph=`Paragraphcontent|>accepted_in_all_contextscontext|>Location.atlocationinconsume_block_elements~parsed_a_tag`At_start_of_line(paragraph::acc)inbeginmatchcontextwith|In_shorthand_list->ifwhere_in_line=`At_start_of_linethenList.revacc,next_token,where_in_lineelserecover_when_not_at_top_levelcontext|In_explicit_list->recover_when_not_at_top_levelcontext|In_tag->recover_when_not_at_top_levelcontext|Top_level->ifwhere_in_line<>`At_start_of_linethenParse_error.should_begin_on_its_own_line~what:(Token.describetoken)location|>Error.warninginput.warnings;letlabel=matchlabelwith|Some""->Parse_error.should_not_be_empty~what:"heading label"location|>Error.warninginput.warnings;None|_->labelinjunkinput;letcontent,brace_location=delimited_inline_element_list~parent_markup:token~parent_markup_location:location~requires_leading_whitespace:trueinputinifcontent=[]thenParse_error.should_not_be_empty~what:(Token.describetoken)location|>Error.warninginput.warnings;letlocation=Location.span[location;brace_location]inletheading=`Heading(level,label,content)inletheading=Location.atlocationheadinginletacc=heading::accinconsume_block_elements~parsed_a_tag`After_textaccendinletwhere_in_line=matchcontextwith|Top_level->`At_start_of_line|In_shorthand_list->`After_shorthand_bullet|In_explicit_list->`After_explicit_list_bullet|In_tag->`After_taginconsume_block_elements~parsed_a_tag:falsewhere_in_line[](* {3 Lists} *)(* Consumes a sequence of implicit list items. Each one consists of a [`Minus]
or [`Plus] token, followed by block elements until:
- a blank line, or
- a list bullet of the opposite kind (e.g. [`Plus] for a [`Minus] list).
This function is called when the next token is known to be [`Minus] or
[`Plus]. It consumes that token, and calls the block element parser (see
above). That parser returns to [implicit_list_items] only on [`Blank_line],
[`End], [`Minus] or [`Plus] at the start of a line, or [`Right_brace]. *)andshorthand_list_items:[`Minus|`Plus]with_location->where_in_line->input->((Ast.nestable_block_elementwith_location)list)list*where_in_line=funfirst_tokenwhere_in_lineinput->letbullet_token=first_token.valueinletrecconsume_list_items:[>]with_location->where_in_line->((Ast.nestable_block_elementwith_location)list)list->((Ast.nestable_block_elementwith_location)list)list*where_in_line=funnext_tokenwhere_in_lineacc->matchnext_token.valuewith|`End|`Right_brace|`Blank_line_|`Tag_|`Begin_section_heading_->List.revacc,where_in_line|`Minus|`Plusasbullet->ifbullet=bullet_tokenthenbeginjunkinput;letcontent,stream_head,where_in_line=block_element_listIn_shorthand_list~parent_markup:bulletinputinifcontent=[]thenParse_error.should_not_be_empty~what:(Token.describebullet)next_token.location|>Error.warninginput.warnings;letacc=content::accinconsume_list_itemsstream_headwhere_in_lineaccendelseList.revacc,where_in_lineinconsume_list_items(first_token:>stopped_implicitlywith_location)where_in_line[](* Consumes a sequence of explicit list items (starting with '{li ...}' and
'{-...}', which are represented by [`Begin_list_item _] tokens).
This function is called immediately after '{ul' or '{ol' ([`Begin_list _]) is
read. The only "valid" way to exit is by reading a [`Right_brace] token,
which is consumed.
Whitespace inside the list, but outside list items, is not significant – this
parsing function consumes all of it. Otherwise, only list item start tokens
are accepted. Everything else is an error. *)andexplicit_list_items:parent_markup:[<Token.t]->input->((Ast.nestable_block_elementwith_location)list)list*Location.span=fun~parent_markupinput->letrecconsume_list_items:((Ast.nestable_block_elementwith_location)list)list->((Ast.nestable_block_elementwith_location)list)list*Location.span=funacc->letnext_token=peekinputinmatchnext_token.valuewith|`End->Parse_error.not_allowednext_token.location~what:(Token.describe`End)~in_what:(Token.describeparent_markup)|>Error.warninginput.warnings;List.revacc,next_token.location|`Right_brace->junkinput;List.revacc,next_token.location|`Space_|`Single_newline_|`Blank_line_->junkinput;consume_list_itemsacc|`Begin_list_itemkindastoken->junkinput;(* '{li', represented by [`Begin_list_item `Li], must be followed by
whitespace. *)ifkind=`Lithenbeginmatch(peekinput).valuewith|`Space_|`Single_newline_|`Blank_line_|`Right_brace->()(* The presence of [`Right_brace] above requires some explanation:
- It is better to be silent about missing whitespace if the next
token is [`Right_brace], because the error about an empty list
item will be generated below, and that error is more important to
the user.
- The [`Right_brace] token also happens to include all whitespace
before it, as a convenience for the rest of the parser. As a
result, not ignoring it could be wrong: there could in fact be
whitespace in the concrete syntax immediately after '{li', just
it is not represented as [`Space], [`Single_newline], or
[`Blank_line]. *)|_->Parse_error.should_be_followed_by_whitespacenext_token.location~what:(Token.printtoken)|>Error.warninginput.warningsend;letcontent,token_after_list_item,_where_in_line=block_element_listIn_explicit_list~parent_markup:tokeninputinifcontent=[]thenParse_error.should_not_be_emptynext_token.location~what:(Token.describetoken)|>Error.warninginput.warnings;beginmatchtoken_after_list_item.valuewith|`Right_brace->junkinput|`End->Parse_error.not_allowedtoken_after_list_item.location~what:(Token.describe`End)~in_what:(Token.describetoken)|>Error.warninginput.warningsend;letacc=content::accinconsume_list_itemsacc|token->letsuggestion=matchtokenwith|`Begin_section_heading_|`Tag_->Printf.sprintf"move %s outside the list."(Token.describetoken)|_->Printf.sprintf"move %s into a list item, %s or %s."(Token.describetoken)(Token.print(`Begin_list_item`Li))(Token.print(`Begin_list_item`Dash))inParse_error.not_allowednext_token.location~what:(Token.describetoken)~in_what:(Token.describeparent_markup)~suggestion|>Error.warninginput.warnings;junkinput;consume_list_itemsaccinconsume_list_items[](* {2 Entry point} *)letparsewarningstokens=letinput={tokens;warnings}inletrecparse_block_elements()=letelements,last_token,_where_in_line=block_element_listTop_level~parent_markup:`Commentinputinmatchlast_token.valuewith|`End->elements|`Right_brace->Parse_error.unpaired_right_bracelast_token.location|>Error.warninginput.warnings;letblock=Location.samelast_token(`Paragraph[Location.samelast_token(`Word"}")])injunkinput;elements@(block::(parse_block_elements()))inparse_block_elements()