package cascade
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=c11bbdc7ab0eee8c03cd162e3e7fd1cb20de62beb13342b3b150a89264ee0056
sha512=43fd97a55c3b1dc4db5c87aa1a5a047d902e902ab2064af3a7c5bf945ebc333f8cd90ecca8013c17be5b950498eace8e0094ee4319eba408285e2bec8d7cdf7b
doc/cascade/Cascade/Cursor/index.html
Module Cascade.CursorSource
Stream cursor over Component.t list.
Validators (Selector, Values, Properties, ...) consume component values rather than raw characters. Cursor.t is the moral equivalent of Reader.t for that input: a mutable cursor with peek / next plus a battery of token-shape helpers (ident, number, parens, ...).
Whitespace components are leading-trimmed by every typed helper, so pattern matching stays simple at call sites.
val of_components :
?source:string ->
?recover:bool ->
?meta:Loc.meta_level ->
?eof_loc:Loc.t ->
Component.t list ->
tof_components ?source ?recover ?meta ?eof_loc cvs is a fresh cursor over cvs. Pass ?source so errors raised while consuming the cursor get a source-context snippet attached (matching of_string's behaviour). Pass ~recover:true to enable per-declaration recovery: validators that honour recover will catch a Parse_error on one declaration, push it to push_warning, and skip to the next ; instead of propagating. ?meta controls snippet construction and defaults to Loc.default_meta_level. ?eof_loc anchors end-of-input errors at a specific location.
sub ?eof_loc parent cvs is a fresh cursor over cvs that inherits parent's source, warnings list, recovery mode, and meta level. Pass ?eof_loc to anchor end-of-input errors at a specific location (e.g. the closing delimiter of a block); defaults to parent's own eof_loc.
func_sub fn parent is a sub-cursor over fn's arguments, anchored so an EOF error inside the function body points at the function's closing ')' rather than the end of the outer input.
recover t is the recovery mode t was built with. Validators that support declaration-level recovery check this to decide whether to catch and skip on a Parse_error or let it propagate.
meta t is the metadata level t was built with. At `Full errors carry source-context snippets; lower levels skip snippet construction.
source t is the preprocessed source text that produced t, when known.
push_warning t e records e as a non-fatal warning on t. A validator in recovery mode catches a Parse_error, pushes it here, skips to a recovery point, and keeps going. Drained via drain_warnings.
drain_warnings t returns and clears the warnings accumulated on t in source order.
of_string ?meta s lexes s into a Component.t list and wraps it. The trailing Eof token is dropped.
of_reader ?meta r consumes the rest of r's input as a component stream and wraps it.
Stream API
peek t is the next component, or None at end of input. Skips any leading whitespace components.
next t consumes and returns the next component (or None at end of input). Skips any leading whitespace components.
remaining t is the un-consumed tail (still includes whitespace).
string_of_components ?trim cvs serializes a component-value list. This is useful for at-rule preludes that must be split structurally before being preserved as raw CSS text.
string_of_remaining t serializes the unconsumed tail without advancing t.
consume_remaining_as_string t serializes and consumes the unconsumed tail.
Whitespace-aware variants
peek / next above transparently skip whitespace components. For grammars where whitespace is significant (CSS selectors -- the descendant combinator is whitespace), use these variants.
peek_raw t is the next component without skipping whitespace.
type head_shape = [ | `Eof| `Semicolon| `Colon| `Comma| `Bang| `Curly_block| `Paren_block| `Square_block| `Ident| `Func| `Other
]Head-shape classification of the next non-whitespace component, returned as a polymorphic variant constant so it carries no allocation.
peek_head_shape t classifies the next non-whitespace component, without boxing the component in Some _.
next_raw t consumes the next component without skipping whitespace.
skip_ws t consumes any leading whitespace and returns whether at least one whitespace component was skipped.
Snapshot / restore
Some grammars (notably CSS selectors, which must distinguish prefix|name from prefix|=value) need lookahead beyond a single component. Save the cursor with save, do speculative consumption, and restore if it didn't pan out.
atomic t f runs f () with a snapshot held; if f raises Parse_error, the cursor is restored before the exception propagates.
lookahead p t runs p t and then restores the cursor, returning the result.
try_typed_call typed t runs the typed reader typed when the next component is a Func. On success, returns Ok value. On Parse_error, restores the cursor, skips past the function call, and returns Error <captured-call> so callers can wrap it in their type's Invalid arm. When the next component isn't a Func the typed reader is run directly (errors propagate).
Errors
Alias for Error.Parse_error.
err ?got t msg raises Parse_error at the current position.
err_expected_but_eof t what raises when end of input is reached while expecting what.
err_unexpected t raises "unexpected token" at the current position.
with_context t label f annotates any Parse_error raised by f with label in the error path.
Token-shape helpers - raising variants
These parse and advance the cursor, or raise Parse_error on mismatch.
ident t consumes the next ident. The keep_case flag is accepted for source compatibility; component idents are already case-preserved.
url t consumes the body of a url(...) call (either a <url-token> or a function call with a quoted argument).
pct t consumes a percentage token. If ~clamp is set, the value is clamped to 0..100.
number_with_unit t consumes a dimension, percentage or number and returns the value and unit (if any).
number_repr_with_unit t is like number_with_unit, but also returns the authored numeric token representation.
Token-shape helpers - option variants
Each foo_opt returns Some _ and advances when the next component matches; otherwise returns None and the cursor is unchanged.
number_opt t consumes and returns a number token if present.
integer_opt t consumes and returns an integer-valued number if present.
percentage_opt t consumes and returns a percentage value if present.
dimension_opt t consumes and returns a dimension value and unit if present.
hash_opt t consumes and returns a hash token value if present.
string_opt t consumes and returns a string token if present.
string_with_quote_opt t consumes and returns a string token plus its quote character if present.
string_repr_with_quote_opt t is like string_with_quote_opt, but also returns the authored string token spelling when source text is available.
delim_opt t consumes and returns a delimiter token if present.
peek_delim t is the char of the next component if it is a Delim token, without advancing the cursor.
peek_semicolon t is true iff the next component is a semicolon token.
peek_at_keyword t is Some s when the next component is At_keyword s.
peek_block t is Some bracket when the next component is a balanced block, returning its opening bracket kind.
at_keyword_opt t consumes the next component if it is an At_keyword token, returning the keyword name without the leading @.
expect_at_keyword name t consumes the @name token or raises.
drain_until_block t consumes components up to (but not including) the next block or semicolon, returning the drained components. Used for at-rule preludes.
Like drain_until_block, but serializes the drained components.
consume_until_semicolon t consumes and serializes components up to, but not including, the next semicolon.
consume_to_decl_end t consumes and serializes components up to, but not including, the next semicolon or top-level ! delimiter.
drain_to_decl_end t consumes components up to (but not including) the next semicolon or top-level ! delimiter, returning the drained list without serialising it.
consume_to_slash_or_semicolon t consumes and serializes components up to, but not including, the next top-level slash delimiter or semicolon.
slash_opt t consumes a / delim if present and returns whether it was consumed.
consume_if c t consumes the next component if it is the delim c.
try_kind kind t consumes kind if it is next and returns whether it matched.
try_kind_pair k1 k2 t consumes k1 followed by k2 if both are next and returns whether they matched.
looking_at t s is true iff the next component (after leading whitespace) starts with s - matches an ident, a function name followed by (, or a delim-based prefix.
looking_at_ident name t is true if the next component is identifier name.
looking_at_func name t is true if the next component is function name.
looking_at_calc t is true if the next component is calc() or the legacy -webkit-calc() spelling.
Expectations
Group / function helpers
parens f t consumes a (...) block and calls f with a fresh cursor over its contents. Raises if the next component is not a parenthesised block.
call name t f consumes a name(...) function call and applies f to a cursor over its arguments. Raises if no such function is next.
function_call name f t consumes a name(...) call and calls f over its arguments. Returns None without advancing if the next component is not a function with that name.
any_function_call f t consumes any function call and applies f to its name and argument cursor.
Enums
enum ?default label table t skips leading whitespace, consumes an ident, and looks it up in table. Falls back to default if provided, otherwise raises.
try_enum table t is like enum but returns None without raising.
enum_calls ?default table t skips leading whitespace, then dispatches on the name of the next function call. Each parser receives the raw cursor (still pointing at the function) and is expected to consume it.
val enum_or_calls :
?default:(t -> 'a) ->
string ->
(string * 'a) list ->
?calls:(string * (t -> 'a)) list ->
t ->
'aenum_or_calls ?default label idents ?calls t skips leading whitespace, first tries to match idents (ident token), then calls (function call), and finally falls back to default. Raises if none apply and no default is given.
val enum_or_var :
?default:(t -> 'a) ->
string ->
(string * 'a) list ->
var:(t -> 'a) ->
t ->
'aenum_or_var label idents ~var t is enum_or_calls specialised to the common case of "match a CSS keyword from idents, or read var(...) via var". Removes the boilerplate of writing the var entry in a calls list at every typed-property reader.
Higher-order combinators
option p t returns Some (p t) on success, None if p raises Parse_error (cursor is rewound).
one_of ps t tries each parser in order, rewinding on failure.
list ?sep ?at_least ?at_most item t parses items separated by sep (default: no separator). Enforces cardinality bounds.
Like many but folds into an accumulator. Returns the final accumulator and the last error (if any).
many p t runs p repeatedly while it succeeds; returns the list of results and the last error message on failure (or None if the stream simply ran out).
pair ?sep a b t parses a followed by b, optionally separated by sep.
triple ?sep a b c t parses a, b, and c, optionally separated by sep.
try_parse_err p t returns Ok v on success or Error msg on parse failure (cursor rewound).
try_parse_full_err p t is like try_parse_err, but also requires p to consume the cursor fully.