Wax_utils.TriviaSourceParsing context for collecting comments and annotations.
associate ctx associates trivia to locations. The second component holds the leftover comments that no location owns (trailing comments, or every comment when there are no locations); the caller prints them as tail trivia.
only restricts the association to the given set of locations — those the printer will actually look up (see get). Comments that would otherwise attach to a non-emitted node bubble up to an emitted one instead of being lost. Collect the set with a dry printing pass.
report_item ctx kind content reports a comment or an annotation.
report_token ctx pos records that a meaningful token ending at byte pos has been encountered on the current line.
with_pos ctx loc v wraps v with location loc.
drop_in_ranges ctx ranges removes every comment whose anchor falls within one of the half-open byte ranges [start, end). Used after conditional specialization splices out a branch: the comments inside the removed source span are discarded rather than re-attaching to a surviving neighbour. The ranges and comments are each sorted once and swept together in a single pass.
Looking up the trivia attached to a location. Rendering it to styled output lives in Styled_printer, which owns the colour theme.
The empty association (before, within and after all empty).
val get :
?collect:(Ast.location, unit) Hashtbl.t ->
t ->
seen:(Ast.location, unit) Hashtbl.t ->
Ast.location option ->
associatedget trivia ~seen loc returns the trivia associated with loc, with de-duplication: it returns dummy_assoc for None, a missing location, or a location already present in seen; on the first real hit it records the location in seen and returns its association. De-duplication is a no-op for formatters (each parser location occurs once) and prevents a comment from being emitted repeatedly when conversion replicates one source location onto several output nodes.
Drop blank-line entries at the end of the list, so emitted tail trivia does not leave spurious blank lines at the end of the file.
The comment text stored by a lexer keeps the source syntax's delimiters (;; …/(; … ;) for WebAssembly, // …//* … */ for Wax). When trivia collected from one format is replayed onto an AST that is printed in the other format (during conversion), the delimiters must be rewritten.
type comment_syntax = {line : string;line-comment prefix, e.g. ";;" or "//"
block_open : string;block-comment opener, e.g. "(;" or "/*"
block_close : string;block-comment closer, e.g. ";)" or "*/"
}retarget ~src ~dst trivia tail rewrites every comment's delimiters from the src syntax to the dst syntax (line-comment prefix and block-comment delimiters), leaving blank lines and annotations untouched. Block delimiters are balanced in stored content, so a global swap preserves nesting.