Page
Library
Module
Module type
Parameter
Class
Class type
Source
Tyxml_syntax.Attribute_value
SourceAttribute value parsers and parser combinators.
type 'a gparser =
?separated_by:string ->
?default:string ->
Ppxlib.Location.t ->
string ->
'a ->
Ppxlib.expression option
Attribute value parsers are assigned to each attribute depending on the type of the attribute's argument, though some attributes have special parsers based on their name, or on a [@@reflect]
annotation. A parser is a function p
such that p loc name value
either:
value
into Some
of a parse tree representing that value, for use with attributes that take an argument, orNone
, for use with attributes that take no argument (for instance, a_selected
).For example, int loc name "3"
converts "3"
into the parse tree {pexp_desc = Pexp_constant (Const_int 3); ...}
.
The parse tree is assigned the location loc
. This should be the location of the start of the value string, but, presently, the location of the element containing the value string is used.
name
is the name of the attribute. This is used only for error reporting.
~separated_by
and ~default
are used internally by combinators to modify the error message (for example, to make nouns plural if an error occurs in a list).
option none parser _ _ s
behaves as follows:
s
= none
, evaluates to a parse tree for None
.parser _ _ s
evaluates to a parse tree for e
, option
evaluates to a parse tree for Some e
.spaces parser _ _ s
splits s
on spaces, then applies parser
to each component. The resulting parse trees for e, e', ...
are combined into a parse tree of [e; e'; ...]
.
Similar to spaces
, but splits on both spaces and commas.
Exported parsers should always use one of those combinators last.
wrap parser module_ _ _ s
applies parser _ _ s
to get a parse tree for e
, then evaluates to the parse tree for module_.Xml.W.return e
.
nowrap parser _ _ _ s
evaluates to parser _ _ s
. The purpose of this combinator is to provide a signature similar to wrap
in situations where wrapping is not wanted.
char _ _ s
, where s
is a string containing a single UTF-8 character c
, produces a parse tree for c
of type char
. Note that this means the range is restricted to the first 256 code points.
bool _ _ s
produces a parse tree for the boolean true
if s = "true"
or ""
and false
if s = "false"
.
onoff _ _ s
produces a parse tree for the boolean true
if s = "on"
or ""
and false
if s = "off"
.
float _ _ s
produces a parse tree for float_of_string s
. This is a slight superset of HTML and SVG decimal fraction number syntax.
number_pair _ _ s
produces a parse tree for
n, None
if s
= (string_of_float n)
, orm, Some n'
if s
is a space- or comma-separated list of representations of two floats.Acts as spaces_or_commas float
, but expects the list to have exactly four elements.
icon_size _ _ s
produces a parse tree for the pair (width, height)
when s
has the form (string_of_int width) ^ x ^ (string_of_int height)
and x
is either "x"
or "X"
.
svg_length _ _ s
produces a parse tree for a value of type Svg_types.Unit.(length quantity)
. s
is expected to have form (string_of_float n) ^ unit
for some number n
and a valid SVG length unit, or no unit.
offset _ _ s
produces a parse tree for
`Number n
if s
= string_of_float n
, or`Percentage n
if s
has form (string_of_float n) ^ "%"
.string _ _ s
produces a parse tree for s
. This is intended for ordinary attributes containing text that requires no further parsing.
variant _ _ s
produces a parse tree for the variand Tyxml_name.polyvar s
. This is intended for attributes whose argument type is a polymorphic variant, none of whose constructors take arguments.
total_variant
is used for parsing arguments whose type is a variant with the following pattern:
| `A | `B | `C | `EverythingElse of string
It behaves like variant
for strings matching the no-argument constructors. Any other string s
is mapped to the parse trees for `EverythingElse s
.
variant_or_empty empty
is used for parsing arguments whose type is a variant, possibly the empty string. It behaves like variant
for every string but the empty one, which will be parsed as if it was the empty
parameter.
presence _ _ _
evaluates to None
. It is used as a "parser" for attributes that do not take arguments.