Kernel.CodegenSourceCode generation for the LR matching machine
This module generates the OCaml code for the LR matching machine from the compiled specification and the generated automaton. The "machine" is translated to a sparse transition table, a bytecode program. An OCaml wrapper is generated to interpret the bytecode and invoke the appropriate semantic actions.
Main components:
spec type holds global configuration including the parser name and lexer definition.output_header, output_trailer functions generate the outer module structure wrapping the generated code.output_rule function is the core code generation function that:Implementation details:
output_table function formats and outputs the already-compacted bytecode and transition table as an OCaml Lrgrep_runtime.program record. The actual compaction is performed by Lrgrep_support.compact inside output_rule before output_table is called.output_execute_function generates a case analysis matching on:bind_capture function handles different capture types:Value: Regular captured values with full type informationStart_loc, End_loc: Start/end position captureslookahead_constraint function generates pattern matching for branches with lookahead constraints, ensuring only the right tokens trigger each branch.output_rule function proceeds in three steps: 1. Compacts the state machine via Lrgrep_support.compact and outputs the bytecode and transition tables 2. Generates semantic actions with proper variable binding, including type recovery for captured values 3. Outputs a wrapper function to glue the interpreter to user actionsval output_table :
Utils.Code_printer.t ->
Syntax.rule ->
('g, 'r, 'a, 'b) Automata.Machine.t ->
(string * Lrgrep_support_packer.table * int array) ->
unitval output_rule :
'g Info.grammar ->
spec ->
Syntax.rule ->
('g, 'r) Spec.clauses ->
('g, 'r) Spec.branches ->
('g, 'r, 'a, 'b) Automata.Machine.t ->
printer