package sedlex
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=b8efae9d2a9a39c2ca2c43a2684f3fb686a8775329f7f9b07c3e3d8a2b7682d6
sha512=ae6ac8282b9e899a510745d64a817d06d908bc61f29530568323eb4145b3db38cf61a82575e2b4c15825f570f2b270ab885c3d27adedbbbe7cdad12510de6d80
doc/sedlex.compiler/Sedlex_compiler/Sedlex/index.html
Module Sedlex_compiler.SedlexSource
Regexp combinators
Regular expressions are built from combinators and compiled to a DFA.
Abstract type of regular expressions.
alt r1 r2 matches r1 or r2 (alternation). When both operands are simple chars regexps, their character sets are merged into one.
repeat r n m matches between n and m repetitions of r (bounded repetition). Requires 0 <= n <= m.
If the argument is a single chars regexp, returns a regexp which matches the complement set. Otherwise returns None.
If each argument is a single chars regexp, returns a regexp which matches the set (arg1 - arg2). Otherwise returns None.
If each argument is a single chars regexp, returns a regexp which matches the intersection set. Otherwise returns None.
Tagged DFA for as bindings
Named sub-match bindings (e.g. Star any as x) are implemented using tagged transitions in the DFA. Each as binding introduces up to two tags that record the start and end positions of the sub-match in the lexbuf's memory cells at runtime. When one boundary can be computed from a known offset, only one tag is needed (bind_start_only / bind_end_only); when both boundaries are known, no tags are needed at all.
Or-patterns (p1 as x) | (p2 as x) additionally use discriminator cells: integer values that record which branch was taken, so the PPX can extract the correct positions at match time.
type tag_op = | Set_position of {}(*
*)Set_position {dst}: record the current lexbuf position in memory celldst.| Set_value of {}(*
*)Set_value {dst; value}: record integervaluein memory celldst(used for or-pattern discriminators).| Copy of {}(*
*)Copy {dst; src}: copy the contents of cellsrcinto celldst. Emitted when determinization must preserve a position that a parallel NFA path is about to overwrite.
Tag operations emitted on DFA transitions.
bind r wraps r with start/end tag epsilon nodes. Returns (wrapped_regexp, start_tag, end_tag) where start_tag and end_tag are the allocated memory cell indices.
Allocate a fresh memory cell for an or-pattern discriminator.
bind_disc r cell value appends an epsilon node that sets cell to value after r matches. Used to tag each branch of an or-pattern so the PPX can tell which branch matched.
Like bind, but only wraps with a start tag (no end tag). Used when the end position can be computed from a known offset.
Like bind, but only wraps with an end tag (no start tag). Used when the start position can be computed from a known offset.
Reset the tag counter. Called before compiling each match%sedlex block.
DFA compilation
type accept = {rule : int;(*The lowest-numbered, hence highest-priority, accepting rule, matching the first-match semantics of
*)match%sedlex.final_ops : tag_op list;(*Tag operations to execute when entering the state, just before
*)Sedlexing.mark: they materialize the accepting configuration into the cells read by the binding extraction code.
}What an accepting state accepts.
type dfa_state = {trans : (Cset.t * int * tag_op list) array;(*Each transition: (character set, target state, tag operations to execute when this transition fires). The operations form a parallel move: a
*)Copyreads its source as it was before any operation of the same list executed, and no two operations write the same cell. They execute before the character of the transition is consumed. A character set is eitherCset.eofor does not contain end of input, and the target of aCset.eoftransition has no transitions: end of input is read once.accept : accept option;(*
*)Nonefor non-accepting states.
}compile rules determinizes the NFA for an array of regexp rules using subset construction. Returns the DFA and the total number of memory cells needed for as bindings. State 0 is always the initial state.
High-level compilation from IR
compile_ir takes an array of Ir.t patterns, handles tag allocation (including Start_plus/End_minus optimizations), discriminator insertion for or-patterns, and DFA construction. Returns the DFA and all information needed to generate binding extraction code.
How to compute a sub-match boundary position at runtime.
type compiled_binding = {name : string;start_pos : pos_expr;end_pos : pos_expr;disc : (int * int) list;(*Discriminator conditions:
*)(cell, value)pairs. Empty for simple (non-or) bindings. For or-patterns, branches with different positions have distinct values; branches with identical positions share a value.
}Information about a single binding in the compiled output.
type compiled_ir = {dfa : dfa;bindings : compiled_binding list array;(*
*)bindings.(i)is the list of binding info for rulei.
}Result of compile_ir.
compile_ir rules compiles an array of IR patterns into a tagged DFA. Raises Assert_failure if invariant checking fails.