Documentation
¶
Overview ¶
Package ast declares the syntax tree of an Ant module, as the parser builds it and the resolver annotates it.
Index ¶
- func Contains(n Node, p Pos) bool
- func Inspect(n Node, f func(Node) bool)
- func Sprint(n Node) string
- type Arg
- type ArrayElem
- type ArrayLit
- type ArrayTarget
- type Assert
- type AssertPart
- type Assign
- type AssignArray
- type BadExpr
- type BadStmt
- type BadType
- type Binary
- type Block
- type BlockScope
- type BoolLit
- type Break
- type Call
- type Continue
- type Decl
- type Defer
- type DurationLit
- type Expr
- type Field
- type FieldType
- type File
- type Fixed
- type FloatLit
- type For
- type FuncDecl
- type FuncLit
- type FuncScope
- type FuncType
- type FuncTypeParam
- type Go
- type Html
- type HtmlAttr
- type HtmlElement
- type HtmlNode
- type Ident
- type If
- type Import
- type ImportName
- type Index
- type IntLit
- type InterfaceDecl
- type InterfaceFunc
- type Is
- type Lambda
- type MapEntry
- type MapLit
- type ModuleScope
- type NameTarget
- type Node
- type NullLit
- type Param
- type Placeholder
- type Pos
- type RecordEntry
- type RecordLit
- type RecordTarget
- type RecordTargetField
- type RecordType
- type Ref
- type RefKind
- type RegexLit
- type Return
- type Select
- type SelectClause
- type Slot
- type SlotName
- type Stmt
- type StringLit
- type Target
- type Template
- type TemplatePart
- type Test
- type Type
- type TypeDecl
- type TypeExpr
- type TypeName
- type TypeParam
- type Unary
- type UnionType
- type VarDecl
- type VoidLit
- type While
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
Contains reports whether p lies in the span of n, from its Start inclusive to its EndPosition exclusive.
func Inspect ¶
Inspect walks the tree under n in source order, calling f for each node, children included when f returns true. Besides the expressions, statements, types, and targets, it visits the parts a node is made of that carry a position: imports' names, type and value parameters, the fields of a record type or destructuring, the entries of literals, call arguments, string parts, select clauses, and the nodes, elements, and attributes of markup.
Types ¶
type ArrayLit ¶
type ArrayLit struct {
Pos Pos
End Pos
Elems []ArrayElem
// Off is the byte offset of the [, and SpreadComma reports a comma after a final
// spread, for the parser when the literal turns out to be a destructuring assignment.
Off int
SpreadComma bool
}
ArrayLit is [ elements ].
func (*ArrayLit) EndPosition ¶
type ArrayTarget ¶
type ArrayTarget struct {
Pos Pos
End Pos
Elems []Target
Rest *NameTarget // nil without a rest
}
ArrayTarget is an array destructuring [a, b, ...rest].
func (*ArrayTarget) EndPosition ¶
func (n *ArrayTarget) EndPosition() Pos
func (*ArrayTarget) Position ¶
func (n *ArrayTarget) Position() Pos
type Assert ¶
type Assert struct {
// Cond is the argument that would fill cond, the first positional one or the one named
// cond, or nil when there is none.
Cond Expr
// Text is the condition's source.
Text string
// Op is the condition's outermost operator when it is a comparison or is, and "" otherwise;
// X and Y index its operands in Parts, Y being -1 for is.
Op string
X, Y int
// Parts are the subexpressions whose values are recorded as the condition evaluates, and
// Index finds each one's place in Parts.
Parts []AssertPart
Index map[Expr]int
}
Assert is what a call of assert needs to report a failure (section 8, assert): its condition, and the subexpressions of the condition whose values the report shows, with the source text of each. The arguments are matched to cond and message as any call's are.
type AssertPart ¶
type AssertPart struct {
Expr Expr
Text string
// Shown marks a part listed on a line of its own; the comparison's operands are not.
Shown bool
// Receiver marks the x of a call x.f(…), recorded only when x is passed to f as its first
// argument: not when it is a namespace, as Array is, or a record whose field f is called.
Receiver bool
}
AssertPart is a subexpression of an assert's condition.
type Assign ¶
Assign is target op value. Target is an Ident, Field, Index, or ArrayTarget.
func (*Assign) EndPosition ¶
type AssignArray ¶
type AssignArray struct {
Pos Pos
End Pos
Elems []Node // Ident, Field, Index, or *AssignArray
Rest Expr // Ident, Field, or Index; nil without a rest
}
AssignArray is the array destructuring target of an assignment: places and nested targets, with an optional rest place.
func (*AssignArray) EndPosition ¶
func (n *AssignArray) EndPosition() Pos
func (*AssignArray) Position ¶
func (n *AssignArray) Position() Pos
type BadExpr ¶
type BadExpr struct{ Pos, End Pos }
BadExpr, BadStmt, and BadType stand for source that did not parse, in a tree the parser built past a syntax error (parser.ParseRecover). They have no meaning: the checker gives a BadExpr the type Invalid and reports nothing about it.
func (*BadExpr) EndPosition ¶
type Binary ¶
Binary is an arithmetic, comparison, equality, logical, or ?? operation.
func (*Binary) EndPosition ¶
type Block ¶
type Block struct {
Pos Pos
End Pos
Stmts []Stmt
HasDefer bool // a defer statement registers directly with this block
Types []*TypeDecl // the types declared directly in the block, bound when it starts
Scope *BlockScope // filled by the resolver; nil when the block declares nothing
}
Block is { statements }.
func (*Block) EndPosition ¶
type BlockScope ¶
type BlockScope struct {
Plain int // number of plain slots
// Names lists the bindings that have a slot, in declaration order, for a debugger to show.
Names []SlotName
}
BlockScope describes the environment a block, loop iteration, or function allocates.
type BoolLit ¶
func (*BoolLit) EndPosition ¶
type Call ¶
type Call struct {
Pos Pos
End Pos
Fn Expr
Args []Arg
Optional bool
Pipe bool
Grouped bool
// Bar is the position of a pipe's |, and Stage is set on each pipe of a pipeline of two
// or more, where a debugger stops before the stage (dap.md section 4).
Bar Pos
Stage bool
// Assert is set on a call of assert, which the parser recognizes by its name, since no
// declaration may shadow a prelude name.
Assert *Assert
}
Call is f(args) or f?.(args). A pipe a | f(b) is a Call with Pipe set and a as the first argument; it evaluates that argument before f. Grouped as for Field.
func (*Call) EndPosition ¶
type Defer ¶
func (*Defer) EndPosition ¶
type DurationLit ¶ added in v0.2.0
DurationLit is a duration literal (section 2), as 2h30m. Text is the literal as written, and Value the Time.Duration it evaluates to.
func (*DurationLit) EndPosition ¶ added in v0.2.0
func (n *DurationLit) EndPosition() Pos
func (*DurationLit) Position ¶ added in v0.2.0
func (n *DurationLit) Position() Pos
type Expr ¶
type Expr interface {
Stmt
// contains filtered or unexported methods
}
Expr is an expression. Every construct of section 5 is an expression, including if, loops, go, defer, return, break, continue, and assignment.
type Field ¶
type Field struct {
Pos Pos // the . or ?.
End Pos
X Expr
Name string
NamePos Pos
Optional bool
Grouped bool
}
Field is a.name or a?.name. Grouped marks one written in parentheses, which ends its postfix chain: a ?. inside skips nothing outside (section 12).
func (*Field) EndPosition ¶
type FieldType ¶
FieldType is one field of a record type. Default is the expression a literal that leaves the field out evaluates, or nil; only the body of a type declaration has defaults (section 4).
func (*FieldType) EndPosition ¶
type File ¶
type File struct {
Path string
Doc string // the module doc, from the //! lines before the first import (section 2), or ""
Imports []*Import
Decls []Decl
// Stmts holds the top-level var declarations and statements in source order: a script's
// body (section 10), or a session input's or a spec example's statements.
Stmts []Stmt
// Script is set for a program module with a statement at the top level; FirstStmt is where
// the first one starts.
Script bool
FirstStmt Pos
// Tests holds a test file's top-level tests in source order (section 10, Tests).
Tests []*Test
// Declarations is set for a declaration file (section 10, Declaration files), parsed in
// the parser's Declarations mode: its funcs have no bodies and its names join the prelude.
Declarations bool
// NamespaceDocs holds the section labels of a declaration file, keyed by namespace name.
NamespaceDocs map[string]string
// End is the position after the last token of the file.
End Pos
// Scope is filled by the resolver: the module record's fields.
Scope *ModuleScope
}
File is a parsed module.
func (*File) EndPosition ¶
type Fixed ¶
type Fixed uint8
Fixed says whether a binding can be reassigned, and if not, why (section 3).
type FloatLit ¶
func (*FloatLit) EndPosition ¶
type For ¶
type For struct {
Pos Pos
End Pos
Target Target
Iter Expr
Body *Block
Scope *BlockScope // the per-iteration scope, filled by the resolver
Assigned []string // the locals declared outside the loop that its body assigns, filled by the resolver
}
func (*For) EndPosition ¶
type FuncDecl ¶
type FuncDecl struct {
Pos Pos
End Pos
Export bool
Owner string // the type for func Type.name, or ""
OwnerPos Pos // where Owner is written, or zero
Name string
NamePos Pos
TypeParams []TypeParam
Params []Param
Return Type // nil when inferred
Body *Block // nil only in declarations mode
Doc string // the doc comment (section 2), or ""
Sig string // the source from func through the result type, as written
// Scope is filled by the resolver.
Scope *FuncScope
}
FuncDecl is a named function, or a function declared on a type when Owner is set.
func (*FuncDecl) EndPosition ¶
type FuncScope ¶
type FuncScope struct {
BlockScope
// X is the slot of the implicit lambda parameter.
X Slot
}
FuncScope describes a function body, lambda, go block, or defer block.
type FuncType ¶
type FuncType struct {
Pos Pos
End Pos
Params []FuncTypeParam
Return Type
}
FuncType is a function type func(T, U): R.
func (*FuncType) EndPosition ¶
type FuncTypeParam ¶
FuncTypeParam is one parameter of a function type, with an optional documenting name.
type Go ¶
func (*Go) EndPosition ¶
type Html ¶
type Html struct {
Pos Pos
End Pos
Content []HtmlNode
Close Pos // the </> of a fragment, or zero for an element
}
Html is an HTML expression (section 5): an element, or a fragment, as its content.
func (*Html) EndPosition ¶
type HtmlAttr ¶
HtmlAttr is an attribute: literal Text when Expr is nil, a value when Expr is set, and every entry of a map when Spread is set, in which case Name is empty.
func (*HtmlAttr) EndPosition ¶
type HtmlElement ¶
type HtmlElement struct {
Pos Pos // the <
End Pos
Name string
NamePos Pos
Attrs []HtmlAttr
Content []HtmlNode
Close Pos
TextOnly bool
}
HtmlElement is an element as written. TextOnly marks a <title> or <textarea> outside <svg> and <math>, whose content may not hold Html. Close is where its end tag starts, or zero when it is written self-closing.
func (*HtmlElement) EndPosition ¶
func (n *HtmlElement) EndPosition() Pos
func (*HtmlElement) Position ¶
func (n *HtmlElement) Position() Pos
type HtmlNode ¶
type HtmlNode struct {
Pos Pos
End Pos
Text string
Expr Expr
Element *HtmlElement
}
HtmlNode is an item of an HTML expression's content: decoded text, an interpolated Expr, or an Element.
func (*HtmlNode) EndPosition ¶
type Ident ¶
Ident is a variable reference. Ref is filled by the resolver.
func (*Ident) EndPosition ¶
type If ¶
func (*If) EndPosition ¶
type Import ¶
type Import struct {
Pos Pos
End Pos
Path string
PathPos Pos // the path string, from its opening quote
PathEnd Pos
Namespace string // the bare name that binds the whole module, or ""
NamespacePos Pos // where Namespace is written, or zero
Names []ImportName // the destructured names, when Namespace is ""
// Module is filled by the loader: the index of the imported module in the program, or -1
// when the module could not be loaded and the loader went on (Config.Recover).
Module int
}
Import is one import declaration.
func (*Import) EndPosition ¶
type ImportName ¶
type ImportName struct {
Pos Pos
End Pos
Name string
Alias string // the local name; equals Name when not renamed
AliasPos Pos // where the alias is written, or zero when not renamed
}
ImportName is one entry of an import list: the export, and the local name it binds.
func (*ImportName) EndPosition ¶
func (n *ImportName) EndPosition() Pos
func (*ImportName) Position ¶
func (n *ImportName) Position() Pos
type IntLit ¶
func (*IntLit) EndPosition ¶
type InterfaceDecl ¶
type InterfaceDecl struct {
Pos Pos
End Pos
Export bool
Name string
NamePos Pos
TypeParams []TypeParam
Funcs []InterfaceFunc
Doc string // the doc comment (section 2), or ""
Sig string // the source from interface through the type parameters, as written
}
InterfaceDecl is an interface declaration (section 4, Interfaces): a named set of function signatures, each taking the satisfying type, Self, first. The checker reads it, and the interpreter discards it, so the name binds nothing at run time.
func (*InterfaceDecl) EndPosition ¶
func (n *InterfaceDecl) EndPosition() Pos
func (*InterfaceDecl) Exported ¶
func (n *InterfaceDecl) Exported() bool
func (*InterfaceDecl) Position ¶
func (n *InterfaceDecl) Position() Pos
type InterfaceFunc ¶
type InterfaceFunc struct {
Pos Pos
End Pos
Name string
NamePos Pos
Params []Param
Return Type
Doc string // the doc comment (section 2), or ""
Sig string // the signature as written
}
InterfaceFunc is one signature of an interface: its name, its parameters, the first of which has the type Self, and its return type.
func (*InterfaceFunc) EndPosition ¶
func (n *InterfaceFunc) EndPosition() Pos
func (*InterfaceFunc) Position ¶
func (n *InterfaceFunc) Position() Pos
type Is ¶
type Is struct {
Pos Pos // the is
End Pos
X Expr
Kind string
KindPos Pos // where Kind is written, or zero
Path Expr
}
Is is x is T. Kind is set for a kind test (null, void, bool, string, func, Array, Map, and the numeric names); otherwise Path is the type's expression.
func (*Is) EndPosition ¶
type ModuleScope ¶
type ModuleScope struct {
Fields []string
Index map[string]int
// Let holds the fields bound by a top-level let, which no assignment may change.
Let map[string]bool
// PendingLet holds, in a session, the names the input being run declares with let: each
// joins Let once its declaration has bound it, so a let whose initializer fails can be
// declared again (section 10, Scripts).
PendingLet map[string]bool
// Types holds, in a session, the type declared by each field that names one, so a later
// input can declare functions on it.
Types map[string]*TypeDecl
}
ModuleScope lists the fields of the module record in declaration order.
type NameTarget ¶
NameTarget binds a name; "_" binds nothing.
func (*NameTarget) EndPosition ¶
func (n *NameTarget) EndPosition() Pos
func (*NameTarget) Position ¶
func (n *NameTarget) Position() Pos
type Node ¶
Node is any syntax node. Position is where it starts, as messages report it, and EndPosition is just after its last token, so the two span the node; Start is where it begins on the page, which for a postfix chain or an operator is its first operand.
type Param ¶
type Param struct {
Pos Pos
End Pos
Name string
Type Type
Default Expr
// Slot is filled by the resolver.
Slot Slot
}
Param is a function parameter.
func (*Param) EndPosition ¶
type Placeholder ¶
type Placeholder struct{ Pos, End Pos }
Placeholder is the "..." of spec mode.
func (*Placeholder) EndPosition ¶
func (n *Placeholder) EndPosition() Pos
func (*Placeholder) Position ¶
func (n *Placeholder) Position() Pos
type Pos ¶
type Pos struct {
Line, Col int
}
Pos is a position in the module's source: 1-based line and 1-based byte column.
type RecordEntry ¶
RecordEntry is a field of a record literal, or a spread.
func (*RecordEntry) EndPosition ¶
func (n *RecordEntry) EndPosition() Pos
func (*RecordEntry) Position ¶
func (n *RecordEntry) Position() Pos
type RecordLit ¶
type RecordLit struct {
Pos Pos
End Pos
Head Expr
Entries []RecordEntry
}
RecordLit is Head{ entries }, where Head is an identifier path.
func (*RecordLit) EndPosition ¶
type RecordTarget ¶
type RecordTarget struct {
Pos Pos
End Pos
Fields []RecordTargetField
}
RecordTarget is a record destructuring { x, y: py, ... }.
func (*RecordTarget) EndPosition ¶
func (n *RecordTarget) EndPosition() Pos
func (*RecordTarget) Position ¶
func (n *RecordTarget) Position() Pos
type RecordTargetField ¶
RecordTargetField is one entry of a record destructuring: the field, and the target it binds (a NameTarget with the field's name for the shorthand).
func (*RecordTargetField) EndPosition ¶
func (n *RecordTargetField) EndPosition() Pos
func (*RecordTargetField) Position ¶
func (n *RecordTargetField) Position() Pos
type RecordType ¶
RecordType is an anonymous record type { name: T, ... }.
func (*RecordType) EndPosition ¶
func (n *RecordType) EndPosition() Pos
func (*RecordType) Position ¶
func (n *RecordType) Position() Pos
type Ref ¶
type Ref struct {
Kind RefKind
Depth int // RefLocal: environments to walk up
Index int // RefLocal: slot index; RefModule: field index; RefImport: import index
// Fixed, for RefLocal and RefModule, says why the binding cannot be assigned.
Fixed Fixed
Name string
}
Ref is where an identifier resolves to.
type RefKind ¶
type RefKind int
RefKind says what an identifier refers to.
const ( RefUnresolved RefKind = iota RefLocal // a slot Depth environments up RefModule // a field of this module's record RefImport // a field of an imported module's record RefPrelude // a prelude name RefUnbound // no scope binds the name: a panic when evaluated RefDiscard // the write-only _: a panic when read )
type RegexLit ¶
type RegexLit struct {
Pos Pos
End Pos
Source string // normalized
Flags string // sorted
Pattern *value.Regex
}
RegexLit is a regular expression literal (section 2). Pattern is the value it evaluates to, built once when the literal is parsed.
func (*RegexLit) EndPosition ¶
type Return ¶
func (*Return) EndPosition ¶
type Select ¶
type Select struct {
Pos Pos
End Pos
Clauses []*SelectClause
}
Select is a select expression (section 5): clauses with a head each, and at most one else, last.
func (*Select) EndPosition ¶
type SelectClause ¶
type SelectClause struct {
Pos Pos
End Pos
Let bool // the binding is written let rather than var
Target *NameTarget
Head *Call
Body *Block
Scope *BlockScope
}
SelectClause is one clause of a select. Head is the call that names the wait, one of Channel.receive, Channel.send, Context.wait, WaitGroup.wait and Time.sleep, which the parser checks, and nil for else. Target binds the head's result when the clause has var x =. Scope is the body's scope with that binding, filled by the resolver, nil when it declares nothing.
func (*SelectClause) EndPosition ¶
func (n *SelectClause) EndPosition() Pos
func (*SelectClause) Keyword ¶
func (c *SelectClause) Keyword() string
Keyword is the keyword that binds the clause's result: var or let.
func (*SelectClause) Position ¶
func (n *SelectClause) Position() Pos
type Slot ¶
type Slot struct {
Index int
Module bool // a module field rather than a local slot
// AssignedNested is set when the binding is assigned inside a nested body (section 7).
AssignedNested bool
// Discard is set for _, which binds nothing: a value stored there is dropped.
Discard bool
// Fixed says why the binding cannot be reassigned, or is Assignable.
Fixed Fixed
}
Slot is where a binding lives in its environment, with the facts narrowing needs.
type SlotName ¶
type SlotName struct {
Name string
Index int
Type bool // a type declared in the block, which a debugger leaves out
Fixed Fixed // why the binding cannot be reassigned, or Assignable
}
SlotName names one slot of a BlockScope.
type Stmt ¶
type Stmt interface {
Node
// contains filtered or unexported methods
}
Stmt is a statement in a block: an expression or a var declaration.
type StringLit ¶
func (*StringLit) EndPosition ¶
type Target ¶
type Target interface {
Node
// contains filtered or unexported methods
}
Target is the left side of a var declaration or a for: a name or a destructuring.
type Template ¶
type Template struct {
Pos Pos
End Pos
Parts []TemplatePart
}
Template is a string with interpolations; each part is literal text or an interpolated expression.
func (*Template) EndPosition ¶
type TemplatePart ¶
TemplatePart is literal text or an interpolated expression; an interpolation spans from its $ to its closing brace.
func (*TemplatePart) EndPosition ¶
func (n *TemplatePart) EndPosition() Pos
func (*TemplatePart) Position ¶
func (n *TemplatePart) Position() Pos
type Test ¶
type Test struct {
Pos Pos
End Pos
Name Expr
Body *Block
Scope *FuncScope
Doc string // the doc comment (section 2) of a top-level test, or ""
}
Test is a test declaration at the top level of a test file, or the statement that runs a subtest inside a test's body (section 10, Tests). Name is a *StringLit for a top-level test, and a *StringLit or a *Template for a subtest.
func (*Test) EndPosition ¶
type Type ¶
type Type interface {
Node
// contains filtered or unexported methods
}
Type is a type expression, read by the checker and discarded by the interpreter except for the field names of a record type.
type TypeDecl ¶
type TypeDecl struct {
Pos Pos
End Pos
Export bool
Owner string
OwnerPos Pos // where Owner is written, or zero
Name string
NamePos Pos
TypeParams []TypeParam
Body Type
Doc string // the doc comment (section 2), or ""
Sig string // the whole declaration from type on, as written
// Slot is filled by the resolver for a type declared in a block.
Slot Slot
}
TypeDecl is a type declaration. In declarations mode Owner may name the namespace the type belongs to, as in type Json.Value, and Body may be nil for an opaque type such as Mutex.
func (*TypeDecl) EndPosition ¶
type TypeExpr ¶
TypeExpr is a type expression type(T): the type T as a value (section 4, Types as values). Pos is the type keyword.
func (*TypeExpr) EndPosition ¶
type TypeName ¶
type TypeName struct {
Pos Pos
End Pos
Path []string
PathPos []Pos // where each part of Path is written
Args []Type
// Head, filled by the resolver for a type the interpreter resolves (section 4, Types as
// values), is the identifier that evaluates the first part of Path; nil for a built-in
// name or a type parameter.
Head *Ident
}
TypeName is a named type with optional type arguments: int, Array[T], Geometry.Point, and also null and void.
func (*TypeName) EndPosition ¶
type TypeParam ¶
TypeParam is a type parameter with an optional bound.
func (*TypeParam) EndPosition ¶
type VarDecl ¶
type VarDecl struct {
Pos Pos
End Pos
Export bool
Let bool // declared with let, so its names cannot be reassigned (section 3)
Owner string // the namespace of a declaration file's constant, let Owner.NAME: T, or ""
OwnerPos Pos // where Owner is written, or zero
Target Target
Type Type // nil when inferred; only with a NameTarget
Init Expr
Doc string // the doc comment (section 2) of a top-level var, or ""
Sig string // the source from var or let through the type, as written, without the initializer
}
VarDecl is a var or let declaration, at the top level or in a block. In declarations mode a constant such as let Math.PI: float64 has an Owner and no Init.