document

package
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DiagLikelyCalculation indicates a line that looks like an assignment
	// but failed to parse as a calculation.
	DiagLikelyCalculation = "LIKELY_CALCULATION"
)

Diagnostic codes

Variables

View Source
var ErrPartialEvaluation = errors.New("partial evaluation: one or more blocks had errors")

ErrPartialEvaluation is returned when evaluation continued past errors. Callers can check errors.Is(err, ErrPartialEvaluation) to distinguish partial evaluation (some blocks failed) from total failure.

Functions

func NewDirectiveResolver

func NewDirectiveResolver(fm *document.Frontmatter) interpreter.DirectiveResolver

NewDirectiveResolver creates an interpreter.DirectiveResolver that adapts *document.Frontmatter for @directive resolution. Used by both the document evaluator and the top-level Eval API.

Types

type BlockDiagnostic

type BlockDiagnostic struct {
	BlockID  string             // ID of the block containing the issue
	Line     int                // Line number within block (1-indexed)
	Severity DiagnosticSeverity // Warning or Error
	Code     string             // Diagnostic code (e.g., "LIKELY_CALCULATION")
	Message  string             // Human-readable message
	Source   string             // The problematic line content
}

BlockDiagnostic represents a warning or error for a specific line in a block.

type CalculationIndicator

type CalculationIndicator struct {
	Name        string                          // Short name for the indicator
	Description string                          // Human-readable description
	Check       func(tokens []lexer.Token) bool // Returns true if pattern matches
}

CalculationIndicator defines a pattern that suggests a line was intended to be a calculation. Each indicator has a check function and description.

func GetCalculationIndicators

func GetCalculationIndicators() []CalculationIndicator

GetCalculationIndicators returns the list of patterns used to detect likely calculation attempts. This is useful for documentation and debugging.

type DiagnosticSeverity

type DiagnosticSeverity int

DiagnosticSeverity indicates the severity of a diagnostic.

const (
	// Warning indicates a potential problem that doesn't prevent evaluation.
	Warning DiagnosticSeverity = iota
	// Error indicates a problem that prevents evaluation.
	Error
)

func (DiagnosticSeverity) String

func (s DiagnosticSeverity) String() string

type Evaluator

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

Evaluator evaluates CalcMark documents using the interpreter. This lives in impl/ because it performs execution, not just validation.

func NewEvaluator

func NewEvaluator() *Evaluator

NewEvaluator creates a new document evaluator.

func (*Evaluator) Diagnostics

func (e *Evaluator) Diagnostics() []BlockDiagnostic

Diagnostics returns warnings and errors collected during evaluation. This includes warnings about TextBlock lines that look like failed calculations.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(doc *document.Document) error

Evaluate evaluates all blocks in the document in dependency order. CalcBlocks are evaluated top-down with accumulated environment. TextBlocks are checked for lines that look like failed calculations.

Returns an error if any CalcBlock fails to evaluate. Use Diagnostics() to get warnings about TextBlocks with likely calculation errors.

func (*Evaluator) EvaluateAffectedBlocks

func (e *Evaluator) EvaluateAffectedBlocks(doc *document.Document, blockIDs []string) error

EvaluateAffectedBlocks efficiently re-evaluates only the specified blocks. This is the surgical API for WYSIWYG editors that need minimal updates.

Usage:

result, _ := doc.InsertBlock(afterID, BlockCalculation, source)
orderedBlocks := doc.GetBlocksInDependencyOrder(result.AffectedBlockIDs)
eval.EvaluateAffectedBlocks(doc, orderedBlocks)

The blocks should be in dependency order (use GetBlocksInDependencyOrder). The environment is NOT reset - it maintains accumulated state from previous evaluations.

func (*Evaluator) EvaluateBlock

func (e *Evaluator) EvaluateBlock(doc *document.Document, blockID string) error

EvaluateBlock evaluates a single block and re-evaluates all blocks for reactive semantics. This is the simple API for REPL usage where full consistency is needed.

For WYSIWYG editors that need surgical updates, use EvaluateAffectedBlocks instead with the AffectedBlockIDs from InsertBlock/ReplaceBlockSource.

func (*Evaluator) GetDisplayFormatter

func (e *Evaluator) GetDisplayFormatter() display.Formatter

GetDisplayFormatter returns the evaluator's display formatter for use by output formatters. After Evaluate(), this includes measurement conventions wired from frontmatter.

func (*Evaluator) GetEnvironment

func (e *Evaluator) GetEnvironment() *interpreter.Environment

GetEnvironment returns the interpreter's environment. Useful for accessing current variable values.

func (*Evaluator) SetDisplayFormatter

func (e *Evaluator) SetDisplayFormatter(df display.Formatter)

SetDisplayFormatter sets the display formatter used for {{var}} interpolation in TextBlocks. If not set, a default en-US formatter is used.

Jump to

Keyboard shortcuts

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