ast

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package ast implements phase P2: building verified renderings from a scanned template, with exact source maps back to the template file. The emission algorithm here is the byte-for-byte mirror of the runtime composer (premise P2); a shared conformance test pins the equality. See docs/design/02-rendering.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderingCount

func RenderingCount(profile dialect.LexerProfile, q *template.QueryTemplate) int

RenderingCount reports how many renderings Renderings would produce for q WITHOUT allocating any of them — the count is a linear sum, so a caller can refuse a pathological template (many @choose/@order-by/ @filter-tree blocks over a large skeleton, each rendering a fresh full-SQL copy) before the set is materialised and exhausts memory. It MUST stay in lock-step with Renderings below: the maximal rendering, plus one per additional @choose ordinal, one per @order-by @default body, one per @in occurrence (question-style dialects only), and one per @filter-tree.

Types

type CaseSelection

type CaseSelection map[int]int

CaseSelection selects one ordinal per @choose block (by block order among the template's Choose items). Missing entries default to 0.

type FragRange

type FragRange struct {
	Item       template.Item
	Start, End int
}

FragRange records where a construct's emission landed in the rendered SQL, including synthesized wrapping.

type InSelection

type InSelection []uint8

InSelection selects the representative arity of each @in construct (by template order) on expanding dialects: 1 (default when nil or short) renders one placeholder — the verified stand-in for every non-empty list, since IN-list growth is parse-invariant — and 0 renders the empty-list form. Ignored on dollar-style dialects.

type OrderSelection

type OrderSelection [][]uint8

OrderSelection selects the emission of each @order-by block (by block order): nil = maximal (all keys, declaration order), empty non-nil = default-or-omit, else a sequence of key<<1|desc elements.

type RenderKind

type RenderKind int
const (
	RenderMaximal RenderKind = iota
	RenderCase
	RenderOrderDefault // an @order-by @default body substituted
	RenderInEmpty      // an @in at arity 0 (expanding dialects only)
	RenderTreeEmpty    // a @filter-tree with the empty tree (renders TRUE)
)

type Rendering

type Rendering struct {
	Kind      RenderKind
	ChooseIdx int // which @choose block differs from maximal (RenderCase)
	CaseIdx   int // selected ordinal: 0..len(Cases)-1, len(Cases)=default
	OrderIdx  int // which @order-by block (RenderOrderDefault)
	InIdx     int // which @in construct (RenderInEmpty)
	TreeIdx   int // which @filter-tree block (RenderTreeEmpty)
	SQL       string
	ParamsSeq []string // template param name per placeholder ($1 = [0])
	Frags     []FragRange
	Map       SourceMap
}

func Render

Render emits one rendering with every guard active and the given case selection. This is the reference implementation of premise P2's emission algorithm.

func RenderShape

func RenderShape(profile dialect.LexerProfile, q *template.QueryTemplate,
	guardMask uint64, sel CaseSelection, orders OrderSelection, ins InSelection) (Rendering, error)

RenderShape emits the SQL of one concrete shape: an @if-present fragment is active iff every one of its guard atoms' bits is set in guardMask (bit order = q.GuardAtoms); orders selects each @order-by block's key sequence; ins selects each @in construct's representative arity (expanding dialects). Used by shape enumeration, `check --exhaustive`, and the property test.

func Renderings

func Renderings(profile dialect.LexerProfile, q *template.QueryTemplate) ([]Rendering, error)

Renderings produces the full verified-rendering set: the maximal rendering first (ordinal 0 everywhere, all @order-by keys listed), then one rendering per remaining @choose ordinal, then one per @order-by @default body. RenderingCount above projects len(result) without allocating; keep the two in lock-step.

type Seg

type Seg struct {
	ROff, RLen int
	TOff       int
	Synth      bool
}

Seg maps a rendered byte range back to the template.

type SourceMap

type SourceMap struct {
	// contains filtered or unexported fields
}

func (SourceMap) ToTemplate

func (m SourceMap) ToTemplate(rOff int) (tOff int, synth bool)

ToTemplate translates a rendered offset to a template offset. For synthesized text it returns the anchor (the construct or param the synthesis belongs to) and synth=true.

type TreeSelection

type TreeSelection []uint8

TreeSelection selects the emission of each @filter-tree block (by template order): 1 (default when nil or short) renders the maximal conjunction of all predicates, 0 renders the empty tree — the literal TRUE the runtime emits for a nil/Unscoped tree. The two are the tree space's verified representatives; arbitrary trees are sound by the compositional argument (spec, `@filter-tree`).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL