parser

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AliasDecl

type AliasDecl struct {
	Name string
	Type Type
	Span Span
}

AliasDecl represents a type alias declaration.

func (*AliasDecl) Pos

func (a *AliasDecl) Pos() Span

type ArrayType

type ArrayType struct {
	Element Type
	Size    Expr // nil for runtime-sized arrays
	Span    Span
}

ArrayType represents an array type.

func (*ArrayType) Pos

func (a *ArrayType) Pos() Span

type AssignStmt

type AssignStmt struct {
	Left  Expr
	Op    TokenKind // =, +=, -=, etc.
	Right Expr
	Span  Span
}

AssignStmt represents an assignment statement.

func (*AssignStmt) Pos

func (a *AssignStmt) Pos() Span

type Attribute

type Attribute struct {
	Name string
	Args []Expr
	Span Span
}

Attribute represents an attribute (e.g., @location(0)).

type BinaryExpr

type BinaryExpr struct {
	Left  Expr
	Op    TokenKind
	Right Expr
	Span  Span
}

BinaryExpr represents a binary expression.

func (*BinaryExpr) Pos

func (b *BinaryExpr) Pos() Span

type BindingArrayType

type BindingArrayType struct {
	Element Type
	Size    Expr // nil for unbounded
	Span    Span
}

BindingArrayType represents a binding array type (binding_array<T, N>).

func (*BindingArrayType) Pos

func (b *BindingArrayType) Pos() Span

type BitcastExpr

type BitcastExpr struct {
	Type Type // Target type
	Expr Expr // Source expression
	Span Span
}

BitcastExpr represents a bitcast expression: bitcast<TargetType>(expr).

func (*BitcastExpr) Pos

func (b *BitcastExpr) Pos() Span

type BlockStmt

type BlockStmt struct {
	Statements []Stmt
	Span       Span
}

BlockStmt represents a block statement.

func (*BlockStmt) Pos

func (b *BlockStmt) Pos() Span

type BreakIfStmt

type BreakIfStmt struct {
	Condition Expr
	Span      Span
}

BreakIfStmt represents a "break if" statement inside a continuing block. WGSL spec: continuing { ... break if condition; }

func (*BreakIfStmt) Pos

func (b *BreakIfStmt) Pos() Span

type BreakStmt

type BreakStmt struct {
	Span Span
}

BreakStmt represents a break statement.

func (*BreakStmt) Pos

func (b *BreakStmt) Pos() Span

type CallExpr

type CallExpr struct {
	Func *Ident
	Args []Expr
	Span Span
}

CallExpr represents a function call.

func (*CallExpr) Pos

func (c *CallExpr) Pos() Span

type ConstAssertDecl

type ConstAssertDecl struct {
	Condition Expr
	Span      Span
}

ConstAssertDecl represents a const_assert declaration (module or function scope). WGSL spec: const_assert expr; — compile-time assertion.

func (*ConstAssertDecl) Pos

func (c *ConstAssertDecl) Pos() Span

type ConstDecl

type ConstDecl struct {
	Name    string
	Type    Type
	Init    Expr
	Span    Span
	IsConst bool // true for `const`, false for `let`
}

ConstDecl represents a const declaration.

func (*ConstDecl) Pos

func (c *ConstDecl) Pos() Span

type ConstructExpr

type ConstructExpr struct {
	Type Type
	Args []Expr
	Span Span
}

ConstructExpr represents a type constructor expression.

func (*ConstructExpr) Pos

func (c *ConstructExpr) Pos() Span

type ContinueStmt

type ContinueStmt struct {
	Span Span
}

ContinueStmt represents a continue statement.

func (*ContinueStmt) Pos

func (c *ContinueStmt) Pos() Span

type Decl

type Decl interface {
	Node
	// contains filtered or unexported methods
}

Decl is the interface for declarations.

func DependencyOrder

func DependencyOrder(decls []Decl) []Decl

DependencyOrder returns declarations sorted in dependency order using DFS-based topological sort. This matches Rust naga's visit_ordered() from front/wgsl/index.rs — declarations are ordered so that every declaration appears after all declarations it references.

When there are no dependencies between declarations, they appear in source order (the outer DFS loop iterates in original order).

type Diagnostic

type Diagnostic struct {
	Severity string
	Rule     string
	Span     Span
}

Diagnostic represents a diagnostic directive.

type DiscardStmt

type DiscardStmt struct {
	Span Span
}

DiscardStmt represents a discard statement.

func (*DiscardStmt) Pos

func (d *DiscardStmt) Pos() Span

type Enable

type Enable struct {
	Extensions []string
	Span       Span
}

Enable represents an enable directive.

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr is the interface for expressions.

type ExprStmt

type ExprStmt struct {
	Expr Expr
	Span Span
}

ExprStmt represents an expression statement.

func (*ExprStmt) Pos

func (e *ExprStmt) Pos() Span

type ForStmt

type ForStmt struct {
	Init      Stmt
	Condition Expr
	Update    Stmt
	Body      *BlockStmt
	Span      Span
}

ForStmt represents a for loop.

func (*ForStmt) Pos

func (f *ForStmt) Pos() Span

type FunctionDecl

type FunctionDecl struct {
	Name        string
	Params      []*Parameter
	ReturnType  Type
	ReturnAttrs []Attribute // Attributes on return type (e.g., @builtin(position), @location(0))
	Attributes  []Attribute
	Body        *BlockStmt
	Span        Span
}

FunctionDecl represents a function declaration.

func (*FunctionDecl) Pos

func (f *FunctionDecl) Pos() Span

type Ident

type Ident struct {
	Name string
	Span Span
}

Ident represents an identifier.

func (*Ident) Pos

func (i *Ident) Pos() Span

type IfStmt

type IfStmt struct {
	Condition Expr
	Body      *BlockStmt
	Else      Stmt // *BlockStmt or *IfStmt
	Span      Span
}

IfStmt represents an if statement.

func (*IfStmt) Pos

func (i *IfStmt) Pos() Span

type IndexExpr

type IndexExpr struct {
	Expr  Expr
	Index Expr
	Span  Span
}

IndexExpr represents an index expression.

func (*IndexExpr) Pos

func (i *IndexExpr) Pos() Span

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes WGSL source code.

func NewLexer

func NewLexer(source string) *Lexer

NewLexer creates a new lexer for the given source.

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() ([]Token, error)

Tokenize returns all tokens from the source.

type Literal

type Literal struct {
	Kind  TokenKind // IntLiteral, FloatLiteral, BoolLiteral
	Value string
	Span  Span
}

Literal represents a literal value.

func (*Literal) Pos

func (l *Literal) Pos() Span

type LoopStmt

type LoopStmt struct {
	Body       *BlockStmt
	Continuing *BlockStmt
	Span       Span
}

LoopStmt represents a loop statement.

func (*LoopStmt) Pos

func (l *LoopStmt) Pos() Span

type MemberExpr

type MemberExpr struct {
	Expr   Expr
	Member string
	Span   Span
}

MemberExpr represents a member access expression.

func (*MemberExpr) Pos

func (m *MemberExpr) Pos() Span

type Module

type Module struct {
	Enables     []Enable
	Diagnostics []Diagnostic
	Structs     []*StructDecl
	Functions   []*FunctionDecl
	GlobalVars  []*VarDecl
	Aliases     []*AliasDecl
	Constants   []*ConstDecl
	Overrides   []*OverrideDecl

	// Declarations preserves all top-level declarations in source order.
	// This is used by the lowerer to register types in the same order as
	// Rust naga, which processes declarations via topological sort that
	// closely follows source order.
	Declarations []Decl
}

Module represents a WGSL module (translation unit).

type NamedType

type NamedType struct {
	Name       string
	TypeParams []Type
	Span       Span
}

NamedType represents a named type (e.g., f32, vec3<f32>).

func (*NamedType) Pos

func (n *NamedType) Pos() Span

type Node

type Node interface {
	Pos() Span
}

Node is the base interface for all AST nodes.

type OverrideDecl

type OverrideDecl struct {
	Name       string
	Type       Type
	Init       Expr // May be nil (override without default value)
	Attributes []Attribute
	Span       Span
}

OverrideDecl represents an override declaration (pipeline-overridable constant). WGSL spec: @id(N) override name: type = default;

func (*OverrideDecl) Pos

func (o *OverrideDecl) Pos() Span

type Parameter

type Parameter struct {
	Name       string
	Type       Type
	Attributes []Attribute
	Span       Span
}

Parameter represents a function parameter.

type ParseError

type ParseError struct {
	Message string
	Token   Token
}

ParseError represents a parsing error.

func (ParseError) Error

func (e ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses WGSL tokens into an AST.

func NewParser

func NewParser(tokens []Token) *Parser

NewParser creates a new parser for the given tokens.

func (*Parser) Parse

func (p *Parser) Parse() (*Module, error)

Parse parses the tokens and returns a Module AST.

type Position

type Position struct {
	Line   int
	Column int
	Offset int
}

Position represents a position in source code.

type PtrType

type PtrType struct {
	AddressSpace string
	PointeeType  Type
	AccessMode   string
	Span         Span
}

PtrType represents a pointer type.

func (*PtrType) Pos

func (p *PtrType) Pos() Span

type ReturnStmt

type ReturnStmt struct {
	Value Expr
	Span  Span
}

ReturnStmt represents a return statement.

func (*ReturnStmt) Pos

func (r *ReturnStmt) Pos() Span

type SourceError

type SourceError struct {
	Message string
	Span    Span
	Source  string // Original source code (for context display)
}

SourceError represents an error with source location information.

func NewSourceError

func NewSourceError(message string, span Span, source string) *SourceError

NewSourceError creates a new SourceError.

func NewSourceErrorf

func NewSourceErrorf(span Span, source string, format string, args ...interface{}) *SourceError

NewSourceErrorf creates a new SourceError with formatted message.

func (*SourceError) Error

func (e *SourceError) Error() string

Error implements the error interface.

func (*SourceError) FormatWithContext

func (e *SourceError) FormatWithContext() string

FormatWithContext returns the error message with source context. Shows the problematic line with a caret pointing to the error location.

type SourceErrors

type SourceErrors []*SourceError

SourceErrors represents a list of source errors.

func (*SourceErrors) Add

func (el *SourceErrors) Add(err *SourceError)

Add adds an error to the list.

func (*SourceErrors) AddError

func (el *SourceErrors) AddError(message string, span Span, source string)

AddError adds an error with the given message and span.

func (SourceErrors) Error

func (el SourceErrors) Error() string

func (SourceErrors) FormatAll

func (el SourceErrors) FormatAll() string

FormatAll returns all errors formatted with context.

func (SourceErrors) HasErrors

func (el SourceErrors) HasErrors() bool

HasErrors returns true if there are any errors.

func (SourceErrors) Len

func (el SourceErrors) Len() int

Len returns the number of errors.

type Span

type Span struct {
	Start  Position
	End    Position
	Source string // Source file name or identifier
}

Span represents a source code location span.

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

Stmt is the interface for statements.

type StructDecl

type StructDecl struct {
	Name    string
	Members []*StructMember
	Span    Span
}

StructDecl represents a struct declaration.

func (*StructDecl) Pos

func (s *StructDecl) Pos() Span

type StructMember

type StructMember struct {
	Name       string
	Type       Type
	Attributes []Attribute
	Span       Span
}

StructMember represents a struct member.

type SwitchCaseClause

type SwitchCaseClause struct {
	Selectors    []Expr     // Case selectors (nil or empty for default)
	IsDefault    bool       // True for default case
	DefaultFirst bool       // True when default appears before selectors (case default, 6:)
	Body         *BlockStmt // Case body
	Span         Span
}

SwitchCaseClause represents a case clause in a switch statement.

type SwitchStmt

type SwitchStmt struct {
	Selector Expr
	Cases    []*SwitchCaseClause
	Span     Span
}

SwitchStmt represents a switch statement.

func (*SwitchStmt) Pos

func (s *SwitchStmt) Pos() Span

type Token

type Token struct {
	Kind   TokenKind
	Lexeme string
	Line   int
	Column int
}

Token represents a lexical token.

type TokenKind

type TokenKind uint8

TokenKind represents the type of token.

const (
	TokenEOF TokenKind = iota
	TokenError

	// Literals
	TokenIdent
	TokenIntLiteral
	TokenFloatLiteral
	TokenBoolLiteral

	// Operators
	TokenPlus                // +
	TokenMinus               // -
	TokenStar                // *
	TokenSlash               // /
	TokenPercent             // %
	TokenAmpersand           // &
	TokenPipe                // |
	TokenCaret               // ^
	TokenTilde               // ~
	TokenBang                // !
	TokenEqual               // =
	TokenLess                // <
	TokenGreater             // >
	TokenDot                 // .
	TokenComma               // ,
	TokenColon               // :
	TokenSemicolon           // ;
	TokenAt                  // @
	TokenArrow               // ->
	TokenPlusPlus            // ++
	TokenMinusMinus          // --
	TokenEqualEqual          // ==
	TokenBangEqual           // !=
	TokenLessEqual           // <=
	TokenGreaterEqual        // >=
	TokenAmpAmp              // &&
	TokenPipePipe            // ||
	TokenLessLess            // <<
	TokenGreaterGreater      // >>
	TokenPlusEqual           // +=
	TokenMinusEqual          // -=
	TokenStarEqual           // *=
	TokenSlashEqual          // /=
	TokenPercentEqual        // %=
	TokenAmpEqual            // &=
	TokenPipeEqual           // |=
	TokenCaretEqual          // ^=
	TokenLessLessEqual       // <<=
	TokenGreaterGreaterEqual // >>=

	// Delimiters
	TokenLeftParen    // (
	TokenRightParen   // )
	TokenLeftBrace    // {
	TokenRightBrace   // }
	TokenLeftBracket  // [
	TokenRightBracket // ]

	// Keywords
	TokenAlias
	TokenBreak
	TokenCase
	TokenConst
	TokenConstAssert
	TokenContinue
	TokenContinuing
	TokenDefault
	TokenDiagnostic
	TokenDiscard
	TokenElse
	TokenEnable
	TokenFalse
	TokenFn
	TokenFor
	TokenIf
	TokenLet
	TokenLoop
	TokenOverride
	TokenReturn
	TokenStruct
	TokenSwitch
	TokenTrue
	TokenVar
	TokenWhile

	// Reserved keywords
	TokenNull
	TokenSelf
	TokenSuper
	TokenTrait
	TokenType
	TokenUsing

	// Type keywords
	TokenBool
	TokenF16
	TokenF32
	TokenF64
	TokenI32
	TokenI64
	TokenU32
	TokenU64
	TokenVec2
	TokenVec3
	TokenVec4
	TokenMat2x2
	TokenMat2x3
	TokenMat2x4
	TokenMat3x2
	TokenMat3x3
	TokenMat3x4
	TokenMat4x2
	TokenMat4x3
	TokenMat4x4
	TokenArray
	TokenAtomic
	TokenPtr
	TokenSampler
	TokenSamplerComparison
	TokenTexture1d
	TokenTexture2d
	TokenTexture2dArray
	TokenTexture3d
	TokenTextureCube
	TokenTextureCubeArray
	TokenTextureMultisampled2d
	TokenTextureStorage1d
	TokenTextureStorage2d
	TokenTextureStorage2dArray
	TokenTextureStorage3d
	TokenTextureDepth2d
	TokenTextureDepth2dArray
	TokenTextureDepthCube
	TokenTextureDepthCubeArray
	TokenTextureDepthMultisampled2d
)

func (TokenKind) String

func (k TokenKind) String() string

String returns the string representation of the token kind.

type Type

type Type interface {
	Node
	// contains filtered or unexported methods
}

Type represents a type.

type UnaryExpr

type UnaryExpr struct {
	Op      TokenKind
	Operand Expr
	Span    Span
}

UnaryExpr represents a unary expression.

func (*UnaryExpr) Pos

func (u *UnaryExpr) Pos() Span

type VarDecl

type VarDecl struct {
	Name         string
	Type         Type
	Init         Expr
	AddressSpace string // function, private, workgroup, uniform, storage
	AccessMode   string // read, write, read_write
	Attributes   []Attribute
	Span         Span
}

VarDecl represents a variable declaration.

func (*VarDecl) Pos

func (v *VarDecl) Pos() Span

type WhileStmt

type WhileStmt struct {
	Condition Expr
	Body      *BlockStmt
	Span      Span
}

WhileStmt represents a while loop.

func (*WhileStmt) Pos

func (w *WhileStmt) Pos() Span

Jump to

Keyboard shortcuts

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