PEGO

Package pego

import "github.com/ornew/pego"

Overview

Package pego is the public API of the PEGO parser framework.

A grammar is written in the PEGO grammar language (see ParseGrammar), built as an AST with the grammar package, or decoded from JSON. Compile turns it into a Parser, and Parser.Parse parses input with it.

Index

Constants

const (
	ChangeEdit        = engine.ChangeEdit
	ChangeOutcome     = engine.ChangeOutcome
	ChangeFullRefresh = engine.ChangeFullRefresh
)
const (
	OutputUnchanged      = engine.OutputUnchanged
	OutputChanged        = engine.OutputChanged
	OutputMayHaveChanged = engine.OutputMayHaveChanged
	OutputUnavailable    = engine.OutputUnavailable
)
const (
	KindString     = value.KindString     // a node whose value is a string: Text
	KindTuple      = value.KindTuple      // a node whose value is a tuple: Len and Elem
	KindList       = value.KindList       // a node whose value is a list: Len and Elem
	KindError      = value.KindError      // an error value of recovery: Text is its message
	KindStruct     = value.KindStruct     // a struct built by an action: Type and Field
	KindTupleValue = value.KindTupleValue // a tuple built by an action, without a range
	KindListValue  = value.KindListValue  // a list built by an action, without a range
	KindValue      = value.KindValue      // another value built by an action: Value
)

The kinds of nodes.

const (
	// CodePoints counts Unicode code points. It is the default.
	CodePoints = engine.CodePoints
	// Bytes counts UTF-8 bytes.
	Bytes = engine.Bytes
)
const (
	InputWorkLimit = limits.InputWorkLimit
	StepWorkLimit  = limits.StepWorkLimit
)
const (
	// DefaultBackend uses Closure for a parser compiled from a grammar and Bytecode for one
	// loaded from a compiled grammar.
	DefaultBackend = engine.Default
	// Closure compiles parsing expressions into Go closures.
	Closure = engine.Closure
	// Bytecode runs language-independent bytecode (see spec/bytecode/README.md) on a VM that uses Go
	// recursion for rule calls.
	Bytecode = engine.Bytecode
	// BytecodeIterative runs the bytecode on a VM that keeps rule calls on its own stack, so deeply
	// nested input does not consume the Go stack.
	BytecodeIterative = engine.BytecodeIterative
)
const (
	// TraceEnter is reported when a rule is called, before the memo is consulted.
	TraceEnter = engine.TraceEnter
	// TraceExit is reported when a rule call returns.
	TraceExit = engine.TraceExit
)
const (
	// CompiledParse serves Parse, ParsePrefix, ParseStream and Iterate without WithTrace or
	// WithProfile.
	CompiledParse = engine.CompiledParse
	// CompiledRecognize serves RecognizeOnly parses without WithTrace or WithProfile.
	CompiledRecognize = engine.CompiledRecognize
	// CompiledDocument serves NewDocument, and Parse, ParsePrefix, ParseStream and Iterate with
	// WithTrace or WithProfile.
	CompiledDocument = engine.CompiledDocument
)
const (
	WalkContinue = value.WalkContinue
	WalkSkip     = value.WalkSkip
	WalkStop     = value.WalkStop
)
const (
	WalkRoot        = value.WalkRoot
	WalkChild       = value.WalkChild
	WalkField       = value.WalkField
	WalkListElement = value.WalkListElement
)

Variables

var ErrQueryCycle = schema.ErrQueryCycle

ErrQueryCycle reports a cycle on the active path through the values.

var ErrQueryNoSourceInfo = schema.ErrQueryNoSourceInfo

ErrQueryNoSourceInfo reports a query with a rule constraint on values that record no rules, because the grammar was compiled without source information.

var ErrSchemaUnavailable = schema.ErrUnavailable

ErrSchemaUnavailable reports that checked grammar metadata is unavailable. In particular, a parser loaded from a compiled file cannot export a schema.

var ErrStreamClosed = engine.ErrStreamClosed

ErrStreamClosed identifies an iterator closed before its terminal result.

var ErrWorkLimit = limits.ErrWorkLimit

ErrWorkLimit identifies engine work-limit failures through errors.Is.

Functions

func IsCompiled source

func IsCompiled(data []byte) bool

IsCompiled reports whether data is a compiled grammar written by MarshalBinary or Marshal.

func ParseGrammar source

func ParseGrammar(src string) (*grammar.Grammar, error)

ParseGrammar parses PEGO source code into a grammar AST.

func Walk source

func Walk(root *Node, visit func(WalkVisit) (WalkAction, error)) error

Walk visits a live node graph in pre-order: children in slice order, then fields in name order, and nested []any elements in index order. Scalar and nil values are visits too. Shared subtrees are visited once per incoming edge; active-path cycles return CycleError. The graph must not be mutated concurrently. A callback error returns unchanged; WalkStop succeeds and WalkSkip skips descendants.

Types

type Backend source

type Backend = engine.Backend

Backend selects how a grammar is executed. All backends produce identical results.

The type it stands for is defined in an internal package as follows. source

type Backend int

Backend is the backend used to run a parse.

func (Backend) String source

func (b Backend) String() string

type CacheStats source

type CacheStats = engine.CacheStats

CacheStats describes the incremental reuse history retained by a Document. EstimatedBytes is an approximation of cache-owned backing storage, excluding text, caller-held trees, shared strings and allocator overhead.

The type it stands for is defined in an internal package as follows. source

type CacheStats struct {
	LiveMemoEntries    int
	RetiredMemoEntries int
	RepetitionElements int
	EstimatedBytes     uint64
}

CacheStats describes retained incremental history. EstimatedBytes includes backing buffers and run records, but excludes map buckets, allocator metadata, document text, result trees, shared expectation/error/environment payloads and strings shared with caller-held results. Inspection is read-only and requires exclusive Document ownership.

type CompileOption source

type CompileOption func(*engine.Options)

CompileOption configures compilation.

func WithDefaultValue source

func WithDefaultValue() CompileOption

WithDefaultValue makes every rule produce the default value of its expression. Value actions (=>) and the declared types of rules are dropped, so the parser returns the default node tree even for grammars with actions, and nothing is checked against the types the actions would have made. Predicates still run, because they decide the match, and the rules that make the values predicates read keep their actions and types. It is a property of the parser: a compiled grammar written from it keeps it.

func WithSourceInfo source

func WithSourceInfo() CompileOption

WithSourceInfo records source information in parse results: the rule that produced each node and where its captures are. Node.Rule, Node.Field, the rule and capture names of JSON and S-expression output, and rule constraints in queries need it. It costs time and memory, so parsers omit it by default.

type CompiledMode source

type CompiledMode = engine.CompiledMode

CompiledMode is the mode of a compiled grammar file, chosen when it is saved. A file holds the module of one mode and serves only the parses of that mode; the closure backend, Grammar and OutputSchema need the grammar source.

The type it stands for is defined in an internal package as follows. source

type CompiledMode uint8

CompiledMode is the mode of a compiled grammar file: the one kind of parse its module serves.

func (CompiledMode) String source

func (m CompiledMode) String() string

String returns the name of the mode: parse, recognize or document.

func ParseCompiledMode source

func ParseCompiledMode(s string) (CompiledMode, error)

ParseCompiledMode returns the mode named s: "parse", "recognize" or "document".

type Cursor source

type Cursor = engine.Cursor

Cursor selects node spans in an explicitly supplied input position unit. At selects a point; Range selects a half-open range. It does not infer or convert units and does not prune children based on a parent's span.

The type it stands for is defined in an internal package as follows. source

type Cursor struct {
	Root *value.Node
	Unit PositionUnit
}

Cursor filters edge-oriented node visits using spans in Unit. Unit must match the parse that populated Root's positions; it is never inferred or converted. Parent spans do not prune traversal. Scalar and list values have no spans.

func (Cursor) At source

func (c Cursor) At(position int32, visit func(value.WalkVisit) (value.WalkAction, error)) error

At visits nodes containing position, including zero-width nodes at position.

func (Cursor) Range source

func (c Cursor) Range(start, end int32, visit func(value.WalkVisit) (value.WalkAction, error)) error

Range visits nodes overlapping [start,end). An empty range matches no nodes. Zero-width nodes match when start <= node.Start < end.

type CycleError source

type CycleError = value.CycleError

CycleError reports the root-to-back-edge path of a cyclic node or []any graph.

The type it stands for is defined in an internal package as follows. source

type CycleError struct{ Path []WalkEdge }

CycleError describes an active-path back-edge, including the root and failing edge.

func (*CycleError) Error source

func (e *CycleError) Error() string

type Document source

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

Document is a text that is edited repeatedly. Parsing after an edit reuses the results of the previous parse that the edit did not affect (incremental parsing), as needed by editors. A Document is not safe for concurrent use.

Reused results are shared with the trees returned by earlier parses, and the nodes after an edit are moved to their new positions in place: an earlier tree changes when Parse runs again, and is then a mix of nodes at new positions (those reused) and at old ones. Parse writes it, so it must not be read concurrently with Parse. Use Node.Clone to keep a tree as it was.

func (*Document) CacheStats source

func (d *Document) CacheStats() CacheStats

CacheStats inspects retained incremental history without changing it. Document methods, including this one, require exclusive ownership.

func (*Document) DrainChanges source

func (d *Document) DrainChanges() []DocumentChange

DrainChanges transfers queued events. Overflow produces one full-refresh marker; draining resets the queue but retains the last comparison snapshot.

func (*Document) Edit source

func (d *Document) Edit(start, end int, text string) error

Edit replaces the range [start, end) of the text, in the document's position unit, with text. With byte positions, start and end must be on character boundaries. Each invalid UTF-8 byte is a separate character; edits may join bytes into a valid UTF-8 character.

func (*Document) EditBatch source

func (d *Document) EditBatch(base DocumentVersion, edits ...TextEdit) error

EditBatch atomically applies ordered, nonoverlapping edits in base coordinates. It rejects stale or foreign tokens, repeated starts, invalid ranges and byte boundaries. An empty batch validates base but changes nothing. A nonempty batch advances one revision and clears parse caches without parsing.

func (*Document) EnableChanges source

func (d *Document) EnableChanges(queueLimit int) error

EnableChanges opts into bounded notifications and one retained comparison snapshot. queueLimit bounds record count, not replacement or snapshot bytes. It must be positive; enabling twice is rejected. Future Parse and Snapshot calls capture and compare immutable outcomes inside their cumulative work budget. Comparison is conservative about aliases and nil-versus-empty collections; this policy does not define language value equality. Interrupted invocations return nil and notify unavailable, breaking the comparison chain.

func (*Document) Parse source

func (d *Document) Parse(opts ...ParseOption) (*Node, error)

Parse parses the current text. opts apply once over construction defaults and may change context, work limits, depth and trace, but not unit, effective backend or recognition mode. Overrides do not change future defaults. Pre-execution interruption preserves caches and statistics; execution or publication interruption discards reusable state while preserving text for retry. Parser-produced syntax diagnostics belong to that call: mutating them does not affect later parses. Returned Nodes may share the mutable Document graph.

func (*Document) ResetHistory source

func (d *Document) ResetHistory()

ResetHistory discards incremental reuse history while preserving text, construction options, version and the statistics of the last parse. Earlier result trees are detached from later node movement.

func (*Document) Snapshot source

func (d *Document) Snapshot(opts ...ParseOption) (*Snapshot, error)

Snapshot parses and freezes this revision using construction defaults plus opts. Cancellation and work limits cover parsing, text, tree and diagnostics; interruption returns nil. Ordinary syntax errors retain the failed version.

func (*Document) Stats source

func (d *Document) Stats() ParseStats

Stats returns the evaluation and reuse counts of the last Parse.

func (*Document) Text source

func (d *Document) Text() string

Text returns the current text.

func (*Document) Unit source

func (d *Document) Unit() PositionUnit

Unit returns the position unit selected when the document was constructed.

func (*Document) Version source

func (d *Document) Version() DocumentVersion

Version returns the current text revision, initially sequence one.

type DocumentChange source

type DocumentChange = engine.DocumentChange

DocumentChange owns an edit operation or an outcome comparison. Edit From/To advance source revisions; outcome From/To identify compared snapshots. Consumers track those streams separately. FullRefresh requires rebuilding current state.

The type it stands for is defined in an internal package as follows. source

type DocumentChange struct {
	Kind        DocumentChangeKind
	From, To    DocumentVersion
	Unit        PositionUnit
	Edits       []TextEdit
	FullRefresh bool
	// Invalidated is conservative: an accepted edit may affect the whole output.
	// Reevaluation remains unknown; invalidation is not evidence of execution.
	Invalidated  bool
	Reevaluation string
	Output       OutputChange
}

type DocumentChangeKind source

type DocumentChangeKind = engine.DocumentChangeKind

DocumentChangeKind distinguishes source operations, captured outcomes and lost history requiring a full refresh.

The type it stands for is defined in an internal package as follows. source

type DocumentChangeKind string

DocumentChange keeps one operation's original coordinate basis. FullRefresh means consumers must rebuild from the latest snapshot instead of replaying edits.

type DocumentVersion source

type DocumentVersion = engine.DocumentVersion

DocumentVersion identifies one text revision of a Document. Its zero value is invalid; tokens are comparable and do not retain document contents.

The type it stands for is defined in an internal package as follows. source

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

DocumentVersion identifies one text revision of one Document. Its zero value is invalid. Tokens are comparable and do not retain document contents.

func (DocumentVersion) Sequence source

func (v DocumentVersion) Sequence() uint64

Sequence returns the text revision number, starting at one.

type Location source

type Location = engine.Location

Location is a position with its line and column.

The type it stands for is defined in an internal package as follows. source

type Location struct {
	Pos  int `json:"pos"`
	Line int `json:"line"`
	Col  int `json:"col"`
}

func (Location) String source

func (l Location) String() string

String returns line:col, or "position N" when the line is unknown (Line is 0: the position was in input that a stream parse had discarded; see TraceEvent.LineCol).

type MissingTokenLimitError source

type MissingTokenLimitError = engine.MissingTokenLimitError

MissingTokenLimitError reports exhaustion of a #missing insertion budget.

The type it stands for is defined in an internal package as follows. source

type MissingTokenLimitError struct {
	Limit, Position int
}

MissingTokenLimitError reports exhaustion of the invocation's insertion budget. Position uses the selected position unit. Exhaustion aborts parsing, including when an alternative that used the budget would otherwise be abandoned.

func (*MissingTokenLimitError) Error source

func (e *MissingTokenLimitError) Error() string

type Node source

type Node = value.Node

Node is a value of a parse result: a node of the input with a range and a string, tuple or list value, an error value of recovery, or a value built by an action. It is opaque; its methods read it (Kind tells which of them apply). Nodes can be encoded to JSON with encoding/json.

The type it stands for is defined in an internal package as follows. source

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

Node is a value produced by parsing. It is a CST node, a terminal, a list, or a struct built by an action. In JSON it is an object with the members type, rule (if any), start, end, text (if any), children (if any) and fields (if any).

func (*Node) Clone source

func (n *Node) Clone() *Node

Clone returns a deep copy of the tree n. Subtrees shared within the tree stay shared in the copy. A Document moves the nodes it reuses after an edit, which changes trees returned by its earlier parses; a clone keeps a tree as it was.

func (*Node) Elem source

func (n *Node) Elem(i int) *Node

Elem returns the element i of a tuple or a list: a node, or nil for an optional that did not match. It panics if i is out of the range [0, Len()).

func (*Node) Elems source

func (n *Node) Elems() iter.Seq2[int, *Node]

Elems returns the elements of a tuple or a list in order, as Elem does.

func (*Node) End source

func (n *Node) End() int

End returns the end of the range of the input that the node spans (exclusive), in the position unit of the parse.

func (*Node) Field source

func (n *Node) Field(name string) any

Field returns the value of the field name of a struct, and, with source information, the value of the capture name of a node: a *Node, an int, a string, a bool, or nil.

func (*Node) Fields source

func (n *Node) Fields() iter.Seq2[string, any]

Fields returns the fields of a struct, and, with source information, the captures of a node, in the order the action wrote them.

func (*Node) IsTerminal source

func (n *Node) IsTerminal() bool

IsTerminal reports whether the node holds text: a string node or an error value.

func (*Node) Kind source

func (n *Node) Kind() Kind

Kind returns what the node stands for.

func (*Node) Len source

func (n *Node) Len() int

Len returns the number of elements of a tuple or a list (KindTuple, KindList, KindTupleValue, KindListValue), and 0 for other nodes and nil.

func (*Node) MarshalJSON source

func (n *Node) MarshalJSON() ([]byte, error)

MarshalJSON encodes the node as an object (see Node).

func (*Node) Rule source

func (n *Node) Rule() string

Rule returns the name of the rule that produced the node, if it was produced by a rule without an action, or "".

func (*Node) Start source

func (n *Node) Start() int

Start returns the start of the range of the input that the node spans, in the position unit of the parse.

func (*Node) String source

func (n *Node) String() string

String renders the value as an S-expression of its shape, without positions, for tests and debugging: a string is quoted, a tuple is ( ), a list [ ], nil is nil, a struct is Name{Field: value}, and an error is error("message"). A node of a named node type N is written with N before its brackets: N("text"), N( ) and N[ ]. With source information, a node is followed by @rule, a captured element is preceded by name:, and a capture whose value is not an element is written after the elements as &name:value.

func (*Node) Text source

func (n *Node) Text() string

Text returns the string value of a KindString node, and the message of a KindError value; it returns "" for other nodes and nil.

func (*Node) Type source

func (n *Node) Type() string

Type returns the node's type name: the kind of a node of the value model (string, tuple, list, error, or Missing for an inserted literal), or otherwise the name of a type defined in the grammar.

func (*Node) Value source

func (n *Node) Value() any

Value returns the value of a KindValue node (such as an int, a string or a bool) and the string of a KindString node; it returns nil for other nodes and nil.

func (*Node) WriteJSON source

func (n *Node) WriteJSON(w io.Writer) error

WriteJSON writes the live node graph as JSON without recursive Node or []any traversal. A nil receiver writes null. Shared edges are encoded separately; active-path cycles fail. It does not close or flush w, and an error may leave a prefix written. Maps and other containers delegate to encoding/json and are outside the iterative depth guarantee. Callers must not mutate reachable values concurrently with this method.

type NodeKind source

type NodeKind = value.Kind

NodeKind is what a Node stands for (Node.Kind).

The type it stands for is defined in an internal package as follows. source

type Kind uint8

Kind is what a Node stands for in the value model.

func (Kind) String source

func (k Kind) String() string

type OutputChange source

type OutputChange = engine.OutputChange

OutputChange is conservative whole-output comparison, never a minimal diff.

The type it stands for is defined in an internal package as follows. source

type OutputChange string

OutputChange describes whole-result equality, not a minimal tree difference.

type OutputSchema source

type OutputSchema = schema.Output

OutputSchema describes possible grammar outputs independently of a parse. Validate checks its version 2 envelope and shape graph, including schemas built in memory. Decoding it with encoding/json performs the same check.

The type it stands for is defined in an internal package as follows. source

type Output struct {
	Format                  string `json:"format"`
	Version                 int    `json:"version"`
	Start                   string `json:"start"`
	PositionUnitIndependent bool   `json:"positionUnitIndependent"`
	// SourceInfo reports that the grammar was compiled with source information, so that nodes
	// record the rules that produced them.
	SourceInfo bool   `json:"sourceInfo"`
	Types      []Type `json:"types"`
	Rules      []Rule `json:"rules"`
}

Output is an experimental, grammar-level description of possible values. It is independent of position units and generated target-language layouts.

func (*Output) UnmarshalJSON source

func (s *Output) UnmarshalJSON(data []byte) error

UnmarshalJSON rejects unsupported envelopes, noncanonical fields, duplicate keys and malformed shape graphs.

func (*Output) Validate source

func (s *Output) Validate() error

Validate checks the version 2 envelope and shape graph. It also accepts schemas constructed directly in memory; named references may be recursive.

type OutputSchemaDiff source

type OutputSchemaDiff struct {
	Types []SchemaTypeChange
	Rules []SchemaRuleChange
}

OutputSchemaDiff contains type and rule changes sorted by ID. It does not classify compatibility or compare envelope fields such as Start.

func DiffOutputSchemas source

func DiffOutputSchemas(before, after *OutputSchema) (*OutputSchemaDiff, error)

DiffOutputSchemas compares version-2 type and rule contents. Type and rule array order, field order, capture order, capture path order and join alternative order are insignificant; tuple element order and named references are preserved. Both inputs must satisfy the complete version-2 schema contract checked by OutputSchema.Validate.

type ParseOption source

type ParseOption func(*engine.ParseOptions)

ParseOption configures a single parse.

func RecognizeOnly source

func RecognizeOnly() ParseOption

RecognizeOnly checks whether the input matches the grammar without building a tree. Parse then returns a nil node and the same syntax errors as a full parse. Actions are not evaluated, so runtime errors in actions are not reported; captures that predicates read are still built. Recognition is faster and allocates less than a full parse. It cannot be used with ParseStream or Document.

func WithBackend source

func WithBackend(b Backend) ParseOption

WithBackend selects the backend.

func WithContext source

func WithContext(ctx context.Context) ParseOption

WithContext selects invocation-local cooperative cancellation. Nil disables it.

func WithDiagnosticCallback source

func WithDiagnosticCallback(f func(*SyntaxError) error) ParseOption

WithDiagnosticCallback reports each committed #recover error during ParseStream or Iterate, immediately before the corresponding stream element. Speculative recoveries are not reported. The callback receives an independent copy; returning an error stops parsing and suppresses that element. A nil callback disables delivery. Non-nil callbacks are invalid with Parse, ParsePrefix and Document. Recoveries in a successful suffix are reported before the terminal result. With or without this option, recovered errors remain available at stream completion.

func WithDiagnosticLimit source

func WithDiagnosticLimit(n int) ParseOption

WithDiagnosticLimit retains at most n committed stream recoveries, keeping the latest errors. Zero retains every error, as in an unconfigured stream parse. Negative values are invalid. This option is valid only with ParseStream and Iterate, including when n is zero. When errors are dropped, the terminal error is *TruncatedSyntaxErrors.

func WithMaxDepth source

func WithMaxDepth(n int) ParseOption

WithMaxDepth limits the nesting of rule calls to n; deeper nesting makes the parse fail with an error. The operand of a prefix operator and the right operand of an infix operator of an Operators Rule expression nest like rule calls. The default is 100,000 (10,000,000 for BytecodeIterative). The limit keeps runaway recursion from exhausting the stack or memory. The Closure and Bytecode backends nest rule calls on the goroutine stack, so raising the limit far above the default can exceed Go's maximum stack size, which aborts the program; use BytecodeIterative for deeper nesting. A call answered from the memo does not nest, so whether a parse that comes close to the limit succeeds can depend on memoization.

func WithMissingTokens source

func WithMissingTokens(limit int) ParseOption

WithMissingTokens bounds attempted #missing literal insertions in one parse. Zero disables insertion; negative limits are invalid. Only Closure whole-input parsing and recognition support insertion. Positive limits are invalid with ParsePrefix, even without #missing in the grammar. ParsePrefix also rejects grammars containing #missing when insertion is disabled. Options resolve in order.

func WithProfile source

func WithProfile(prof *Profile) ParseOption

WithProfile adds the cost of the parse to prof. It traces the parse (see WithTrace), so the parse is several times slower, and the times in prof are meaningful relative to each other only. A nil prof is ignored.

func WithTrace source

func WithTrace(f func(TraceEvent)) ParseOption

WithTrace calls f at the start and at the end of every rule call of the parse, in order, so that the events nest like the calls (the depth of an event is in TraceEvent.Depth). It reports the calls the backend makes: a call skipped because the next character cannot start the rule (first-character dispatch) is not reported, and the backends do not skip the same calls. Tracing does not change the result, but it makes the parse several times slower. The event's methods (LineCol, Text, Failure) read the parser's state, so call them from f; its fields can be kept. With several WithTrace options, each function is called in order. A nil f is ignored.

Tracing works with every backend, with Parse, ParsePrefix, RecognizeOnly, ParseStream and Document (whose option applies to every Document.Parse), but not with generated Go parsers.

func WithUnit source

func WithUnit(u PositionUnit) ParseOption

WithUnit selects the position unit. Matching always proceeds by code point, whatever the unit.

func WithWorkLimits source

func WithWorkLimits(limits WorkLimits) ParseOption

WithWorkLimits sets both invocation-local work limits. Zero means unlimited.

type ParseStats source

type ParseStats = engine.Stats

ParseStats counts rule evaluations and reused results in a Document.Parse.

The type it stands for is defined in an internal package as follows. source

type Stats struct {
	Evaluated       int // number of rule body evaluations
	Reused          int // combined memo hits and resumed repetition elements
	MemoHits        int // memo-result reuse
	ResumedElements int // repetition elements reused
}

Stats counts rule evaluations and memo uses during a parse.

type Parser source

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

Parser is a compiled grammar. It is safe for concurrent use by multiple goroutines.

func Compile source

func Compile(g *grammar.Grammar, start string, opts ...CompileOption) (*Parser, error)

Compile compiles the grammar g into a parser that starts parsing at the rule start, which must be a start rule, a rule marked @start. An empty start selects the default start rule.

func CompileSource source

func CompileSource(src, start string, opts ...CompileOption) (*Parser, error)

CompileSource compiles PEGO source code into a parser that starts parsing at the rule start, as Compile does.

func LoadParser source

func LoadParser(data []byte) (*Parser, error)

LoadParser loads a parser saved by MarshalBinary or Marshal. Corruption is detected by a checksum and structural validation, but types are not checked again: load only data from trusted sources, such as the output of pego compile or MarshalBinary. The parser runs on the bytecode backends and serves the parses of the file's mode only; any other parse returns an error that names the mode it needs.

func (*Parser) CompiledMode source

func (p *Parser) CompiledMode() (CompiledMode, bool)

CompiledMode returns the mode of a parser loaded with LoadParser and true, or false for a parser compiled from a grammar, which serves every mode.

func (*Parser) DefaultStart source

func (p *Parser) DefaultStart() string

DefaultStart returns the default start rule of the grammar, which Compile selects for an empty start.

func (*Parser) DefaultValue source

func (p *Parser) DefaultValue() bool

DefaultValue reports whether the parser produces default values (WithDefaultValue).

func (*Parser) Grammar source

func (p *Parser) Grammar() *grammar.Grammar

Grammar returns the parser's grammar, or nil for a parser loaded from a compiled grammar.

func (*Parser) Iterate source

func (p *Parser) Iterate(r io.Reader, opts ...ParseOption) (*StreamIterator, error)

Iterate validates a #stream parser and returns a lazy pull iterator. Reader access begins on Next; lookahead and buffering may still read ahead. With a diagnostic callback, Next reports committed recoveries before their element.

func (*Parser) Marshal source

func (p *Parser) Marshal(mode CompiledMode) ([]byte, error)

Marshal encodes the parser in the .pegoc format: the bytecode of mode and the start rule. LoadParser restores it without parsing, analyzing, type checking or compiling the grammar again. A loaded parser is saved only in its own mode (CompiledMode), which reproduces the file it was loaded from; saving another mode needs the grammar source.

func (*Parser) MarshalBinary source

func (p *Parser) MarshalBinary() ([]byte, error)

MarshalBinary encodes the parser in parse mode (CompiledParse); see Marshal.

func (*Parser) NewDocument source

func (p *Parser) NewDocument(text string, opts ...ParseOption) (*Document, error)

NewDocument creates a Document for text. opts select its fixed unit and backend and the defaults for later Parse calls. Construction and each Parse use fresh work counters. Edit uses the same unit and is outside parse work controls. It returns an error if the backend is unavailable (Closure on a loaded parser) or the parser is loaded from a compiled grammar that is not in document mode.

func (*Parser) OutputSchema source

func (p *Parser) OutputSchema(opts ...SchemaOption) (*OutputSchema, error)

OutputSchema exports checked grammar metadata. It does not inspect parse results or change parsing.

func (*Parser) Parse source

func (p *Parser) Parse(input string, opts ...ParseOption) (*Node, error)

Parse parses the whole input and returns the value of the start rule. If the input does not match, it returns a *SyntaxError. If the parse recovered from errors with #recover, it returns both the node and the SyntaxErrors.

func (*Parser) ParsePrefix source

func (p *Parser) ParsePrefix(name, input string, opts ...ParseOption) (PrefixResult, error)

ParsePrefix parses the named start rule at the beginning of input without requiring whole-input consumption; an empty name selects the grammar's default start rule. EOF assertions still refer to the complete input. Positions remain local to input. For a source slice, add its starting byte offset to ConsumedBytes to find the consumed endpoint in the original source. Recovered success returns the result and SyntaxErrors. Unrecovered syntax failures and fatal errors return a zero result. A zero-width success returns zero consumption; a nil Node does not imply failure. Parse remains whole-input. WithDiagnosticCallback and positive WithMissingTokens limits are invalid for this operation. Grammars containing #missing are unsupported even when insertion is disabled.

func (*Parser) ParseStream source

func (p *Parser) ParseStream(r io.Reader, emit func(*Node) error, opts ...ParseOption) error

ParseStream parses input read from r and passes each element of the #stream repetition at the top level of the start rule to emit as soon as it matches. Emitted elements and the input before them are not retained, so the input never has to fit in memory. If emit returns an error, parsing stops and ParseStream returns that error. An element without a value (for example, one that is discarded with -) is passed as nil. As in any repetition, an element that matches without consuming input ends it. WithContext and WithWorkLimits apply to this invocation. Cancellation cannot interrupt a blocked reader or callback; the caller must arrange for it to return. A returned reader or emit error wins over cancellation first observed after that call. Delivered elements remain caller-owned, and retrying an interrupted stream requires caller-managed input replay. WithDiagnosticCallback delivers committed recoveries immediately before their elements; its error stops parsing before that element is emitted. Recovered errors are still returned at EOF. WithDiagnosticLimit bounds the retained recovery tail and explicitly marks a truncated result.

func (*Parser) SourceInfo source

func (p *Parser) SourceInfo() bool

SourceInfo reports whether the parser records source information (WithSourceInfo).

func (*Parser) Start source

func (p *Parser) Start() string

Start returns the name of the parser's start rule.

func (*Parser) Starts source

func (p *Parser) Starts() []string

Starts returns the start rules of the grammar, the rules marked @start where parsing may begin, in declaration order. The one marked @start(default), or the only one, is the default.

func (*Parser) WithStart source

func (p *Parser) WithStart(name string) (*Parser, error)

WithStart returns a parser that starts at the rule name, which must be a start rule; an empty name selects the grammar's default start rule. The compiled grammar is shared.

type PositionUnit source

type PositionUnit = engine.PositionUnit

PositionUnit is the unit of input positions: node Start and End (also in actions), syntax error positions and columns, and the len builtin.

The type it stands for is defined in an internal package as follows. source

type PositionUnit int

PositionUnit is the unit of positions in the input.

func (PositionUnit) String source

func (u PositionUnit) String() string

type PrefixResult source

type PrefixResult struct {
	Node          *Node
	Consumed      int // Selected parser Unit; defaults to CodePoints.
	ConsumedBytes int // Exact UTF-8 byte endpoint in the supplied input.
}

PrefixResult reports a rule's value and successful cursor. Consumption comes from the runtime cursor, even when Node is nil or its span differs.

type Profile source

type Profile = engine.Profile

Profile accumulates the cost of each rule over the parses given WithProfile: calls, body evaluations, memo hits, matches and failures, the input consumed and the input examined by failed calls, repeated evaluations at a position, and time. Hints summarizes where to look. A Profile is not safe for concurrent use.

The type it stands for is defined in an internal package as follows. source

type Profile struct {
	// Parses is the number of parses profiled.
	Parses int `json:"parses"`
	// Calls, Evals and MemoHits total the rules' counts.
	Calls    int `json:"calls"`
	Evals    int `json:"evals"`
	MemoHits int `json:"memoHits"`
	// ReusedElements totals repetition elements reused by completed root calls. Nested rule
	// rows also include this work, but their counts are not added to this total again.
	ReusedElements int `json:"reusedElements"`
	// Examined totals, over the parses, the end of the input the start rule examined.
	Examined int `json:"examined"`
	// Time totals the time of the parses' start rule calls (in nanoseconds in JSON).
	Time time.Duration `json:"time"`
	// Rules holds the rules called, in the order of their first call.
	Rules []*RuleProfile `json:"rules"`
	// contains filtered or unexported fields
}

Profile accumulates the cost of each rule over the parses it is attached to (as their trace function, Profile.Trace). It is not safe for concurrent use.

func (*Profile) Hints source

func (pr *Profile) Hints() []string

Hints returns observations about the profile that point at where a grammar does more work than it needs to, most important first. They are heuristics: each names what to look at, not what is wrong.

func (*Profile) Trace source

func (pr *Profile) Trace(e TraceEvent)

Trace records the event e. Pass it as the trace function of the parses to profile.

type QueryCapture source

type QueryCapture = schema.QueryCapture

The type it stands for is defined in an internal package as follows. source

type QueryCapture struct {
	Name, Category string
	Value          any
	Parent         *value.Node
	Path           []QueryEdge
}

QueryCapture borrows Value and Parent from the graph. Path identifies the edges from the root: "element" (Index) into a tuple, a list or a node's value, "field" (Name) into a struct or error, "capture" (Name) to a node's capture that is not an element, and "value" from a node to its value. Shared occurrences remain distinct.

type QueryEdge source

type QueryEdge = schema.QueryEdge

The type it stands for is defined in an internal package as follows. source

type QueryEdge struct {
	Kind  string
	Name  string
	Index int
}

QueryEdge is one edge of the path from the root to a captured value (QueryCapture).

type QueryFieldPattern source

type QueryFieldPattern = schema.QueryFieldPattern

The type it stands for is defined in an internal package as follows. source

type QueryFieldPattern struct {
	Name    string
	Pattern QueryPattern
}

QueryFieldPattern requires the field Name, whose value matches Pattern.

type QueryMatch source

type QueryMatch = schema.QueryMatch

The type it stands for is defined in an internal package as follows. source

type QueryMatch struct{ Captures []QueryCapture }

QueryMatch is one match of a pattern: the values its captures name.

type QueryOption source

type QueryOption func(*schema.QueryOptions)

QueryOption configures one query evaluation.

func WithQueryContext source

func WithQueryContext(ctx context.Context) QueryOption

WithQueryContext selects cooperative cancellation. Nil disables it.

func WithQueryWorkLimits source

func WithQueryWorkLimits(limits WorkLimits) QueryOption

WithQueryWorkLimits bounds query work. MaxSteps is charged for traversal, matching, paths and results; MaxInputWork is unused by structural queries. A zero limit means unlimited work.

type QueryPattern source

type QueryPattern = schema.QueryPattern

QueryPattern is a programmatic structural pattern. Empty constraints match any value of the applicable category. Elements form an ordered prefix of a tuple, a list or a node's value; required fields must be present, while extra elements and fields are allowed.

The type it stands for is defined in an internal package as follows. source

type QueryPattern struct {
	Type     string
	Rule     string
	Category string
	Capture  string
	Fields   []QueryFieldPattern
	Value    *QueryPattern
	Elements []QueryPattern
}

QueryPattern is the experimental programmatic structural pattern model. Empty Type, Rule and Category are unconstrained. Categories are node, struct, tuple, list, int, string, bool, nil and error. Type names a struct type, or error for the values of error recovery. Rule requires a node produced by the rule, which only grammars compiled with source information record. Fields require a struct or error value. Elements match the first elements of a tuple or list, and see through a node to its value as the language's x[i] does; Value matches the value of a node.

type QuerySchema source

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

QuerySchema is an output schema validated once for compiling many queries. It owns a copy of the schema it was prepared from, so later changes to that schema do not affect it. It is immutable and safe for concurrent use. Obtain one from PrepareQuerySchema: a nil or zero QuerySchema holds no schema and defers type and field checks until evaluation.

func PrepareQuerySchema source

func PrepareQuerySchema(out *OutputSchema) (*QuerySchema, error)

PrepareQuerySchema validates and copies the schema. Compiling a query with the result performs the same checks as CompileStructuralQuery with the schema, but does not validate the schema again.

func (*QuerySchema) CompileStructuralQuery source

func (s *QuerySchema) CompileStructuralQuery(pattern QueryPattern) (*StructuralQuery, error)

CompileStructuralQuery validates and copies a pattern, rejecting known impossibilities in the prepared schema. A nil or zero QuerySchema defers type and field checks until evaluation, as a nil OutputSchema does.

type RuleProfile source

type RuleProfile = engine.RuleProfile

RuleProfile is the cost of one rule in a Profile.

The type it stands for is defined in an internal package as follows. source

type RuleProfile struct {
	Rule string `json:"rule"`
	// Calls is the number of calls. Each call either evaluated the rule body or took the result from
	// the memo (MemoHits); it matched (Matched) or failed (Failed).
	Calls    int `json:"calls"`
	MemoHits int `json:"memoHits"`
	Matched  int `json:"matched"`
	Failed   int `json:"failed"`
	// Evals is the number of body evaluations: one per call not answered by the memo, and one more
	// for each step a left-recursive match grows.
	Evals int `json:"evals"`
	// ReusedElements counts repetition elements reused during the calls, nested calls included.
	// A nested call can report the same reuse as its parent; this is not a count of skipped calls.
	ReusedElements int `json:"reusedElements"`
	// Consumed totals the input matched by the calls that matched.
	Consumed int `json:"consumed"`
	// Wasted totals the input examined by the calls that evaluated the body and failed: the work
	// that led to nothing. It is inclusive: a failed call counts what the calls it made examined.
	// LongestFail is the most input a single failed call examined, at LongestFailAt.
	Wasted        int      `json:"wasted"`
	LongestFail   int      `json:"longestFail"`
	LongestFailAt Location `json:"longestFailAt"`
	// Repeats is the number of calls that evaluated the body at a position (and binding level)
	// where an earlier call of the same parse had evaluated it already. MaxEvals is the most
	// evaluating calls at one position, at MaxEvalsAt. A memoized rule is evaluated at most twice
	// at a position (the memo keeps a result from the second call on); a rule that is not
	// memoized is evaluated each time its callers are.
	Repeats    int      `json:"repeats"`
	MaxEvals   int      `json:"maxEvals"`
	MaxEvalsAt Location `json:"maxEvalsAt"`
	// Time is the time spent in the calls, nested calls included. A call within a call of the
	// same rule counts once. SelfTime excludes the time of nested calls. Both include the
	// overhead of measuring them, so they are meaningful relative to each other. JSON gives them in
	// nanoseconds.
	Time     time.Duration `json:"time"`
	SelfTime time.Duration `json:"selfTime"`
}

RuleProfile is the cost of one rule. Calls of a rule's value-free twin count as calls of the rule.

type SchemaCapture source

type SchemaCapture = schema.Capture

The type it stands for is defined in an internal package as follows. source

type Capture struct {
	Name  string  `json:"name"`
	Paths [][]int `json:"paths,omitempty"`
	Side  bool    `json:"side,omitempty"`
	Shape Shape   `json:"shape"`
}

Capture locates a capture of a node. Each path is a path of tuple indexes inside the node's value, one for each place the capture can be; the alternative that matched selects one. Side reports that the capture can also be in a positive lookahead, whose value is kept beside the tuple. Shape is the shape of the capture's value.

type SchemaChangeKind source

type SchemaChangeKind string

SchemaChangeKind identifies an entry added, removed, or changed by a diff.

const (
	SchemaAdded   SchemaChangeKind = "added"
	SchemaRemoved SchemaChangeKind = "removed"
	SchemaChanged SchemaChangeKind = "changed"
)

type SchemaField source

type SchemaField = schema.Field

The type it stands for is defined in an internal package as follows. source

type Field struct {
	Name  string `json:"name"`
	Shape Shape  `json:"shape"`
}

Field is a field of a struct type.

type SchemaOption source

type SchemaOption func(*schemaOptions)

SchemaOption configures one output schema export.

func WithAllSchemaRules source

func WithAllSchemaRules() SchemaOption

WithAllSchemaRules includes unreachable rules and declared types in the schema. By default, export starts at the parser's configured start rule.

type SchemaRule source

type SchemaRule = schema.Rule

The type it stands for is defined in an internal package as follows. source

type Rule struct {
	Name   string `json:"name"`
	Origin string `json:"origin"`
	Result Shape  `json:"result"`
}

Rule is a rule. Its result refers to the inferred type of the rule; the origin is "declared" when the rule declares its type.

type SchemaRuleChange source

type SchemaRuleChange struct {
	ID     string
	Kind   SchemaChangeKind
	Before *SchemaRule
	After  *SchemaRule
}

SchemaRuleChange describes one rule, using the rule name as its ID. Before is nil for additions; After is nil for removals.

type SchemaShape source

type SchemaShape = schema.Shape

The type it stands for is defined in an internal package as follows. source

type Shape struct {
	Kind         string    `json:"kind"`
	Ref          string    `json:"ref,omitempty"`
	Scalar       string    `json:"scalar,omitempty"`
	Element      *Shape    `json:"element,omitempty"`
	Elements     []Shape   `json:"elements,omitempty"`
	Alternatives []Shape   `json:"alternatives,omitempty"`
	Fields       []Field   `json:"fields,omitempty"`
	Value        *Shape    `json:"value,omitempty"`
	Captures     []Capture `json:"captures,omitempty"`
}

Shape describes the values of a type. Kind selects the members:

  • "scalar": Scalar is int, string or bool.
  • "nil", "top" (every value), "error" (a value of error recovery) and "missing" (a token that recovery inserted, a kind of error) have no members.
  • "node": Value is the shape of the node's value, and Captures locates its captures.
  • "tuple": Elements are the shapes of the elements, in order; the unit tuple has none.
  • "list" and "optional": Element is the shape of the elements, or of the value when present.
  • "join": Alternatives are the shapes of the alternatives.
  • "struct": Fields are the fields of a defined struct type.
  • "ref": Ref is the ID of a type of the schema; recursive types are never expanded.

type SchemaType source

type SchemaType = schema.Type

The type it stands for is defined in an internal package as follows. source

type Type struct {
	ID     string `json:"id"`
	Name   string `json:"name"`
	Origin string `json:"origin"`
	Shape  Shape  `json:"shape"`
}

Type is a struct type defined in the grammar (origin "declared") or the value of a rule (origin "inferred").

type SchemaTypeChange source

type SchemaTypeChange struct {
	ID     string
	Kind   SchemaChangeKind
	Before *SchemaType
	After  *SchemaType
}

SchemaTypeChange describes one type, keyed by its namespace-qualified ID. Before is nil for additions; After is nil for removals.

type Snapshot source

type Snapshot = engine.Snapshot

Snapshot is an immutable document parse outcome.

The type it stands for is defined in an internal package as follows. source

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

Snapshot owns an immutable parse outcome for one document revision. Readers may use it concurrently with later edits of its single-owner Document.

func (*Snapshot) Error source

func (s *Snapshot) Error() error

Error returns a defensive copy of the captured diagnostic.

func (*Snapshot) Materialize source

func (s *Snapshot) Materialize() *value.Node

Materialize makes an independent mutable graph, preserving sharing, cycles, nil child slots, and nil versus allocated-empty slices.

func (*Snapshot) Root source

func (s *Snapshot) Root() SnapshotNode

Root returns the captured root, or the zero handle for a nil result.

func (*Snapshot) Stats source

func (s *Snapshot) Stats() Stats

Stats returns the evaluation and memo counts for this capture's parse.

func (*Snapshot) Text source

func (s *Snapshot) Text() string

Text returns the captured text.

func (*Snapshot) Unit source

func (s *Snapshot) Unit() PositionUnit

Unit returns the position unit of the captured graph.

func (*Snapshot) Version source

func (s *Snapshot) Version() DocumentVersion

Version returns the captured text revision.

type SnapshotCaptureError source

type SnapshotCaptureError = engine.SnapshotCaptureError

SnapshotCaptureError reports unsupported mutable or custom field values.

The type it stands for is defined in an internal package as follows. source

type SnapshotCaptureError struct {
	Path       string
	ValueType  reflect.Type
	ParseError error
}

SnapshotCaptureError reports an unsupported dynamic field type. It retains type metadata and an owned parse error, never the unsupported field value.

func (*SnapshotCaptureError) Error source

func (e *SnapshotCaptureError) Error() string

func (*SnapshotCaptureError) Unwrap source

func (e *SnapshotCaptureError) Unwrap() error

type SnapshotField source

type SnapshotField = engine.SnapshotField

SnapshotField stores a read-only node handle or immutable scalar.

The type it stands for is defined in an internal package as follows. source

type SnapshotField struct {
	Name  string
	Value any
}

SnapshotField holds an immutable scalar or SnapshotNode, never a mutable Node.

type SnapshotNode source

type SnapshotNode = engine.SnapshotNode

SnapshotNode is a comparable read-only node handle; its zero value is nil.

The type it stands for is defined in an internal package as follows. source

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

SnapshotNode is a comparable read-only handle. Its zero value denotes nil.

func (SnapshotNode) Child source

func (n SnapshotNode) Child(i int) SnapshotNode

Child returns an indexed child; an invalid index panics like slice indexing.

func (SnapshotNode) ChildCount source

func (n SnapshotNode) ChildCount() int

ChildCount includes nil child slots.

func (SnapshotNode) End source

func (n SnapshotNode) End() int32

End returns the exclusive end position.

func (SnapshotNode) Field source

func (n SnapshotNode) Field(name string) (any, bool)

Field distinguishes absent fields from present nil values.

func (SnapshotNode) Fields source

func (n SnapshotNode) Fields() []SnapshotField

Fields returns an owned slice in stored order. Typed nil nodes remain zero handles.

func (SnapshotNode) IsTerminal source

func (n SnapshotNode) IsTerminal() bool

IsTerminal reports whether the node is terminal.

func (SnapshotNode) Rule source

func (n SnapshotNode) Rule() string

Rule returns the originating rule, or an empty string for nil.

func (SnapshotNode) Start source

func (n SnapshotNode) Start() int32

Start returns the inclusive start position.

func (SnapshotNode) Text source

func (n SnapshotNode) Text() string

Text returns terminal text.

func (SnapshotNode) Type source

func (n SnapshotNode) Type() string

Type returns the node type, or an empty string for nil.

func (SnapshotNode) Valid source

func (n SnapshotNode) Valid() bool

Valid reports whether the handle denotes a non-nil node.

type StreamIterator source

type StreamIterator = engine.StreamIterator

StreamIterator delivers one committed stream element per Next call. It is single-owner; Next and Close must not overlap. The reader remains caller-owned, and Close cannot interrupt a blocked Read.

The type it stands for is defined in an internal package as follows. source

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

StreamIterator advances synchronously to one committed element at a time. It is single-owner: Next and Close must not overlap, including callbacks. Close releases parser state but never closes or interrupts its reader. The standard library pull coroutine has no independently scheduled producer.

func (*StreamIterator) Close source

func (it *StreamIterator) Close()

Close abandons unfinished parsing and is idempotent. It cannot interrupt a Read already blocked in caller code, and must not overlap a call to Next.

func (*StreamIterator) Next source

func (it *StreamIterator) Next() (n *value.Node, ok bool, err error)

Next returns (node,true,nil), even for a successful discarded nil value. Clean EOF is (nil,false,nil); terminal errors remain stable on later calls.

type StructuralQuery source

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

StructuralQuery is an immutable compiled programmatic pattern.

func CompileStructuralQuery source

func CompileStructuralQuery(pattern QueryPattern, out *OutputSchema) (*StructuralQuery, error)

CompileStructuralQuery validates and copies a pattern. A nil schema defers type and field checks until evaluation; a supplied schema is validated first and rejects known impossibilities.

func (*StructuralQuery) Matches source

func (q *StructuralQuery) Matches(root *Node, opts ...QueryOption) ([]QueryMatch, error)

Matches evaluates every incoming graph edge in preorder: children by index, then fields by name, then nested list elements by index. Results borrow graph values; callers must keep the graph stable. Cancellation, work-limit exhaustion and cycles return an error without partial results.

type SyntaxError source

type SyntaxError = engine.SyntaxError

SyntaxError reports that the input does not match the grammar.

The type it stands for is defined in an internal package as follows. source

type SyntaxError struct {
	Pos      int      // position, in the parse's position unit
	Line     int      // 1-based line
	Col      int      // 1-based column, in the position unit of the parse
	Expected []string // what was expected at that position
	Messages []string // messages given by @error
}

SyntaxError reports that the input did not match the grammar.

func (*SyntaxError) Error source

func (e *SyntaxError) Error() string

func (*SyntaxError) Message source

func (e *SyntaxError) Message() string

Message returns the error description without the position.

type SyntaxErrors source

type SyntaxErrors = engine.SyntaxErrors

SyntaxErrors lists the syntax errors recovered with #recover or #missing.

The type it stands for is defined in an internal package as follows. source

type SyntaxErrors []*SyntaxError

SyntaxErrors is a list of syntax errors, including those recovered by error recovery.

func (SyntaxErrors) Error source

func (l SyntaxErrors) Error() string

type TextEdit source

type TextEdit = engine.TextEdit

TextEdit replaces [Start, End) in the document's position unit. EditBatch interprets every range in the supplied base version's coordinates.

The type it stands for is defined in an internal package as follows. source

type TextEdit struct {
	Start, End int
	Text       string
}

TextEdit replaces [Start, End) in the position unit of its base Document.

type TraceEvent source

type TraceEvent = engine.TraceEvent

TraceEvent describes the start (TraceEnter) or the end (TraceExit) of a rule call. See WithTrace.

The type it stands for is defined in an internal package as follows. source

type TraceEvent struct {
	Kind TraceKind
	// Rule is the name of the rule called.
	Rule string
	// Level is the binding level the rule was called with: for an Operators Rule rule called as e:level,
	// the operators that bind no tighter than that level are left to the caller. It is 0 for
	// calls without a level.
	Level int
	// Depth is the nesting depth of the call: 1 for the start rule.
	Depth int
	// Pos is the position at which the call starts.
	Pos int
	// Lookahead reports that the call is inside a lookahead (& or !), where failures record no
	// expectations.
	Lookahead bool

	// Matched reports whether the rule matched.
	Matched bool
	// End is the position after the match (Pos if the rule failed).
	End int
	// Memo reports that the result came from the memo, without evaluating the rule body.
	Memo bool
	// Evals is the number of times this call evaluated the rule body: 0 for a result from the
	// memo, 1 usually, and one more per step for a left-recursive rule whose match grows. Bodies of
	// nested calls are not included.
	Evals int
	// ReusedElements counts repetition elements reused during this call, including nested calls.
	// It measures resumption performed in this invocation, not the calls skipped by reusing an
	// element or work from the parse that originally built a memo result.
	ReusedElements int
	// Examined is the end of the input the call examined (exclusive): looking at a character to
	// reject it counts, as do lookaheads.
	Examined int
	// contains filtered or unexported fields
}

TraceEvent describes the start or the end of a rule call. Events nest: every TraceEnter is followed, after the events of the calls the rule makes, by its TraceExit, unless the parse is aborted: by a runtime error in an action, an error returned by a stream's emit function or reader, the nesting limit, or a panic in the trace function, which propagates out of the parse unchanged.

The methods LineCol, Text and Failure read the parser's state: call them only from the trace function, while it handles the event.

func (TraceEvent) Failure source

func (e TraceEvent) Failure() *SyntaxError

Failure returns, for an exit event, the farthest failure recorded during the call: the position and what was expected there, as a parse that failed there would report it. It is nil when the call recorded no expectation (inside a lookahead, or when nothing failed). A call that matched can have one too: the input after its match was tried and rejected.

func (TraceEvent) LineCol source

func (e TraceEvent) LineCol(pos int) (line, col int)

LineCol returns the 1-based line and column of the position pos. In a stream parse, it returns 0, 0 for a position (other than 0) in input that has been discarded or not read yet: keeping the line starts of discarded input would make the memory of a stream grow with its length.

func (TraceEvent) Recovered source

func (e TraceEvent) Recovered() []*SyntaxError

Recovered returns, for an exit event of a call that matched, the syntax errors that #recover recovered from during the call, in nested calls too, or taken from the memo with the call's result. The expectations of a recovered error are not part of Failure: recovering removes them from the record of the call that contains the #recover.

func (TraceEvent) Text source

func (e TraceEvent) Text() string

Text returns the input the call matched (empty for an enter event or a failure). Input that a stream parse has already discarded is not available.

type TraceKind source

type TraceKind = engine.TraceKind

TraceKind tells whether a TraceEvent starts or ends a rule call.

The type it stands for is defined in an internal package as follows. source

type TraceKind uint8

func (TraceKind) String source

func (k TraceKind) String() string

type TruncatedSyntaxErrors source

type TruncatedSyntaxErrors = engine.TruncatedSyntaxErrors

TruncatedSyntaxErrors reports a bounded tail of recovered stream errors. Total and Dropped are lower bounds when CountsSaturated is true.

The type it stands for is defined in an internal package as follows. source

type TruncatedSyntaxErrors struct {
	Errors          SyntaxErrors
	Total           uint64
	Dropped         uint64
	CountsSaturated bool
}

TruncatedSyntaxErrors is the incomplete result of a stream parsed with a positive diagnostic limit. Errors is the retained tail in occurrence order. Saturated counts are lower bounds; truncation and the tail remain exact.

func (*TruncatedSyntaxErrors) Error source

func (e *TruncatedSyntaxErrors) Error() string

func (*TruncatedSyntaxErrors) Unwrap source

func (e *TruncatedSyntaxErrors) Unwrap() error

Unwrap exposes the retained errors to errors.As as SyntaxErrors.

type WalkAction source

type WalkAction = value.WalkAction

WalkAction controls whether a walk descends, skips descendants, or stops.

The type it stands for is defined in an internal package as follows. source

type WalkAction uint8

WalkAction controls descent after a visit.

type WalkEdge source

type WalkEdge = value.WalkEdge

WalkEdge identifies a root, child, field, or nested []any list edge.

The type it stands for is defined in an internal package as follows. source

type WalkEdge struct {
	Kind       WalkEdgeKind
	ChildIndex int
	FieldName  string
	ListIndex  int
}

WalkEdge identifies an incoming edge. Only its Kind's index or name is meaningful.

type WalkEdgeKind source

type WalkEdgeKind = value.WalkEdgeKind

WalkEdgeKind identifies the kind of a visit's incoming edge.

The type it stands for is defined in an internal package as follows. source

type WalkEdgeKind uint8

WalkEdgeKind identifies the incoming edge of a value.

type WalkVisit source

type WalkVisit = value.WalkVisit

WalkVisit describes a live value, its incoming edge and nearest owning node. Node is non-nil only for node values. Depth counts edges from the root.

The type it stands for is defined in an internal package as follows. source

type WalkVisit struct {
	Value  any
	Node   *Node
	Parent *Node
	Depth  int
	Edge   WalkEdge
}

WalkVisit describes a live value and its nearest owning node. Depth counts edges. Node is populated only for non-nil *Node values; Value retains typed nil leaves.

type WorkLimitError source

type WorkLimitError = limits.WorkLimitError

WorkLimitError describes a reservation that would exceed a work limit.

The type it stands for is defined in an internal package as follows. source

type WorkLimitError struct {
	Kind      WorkLimitKind
	Limit     uint64
	Used      uint64
	Requested uint64
}

WorkLimitError reports a reservation that would exceed one work limit. Used is the counter value before the rejected reservation; Requested is the complete atomic reservation that could not be made.

func (*WorkLimitError) Error source

func (e *WorkLimitError) Error() string

func (*WorkLimitError) Is source

func (e *WorkLimitError) Is(target error) bool

Is reports whether target is the work-limit sentinel.

type WorkLimitKind source

type WorkLimitKind = limits.WorkLimitKind

WorkLimitKind identifies the exhausted counter.

The type it stands for is defined in an internal package as follows. source

type WorkLimitKind uint8

WorkLimitKind identifies the counter that exhausted its limit.

type WorkLimits source

type WorkLimits = limits.WorkLimits

WorkLimits configures cumulative engine work for a parse invocation. Zero limits are unlimited. Counts depend on backend, input unit and version.

The type it stands for is defined in an internal package as follows. source

type WorkLimits struct {
	MaxInputWork uint64
	MaxSteps     uint64
}

WorkLimits bounds cumulative engine work for one parse invocation. A zero limit leaves that counter unlimited.