std / saurus/ast

saurus/ast

import "std:saurus/ast"

Abstract syntax tree node definitions for the Saurus parser. Defines expression, statement, declaration, and type node structures along with accessor functions for all node fields.

View source on Codeberg →

Enum Types

#
type TypeKind int

const (
    TYPE_NAME    TypeKind = iota
    TYPE_POINTER
    TYPE_ARRAY
    TYPE_SLICE
    TYPE_LIST
    TYPE_MAP
    TYPE_FUNC
    TYPE_STRUCT
)

Discriminant for TypeNode. Identifies the kind of type: named type, pointer, array, slice, list, map, function type, or struct type.

#
type ExprKind int

const (
    EXPR_IDENT; EXPR_INT; EXPR_FLOAT; EXPR_STRING
    EXPR_RUNE; EXPR_BOOL; EXPR_NIL; EXPR_BINARY
    EXPR_UNARY; EXPR_CALL; EXPR_INDEX; EXPR_SLICE
    EXPR_SELECTOR; EXPR_PKG_ACCESS; EXPR_CAST
    EXPR_COMPOSITE; EXPR_TYPE; EXPR_PAREN
    EXPR_EXPAND; EXPR_FUNC_LIT; EXPR_IOTA
)

Discriminant for Expr nodes. Covers all expression forms: literals, identifiers, binary/unary operations, function calls, indexing, slicing, selectors, package access, type casts, composite literals, type expressions, parenthesized expressions, variadic expansion, anonymous functions, and iota.

#
type CastKind int

const (
    CAST_REGULAR CastKind = iota
    CAST_AS
    CAST_ASSUME
)

Distinguishes between regular type conversion, the as safe cast operator, and the assume unsafe pointer cast.

#
type StmtKind int

const (
    STMT_BLOCK; STMT_EXPR; STMT_ASSIGN; STMT_SHORT_DECL
    STMT_VAR_DECL; STMT_CONST_DECL; STMT_IF; STMT_FOR
    STMT_SWITCH; STMT_TYPE_SWITCH; STMT_RETURN; STMT_BREAK
    STMT_CONTINUE; STMT_DEFER; STMT_FALLTHROUGH
    STMT_INC_DEC; STMT_FREE; STMT_REMOVE; STMT_DIRECTIVE
)

Discriminant for Stmt nodes. Covers all statement forms: blocks, expressions, assignments, short declarations, var/const declarations, control flow (if, for, switch, type switch), return, break, continue, defer, fallthrough, increment/decrement, free, remove, and directives.

#
type DeclKind int

const (
    DECL_VAR DeclKind = iota
    DECL_CONST
    DECL_TYPE
    DECL_FUNC
    DECL_STRUCT
    DECL_DIRECTIVE
)

Discriminant for Decl nodes. Covers top-level declarations: variables, constants, type aliases, functions, structs, and compiler directives.

#
type BuildKind int

const (
    BUILD_TAG BuildKind = iota
    BUILD_NOT
    BUILD_AND
    BUILD_OR
)

Discriminant for build constraint expressions. Supports tag matching, boolean NOT, AND, and OR combinations.

Struct Types

#
type Field struct {
    name string
    typ  *TypeNode
}

A struct field with a name and type.

#
type Param struct {
    name string
    typ  *TypeNode
}

A function parameter with a name and type.

#
type TypeNode struct { /* kind-discriminated */ }

A type expression node. The TypeKind discriminant determines which fields are valid. Supports named types, pointers, arrays (with size), slices, lists, maps (with key type), function types (with params/returns), and struct types (with fields).

#
type CompField struct {
    name  string
    value *Expr
}

A field in a composite literal expression (e.g. Point{x = 1, y = 2}).

#
type Expr struct { /* kind-discriminated */ }

An expression node. The ExprKind discriminant determines which fields are valid. Heap-allocated; must be freed with FreeExpr.

#
type SwitchCase struct {
    values list[*Expr]
    body   list[*Stmt]
}

A case clause in an expression switch statement. An empty values list represents the default case.

#
type TypeSwitchCase struct {
    typ  *TypeNode
    body list[*Stmt]
}

A case clause in a type switch statement. A nil typ represents the default case.

#
type Stmt struct { /* kind-discriminated */ }

A statement node. The StmtKind discriminant determines which fields are valid. Heap-allocated; must be freed with FreeStmt.

#
type Decl struct { /* kind-discriminated */ }

A top-level declaration node. The DeclKind discriminant determines which fields are valid. Heap-allocated; must be freed with FreeDecl.

#
type Import struct {
    path  string
    alias string
    pos   token.Pos
}

An import declaration with the package path, optional alias, and source position.

#
type Directive struct {
    name string
    pos  token.Pos
    args list[string]
}

A compiler directive (e.g. #extern, #inline) with its name, position, and string arguments.

#
type BuildExpr struct { /* kind-discriminated */ }

A build constraint expression from #build directives. The BuildKind discriminant determines the operation: tag match, NOT, AND, or OR.

#
type File struct {
    filename   string
    pkg        string
    imports    list[Import]
    decls      list[*Decl]
    directives list[Directive]
    buildExpr  *BuildExpr
}

Represents a parsed source file. Contains the filename, package name, imports, top-level declarations, file-level directives, and an optional build constraint expression. Must be freed with FreeFile.

Field/Param Accessors

#
func FieldName(f Field) string

Returns the name of a struct field.

#
func FieldType(f Field) *TypeNode

Returns the type of a struct field.

#
func ParamName(p Param) string

Returns the name of a function parameter.

#
func ParamType(p Param) *TypeNode

Returns the type of a function parameter.

Import Accessors

#
func ImportPath(i Import) string

Returns the import path string.

#
func ImportAlias(i Import) string

Returns the import alias, or empty string if none.

#
func ImportPos(i Import) token.Pos

Returns the source position of the import.

Directive Accessors

#
func DirectiveName(d Directive) string

Returns the directive name (e.g. "extern", "inline").

#
func DirectivePos(d Directive) token.Pos

Returns the source position of the directive.

#
func DirectiveArgs(d Directive) list[string]

Returns the list of string arguments for the directive.

File Accessors

#
func FileName(f *File) string

Returns the source filename.

#
func FilePkg(f *File) string

Returns the package name declared in the file.

#
func FileImports(f *File) list[Import]

Returns the list of imports.

#
func FileDecls(f *File) list[*Decl]

Returns the list of top-level declarations.

#
func FileDirectives(f *File) list[Directive]

Returns the list of file-level directives.

#
func FileBuildExpr(f *File) *BuildExpr

Returns the build constraint expression, or nil if none.

Expr Accessors

#
func ExprKindOf(e *Expr) ExprKind

Returns the kind discriminant of an expression node.

#
func ExprPos(e *Expr) token.Pos

Returns the source position of the expression.

#
func ExprName(e *Expr) string

Returns the identifier name (EXPR_IDENT, EXPR_SELECTOR).

#
func ExprPkg(e *Expr) string

Returns the package name for EXPR_PKG_ACCESS.

#
func ExprLit(e *Expr) string

Returns the literal text (EXPR_INT, EXPR_FLOAT, EXPR_STRING).

#
func ExprFloatVal(e *Expr) float64

Returns the parsed float64 value (EXPR_FLOAT).

#
func ExprRuneVal(e *Expr) rune

Returns the parsed rune value (EXPR_RUNE).

#
func ExprBoolVal(e *Expr) bool

Returns the boolean value (EXPR_BOOL).

#
func ExprOp(e *Expr) token.Kind

Returns the operator token kind (EXPR_BINARY, EXPR_UNARY).

#
func ExprLeft(e *Expr) *Expr

Returns the left operand (EXPR_BINARY).

#
func ExprRight(e *Expr) *Expr

Returns the right operand (EXPR_BINARY).

#
func ExprOperand(e *Expr) *Expr

Returns the operand (EXPR_UNARY, EXPR_PAREN, EXPR_EXPAND).

#
func ExprArgs(e *Expr) list[*Expr]

Returns the argument list (EXPR_CALL).

#
func ExprSliceLow(e *Expr) *Expr

Returns the low bound of a slice expression (EXPR_SLICE).

#
func ExprSliceHigh(e *Expr) *Expr

Returns the high bound of a slice expression (EXPR_SLICE).

#
func ExprType(e *Expr) *TypeNode

Returns the type node (EXPR_CAST, EXPR_COMPOSITE, EXPR_TYPE).

#
func ExprCastKind(e *Expr) CastKind

Returns the cast kind for EXPR_CAST (regular, as, or assume).

#
func ExprCompFields(e *Expr) list[CompField]

Returns the composite literal fields (EXPR_COMPOSITE).

#
func ExprFuncParams(e *Expr) list[Param]

Returns the parameter list of an anonymous function (EXPR_FUNC_LIT).

#
func ExprFuncReturns(e *Expr) list[*TypeNode]

Returns the return types of an anonymous function (EXPR_FUNC_LIT).

#
func ExprFuncVariadic(e *Expr) bool

Reports whether the anonymous function is variadic (EXPR_FUNC_LIT).

#
func ExprBody(e *Expr) list[*Stmt]

Returns the body statements of an anonymous function (EXPR_FUNC_LIT).

Stmt Accessors

#
func StmtKindOf(s *Stmt) StmtKind

Returns the kind discriminant of a statement node.

#
func StmtPos(s *Stmt) token.Pos

Returns the source position of the statement.

#
func StmtStmts(s *Stmt) list[*Stmt]

Returns the list of statements in a block (STMT_BLOCK).

#
func StmtExpr(s *Stmt) *Expr

Returns the expression (STMT_EXPR, STMT_DEFER, STMT_FREE, STMT_INC_DEC, STMT_RETURN).

#
func StmtAssignLeft(s *Stmt) list[*Expr]

Returns the left-hand side expressions (STMT_ASSIGN).

#
func StmtAssignOp(s *Stmt) token.Kind

Returns the assignment operator kind (STMT_ASSIGN).

#
func StmtAssignRight(s *Stmt) list[*Expr]

Returns the right-hand side expressions (STMT_ASSIGN).

#
func StmtNames(s *Stmt) list[string]

Returns the declared names (STMT_SHORT_DECL).

#
func StmtValues(s *Stmt) list[*Expr]

Returns the initializer values (STMT_SHORT_DECL, STMT_RETURN).

#
func StmtDeclName(s *Stmt) string

Returns the declared variable/constant name (STMT_VAR_DECL, STMT_CONST_DECL).

#
func StmtDeclType(s *Stmt) *TypeNode

Returns the declared type (STMT_VAR_DECL, STMT_CONST_DECL).

#
func StmtDeclValue(s *Stmt) *Expr

Returns the initializer expression (STMT_VAR_DECL, STMT_CONST_DECL).

#
func StmtCond(s *Stmt) *Expr

Returns the condition expression (STMT_IF, STMT_FOR).

#
func StmtInit(s *Stmt) *Stmt

Returns the init statement (STMT_IF, STMT_FOR, STMT_SWITCH).

#
func StmtThenBlock(s *Stmt) list[*Stmt]

Returns the then-block statements (STMT_IF).

#
func StmtElseBlock(s *Stmt) list[*Stmt]

Returns the else-block statements (STMT_IF).

#
func StmtPost(s *Stmt) *Stmt

Returns the post statement of a for loop (STMT_FOR).

#
func StmtForKey(s *Stmt) string

Returns the key/index variable name in a range loop (STMT_FOR).

#
func StmtForValue(s *Stmt) string

Returns the value variable name in a range loop (STMT_FOR).

#
func StmtRangeExpr(s *Stmt) *Expr

Returns the collection expression in a range loop (STMT_FOR).

#
func StmtForBody(s *Stmt) list[*Stmt]

Returns the loop body statements (STMT_FOR).

#
func StmtSwitchTag(s *Stmt) *Expr

Returns the switch tag expression (STMT_SWITCH).

#
func StmtSwitchCases(s *Stmt) list[SwitchCase]

Returns the case clauses (STMT_SWITCH).

#
func StmtTSBinding(s *Stmt) string

Returns the binding variable name in a type switch (STMT_TYPE_SWITCH).

#
func StmtTSExpr(s *Stmt) *Expr

Returns the expression being switched on in a type switch (STMT_TYPE_SWITCH).

#
func StmtTSCases(s *Stmt) list[TypeSwitchCase]

Returns the type switch case clauses (STMT_TYPE_SWITCH).

#
func StmtIncDecOp(s *Stmt) token.Kind

Returns the operator (INC or DEC) for STMT_INC_DEC.

#
func StmtRemoveMap(s *Stmt) *Expr

Returns the map expression in a remove statement (STMT_REMOVE).

#
func StmtRemoveKeys(s *Stmt) list[*Expr]

Returns the key expressions to remove (STMT_REMOVE).

Decl Accessors

#
func DeclKindOf(d *Decl) DeclKind

Returns the kind discriminant of a declaration node.

#
func DeclPos(d *Decl) token.Pos

Returns the source position of the declaration.

#
func DeclName(d *Decl) string

Returns the declared name.

#
func DeclType(d *Decl) *TypeNode

Returns the declared type (DECL_VAR, DECL_CONST, DECL_TYPE).

#
func DeclValue(d *Decl) *Expr

Returns the initializer expression (DECL_VAR, DECL_CONST).

#
func DeclParams(d *Decl) list[Param]

Returns the parameter list (DECL_FUNC).

#
func DeclReturns(d *Decl) list[*TypeNode]

Returns the return type list (DECL_FUNC).

#
func DeclVariadic(d *Decl) bool

Reports whether the function is variadic (DECL_FUNC).

#
func DeclFuncBody(d *Decl) list[*Stmt]

Returns the function body statements (DECL_FUNC). Nil for extern functions.

#
func DeclFields(d *Decl) list[Field]

Returns the struct fields (DECL_STRUCT).

#
func DeclDirectives(d *Decl) list[Directive]

Returns the directives attached to a declaration.

TypeNode Accessors

#
func TypeNodeKind(t *TypeNode) TypeKind

Returns the kind of the type node.

#
func TypeNodePos(t *TypeNode) token.Pos

Returns the source position of the type.

#
func TypeNodePkg(t *TypeNode) string

Returns the package qualifier for a named type (TYPE_NAME).

#
func TypeNodeName(t *TypeNode) string

Returns the type name (TYPE_NAME).

#
func TypeNodeElem(t *TypeNode) *TypeNode

Returns the element type (TYPE_POINTER, TYPE_ARRAY, TYPE_SLICE, TYPE_LIST, TYPE_MAP).

#
func TypeNodeKey(t *TypeNode) *TypeNode

Returns the key type (TYPE_MAP).

#
func TypeNodeSize(t *TypeNode) *Expr

Returns the array size expression (TYPE_ARRAY).

#
func TypeNodeParams(t *TypeNode) list[*TypeNode]

Returns the parameter types (TYPE_FUNC).

#
func TypeNodeReturns(t *TypeNode) list[*TypeNode]

Returns the return types (TYPE_FUNC).

#
func TypeNodeVariadic(t *TypeNode) bool

Reports whether the function type is variadic (TYPE_FUNC).

#
func TypeNodeFields(t *TypeNode) list[Field]

Returns the struct fields (TYPE_STRUCT).

BuildExpr Accessors

#
func BuildExprKind(b *BuildExpr) BuildKind

Returns the kind of build constraint expression.

#
func BuildExprTag(b *BuildExpr) string

Returns the tag name (BUILD_TAG).

#
func BuildExprOperand(b *BuildExpr) *BuildExpr

Returns the operand (BUILD_NOT).

#
func BuildExprLeft(b *BuildExpr) *BuildExpr

Returns the left operand (BUILD_AND, BUILD_OR).

#
func BuildExprRight(b *BuildExpr) *BuildExpr

Returns the right operand (BUILD_AND, BUILD_OR).

Memory Management

#
func FreeExpr(e *Expr)

Recursively frees an expression node and all its children.

#
func FreeStmt(s *Stmt)

Recursively frees a statement node and all its children.

#
func FreeDecl(d *Decl)

Recursively frees a declaration node and all its children.

#
func FreeFile(f *File)

Frees a parsed file and all its declarations, imports, and directives.