ast

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package ast defines the TDL parse tree, which mirrors source text 1:1 with names left unresolved. See docs/design/ir.md for the resolved model.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(file *File) string

Dump renders file as an indented tree, one node per line, with the source position of each. It is the output of `tdl ast`.

func Fprint

func Fprint(file *File) string

Fprint renders file as canonical TDL source, the output of `tdl fmt`. Layout depends only on the tree and its comments, never on the input's whitespace, so formatting is idempotent.

func PrintDecl added in v0.2.8

func PrintDecl(decl Decl) string

PrintDecl renders one declaration in canonical form, without its doc comment, deprecation, or surrounding comments, as a hover shows it.

func PrintTypeRef added in v0.2.8

func PrintTypeRef(t *TypeRef) string

PrintTypeRef renders a type reference as the formatter writes it.

func PrintUnitExpr added in v0.1.4

func PrintUnitExpr(e *UnitExpr) string

PrintUnitExpr renders a unit expression without spaces around its operators: `kg*m/s^2`. Lowering uses it to record a unit's spelling.

Types

type AliasDecl

type AliasDecl struct {
	DeclHead
	Params []*TypeParam
	Target *TypeRef
}

AliasDecl is an `alias Name = TypeRef` declaration, optionally parameterized.

type AssocTypeBind

type AssocTypeBind struct {
	P      Position
	N      string
	Target *TypeRef
}

AssocTypeBind binds an associated type in an instance.

type AssocTypeReq

type AssocTypeReq struct {
	DeclHead
	Kind *Kind
}

AssocTypeReq is a `type Cursor` requirement in a class. Dep is always nil.

type ClassDecl

type ClassDecl struct {
	DeclHead
	Params   []*TypeParam
	FunDeps  []*FunDep
	Conforms []*ClassRef // superclasses
	Requires []*ClassRef
	Members  []Member
	End      Position // the body's `}`
}

ClassDecl is a class. Conformance to it is nominal and always declared.

type ClassRef

type ClassRef struct {
	P         Position
	Qualifier string
	N         string
	Args      []*TypeArg
}

ClassRef names a class, optionally qualified and applied to arguments.

type Comment added in v0.1.7

type Comment struct {
	P    Position
	Text string // the text after the slashes, with one leading space removed
}

Comment is one ordinary `//` comment.

type Constraint

type Constraint struct {
	P    Position
	N    string
	Args []*Literal
}

Constraint is one entry in a `where { ... }` block. The set of names is open.

type Decl

type Decl interface {
	Pos() Position
	Name() string
	Head() *DeclHead
}

Decl is a top-level declaration. Every form embeds DeclHead.

type DeclHead

type DeclHead struct {
	Doc []string

	// DocP is where the doc comment was written, zero without one.
	DocP Position

	P   Position
	N   string
	Dep *Deprecation
}

DeclHead is the part every declaration shares.

func (*DeclHead) Head added in v0.1.8

func (h *DeclHead) Head() *DeclHead

func (*DeclHead) Name

func (h *DeclHead) Name() string

func (*DeclHead) Pos

func (h *DeclHead) Pos() Position

type Deprecation

type Deprecation struct {
	P      Position
	Reason string // "" when written without a reason
}

Deprecation marks a declaration, field, or variant as deprecated.

type Directive

type Directive struct {
	P    Position
	N    string
	Args []*Literal
}

Directive is an instruction to a backend, opaque to the compiler.

type EnumDecl

type EnumDecl struct {
	DeclHead
	Params   []*TypeParam
	Conforms []*ClassRef
	Requires []*ClassRef
	Variants []*Variant
	End      Position // the body's `}`
}

EnumDecl is a closed set of variants, each optionally carrying fields.

type Field

type Field struct {
	DeclHead
	Owned       bool // composition rather than reference
	Type        *TypeRef
	Constraints []*Constraint
	Default     *Literal
	End         Position // the constraint block's `}`; zero without one
}

Field is a named, typed member.

type File

type File struct {
	Filename string
	Package  *PackageDecl // nil if omitted
	Imports  []*ImportDecl
	Decls    []Decl

	// Comments holds every ordinary `//` comment in source order. Doc
	// comments live in the Doc of the declaration they precede.
	Comments []*Comment

	End Position
}

File is a single parsed .tdl source file.

type FunDep

type FunDep struct {
	P    Position
	From []string
	To   []string
}

FunDep states that some class parameters determine others.

type ImportDecl

type ImportDecl struct {
	Doc  []string
	DocP Position // where the doc comment was written; zero without one

	P     Position
	Path  string
	Alias string // "_" merges the imported names into the current scope
}

ImportDecl is an `import "path.tdl" as alias` declaration.

type Include

type Include struct {
	P    Position
	Type *ClassRef
}

Include copies a mixin's fields into the including declaration.

func (*Include) Pos added in v0.1.8

func (i *Include) Pos() Position

type InstanceDecl

type InstanceDecl struct {
	DeclHead // N is the class name
	Params   []*TypeParam
	Class    *ClassRef
	For      *TypeRef // set for `instance C for T`, nil for `instance C<T>`
	Requires []*ClassRef
	Binds    []*AssocTypeBind
	End      Position // the bind block's `}`; zero without one
}

InstanceDecl declares that a type satisfies a class.

type Kind

type Kind struct {
	P     Position
	N     string // "type" or "unit"; empty when Paren is set
	Paren *Kind
	Arrow *Kind // `left -> Arrow`; nil for a bare atom
}

Kind is a kind expression. Arrow associates to the right.

type Literal

type Literal struct {
	P     Position
	Kind  LiteralKind
	Text  string     // decoded for LitString, pattern body for LitRegex, source text otherwise
	Items []*Literal // set for LitList
	Lo    *Literal   // set for LitRange; nil when the range is open below
	Hi    *Literal   // set for LitRange; nil when the range is open above
}

Literal is a field default, constraint argument, or directive argument.

type LiteralKind

type LiteralKind int

LiteralKind is the form a Literal takes.

const (
	LitString LiteralKind = iota
	LitInt
	LitFloat
	LitBool
	LitList
	LitName  // a dotted name, denoting an enum variant
	LitRegex // /.../, a constraint argument
	LitRange // 3..254, 1.., ..254
)

type Member

type Member interface {
	Pos() Position
}

Member is one item in a StructDecl body: a Field or an Include.

type NewtypeDecl

type NewtypeDecl struct {
	DeclHead
	Params      []*TypeParam
	Base        *TypeRef
	Requires    []*ClassRef
	Constraints []*Constraint
	End         Position // the constraint block's `}`; zero without one
}

NewtypeDecl is a `type Name: Base` declaration.

type PackageDecl

type PackageDecl struct {
	P    Position
	Path string // dotted, e.g. "shop.orders"
}

PackageDecl is a `package <dotted.ident>` declaration.

type Position

type Position = lex.Position

Position identifies a location in a source file.

type PrimitiveDecl

type PrimitiveDecl struct {
	DeclHead
	Kind *Kind // nil when the kind is left to inference
}

PrimitiveDecl is a `primitive Name` or `primitive Name: Kind` declaration of an opaque root type.

type StructDecl

type StructDecl struct {
	DeclHead
	Keyword  string // "type" or "mixin"
	Params   []*TypeParam
	Conforms []*ClassRef
	Requires []*ClassRef
	Members  []Member
	End      Position // the body's `}`
}

StructDecl is a `type` or `mixin` declaration with a body of members.

type TargetDecl

type TargetDecl struct {
	DeclHead
	For     string // the dotted package name the target applies to
	Entries []*TargetEntry
	End     Position // the block's `}`
}

TargetDecl is a `target go for billing { ... }` block.

type TargetEntry

type TargetEntry struct {
	P         Position
	Path      string         // "" for a bare directive
	Directive *Directive     // nil when Entries is set
	Entries   []*TargetEntry // nil when Directive is set
	End       Position       // the nested block's `}`; zero without one
}

TargetEntry is one entry in a TargetDecl: a nested block, a path mapped to a directive, or a bare directive.

type TypeArg

type TypeArg struct {
	P    Position
	Type *TypeRef  // set unless Unit is
	Unit *UnitExpr // set only when operators made the argument unambiguous
}

TypeArg is one argument in a `<...>` list: a type or a unit. A bare name is recorded as a type and the resolver decides.

type TypeParam

type TypeParam struct {
	P    Position
	N    string
	Kind *Kind // nil when inferred from use
}

TypeParam is one parameter in a `<...>` parameter list.

type TypeRef

type TypeRef struct {
	P Position

	Qualifier string // "" if unqualified; set for "alias.Type"
	N         string // "" for the collection forms below
	Args      []*TypeArg

	List *TypeRef // [T]
	Set  *TypeRef // {T}

	MapKey   *TypeRef // {K -> V}
	MapValue *TypeRef

	Optional bool // trailing ?
	Nullable bool // trailing | null
}

TypeRef is a reference to a type, with collection and optionality sugar recorded as written. The resolver lowers it to prelude types.

type UnitDecl

type UnitDecl struct {
	DeclHead
	Expr *UnitExpr // nil for a base unit
}

UnitDecl is a base `unit kg` or a derived `unit N = kg*m/s^2` declaration.

type UnitExpr

type UnitExpr struct {
	P     Position
	Terms []*UnitTerm
}

UnitExpr is a product and quotient of unit terms.

type UnitTerm

type UnitTerm struct {
	P     Position
	Op    string    // "" for the first term, otherwise "*" or "/"
	N     string    // unit name; "" when Paren is set
	Exp   int       // exponent; 1 when written without one
	Paren *UnitExpr // set for a parenthesized sub-expression
}

UnitTerm is one factor of a UnitExpr.

type Variant

type Variant struct {
	DeclHead
	Fields []*Field // nil for a variant without a payload
	End    Position // the payload's `}`; zero without one
}

Variant is one alternative in an EnumDecl.

Jump to

Keyboard shortcuts

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