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 ¶
- func Doc(d Decl) []string
- func Dump(file *File) string
- func Fprint(file *File) string
- func PrintUnitExpr(e *UnitExpr) string
- type AliasDecl
- type AssocTypeBind
- type AssocTypeReq
- type ClassDecl
- type ClassRef
- type Comment
- type Constraint
- type Decl
- type DeclHead
- type Deprecation
- type Directive
- type EnumDecl
- type Field
- type File
- type FunDep
- type ImportDecl
- type Include
- type InstanceDecl
- type KeyRequirement
- type Kind
- type Literal
- type LiteralKind
- type Member
- type NewtypeDecl
- type PackageDecl
- type Position
- type PrimitiveDecl
- type StructDecl
- type TargetDecl
- type TargetEntry
- type TypeArg
- type TypeParam
- type TypeRef
- type UnitDecl
- type UnitExpr
- type UnitTerm
- type Variant
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dump ¶
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 ¶
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 PrintUnitExpr ¶ added in v0.1.4
PrintUnitExpr renders a unit expression the way `tdl fmt` writes it.
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 ¶
AliasDecl is an `alias Name = TypeRef` declaration, optionally parameterized. An alias is transparent: it is expanded rather than referenced.
type AssocTypeBind ¶
AssocTypeBind supplies a type for one of a class's associated type requirements.
type AssocTypeReq ¶
AssocTypeReq is a `type Cursor` requirement: an implementor supplies a type, and an instance binds it.
func (*AssocTypeReq) MemberPos ¶
func (a *AssocTypeReq) MemberPos() Position
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 ¶
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 ¶
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 ¶
Decl is a top-level declaration. `class` and `instance` arrive with phase 4 of docs/design/parser-plan.md; every other form is here.
type DeclHead ¶
type DeclHead struct {
Doc []string
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.
type Deprecation ¶
Deprecation marks a declaration, field, or variant as on its way out.
func Deprecated ¶
func Deprecated(d Decl) *Deprecation
Deprecated returns the deprecation attached to a declaration, or nil.
type Directive ¶
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 {
Doc []string
P Position
N string
Key bool // part of the entity's identity
Owned bool // composition rather than reference
Dep *Deprecation
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 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 ¶
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
P Position
Path string
Alias string // "_" merges the imported names into the current scope
}
ImportDecl is an `import "path.tdl" as alias` declaration.
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 KeyRequirement ¶
type KeyRequirement struct {
P Position
}
KeyRequirement is a bare `key` in a class body: an implementor must have some key, without the class saying which.
func (*KeyRequirement) MemberPos ¶
func (k *KeyRequirement) MemberPos() Position
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 {
MemberPos() 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 ¶
PackageDecl is a `package <dotted.ident>` declaration.
type PrimitiveDecl ¶
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 // "entity", "value", or "mixin"
Params []*TypeParam
Conforms []*ClassRef
Requires []*ClassRef
Members []Member
End Position // the body's `}`
}
StructDecl is a declaration with a body of members: `entity`, `value`, or `mixin`. The three share a shape and differ in meaning, so the keyword is recorded rather than split across three 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 ¶
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 ¶
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.