ast

package
v0.2.9 Latest Latest
Warning

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

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

Documentation

Overview

Package ast defines the TDL abstract syntax tree: a parse tree that mirrors source text 1:1, with names left unresolved. See docs/design/ir.md for the resolved semantic model backends consume.

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 formatting produced by `tdl fmt`. It is idempotent: formatting canonical output changes nothing.

Whitespace is insignificant in TDL, so the formatter owns layout entirely. Its decisions depend only on the tree and on where the comments sit in it, never on how the input was written, which is what makes idempotence hold.

func PrintDecl added in v0.2.8

func PrintDecl(decl Decl) string

PrintDecl renders one declaration in canonical form, without its doc comment, its deprecation, or the ordinary comments around it. It is the declaration as a hover shows it, which renders those separately.

func PrintTypeRef added in v0.2.8

func PrintTypeRef(t *TypeRef) string

PrintTypeRef renders a type reference as the formatter writes it, which is how an editor's outline shows a field's type.

func PrintUnitExpr added in v0.1.4

func PrintUnitExpr(e *UnitExpr) string

PrintUnitExpr renders a unit expression without spaces around its operators, the form the spec uses: `kg*m/s^2`.

Exported because lowering records what a unit was written as beside what it reduces to, and reconstructing the text there would be a second printer to keep in step with this one.

Types

type AliasDecl

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

AliasDecl is an `alias Name = TypeRef` declaration, optionally parameterized. An alias is transparent: it is expanded rather than referenced.

type AssocTypeBind

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

AssocTypeBind supplies a type for one of a class's associated type requirements.

type AssocTypeReq

type AssocTypeReq struct {
	DeclHead
	Kind *Kind
}

AssocTypeReq is a `type Cursor` requirement: an implementor supplies a type, and an instance binds it. Nothing deprecates one, so Dep is nil.

type ClassDecl

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

ClassDecl is a contract. It declares nothing into the types that satisfy it; conformance 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. It is syntactically a named type reference, but the two are different kinds of thing and the tree keeps them apart.

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. The compiler checks the arity and argument kinds of the standard names and passes everything else through, so a backend may understand a constraint the compiler has never heard of.

type Decl

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

Decl is a top-level declaration. Every form embeds DeclHead, which is what Head returns.

type DeclHead

type DeclHead struct {
	Doc []string

	// DocP is where the doc comment was written, zero without one. The
	// formatter orders it against the ordinary comments around it, which
	// are placed by position and not carried by the tree.
	DocP Position

	P   Position
	N   string
	Dep *Deprecation
}

DeclHead is the part every declaration shares: its doc comment, its position, its name, and whether it is deprecated.

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 on its way out.

type Directive

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

Directive is an opaque instruction to a backend. The compiler checks its shape and hands it over; what it means is the backend's business.

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. A variant may carry fields, which makes enum the language's sum type.

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. Its head is a declaration's: a doc comment, a position, a name, and a deprecation.

type File

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

	// Comments holds every ordinary `//` comment in the file, in source
	// order. They are not attached to any node: a comment can sit
	// anywhere, so the formatter places each one by position rather than
	// the tree carrying it. Doc comments are not here; those belong to the
	// declaration they precede and live in its Doc.
	Comments []*Comment

	// End is the position of the end of the file, which is what a comment
	// after the last declaration is placed against.
	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 parameters determine others, which makes a multi-parameter class a function rather than a table.

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 when written with `for`, nil when written with type arguments
	Requires []*ClassRef
	Binds    []*AssocTypeBind
	End      Position // the bind block's `}`; zero without one
}

InstanceDecl declares that a type satisfies a class.

`instance C for T` is sugar for `instance C<T>`, available when the class takes one parameter. The parser records which was written.

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. Name is "type" or "unit" for an atom, or Paren holds a parenthesized kind; Arrow is set when this kind is the left side of an arrow, which 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 literal value: a field default, a constraint argument, or a directive argument.

type LiteralKind

type LiteralKind int

LiteralKind identifies which 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. A newtype is distinct from the type it is built on.

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. It introduces an opaque, irreducible 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 declaration with a body of members: a `type` or a `mixin`. The two share a shape and differ in meaning, so the keyword is recorded rather than split across two identical node types.

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. Everything a code generator needs lives here rather than in the model.

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 path scoping a nested block, a path mapped to a directive, or a bare directive applying to the enclosing scope.

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. It is a type or a unit, and the two are told apart by kind rather than by syntax: a bare name could be either, so the parser records what was written and the resolver decides against the declaration being applied.

type TypeParam

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

TypeParam is one parameter in a `<...>` parameter list, with an optional kind annotation.

type TypeRef

type TypeRef struct {
	P Position

	// Named form: an optionally qualified name with optional arguments.
	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.

The collection and optionality forms are sugar for prelude types, and the parser records the form as written: lowering to List, Set, Map, Option, and Nullable is the resolver's job.

type UnitDecl

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

UnitDecl is a `unit kg` or `unit N = kg*m/s^2` declaration. A unit without an expression is a base unit; one with an expression is derived and reduces to base dimensions before comparison.

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