syntax

package
v0.0.0-...-3c7d8b7 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// There is no statically resolvable binding name.
	ErrBareImport_Dynamic = errors.New("there is no statically resolvable binding name")
	// The import source is not a valid path or the path stem not a valid
	// identifier.
	ErrBareImport_PathInvalid = errors.New("the import source is not a valid path or the path stem not a valid identifier")
	// The import source is not a valid package spec.
	ErrBareImport_PackageInvalid = errors.New("the import source is not a valid package spec")
)

Reasons why a bare name cannot be determined for an import source.

BareImportError

ArgSet contains syntax kinds that can start an argument in a function call.

View Source
var ArrayOrDictItemSet = CodeExprSet.Add(SyntaxKindDots)

ArrayOrDictItemSet contains syntax kinds that can start an argument in a function call.

View Source
var AtomicCodeExprSet = AtomicCodePrimarySet

AtomicCodeExprSet contains syntax kinds that can start an atomic code expression.

AtomicCodePrimarySet contains syntax kinds that can start an atomic code primary.

BinaryOpSet contains syntax kinds that are binary operators.

View Source
var CodeExprSet = CodePrimarySet.Union(UnaryOpSet)

CodeExprSet contains syntax kinds that can start a code expression.

CodePrimarySet contains syntax kinds that can start a code primary.

View Source
var DestructuringItemSet = PatternSet.Add(SyntaxKindDots)

DestructuringItemSet contains syntax kinds that can start a destructuring item.

View Source
var ErrUnnumberable = errors.New("cannot number within this interval")

Indicates that a node cannot be numbered within a given interval.

MathExprSet contains syntax kinds that can start a math expression.

View Source
var MathShorthand_LIST = map[string]rune{
	"...":  '…',
	"-":    '−',
	"*":    '∗',
	"~":    '∼',
	"!=":   '≠',
	":=":   '≔',
	"::=":  '⩴',
	"=:":   '≕',
	"<<":   '≪',
	"<<<":  '⋘',
	">>":   '≫',
	">>>":  '⋙',
	"<=":   '≤',
	">=":   '≥',
	"->":   '→',
	"-->":  '⟶',
	"|->":  '↦',
	">->":  '↣',
	"->>":  '↠',
	"<-":   '←',
	"<--":  '⟵',
	"<-<":  '↢',
	"<<-":  '↞',
	"<->":  '↔',
	"<-->": '⟷',
	"~>":   '⇝',
	"~~>":  '⟿',
	"<~":   '⇜',
	"<~~":  '⬳',
	"=>":   '⇒',
	"|=>":  '⤇',
	"==>":  '⟹',
	"<==":  '⟸',
	"<=>":  '⇔',
	"<==>": '⟺',
	"[|":   '⟦',
	"|]":   '⟧',
	"||":   '‖',
}

A list of all shorthands in math mode.

View Source
var ParamSet = PatternSet.Add(SyntaxKindDots)

ParamSet contains syntax kinds that can start a parameter in a parameter list.

View Source
var PatternLeafSet = AtomicCodeExprSet

PatternLeafSet contains syntax kinds that can start a pattern leaf.

PatternSet contains syntax kinds that can start a pattern.

View Source
var Shorthand_LIST = map[string]rune{
	"...": '…',
	"~":   '\u00A0',
	"-":   '\u2212',
	"--":  '\u2013',
	"---": '\u2014',
	"-?":  '\u00AD',
}

A list of all shorthands in markup mode.

LIST

View Source
var (
	// The full range of numbers available for source file span numbering.
	SpanFull = ranges.NewRange(2, rangeBase)
)

StmtSet contains syntax kinds that can start a statement.

View Source
var TABLE = [128]bool{
	' ':    true,
	'\t':   true,
	'\n':   true,
	'\x0b': true,
	'\x0c': true,
	'\r':   true,
	'\\':   true,
	'/':    true,
	'[':    true,
	']':    true,
	'~':    true,
	'-':    true,
	'.':    true,
	'\'':   true,
	'"':    true,
	'*':    true,
	'_':    true,
	':':    true,
	'h':    true,
	'`':    true,
	'$':    true,
	'<':    true,
	'>':    true,
	'@':    true,
	'#':    true,
}

UnaryOpSet contains syntax kinds that are unary operators.

Functions

func BinOp_from_kind

func BinOp_from_kind(token SyntaxKind) option.Option[BinOp]

Try to convert the token into a binary operation.

from_kind

func Expr_cast_with_space

func Expr_cast_with_space(node *SyntaxNode) option.Option[Expr]

cast_with_space

func Expr_from_untyped

func Expr_from_untyped(node *SyntaxNode) option.Option[Expr]

from_untyped

func Expr_hash

func Expr_hash(expr Expr) bool

Can this expression be embedded into markup with a hash?

hash

func Expr_is_literal

func Expr_is_literal(expr Expr) bool

Is this a literal?

is_literal

func GraphemeLen

func GraphemeLen(str string) (int, bool)

graphemes

func Node_is_ident

func Node_is_ident(node *LinkedNode) bool

Whether the node is one of the two identifier nodes.

is_ident

func PrintNode

func PrintNode(out io.Writer, node *SyntaxNode)

func PrintRoot

func PrintRoot(root *SyntaxNode)

func SyntaxNode_cast

func SyntaxNode_cast[T AstNode](node *SyntaxNode) option.Option[T]

Try to convert the node to a typed AST node.

cast

func SyntaxNode_cast_first

func SyntaxNode_cast_first[T AstNode](node *SyntaxNode) T

Get the first child of AST type `T` or a placeholder if none.

cast_first

func SyntaxNode_cast_last

func SyntaxNode_cast_last[T AstNode](node *SyntaxNode) T

Get the last child of AST type `T` or a placeholder if none.

cast_last

func SyntaxNode_is

func SyntaxNode_is[T AstNode](node *SyntaxNode) bool

Whether the node can be cast to the given AST node.

is

func SyntaxNode_try_cast_first

func SyntaxNode_try_cast_first[T AstNode](node *SyntaxNode) option.Option[T]

Find the first child that can cast to the AST type `T`.

try_cast_first

func SyntaxNode_try_cast_last

func SyntaxNode_try_cast_last[T AstNode](node *SyntaxNode) option.Option[T]

Find the last child that can cast to the AST type `T`.

try_cast_last

func UnOp_from_kind

func UnOp_from_kind(token SyntaxKind) option.Option[UnOp]

Try to convert the token into a unary operation.

Types

type Arg

type Arg interface {
	AstNode
	// contains filtered or unexported methods
}

An argument to a function call.

type Arg_Named

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

A named argument: `draw: false`.

Named

type Arg_Pos

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

A positional argument: `12`.

Pos

type Arg_Spread

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

A spread argument: `..things`.

Spread

type Args

type Args struct {
	*SyntaxNode
}

A function call's argument list: `(12pt, y)`.

func Args_default

func Args_default() *Args

type Array

type Array struct {
	*SyntaxNode
}

An array: `(1, "hi", 12cm)`.

func Array_default

func Array_default() *Array

type ArrayItem

type ArrayItem interface {
	AstNode
	// contains filtered or unexported methods
}

An item in an array.

type ArrayItem_Pos

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

A bare expression: `12`.

Pos

type ArrayItem_Spread

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

A spread expression: `..things`.

Spread

type Assoc

type Assoc uint8

The associativity of a binary operator.

const (
	// Left-associative: `a + b + c` is equivalent to `(a + b) + c`.
	//
	// Left
	Assoc_Left Assoc = iota + 1
	// Right-associative: `a = b = c` is equivalent to `a = (b = c)`.
	//
	// Right
	Assoc_Right
)

type AstNode

type AstNode interface {
	// contains filtered or unexported methods
}

A typed AST node.

type AtNewline

type AtNewline interface {
	// contains filtered or unexported methods
}

How to proceed with parsing when at a newline.

type AtNewline_ContextualContinue

type AtNewline_ContextualContinue struct{}

Continue only if there is a continuation with `else` or `.` (Code only).

ContextualContinue

type AtNewline_Continue

type AtNewline_Continue struct{}

Continue at newlines.

Continue

type AtNewline_RequireColumn

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

Require that the token's column be greater or equal to a column (Markup only). If this is `0`, acts like `Continue`; if this is `usize::MAX`, acts like `Stop`.

RequireColumn

func NewAtNewline_RequireColumn

func NewAtNewline_RequireColumn(min_col uint) *AtNewline_RequireColumn

type AtNewline_Stop

type AtNewline_Stop struct{}

Stop at any newline.

Stop

type AtNewline_StopParBreak

type AtNewline_StopParBreak struct{}

Stop only at a parbreak, not normal newlines (Markup only).

StopParBreak

type Auto

type Auto struct {
	*SyntaxNode
}

The `auto` literal.

func Auto_default

func Auto_default() *Auto

type BinOp

type BinOp uint8

A binary operator.

const (
	// The addition operator: `+`.
	//
	// Add
	BinOp_Add BinOp = iota + 1
	// The subtraction operator: `-`.
	//
	// Sub
	BinOp_Sub
	// The multiplication operator: `*`.
	//
	// Mul
	BinOp_Mul
	// The division operator: `/`.
	//
	// Div
	BinOp_Div
	// The short-circuiting boolean `and`.
	//
	// And
	BinOp_And
	// The short-circuiting boolean `or`.
	//
	// Or
	BinOp_Or
	// The equality operator: `==`.
	//
	// Eq
	BinOp_Eq
	// The inequality operator: `!=`.
	//
	// Neq
	BinOp_Neq
	// The less-than operator: `<`.
	//
	// Lt
	BinOp_Lt
	// The less-than or equal operator: `<=`.
	//
	// Leq
	BinOp_Leq
	// The greater-than operator: `>`.
	//
	// Gt
	BinOp_Gt
	// The greater-than or equal operator: `>=`.
	//
	// Geq
	BinOp_Geq
	// The assignment operator: `=`.
	//
	// Assign
	BinOp_Assign
	// The containment operator: `in`.
	//
	// In
	BinOp_In
	// The inverse containment operator: `not in`.
	//
	// NotIn
	BinOp_NotIn
	// The add-assign operator: `+=`.
	//
	// AddAssign
	BinOp_AddAssign
	// The subtract-assign operator: `-=`.
	//
	// SubAssign
	BinOp_SubAssign
	// The multiply-assign operator: `*=`.
	//
	// MulAssign
	BinOp_MulAssign
	// The divide-assign operator: `/=`.
	//
	// DivAssign
	BinOp_DivAssign
)

func (BinOp) String

func (op BinOp) String() string

The string representation of this operation.

as_str

type Binary

type Binary struct {
	*SyntaxNode
}

A binary operation: `a + b`.

func Binary_default

func Binary_default() *Binary

type Bool

type Bool struct {
	*SyntaxNode
}

A boolean: `true`, `false`.

func Bool_default

func Bool_default() *Bool

type Checkpoint

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

A checkpoint of the parser which can fully restore it to a previous state.

type Closure

type Closure struct {
	*SyntaxNode
}

A closure: `(x, y) => z`.

func Closure_default

func Closure_default() *Closure

type Code

type Code struct {
	*SyntaxNode
}

The body of a code block.

func Code_default

func Code_default() *Code

type CodeBlock

type CodeBlock struct {
	*SyntaxNode
}

A code block: `{ let x = 1; x + 2 }`.

func CodeBlock_default

func CodeBlock_default() *CodeBlock

type Conditional

type Conditional struct {
	*SyntaxNode
}

An if-else conditional: `if x { y } else { z }`.

func Conditional_default

func Conditional_default() *Conditional

type ContentBlock

type ContentBlock struct {
	*SyntaxNode
}

A content block: `[*Hi* there!]`.

func ContentBlock_default

func ContentBlock_default() *ContentBlock

type Contextual

type Contextual struct {
	*SyntaxNode
}

A contextual expression: `context text.lang`.

func Contextual_default

func Contextual_default() *Contextual

type DestructAssignment

type DestructAssignment struct {
	*SyntaxNode
}

An assignment expression `(x, y) = (1, 2)`.

func DestructAssignment_default

func DestructAssignment_default() *DestructAssignment

type Destructuring

type Destructuring struct {
	*SyntaxNode
}

A destructuring pattern: `x` or `(x, _, ..y)`.

func Destructuring_default

func Destructuring_default() *Destructuring

type DestructuringItem

type DestructuringItem interface {
	AstNode
	// contains filtered or unexported methods
}

The kind of an element in a destructuring pattern.

type DestructuringItem_Named

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

A renamed destructuring: `x: y`.

Named

type DestructuringItem_Pattern

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

A sub-pattern: `x`.

Pattern

type DestructuringItem_Spread

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

A destructuring sink: `..y` or `..`.

Spread

type Dict

type Dict struct {
	*SyntaxNode
}

A dictionary: `(thickness: 3pt, dash: "solid")`.

func Dict_default

func Dict_default() *Dict

type DictItem

type DictItem interface {
	AstNode
	// contains filtered or unexported methods
}

An item in an dictionary expression.

type DictItem_Keyed

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

A keyed pair: `"spacy key": true`.

Keyed

type DictItem_Named

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

A named pair: `thickness: 3pt`.

Named

type DictItem_Spread

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

A spread expression: `..things`.

Spread

type Emph

type Emph struct {
	*SyntaxNode
}

Emphasized content: `_Emphasized_`.

func Emph_default

func Emph_default() *Emph

type EnumItem

type EnumItem struct {
	*SyntaxNode
}

An item in an enumeration (numbered list): `+ ...` or `1. ...`.

func EnumItem_default

func EnumItem_default() *EnumItem

type Equation

type Equation struct {
	*SyntaxNode
}

A mathematical equation: `$x$`, `$ x^2 $`.

func Equation_default

func Equation_default() *Equation

type ErrorNode

type ErrorNode struct {
	// The source text of the node.
	Text string
	// The syntax error.
	Error *SyntaxError
}

An error node in the untyped syntax tree.

func NewErrorNode

func NewErrorNode(error *SyntaxError, text string) *ErrorNode

Create new error node.

new

func (*ErrorNode) String

func (node *ErrorNode) String() string

func (*ErrorNode) SyntaxKind

func (*ErrorNode) SyntaxKind() SyntaxKind

type Escape

type Escape struct {
	*SyntaxNode
}

An escape sequence: `\#`, `\u{1F5FA}`.

func Escape_default

func Escape_default() *Escape

type Expr

type Expr interface {
	AstNode
	// contains filtered or unexported methods
}

An expression in markup, math or code.

func Expr_default

func Expr_default() Expr

default

type FieldAccess

type FieldAccess struct {
	*SyntaxNode
}

A field access: `properties.age`.

func FieldAccess_default

func FieldAccess_default() *FieldAccess

type FileID

type FileID uint16 // NonZeroU16

FileID identifies a file in a project or package.

This type is globally interned and thus cheap to copy, compare, and hash.

func FileIDFromUint16

func FileIDFromUint16(v uint16) FileID

FileIDFromUint16 constructs a FileID from a raw number.

Should only be used with numbers retrieved via Uint16. Misuse may result in panics.

func NewFakeFileID

func NewFakeFileID(vpath *VirtualPath) FileID

NewFakeFileID creates a new unique ("fake") file specification, which is not accessible by vpath.

Caution: the ID returned is the *only* identifier of the file. Constructing a file ID with a path will not reuse it, even if the path is the same. This method should only be used for generating "virtual" file IDs such as content read from stdin.

func NewFileID

func NewFileID(spec option.Option[*PackageSpec], vpath *VirtualPath) FileID

NewFileID creates a new interned file specification.

The path must start with a `/` or this function will panic. The path is normalized before interning.

func (FileID) Join

func (id FileID) Join(path string) FileID

Join resolves a file location relative to this file.

func (FileID) Spec

func (id FileID) Spec() option.Option[*PackageSpec]

Spec returns the package the file resides in, if any.

func (FileID) String

func (id FileID) String() string

String returns a string representation of the FileID.

func (FileID) Uint16

func (id FileID) Uint16() uint16

Uint16 extracts the raw underlying number.

func (FileID) VPath

func (id FileID) VPath() *VirtualPath

VPath returns the absolute and normalized path to the file within the project or package.

func (FileID) WithExtension

func (id FileID) WithExtension(extension string) FileID

WithExtension returns the same file location but with a different extension.

type Float

type Float struct {
	*SyntaxNode
}

A floating-point number: `1.2`, `10e-4`.

func Float_default

func Float_default() *Float

type ForLoop

type ForLoop struct {
	*SyntaxNode
}

A for loop: `for x in y { z }`.

func ForLoop_default

func ForLoop_default() *ForLoop

type FuncCall

type FuncCall struct {
	*SyntaxNode
}

An invocation of a function or method: `f(x, y)`.

func FuncCall_default

func FuncCall_default() *FuncCall

type FuncReturn

type FuncReturn struct {
	*SyntaxNode
}

A return from a function: `return`, `return x + 1`.

func FuncReturn_default

func FuncReturn_default() *FuncReturn

type GroupState

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

State for array/dictionary parsing.

type Heading

type Heading struct {
	*SyntaxNode
}

A section heading: `= Introduction`.

func Heading_default

func Heading_default() *Heading

type Ident

type Ident struct {
	*SyntaxNode
}

An identifier: `it`.

func Ident_default

func Ident_default() *Ident

type ImportItem

type ImportItem interface {
	// contains filtered or unexported methods
}

An imported item, potentially renamed to another identifier.

type ImportItemPath

type ImportItemPath struct {
	*SyntaxNode
}

A path to a submodule's imported name: `a.b.c`.

func ImportItemPath_default

func ImportItemPath_default() *ImportItemPath

type ImportItem_Renamed

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

A renamed import (the item was bound to a different name in the scope than the one it was defined as).

Renamed

type ImportItem_Simple

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

A non-renamed import (the item's name in the scope is the same as its name).

Simple

type ImportItems

type ImportItems struct {
	*SyntaxNode
}

Items to import from a module: `a, b, c`.

func ImportItems_default

func ImportItems_default() *ImportItems

type Imports

type Imports interface {
	// contains filtered or unexported methods
}

The items that ought to be imported from a file.

type Imports_Items

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

The specified items from the file should be imported.

Items

type Imports_Wildcard

type Imports_Wildcard struct {
}

All items in the scope of the file should be imported.

Wildcard

type InnerNode

type InnerNode struct {
	// What kind of node this is (each kind would have its own struct in a
	// strongly typed AST).
	Kind SyntaxKind
	// The byte length of the node in the source.
	Len uint
	// The node's span.
	Span Span
	// The number of nodes in the whole subtree, including this node.
	Descendants uint
	// Whether this node or any of its children are erroneous.
	Erroneous bool
	// The upper bound of this node's numbering range.
	Upper uint64
	// This node's children, losslessly make up this node.
	Children []*SyntaxNode
}

An inner node in the untyped syntax tree.

func NewInnerNode

func NewInnerNode(kind SyntaxKind, children []*SyntaxNode) *InnerNode

Create a new inner node with the given kind and children.

new

func (*InnerNode) String

func (node *InnerNode) String() string

func (*InnerNode) SyntaxKind

func (node *InnerNode) SyntaxKind() SyntaxKind

type Int

type Int struct {
	*SyntaxNode
}

An integer: `120`.

func Int_default

func Int_default() *Int

type Interner

type Interner struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Interner is a package-path interner.

type Keyed

type Keyed struct {
	*SyntaxNode
}

A keyed pair: `"spacy key": true`.

func Keyed_default

func Keyed_default() *Keyed

type Label

type Label struct {
	*SyntaxNode
}

A label: `<intro>`.

func Label_default

func Label_default() *Label

type LeafNode

type LeafNode struct {
	// What kind of node this is (each kind would have its own struct in a
	// strongly typed AST).
	Kind SyntaxKind
	// The source text of the node.
	Text string
	// The node's span.
	Span Span
}

A leaf node in the untyped syntax tree.

func NewLeafNode

func NewLeafNode(kind SyntaxKind, text string) *LeafNode

Create a new leaf node.

new

func (*LeafNode) String

func (node *LeafNode) String() string

func (*LeafNode) SyntaxKind

func (node *LeafNode) SyntaxKind() SyntaxKind

type LetBinding

type LetBinding struct {
	*SyntaxNode
}

A let binding: `let x = 1`.

func LetBinding_default

func LetBinding_default() *LetBinding

type LetBindingKind

type LetBindingKind interface {
	// contains filtered or unexported methods
}

The kind of a let binding, either a normal one or a closure.

type LetBindingKind_Closure

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

A closure binding: `let f(x) = 1`.

Closure

type LetBindingKind_Normal

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

A normal binding: `let x = 1`.

Normal

type Lexer

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

An iterator over a source code string which returns tokens.

func NewLexer

func NewLexer(text string, mode SyntaxMode) *Lexer

Create a new lexer with the given mode and a prefix to offset column calculations.

new

type Line

type Line struct {
	// The UTF-8 byte offset where the line starts.
	ByteIdx uint
	// The UTF-16 code unit offset where the line starts.
	Utf16Idx uint
}

Line represents metadata about a line.

type Linebreak

type Linebreak struct {
	*SyntaxNode
}

A forced line break: `\`.

func Linebreak_default

func Linebreak_default() *Linebreak

type Lines

type Lines struct {
	// The text as a string slice.
	Text  string
	Lines []Line
}

Lines is a text buffer and metadata about lines.

This is internally reference-counted and thus cheap to clone.

func NewLines

func NewLines(text string) *Lines

NewLines creates a new Lines buffer from the text and computes the line metadata.

func (*Lines) ByteToColumn

func (l *Lines) ByteToColumn(byteIdx uint) (uint, bool)

ByteToColumn returns the index of the column at the byte index.

The column is defined as the number of characters in the line before the byte index. It returns false if the byte index is out of bounds.

func (*Lines) ByteToLine

func (l *Lines) ByteToLine(byteIdx uint) (uint, bool)

ByteToLine returns the index of the line that contains the given byte index.

It returns false if the byte index is out of bounds.

func (*Lines) ByteToLineColumn

func (l *Lines) ByteToLineColumn(byteIdx uint) (Position, bool)

ByteToLineColumn returns the index of the line and column at the byte index.

It returns false if the byte index is out of bounds.

func (*Lines) ByteToUtf16

func (l *Lines) ByteToUtf16(byteIdx uint) (uint, bool)

ByteToUtf16 returns the index of the UTF-16 code unit at the byte index.

It returns false if the byte index is out of bounds.

func (*Lines) Edit

func (l *Lines) Edit(replace ranges.Range, with string)

Edit edits the source file by replacing the given range.

The method panics if the replace range is out of bounds.

func (*Lines) LenBytes

func (l *Lines) LenBytes() uint

LenBytes returns the length of the file in UTF-8 encoded bytes.

func (*Lines) LenLines

func (l *Lines) LenLines() uint

LenLines returns the length of the file in lines.

func (*Lines) LenUtf16

func (l *Lines) LenUtf16() uint

LenUtf16 returns the length of the file in UTF-16 code units.

func (*Lines) LineColumnToByte

func (l *Lines) LineColumnToByte(lineIdx uint, columnIdx uint) (uint, bool)

LineColumnToByte returns the byte index of the given (line, column) pair.

The column defines the number of characters to go beyond the start of the line. It returns false if the line or column is out of bounds.

func (*Lines) LineToByte

func (l *Lines) LineToByte(lineIdx uint) (uint, bool)

LineToByte returns the byte position at which the given line starts.

It returns false if the line index is out of bounds.

func (*Lines) LineToRange

func (l *Lines) LineToRange(lineIdx uint) (ranges.Range, bool)

LineToRange returns the range which encloses the given line.

It returns false if the line index is out of bounds.

func (*Lines) Replace

func (l *Lines) Replace(new string) bool

Replace fully replaces the source text.

It performs a naive (suffix/prefix-based) diff of the old and new text to produce the smallest single edit that transforms the old source into the new one.

It returns whether any changes were made.

func (*Lines) ReplacementRange

func (l *Lines) ReplacementRange(new string) (ReplacementRange, bool)

ReplacementRange returns the common prefix and suffix lengths.

It returns false if the old and new strings are equal.

func (*Lines) Utf16ToByte

func (l *Lines) Utf16ToByte(utf16Idx uint) (uint, bool)

Utf16ToByte returns the byte index at the UTF-16 code unit.

It returns false if the UTF-16 index is out of bounds.

type Link struct {
	*SyntaxNode
}

A hyperlink: `https://typst.org`.

func Link_default() *Link

type LinkedChildren

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

An iterator over the children of a linked node.

func (*LinkedChildren) Children

func (l *LinkedChildren) Children() iter.Seq2[int, *LinkedNode]

func (*LinkedChildren) RevChildren

func (l *LinkedChildren) RevChildren() iter.Seq2[int, *LinkedNode]

type LinkedNode

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

A syntax node in a context.

Knows its exact offset in the file and provides access to its children, parent and siblings.

**Note that all sibling and leaf accessors skip over trivia!**

func NewLinkedNode

func NewLinkedNode(root *SyntaxNode) *LinkedNode

Start a new traversal at a root node.

new

func (*LinkedNode) Range

func (link *LinkedNode) Range() ranges.Range

The byte range of this node in the source file.

range

func (*LinkedNode) String

func (link *LinkedNode) String() string

type ListItem

type ListItem struct {
	*SyntaxNode
}

An item in a bullet list: `- ...`.

func ListItem_default

func ListItem_default() *ListItem

type LoopBreak

type LoopBreak struct {
	*SyntaxNode
}

A break from a loop: `break`.

func LoopBreak_default

func LoopBreak_default() *LoopBreak

type LoopContinue

type LoopContinue struct {
	*SyntaxNode
}

A continue in a loop: `continue`.

func LoopContinue_default

func LoopContinue_default() *LoopContinue

type Marker

type Marker uint

A marker representing a node's position in the parser. Mainly used for wrapping, but can also index into the parser to access the node, like `p.index(m)`.

type Markup

type Markup struct {
	*SyntaxNode
}

The syntactical root capable of representing a full parsed document.

func Markup_default

func Markup_default() *Markup

type Math

type Math struct {
	*SyntaxNode
}

The contents of a mathematical equation: `x^2 + 1`.

func Math_default

func Math_default() *Math

type MathAlignPoint

type MathAlignPoint struct {
	*SyntaxNode
}

An alignment point in math: `&`.

func MathAlignPoint_default

func MathAlignPoint_default() *MathAlignPoint

type MathAttach

type MathAttach struct {
	*SyntaxNode
}

A base with optional attachments in math: `a_1^2`.

func MathAttach_default

func MathAttach_default() *MathAttach

type MathDelimited

type MathDelimited struct {
	*SyntaxNode
}

Matched delimiters in math: `[x + y]`.

func MathDelimited_default

func MathDelimited_default() *MathDelimited

type MathFrac

type MathFrac struct {
	*SyntaxNode
}

A fraction in math: `x/2`

func MathFrac_default

func MathFrac_default() *MathFrac

type MathIdent

type MathIdent struct {
	*SyntaxNode
}

An identifier in math: `pi`.

func MathIdent_default

func MathIdent_default() *MathIdent

type MathOp

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

type MathPrimes

type MathPrimes struct {
	*SyntaxNode
}

Grouped primes in math: `a”'`.

func MathPrimes_default

func MathPrimes_default() *MathPrimes

type MathRoot

type MathRoot struct {
	*SyntaxNode
}

A root in math: `√x`, `∛x` or `∜x`.

func MathRoot_default

func MathRoot_default() *MathRoot

type MathShorthand

type MathShorthand struct {
	*SyntaxNode
}

A shorthand for a unicode codepoint in math: `a <= b`.

func MathShorthand_default

func MathShorthand_default() *MathShorthand

type MathText

type MathText struct {
	*SyntaxNode
}

A lone text fragment in math: `x`, `25`, `3.1415`, `=`, `[`.

func MathText_default

func MathText_default() *MathText

type MathTextKind

type MathTextKind interface {
	// contains filtered or unexported methods
}

The underlying text kind.

type MathTextKind_Character

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

type MathTextKind_Number

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

type MemoArena

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

Extra parser state for efficiently recovering from mispredicted parses.

This is the same idea as packrat parsing, but we use it only in the limited case of parenthesized structures. See [`expr_with_paren`] for more.

func NewMemoArena

func NewMemoArena() *MemoArena

type MemoCheckpoint

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

type MemoKey

type MemoKey uint

A type alias for the memo key so it doesn't get confused with other usizes.

The memo is keyed by the index into `text` of the current token's start.

type MemoPair

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

func (*MemoPair) Clone

func (memo_pair *MemoPair) Clone() *MemoPair

type ModuleImport

type ModuleImport struct {
	*SyntaxNode
}

A module import: `import "utils.typ": a, b, c`.

func ModuleImport_default

func ModuleImport_default() *ModuleImport

type ModuleInclude

type ModuleInclude struct {
	*SyntaxNode
}

A module include: `include "chapter1.typ"`.

func ModuleInclude_default

func ModuleInclude_default() *ModuleInclude

type Named

type Named struct {
	*SyntaxNode
}

A named pair: `thickness: 3pt`.

func Named_default

func Named_default() *Named

type Newline

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

Information about newlines in a group of trivia.

type None

type None struct {
	*SyntaxNode
}

The `none` literal.

func None_default

func None_default() *None

type Numeric

type Numeric struct {
	*SyntaxNode
}

A numeric value with a unit: `12pt`, `3cm`, `2em`, `90deg`, `50%`.

func Numeric_default

func Numeric_default() *Numeric

type PackageInfo

type PackageInfo struct {
	// The name of the package within its namespace.
	Name string `toml:"name"`
	// The package's version.
	Version PackageVersion `toml:"version"`
	// The path of the entrypoint into the package.
	Entrypoint string `toml:"entrypoint"`
	// A list of the package's authors.
	Authors []string `toml:"authors,omitempty"`
	// The package's license.
	License string `toml:"license,omitempty"`
	// A short description of the package.
	Description string `toml:"description,omitempty"`
	// A link to the package's web presence.
	Homepage string `toml:"homepage,omitempty"`
	// A link to the repository where this package is developed.
	Repository string `toml:"repository,omitempty"`
	// An array of search keywords for the package.
	Keywords []string `toml:"keywords,omitempty"`
	// An array with up to three of the predefined categories to help users
	// discover the package.
	Categories []string `toml:"categories,omitempty"`
	// An array of disciplines defining the target audience for which the package
	// is useful.
	Disciplines []string `toml:"disciplines,omitempty"`
	// The minimum required compiler version for the package.
	Compiler *VersionBound `toml:"compiler,omitempty"`
	// An array of globs specifying files that should not be part of the
	// published bundle.
	Exclude []string `toml:"exclude,omitempty"`
	// All parsed but unknown fields, this can be used for validation.
	UnknownFields UnknownFields `toml:"-"`
}

The `[package]` key in the manifest.

The `UnknownFields` contains fields which were found but not expected.

func NewPackageInfo

func NewPackageInfo(name string, version PackageVersion, entrypoint string) *PackageInfo

NewPackageInfo creates a new package info with only required fields.

type PackageManifest

type PackageManifest struct {
	// Details about the package itself.
	Pkg *PackageInfo `toml:"package"`
	// Details about the template, if the package is one.
	Template *TemplateInfo `toml:"template,omitempty"`
	// The tools section for third-party configuration.
	Tool *ToolInfo `toml:"tool,omitempty"`
	// All parsed but unknown fields, this can be used for validation.
	UnknownFields UnknownFields `toml:"-"`
}

A parsed package manifest.

The `UnknownFields` contains fields which were found but not expected.

func NewPackageManifest

func NewPackageManifest(pkg *PackageInfo) *PackageManifest

NewPackageManifest creates a new package manifest with the given package info.

func ParsePackageManifestFromToml

func ParsePackageManifestFromToml(content string) (*PackageManifest, error)

func (*PackageManifest) Validate

func (manifest *PackageManifest) Validate(spec *PackageSpec) error

Validate ensures that this manifest is indeed for the specified package.

type PackageSpec

type PackageSpec struct {
	// The namespace the package lives in.
	Namespace string
	// The name of the package within its namespace.
	Name string
	// The package's version.
	Version PackageVersion
}

PackageSpec identifies a package.

func ParsePackageSpec

func ParsePackageSpec(str string) (*PackageSpec, error)

ParsePackageSpec parses a package spec from string.

func (*PackageSpec) Clone

func (spec *PackageSpec) Clone() *PackageSpec

func (*PackageSpec) String

func (spec *PackageSpec) String() string

func (*PackageSpec) Versionless

func (spec *PackageSpec) Versionless() *VersionlessPackageSpec

type PackageVersion

type PackageVersion struct {
	Major uint32
	Minor uint32
	Patch uint32
}

PackageVersion represents a package's version.

func CurrentCompilerVersion

func CurrentCompilerVersion() PackageVersion

func ParsePackageVersion

func ParsePackageVersion(str string) (PackageVersion, error)

ParsePackageVersion parses a version string.

func (PackageVersion) MatchesEQ

func (v PackageVersion) MatchesEQ(bound *VersionBound) bool

MatchesEQ performs an `==` match with the given version bound. Version elements missing in the bound are ignored.

func (PackageVersion) MatchesGE

func (v PackageVersion) MatchesGE(bound *VersionBound) bool

MatchesGE performs a `>=` match with the given versions. The match succeeds when either a `==` or `>` match does.

func (PackageVersion) MatchesGT

func (v PackageVersion) MatchesGT(bound *VersionBound) bool

MatchesGT performs a `>` match with the given version bound. The match only succeeds if some version element in the bound is actually greater than that of the version.

func (PackageVersion) MatchesLE

func (v PackageVersion) MatchesLE(bound *VersionBound) bool

MatchesLE performs a `<=` match with the given versions. The match succeeds when either a `==` or `<` match does.

func (PackageVersion) MatchesLT

func (v PackageVersion) MatchesLT(bound *VersionBound) bool

MatchesLT performs a `<` match with the given version bound. The match only succeeds if some version element in the bound is actually less than that of the version.

func (PackageVersion) String

func (v PackageVersion) String() string

func (*PackageVersion) UnmarshalText

func (v *PackageVersion) UnmarshalText(text []byte) error

type Pair

type Pair struct {
	Spec  option.Option[*PackageSpec]
	VPath *VirtualPath
}

Pair is an interned pair of a package specification and a path.

type Param

type Param interface {
	AstNode
	// contains filtered or unexported methods
}

A parameter to a closure.

type Param_Named

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

A named parameter with a default value: `draw: false`.

Named

type Param_Pos

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

A positional parameter: `x`.

Pos

type Param_Spread

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

An argument sink: `..args` or `..`.

Spread

type Params

type Params struct {
	*SyntaxNode
}

A closure's parameters: `(x, y)`.

func Params_default

func Params_default() *Params

type Parbreak

type Parbreak struct {
	*SyntaxNode
}

A paragraph break, indicated by one or multiple blank lines.

func Parbreak_default

func Parbreak_default() *Parbreak

type Parenthesized

type Parenthesized struct {
	*SyntaxNode
}

A grouped expression: `(1 + 2)`.

func Parenthesized_default

func Parenthesized_default() *Parenthesized

type Parser

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

Manages parsing a stream of tokens into a tree of [`SyntaxNode`]s.

The implementation presents an interface that investigates a current `token` with a [`SyntaxKind`] and can take one of the following actions:

  1. Eat a token: push `token` onto the `nodes` vector as a [leaf node](`SyntaxNode::leaf`) and prepare a new `token` by calling into the lexer.
  2. Wrap nodes from a marker to the end of `nodes` (excluding `token` and any attached trivia) into an [inner node](`SyntaxNode::inner`) of a specific `SyntaxKind`.
  3. Produce or convert nodes into an [error node](`SyntaxNode::error`) when something expected is missing or something unexpected is found.

Overall the parser produces a nested tree of SyntaxNodes as a "_Concrete_ Syntax Tree." The raw Concrete Syntax Tree should contain the entire source text, and is used as-is for e.g. syntax highlighting and IDE features. In `ast.rs` the CST is interpreted as a lazy view over an "_Abstract_ Syntax Tree." The AST module skips over irrelevant tokens -- whitespace, comments, code parens, commas in function args, etc. -- as it iterates through the tree.

### Modes

The parser manages the transitions between the three modes of Typst through [syntax modes](`SyntaxMode`) and [newline modes](`AtNewline`).

The syntax modes map to the three Typst modes and are stored in the lexer, changing which `SyntaxKind`s it will generate.

The newline mode is used to determine whether a newline should end the current expression. If so, the parser temporarily changes `token`'s kind to a fake [`SyntaxKindEnd`]. When the parser exits the mode the original `SyntaxKind` is restored.

func NewParser

func NewParser(text string, offset uint, mode SyntaxMode) *Parser

Create a new parser starting from the given text offset and syntax mode.

new

type PartialState

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

State needed to restore the parser's current token and the lexer (but not the nodes vector).

func (*PartialState) Clone

func (state *PartialState) Clone() *PartialState

type Pattern

type Pattern interface {
	AstNode
	// contains filtered or unexported methods
}

The kind of a pattern.

type Pattern_Destructuring

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

A destructuring pattern: `(x, _, ..y)`.

Destructuring

type Pattern_Normal

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

A single expression: `x`.

Normal

func Pattern_Normal_default

func Pattern_Normal_default() *Pattern_Normal

type Pattern_Parenthesized

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

A parenthesized pattern.

Parenthesized

type Pattern_Placeholder

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

A placeholder: `_`.

Placeholder

type Position

type Position struct {
	Line uint
	Col  uint
}

Position represents a line and column pair.

type Raw

type Raw struct {
	*SyntaxNode
}

Raw text with optional syntax highlighting: “ `...` “.

func Raw_default

func Raw_default() *Raw

type RawDelim

type RawDelim struct {
	*SyntaxNode
}

A raw delimiter in single or 3+ backticks: “ ` “.

func RawDelim_default

func RawDelim_default() *RawDelim

type RawLang

type RawLang struct {
	*SyntaxNode
}

A language tag at the start of raw element: “typ “.

func RawLang_default

func RawLang_default() *RawLang

type Ref

type Ref struct {
	*SyntaxNode
}

A reference: `@target`, `@target[..]`.

func Ref_default

func Ref_default() *Ref

type RenamedImportItem

type RenamedImportItem struct {
	*SyntaxNode
}

A renamed import item: `a as d`

func RenamedImportItem_default

func RenamedImportItem_default() *RenamedImportItem

type ReplacementRange

type ReplacementRange struct {
	Prefix uint
	Suffix uint
}

ReplacementRange holds the common prefix and suffix lengths.

type Repr

type Repr interface {
	SyntaxKind() SyntaxKind
	// contains filtered or unexported methods
}

The three internal representations.

type SetRule

type SetRule struct {
	*SyntaxNode
}

A set rule: `set text(...)`.

func SetRule_default

func SetRule_default() *SetRule

type Shorthand

type Shorthand struct {
	*SyntaxNode
}

A shorthand for a unicode codepoint. For example, `~` for a non-breaking space or `-?` for a soft hyphen.

func Shorthand_default

func Shorthand_default() *Shorthand

type ShowRule

type ShowRule struct {
	*SyntaxNode
}

A show rule: `show heading: it => emph(it.body)`.

func ShowRule_default

func ShowRule_default() *ShowRule

type Side

type Side uint8

Indicates whether the cursor is before the related byte index, or after.

const (
	SideBefore Side = iota + 1
	SideAfter
)

type SmartQuote

type SmartQuote struct {
	*SyntaxNode
}

A smart quote: `'` or `"`.

func SmartQuote_default

func SmartQuote_default() *SmartQuote

type Source

type Source struct {
	// The ID of the source file.
	ID FileID
	// The root node of the file's untyped syntax tree.
	Root *SyntaxNode
	// An acceleration structure for conversion of UTF-8, UTF-16 and
	// line/column indices.
	Lines *Lines
}

Source represents a source file.

All line and column indices start at zero, just like byte indices. For user-facing display, add 1 to them.

Values of this type are cheap to clone and hash.

func NewDetachedSource

func NewDetachedSource(text string) *Source

NewDetachedSource creates a source file without a real ID and path, usually for testing.

It panics if the text cannot be properly parsed.

func NewSource

func NewSource(id FileID, text string) *Source

NewSource creates a new source file from an ID and text.

It panics if the text cannot be properly parsed.

func (*Source) Edit

func (s *Source) Edit(replace ranges.Range, with string) ranges.Range

Edit edits the source file by replacing the given range.

It returns the range in the new source that was ultimately reparsed.

This method panics if the replace range is out of bounds.

func (*Source) Find

func (s *Source) Find(span Span) (*LinkedNode, bool)

Find finds the node with the given span.

It returns None if the span does not point into this source file.

func (*Source) Range

func (s *Source) Range(span Span) (ranges.Range, bool)

Range returns the byte range for the given span in this file.

It returns false if the span does not point into this source file.

Typically, it's easier to use `WorldExt::Range` instead.

func (*Source) Replace

func (s *Source) Replace(new string) ranges.Range

Replace fully replaces the source text.

It performs a naive (suffix/prefix-based) diff of the old and new text to produce the smallest single edit that transforms the old source into the new one. The method then calls Edit with this change.

Replace returns the range in the new source that was ultimately reparsed.

func (*Source) String

func (s *Source) String() string

String returns a string representation of the Source.

func (*Source) Text

func (s *Source) Text() string

Text returns the whole source as a string.

type Space

type Space struct {
	*SyntaxNode
}

Whitespace in markup or math. Has at most one newline in markup, as more indicate a paragraph break.

func Space_default

func Space_default() *Space

type Span

type Span uint64

A Span defines a range in a file.

It's used to track which source section an element stems from or an error applies to.

This type takes up 8 bytes and is copyable.

Data layout: | 16 bits file id | 48 bits number |

Number =

  • 1 means detached
  • 2..2^47-1 is a numbered span
  • 2^47..2^48-1 is a raw range span. To retrieve it, you must subtract `rangeBase` and then use shifting/bitmasking to extract the components.

func AstNode_span

func AstNode_span(node AstNode) Span

The source code location.

span

func FindFirstNonDetached

func FindFirstNonDetached(spans iter.Seq[Span]) Span

FindFirstNonDetached finds the first non-detached span in the iterator.

func NewDetachedSpan

func NewDetachedSpan() Span

NewDetachedSpan creates a span that does not point into any file.

func NewSpanFromNumber

func NewSpanFromNumber(id FileID, number uint64) (Span, bool)

NewSpanFromNumber creates a new span from a file ID and a number.

It returns true if the number is within the valid range.

func NewSpanFromRange

func NewSpanFromRange(id FileID, byteRange ranges.Range) Span

NewSpanFromRange creates a new span from a raw byte range. If a range part exceeds the maximum value (2^23), it is saturated.

func NewSpanFromUint64

func NewSpanFromUint64(v uint64) Span

NewSpanFromUint64 constructs a span from a raw number.

func NewSpanPacked

func NewSpanPacked(id FileID, low uint64) Span

NewSpanPacked packs a file ID and the low bits into a span.

func (Span) ID

func (s Span) ID() (FileID, bool)

ID returns the ID of the file the span points into. It returns true if the span is not detached.

func (Span) IsDetached

func (s Span) IsDetached() bool

IsDetached returns whether the span is detached.

func (Span) Number

func (s Span) Number() uint64

Number returns the unique number of the span within its source.

func (Span) Or

func (s Span) Or(other Span) Span

Or returns other if s is detached and s otherwise.

func (Span) RawRange

func (s Span) RawRange() (ranges.Range, bool)

RawRange extracts a raw byte range from the span. It returns true if the span is a raw range span.

func (Span) ResolvePath

func (s Span) ResolvePath(path string) (FileID, error)

ResolvePath resolves a file location relative to this span's source.

func (Span) Uint64

func (s Span) Uint64() uint64

Uint64 extracts the raw underlying number.

type Spanned

type Spanned[T any] struct {
	// The spanned value.
	Value T
	// The value's location in source code.
	Span Span
}

A Spanned value with a span locating it in the source code.

func NewSpanned

func NewSpanned[T any](v T, span Span) Spanned[T]

NewSpanned creates a new instance from a value and its span.

func (Spanned[T]) Map

func (s Spanned[T]) Map(f func(T) T) Spanned[T]

Map maps the value using a function.

type Spread

type Spread struct {
	*SyntaxNode
}

A spread: `..x` or `..x.at(0)`.

func Spread_default

func Spread_default() *Spread

type Str

type Str struct {
	*SyntaxNode
}

A quoted string: `"..."`.

func Str_default

func Str_default() *Str

type Strong

type Strong struct {
	*SyntaxNode
}

Strong content: `*Strong*`.

func Strong_default

func Strong_default() *Strong

type SyntaxError

type SyntaxError struct {
	// The node's span.
	Span Span
	// The error message.
	Message string
	// Additional hints to the user, indicating how this error could be avoided
	// or worked around.
	Hints []string
}

A syntactical error.

func NewSyntaxError

func NewSyntaxError(message string) *SyntaxError

Create a new detached syntax error.

new

type SyntaxKind

type SyntaxKind uint8

SyntaxKind is a syntactical building block of a Typst file.

It can be created by the lexer or by the parser.

const (
	// The end of token stream.
	SyntaxKindEnd SyntaxKind = iota + 1
	// An invalid sequence of characters.
	SyntaxKindError

	// A shebang: `#! ...`
	SyntaxKindShebang
	// A line comment: `// ...`.
	SyntaxKindLineComment
	// A block comment: `/* ... */`.
	SyntaxKindBlockComment

	// The contents of a file or content block.
	SyntaxKindMarkup
	// Plain text without markup.
	SyntaxKindText
	// Whitespace. Contains at most one newline in markup, as more indicate a
	// paragraph break.
	SyntaxKindSpace
	// A forced line break: `\`.
	SyntaxKindLinebreak
	// A paragraph break, indicated by one or multiple blank lines.
	SyntaxKindParbreak
	// An escape sequence: `\#`, `\u{1F5FA}`.
	SyntaxKindEscape
	// A shorthand for a unicode codepoint. For example, `~` for non-breaking
	// space or `-?` for a soft hyphen.
	SyntaxKindShorthand
	// A smart quote: `'` or `"`.
	SyntaxKindSmartQuote
	// Strong content: `*Strong*`.
	SyntaxKindStrong
	// Emphasized content: `_Emphasized_`.
	SyntaxKindEmph
	// Raw text with optional syntax highlighting: “ `...` “.
	SyntaxKindRaw
	// A language tag at the start of raw text: “typ “.
	SyntaxKindRawLang
	// A raw delimiter consisting of 1 or 3+ backticks: “ ` “.
	SyntaxKindRawDelim
	// A sequence of whitespace to ignore in a raw text: `    `.
	SyntaxKindRawTrimmed
	// A hyperlink: `https://typst.org`.
	SyntaxKindLink
	// A label: `<intro>`.
	SyntaxKindLabel
	// A reference: `@target`, `@target[..]`.
	SyntaxKindRef
	// Introduces a reference: `@target`.
	SyntaxKindRefMarker
	// A section heading: `= Introduction`.
	SyntaxKindHeading
	// Introduces a section heading: `=`, `==`, ...
	SyntaxKindHeadingMarker
	// An item in a bullet list: `- ...`.
	SyntaxKindListItem
	// Introduces a list item: `-`.
	SyntaxKindListMarker
	// An item in an enumeration (numbered list): `+ ...` or `1. ...`.
	SyntaxKindEnumItem
	// Introduces an enumeration item: `+`, `1.`.
	SyntaxKindEnumMarker
	// An item in a term list: `/ Term: Details`.
	SyntaxKindTermItem
	// Introduces a term item: `/`.
	SyntaxKindTermMarker
	// A mathematical equation: `$x$`, `$ x^2 $`.
	SyntaxKindEquation

	// The contents of a mathematical equation: `x^2 + 1`.
	SyntaxKindMath
	// A lone text fragment in math: `x`, `25`, `3.1415`, `=`, `|`, `[`.
	SyntaxKindMathText
	// An identifier in math: `pi`.
	SyntaxKindMathIdent
	// A shorthand for a unicode codepoint in math: `a <= b`.
	SyntaxKindMathShorthand
	// An alignment point in math: `&`.
	SyntaxKindMathAlignPoint
	// Matched delimiters in math: `[x + y]`.
	SyntaxKindMathDelimited
	// A base with optional attachments in math: `a_1^2`.
	SyntaxKindMathAttach
	// Grouped primes in math: `a”'`.
	SyntaxKindMathPrimes
	// A fraction in math: `x/2`.
	SyntaxKindMathFrac
	// A root in math: `√x`, `∛x` or `∜x`.
	SyntaxKindMathRoot

	// A hash that switches into code mode: `#`.
	SyntaxKindHash
	// A left curly brace, starting a code block: `{`.
	SyntaxKindLeftBrace
	// A right curly brace, terminating a code block: `}`.
	SyntaxKindRightBrace
	// A left square bracket, starting a content block: `[`.
	SyntaxKindLeftBracket
	// A right square bracket, terminating a content block: `]`.
	SyntaxKindRightBracket
	// A left round parenthesis, starting a grouped expression, collection
	// argument or parameter list: `(`.
	SyntaxKindLeftParen
	// A right round parenthesis, terminating a grouped expression, collection
	// argument or parameter list: `)`.
	SyntaxKindRightParen
	// A comma separator in a sequence: `,`.
	SyntaxKindComma
	// A semicolon terminating an expression: `;`.
	SyntaxKindSemicolon
	// A colon between name/key and value in a dictionary, argument or
	// parameter list, or between the term and body of a term list term: `:`.
	SyntaxKindColon
	// The strong text toggle, multiplication operator, and wildcard import
	// symbol: `*`.
	SyntaxKindStar
	// Toggles emphasized text and indicates a subscript in math: `_`.
	SyntaxKindUnderscore
	// Starts and ends a mathematical equation: `$`.
	SyntaxKindDollar
	// The unary plus and binary addition operator: `+`.
	SyntaxKindPlus
	// The unary negation and binary subtraction operator: `-`.
	SyntaxKindMinus
	// The division operator and fraction operator in math: `/`.
	SyntaxKindSlash
	// The superscript operator in math: `^`.
	SyntaxKindHat
	// The prime in math: `'`.
	SyntaxKindPrime
	// The field access and method call operator: `.`.
	SyntaxKindDot
	// The assignment operator: `=`.
	SyntaxKindEq
	// The equality operator: `==`.
	SyntaxKindEqEq
	// The inequality operator: `!=`.
	SyntaxKindExclEq
	// The less-than operator: `<`.
	SyntaxKindLt
	// The less-than or equal operator: `<=`.
	SyntaxKindLtEq
	// The greater-than operator: `>`.
	SyntaxKindGt
	// The greater-than or equal operator: `>=`.
	SyntaxKindGtEq
	// The add-assign operator: `+=`.
	SyntaxKindPlusEq
	// The subtract-assign operator: `-=`.
	SyntaxKindHyphEq
	// The multiply-assign operator: `*=`.
	SyntaxKindStarEq
	// The divide-assign operator: `/=`.
	SyntaxKindSlashEq
	// Indicates a spread or sink: `..`.
	SyntaxKindDots
	// An arrow between a closure's parameters and body: `=>`.
	SyntaxKindArrow
	// A root: `√`, `∛` or `∜`.
	SyntaxKindRoot

	// The `not` operator.
	SyntaxKindNot
	// The `and` operator.
	SyntaxKindAnd
	// The `or` operator.
	SyntaxKindOr
	// The `none` literal.
	SyntaxKindNone
	// The `auto` literal.
	SyntaxKindAuto
	// The `let` keyword.
	SyntaxKindLet
	// The `set` keyword.
	SyntaxKindSet
	// The `show` keyword.
	SyntaxKindShow
	// The `context` keyword.
	SyntaxKindContext
	// The `if` keyword.
	SyntaxKindIf
	// The `else` keyword.
	SyntaxKindElse
	// The `for` keyword.
	SyntaxKindFor
	// The `in` keyword.
	SyntaxKindIn
	// The `while` keyword.
	SyntaxKindWhile
	// The `break` keyword.
	SyntaxKindBreak
	// The `continue` keyword.
	SyntaxKindContinue
	// The `return` keyword.
	SyntaxKindReturn
	// The `import` keyword.
	SyntaxKindImport
	// The `include` keyword.
	SyntaxKindInclude
	// The `as` keyword.
	SyntaxKindAs

	// The contents of a code block.
	SyntaxKindCode
	// An identifier: `it`.
	SyntaxKindIdent
	// A boolean: `true`, `false`.
	SyntaxKindBool
	// An integer: `120`.
	SyntaxKindInt
	// A floating-point number: `1.2`, `10e-4`.
	SyntaxKindFloat
	// A numeric value with a unit: `12pt`, `3cm`, `2em`, `90deg`, `50%`.
	SyntaxKindNumeric
	// A quoted string: `"..."`.
	SyntaxKindStr
	// A code block: `{ let x = 1; x + 2 }`.
	SyntaxKindCodeBlock
	// A content block: `[*Hi* there!]`.
	SyntaxKindContentBlock
	// A grouped expression: `(1 + 2)`.
	SyntaxKindParenthesized
	// An array: `(1, "hi", 12cm)`.
	SyntaxKindArray
	// A dictionary: `(thickness: 3pt, dash: "solid")`.
	SyntaxKindDict
	// A named pair: `thickness: 3pt`.
	SyntaxKindNamed
	// A keyed pair: `"spacy key": true`.
	SyntaxKindKeyed
	// A unary operation: `-x`.
	SyntaxKindUnary
	// A binary operation: `a + b`.
	SyntaxKindBinary
	// A field access: `properties.age`.
	SyntaxKindFieldAccess
	// An invocation of a function or method: `f(x, y)`.
	SyntaxKindFuncCall
	// A function call's argument list: `(12pt, y)`.
	SyntaxKindArgs
	// Spread arguments or an argument sink: `..x`.
	SyntaxKindSpread
	// A closure: `(x, y) => z`.
	SyntaxKindClosure
	// A closure's parameters: `(x, y)`.
	SyntaxKindParams
	// A let binding: `let x = 1`.
	SyntaxKindLetBinding
	// A set rule: `set text(...)`.
	SyntaxKindSetRule
	// A show rule: `show heading: it => emph(it.body)`.
	SyntaxKindShowRule
	// A contextual expression: `context text.lang`.
	SyntaxKindContextual
	// An if-else conditional: `if x { y } else { z }`.
	SyntaxKindConditional
	// A while loop: `while x { y }`.
	SyntaxKindWhileLoop
	// A for loop: `for x in y { z }`.
	SyntaxKindForLoop
	// A module import: `import "utils.typ": a, b, c`.
	SyntaxKindModuleImport
	// Items to import from a module: `a, b, c`.
	SyntaxKindImportItems
	// A path to an imported name from a submodule: `a.b.c`.
	SyntaxKindImportItemPath
	// A renamed import item: `a as d`.
	SyntaxKindRenamedImportItem
	// A module include: `include "chapter1.typ"`.
	SyntaxKindModuleInclude
	// A break from a loop: `break`.
	SyntaxKindLoopBreak
	// A continue in a loop: `continue`.
	SyntaxKindLoopContinue
	// A return from a function: `return`, `return x + 1`.
	SyntaxKindFuncReturn
	// A destructuring pattern: `(x, _, ..y)`.
	SyntaxKindDestructuring
	// A destructuring assignment expression: `(x, y) = (1, 2)`.
	SyntaxKindDestructAssignment

	SyntaxKindFirst = SyntaxKindEnd
	SyntaxKindLast  = SyntaxKindDestructAssignment
	NSyntaxKinds    = uint(SyntaxKindLast - SyntaxKindFirst + 1)
)

Syntax kinds.

func (SyntaxKind) IsBlock

func (kind SyntaxKind) IsBlock() bool

IsBlock reports whether the node is a code or content block.

func (SyntaxKind) IsError

func (kind SyntaxKind) IsError() bool

IsError reports Whether the node is an error.

func (SyntaxKind) IsGrouping

func (kind SyntaxKind) IsGrouping() bool

IsGrouping reports whether the node is a bracket, brace, or parenthesis.

func (SyntaxKind) IsKeyword

func (kind SyntaxKind) IsKeyword() bool

IsKeyword reports whether the node is a keyword.

func (SyntaxKind) IsStmt

func (kind SyntaxKind) IsStmt() bool

IsStmt reports whether the node needs termination through a semicolon or linebreak.

func (SyntaxKind) IsTerminator

func (kind SyntaxKind) IsTerminator() bool

IsTerminator reports whether the node terminates a preceding expression.

func (SyntaxKind) IsTrivia

func (kind SyntaxKind) IsTrivia() bool

IsTrivia reports whether the node is automatically skipped by the parser in code and math mode.

func (SyntaxKind) Name

func (kind SyntaxKind) Name() string

Name returns a human-readable name for the kind.

func (SyntaxKind) String

func (kind SyntaxKind) String() string

type SyntaxMode

type SyntaxMode uint8

SyntaxMode is a syntax mode of a portion of Typst code.

const (
	// Text and markup, as in the top level.
	SyntaxModeMarkup SyntaxMode = iota + 1
	// Math atoms, operators, etc., as in equations.
	SyntaxModeMath
	// Keywords, literals and operators, as after hashes.
	SyntaxModeCode
)

Syntax modes.

type SyntaxNode

type SyntaxNode struct {
	Repr Repr
}

A node in the untyped syntax tree.

func NewError

func NewError(error *SyntaxError, text string) *SyntaxNode

Create a new error node.

error

func NewInner

func NewInner(kind SyntaxKind, children []*SyntaxNode) *SyntaxNode

Create a new inner node with children.

inner

func NewLeaf

func NewLeaf(kind SyntaxKind, text string) *SyntaxNode

Create a new leaf node.

leaf

func Parse

func Parse(text string) *SyntaxNode

Parses a source file as top-level markup.

func SyntaxNode_Default

func SyntaxNode_Default() *SyntaxNode

func SyntaxNode_placeholder

func SyntaxNode_placeholder(kind SyntaxKind) *SyntaxNode

Create a dummy node of the given kind.

Panics if `kind` is `SyntaxKind::Error`.

placeholder

func (*SyntaxNode) String

func (node *SyntaxNode) String() string

func (*SyntaxNode) SyntaxKind

func (node *SyntaxNode) SyntaxKind() SyntaxKind

type SyntaxSet

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

SyntaxSet represents a set of syntax kinds.

func NewSyntaxSet

func NewSyntaxSet(kinds ...SyntaxKind) *SyntaxSet

NewSyntaxSet creates a new set from a slice of kinds.

func (*SyntaxSet) Add

func (set *SyntaxSet) Add(kind SyntaxKind) *SyntaxSet

Add returns a new set with the given kind inserted. The original set is not modified.

func (*SyntaxSet) Clone

func (set *SyntaxSet) Clone() *SyntaxSet

Clone returns a copy of the set.

func (*SyntaxSet) Contains

func (set *SyntaxSet) Contains(kind SyntaxKind) bool

Contains reports whether the set contains the given syntax kind.

func (*SyntaxSet) Union

func (set *SyntaxSet) Union(other *SyntaxSet) *SyntaxSet

Union returns a new set combining this set with another. The original set is not modified.

type Tag

type Tag uint8

A syntax highlighting tag.

const (
	// A line or block comment.
	//
	// Comment
	Tag_Comment Tag = iota + 1
	// Punctuation in code.
	//
	// Punctuation
	Tag_Punctuation
	// An escape sequence or shorthand.
	//
	// Escape
	Tag_Escape
	// Strong markup.
	//
	// Strong
	Tag_Strong
	// Emphasized markup.
	//
	// Emph
	Tag_Emph
	// A hyperlink.
	//
	// Link
	Tag_Link
	// Raw text.
	//
	// Raw
	Tag_Raw
	// A label.
	//
	// Label
	Tag_Label
	// A reference to a label.
	//
	// Ref
	Tag_Ref
	// A section heading.
	//
	// Heading
	Tag_Heading
	// A marker of a list, enumeration, or term list.
	//
	// ListMarker
	Tag_ListMarker
	// A term in a term list.
	//
	// ListTerm
	Tag_ListTerm
	// The delimiters of an equation.
	//
	// MathDelimiter
	Tag_MathDelimiter
	// An operator with special meaning in an equation.
	//
	// MathOperator
	Tag_MathOperator
	// A keyword.
	//
	// Keyword
	Tag_Keyword
	// An operator in code.
	//
	// Operator
	Tag_Operator
	// A numeric literal.
	//
	// Number
	Tag_Number
	// A string literal.
	//
	// String
	Tag_String
	// A function or method name.
	//
	// Function
	Tag_Function
	// An interpolated variable in markup or math.
	//
	// Interpolated
	Tag_Interpolated
	// A syntax error.
	//
	// Error
	Tag_Error
)

type TemplateInfo

type TemplateInfo struct {
	// The directory within the package that contains the files that should be
	// copied into the user's new project directory.
	Path string `toml:"path"`
	// A path relative to the template's path that points to the file serving as
	// the compilation target.
	Entrypoint string `toml:"entrypoint"`
	// A path relative to the package's root that points to a PNG or lossless
	// WebP thumbnail for the template.
	Thumbnail string `toml:"thumbnail,omitempty"`
	// All parsed but unknown fields, this can be used for validation.
	UnknownFields UnknownFields `toml:"-"`
}

The `[template]` key in the manifest.

The `UnknownFields` contains fields which were found but not expected.

func NewTemplateInfo

func NewTemplateInfo(path, entrypoint string) *TemplateInfo

NewTemplateInfo creates a new template info with only required fields.

type TermItem

type TermItem struct {
	*SyntaxNode
}

An item in a term list: `/ Term: Details`.

func TermItem_default

func TermItem_default() *TermItem

type Text

type Text struct {
	*SyntaxNode
}

Plain text without markup.

func Text_default

func Text_default() *Text

type Token

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

A single token returned from the lexer with a cached [`SyntaxKind`] and a record of preceding trivia.

func Parser_lex

func Parser_lex(nodes *vector.Vector[*SyntaxNode], lexer *Lexer, nl_mode AtNewline) *Token

Move the lexer forward and prepare the current token. In Code, this might insert a temporary [`SyntaxKindEnd`] based on our newline mode.

This is not a method on `self` because we need a valid token before we can initialize the parser.

lex

func (*Token) Clone

func (token *Token) Clone() *Token

type ToolInfo

type ToolInfo struct {
	// Any fields parsed in the tool section.
	Sections map[string]internaltoml.Table
}

The `[tool]` key in the manifest. This field can be used to retrieve 3rd-party tool configuration.

type UnOp

type UnOp uint8

A unary operator.

const (
	// The plus operator: `+`.
	//
	// Pos
	UnOp_Pos UnOp = iota + 1
	// The negation operator: `-`.
	//
	// Neg
	UnOp_Neg
	// The boolean `not`.
	//
	// Not
	UnOp_Not
)

func (UnOp) String

func (op UnOp) String() string

The string representation of this operation.

as_str

type Unary

type Unary struct {
	*SyntaxNode
}

A unary operation: `-x`.

func Unary_default

func Unary_default() *Unary

type Underscore

type Underscore struct {
	*SyntaxNode
}

An underscore: `_`

func Underscore_default

func Underscore_default() *Underscore

type Unit

type Unit uint8

Unit of a numeric value.

const (
	// Points.
	//
	// Pt
	Unit_Pt Unit = iota + 1
	// Millimeters.
	//
	// Mm
	Unit_Mm
	// Centimeters.
	//
	// Cm
	Unit_Cm
	// Inches.
	//
	// In
	Unit_In
	// Radians.
	//
	// Rad
	Unit_Rad
	// Degrees.
	//
	// Deg
	Unit_Deg
	// Font-relative: `1em` is the same as the font size.
	//
	// Em
	Unit_Em
	// Fractions: `fr`.
	//
	// Fr
	Unit_Fr
	// Percentage: `%`.
	//
	// Percent
	Unit_Percent
)

type UnknownFields

type UnknownFields map[string]any

A type alias for a map of key-value pairs used to collect unknown fields.

type VersionBound

type VersionBound struct {
	// The bounds's major version.
	Major uint32
	// The bounds's minor version.
	Minor *uint32
	// The bounds's patch version. Can only be present if minor is too.
	Patch *uint32
}

VersionBound represents a version bound for compatibility specification.

func ParseVersionBound

func ParseVersionBound(str string) (*VersionBound, error)

ParseVersionBound parses a version bound from string.

func (*VersionBound) String

func (v *VersionBound) String() string

func (*VersionBound) UnmarshalText

func (v *VersionBound) UnmarshalText(buf []byte) error

UnmarshalText implements encoding.TextUnmarshaler (for toml).

type VersionlessPackageSpec

type VersionlessPackageSpec struct {
	// The namespace the package lives in.
	Namespace string
	// The name of the package within its namespace.
	Name string
}

VersionlessPackageSpec identifies a package, but not a specific version.

func ParseVersionlessPackageSpec

func ParseVersionlessPackageSpec(str string) (*VersionlessPackageSpec, error)

ParseVersionlessPackageSpec parses a versionless package spec from string.

func (*VersionlessPackageSpec) At

At fills in the version to get a complete PackageSpec.

func (*VersionlessPackageSpec) String

func (spec *VersionlessPackageSpec) String() string

type VirtualPath

type VirtualPath struct {
	Path string
}

VirtualPath is an absolute path in the virtual file system of a project or package.

func NewVirtualPath

func NewVirtualPath(path string) *VirtualPath

NewVirtualPath creates a new virtual path.

Even if it doesn't start with `/` or `\`, it is still interpreted as starting from the root.

func (*VirtualPath) Join

func (vpath *VirtualPath) Join(path string) *VirtualPath

Join resolves a path relative to this virtual path.

func (*VirtualPath) WithExtension

func (vpath *VirtualPath) WithExtension(extension string) *VirtualPath

WithExtension returns the path, but with a different extension.

type WhileLoop

type WhileLoop struct {
	*SyntaxNode
}

A while loop: `while x { y }`.

func WhileLoop_default

func WhileLoop_default() *WhileLoop

Jump to

Keyboard shortcuts

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