parser

package
v0.4.23 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

pkg/parser/ast.go

pkg/parser/parser.go

Index

Constants

View Source
const (
	LOWEST      int // lowest precedence for expression parsing
	TERNARY         // ? : (right-associative)
	ASSIGN          // =, +=, -= (right-associative, binds after LOWEST)
	OR              // ||
	AND             // &&
	EQUALS          // ==, !=
	LESSGREATER     // <, >, <=, >=
	SUM             // +, -
	PRODUCT         // *, /, %
	PREFIX          // !, -
	CALL            // fn()
	INDEX           // arr[i]
	DOT             // obj.field
)

Operator precedence constants

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayLiteral

type ArrayLiteral struct {
	Token    lexer.Token // the '[' token
	Elements []Expression
}

ArrayLiteral represents an array literal expression

func (*ArrayLiteral) String

func (al *ArrayLiteral) String() string

String returns a string representation of the array literal

func (*ArrayLiteral) TokenLiteral

func (al *ArrayLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type ArrowFunctionLiteral

type ArrowFunctionLiteral struct {
	Token      lexer.Token // The '=>' token
	Parameters []*Identifier
	Body       *BlockStatement
	Expression Expression // For single expression arrow functions
}

ArrowFunctionLiteral represents an arrow function expression

func (*ArrowFunctionLiteral) String

func (afl *ArrowFunctionLiteral) String() string

String returns a string representation of the arrow function literal

func (*ArrowFunctionLiteral) TokenLiteral

func (afl *ArrowFunctionLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type AssignmentExpression

type AssignmentExpression struct {
	Token lexer.Token // The '=' token
	Left  Expression
	Value Expression
}

AssignmentExpression represents an assignment expression

func (*AssignmentExpression) String

func (ae *AssignmentExpression) String() string

String returns a string representation of the assignment expression

func (*AssignmentExpression) TokenLiteral

func (ae *AssignmentExpression) TokenLiteral() string

TokenLiteral returns the token literal

type BlockStatement

type BlockStatement struct {
	Token      lexer.Token // the '{' token
	Statements []Statement
}

BlockStatement represents a block of statements

func (*BlockStatement) String

func (bs *BlockStatement) String() string

String returns a string representation of the block statement

func (*BlockStatement) TokenLiteral

func (bs *BlockStatement) TokenLiteral() string

TokenLiteral returns the token literal

type BooleanLiteral

type BooleanLiteral struct {
	Token lexer.Token
	Value bool
}

BooleanLiteral represents a boolean literal expression

func (*BooleanLiteral) String

func (bl *BooleanLiteral) String() string

String returns a string representation of the boolean literal

func (*BooleanLiteral) TokenLiteral

func (bl *BooleanLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type BreakStatement

type BreakStatement struct {
	Token lexer.Token // the 'break' token
}

BreakStatement represents a break statement

func (*BreakStatement) String

func (bs *BreakStatement) String() string

String returns a string representation of the break statement

func (*BreakStatement) TokenLiteral

func (bs *BreakStatement) TokenLiteral() string

TokenLiteral returns the token literal

type CallExpression

type CallExpression struct {
	Token     lexer.Token // The '(' token
	Function  Expression  // Identifier or FunctionLiteral
	Arguments []Expression
}

CallExpression represents a function call expression

func (*CallExpression) String

func (ce *CallExpression) String() string

String returns a string representation of the call expression

func (*CallExpression) TokenLiteral

func (ce *CallExpression) TokenLiteral() string

TokenLiteral returns the token literal

type CaseStatement

type CaseStatement struct {
	Token       lexer.Token // The 'case' token
	Expression  Expression
	Consequence *BlockStatement
}

CaseStatement represents a case statement in switch

func (*CaseStatement) String

func (cs *CaseStatement) String() string

String returns a string representation of the case statement

func (*CaseStatement) TokenLiteral

func (cs *CaseStatement) TokenLiteral() string

TokenLiteral returns the token literal

type CatchStatement

type CatchStatement struct {
	Token     lexer.Token // The 'catch' token
	Exception *Identifier
	Block     *BlockStatement
}

CatchStatement represents a catch statement

func (*CatchStatement) String

func (cs *CatchStatement) String() string

String returns a string representation of the catch statement

func (*CatchStatement) TokenLiteral

func (cs *CatchStatement) TokenLiteral() string

TokenLiteral returns the token literal

type ClassStatement

type ClassStatement struct {
	Token      lexer.Token // The 'class' token
	Name       *Identifier
	SuperClass *Identifier
	Methods    []*FunctionLiteral
	Fields     []*VarStatement
}

ClassStatement represents a class declaration

func (*ClassStatement) String

func (cs *ClassStatement) String() string

String returns a string representation of the class statement

func (*ClassStatement) TokenLiteral

func (cs *ClassStatement) TokenLiteral() string

TokenLiteral returns the token literal

type CompoundAssignmentExpression

type CompoundAssignmentExpression struct {
	Token    lexer.Token // The operator token, e.g. +=, -=
	Left     Expression
	Operator string
	Right    Expression
}

CompoundAssignmentExpression represents compound assignment operators (+=, -=, etc.)

func (*CompoundAssignmentExpression) String

func (cae *CompoundAssignmentExpression) String() string

String returns a string representation of the compound assignment expression

func (*CompoundAssignmentExpression) TokenLiteral

func (cae *CompoundAssignmentExpression) TokenLiteral() string

TokenLiteral returns the token literal

type ConstStatement

type ConstStatement struct {
	Token lexer.Token // the 'const' token
	Name  *Identifier
	Value Expression
}

ConstStatement represents a constant declaration statement

func (*ConstStatement) String

func (cs *ConstStatement) String() string

String returns a string representation of the const statement

func (*ConstStatement) TokenLiteral

func (cs *ConstStatement) TokenLiteral() string

TokenLiteral returns the token literal

type ContinueStatement

type ContinueStatement struct {
	Token lexer.Token // the 'continue' token
}

ContinueStatement represents a continue statement

func (*ContinueStatement) String

func (cs *ContinueStatement) String() string

String returns a string representation of the continue statement

func (*ContinueStatement) TokenLiteral

func (cs *ContinueStatement) TokenLiteral() string

TokenLiteral returns the token literal

type DefaultStatement

type DefaultStatement struct {
	Token       lexer.Token // The 'default' token
	Consequence *BlockStatement
}

DefaultStatement represents a default statement in switch

func (*DefaultStatement) String

func (ds *DefaultStatement) String() string

String returns a string representation of the default statement

func (*DefaultStatement) TokenLiteral

func (ds *DefaultStatement) TokenLiteral() string

TokenLiteral returns the token literal

type DotExpression

type DotExpression struct {
	Token    lexer.Token // The '.' token
	Object   Expression
	Property *Identifier
}

DotExpression represents a dot expression for property access

func (*DotExpression) String

func (de *DotExpression) String() string

String returns a string representation of the dot expression

func (*DotExpression) TokenLiteral

func (de *DotExpression) TokenLiteral() string

TokenLiteral returns the token literal

type ExportStatement

type ExportStatement struct {
	Token      lexer.Token // The 'export' token
	Exportable Statement
}

ExportStatement represents an export statement

func (*ExportStatement) String

func (es *ExportStatement) String() string

String returns a string representation of the export statement

func (*ExportStatement) TokenLiteral

func (es *ExportStatement) TokenLiteral() string

TokenLiteral returns the token literal

type Expression

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

Expression represents an expression node

type ExpressionStatement

type ExpressionStatement struct {
	Token      lexer.Token // the first token of the expression
	Expression Expression
}

ExpressionStatement represents an expression statement

func (*ExpressionStatement) String

func (es *ExpressionStatement) String() string

String returns a string representation of the expression statement

func (*ExpressionStatement) TokenLiteral

func (es *ExpressionStatement) TokenLiteral() string

TokenLiteral returns the token literal

type FinallyStatement

type FinallyStatement struct {
	Token lexer.Token // The 'finally' token
	Block *BlockStatement
}

FinallyStatement represents a finally statement

func (*FinallyStatement) String

func (fs *FinallyStatement) String() string

String returns a string representation of the finally statement

func (*FinallyStatement) TokenLiteral

func (fs *FinallyStatement) TokenLiteral() string

TokenLiteral returns the token literal

type FloatLiteral

type FloatLiteral struct {
	Token lexer.Token
	Value float64
}

FloatLiteral represents a float literal expression

func (*FloatLiteral) String

func (fl *FloatLiteral) String() string

String returns a string representation of the float literal

func (*FloatLiteral) TokenLiteral

func (fl *FloatLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type ForInStatement

type ForInStatement struct {
	Token    lexer.Token // The 'for' token
	Key      *Identifier
	Value    *Identifier
	Iterable Expression
	Body     *BlockStatement
}

ForInStatement represents a for-in loop statement

func (*ForInStatement) String

func (fis *ForInStatement) String() string

String returns a string representation of the for-in statement

func (*ForInStatement) TokenLiteral

func (fis *ForInStatement) TokenLiteral() string

TokenLiteral returns the token literal

type ForStatement

type ForStatement struct {
	Token     lexer.Token // the 'for' token
	Init      Statement
	Condition Expression
	Update    Statement
	Body      *BlockStatement
}

ForStatement represents a for statement

func (*ForStatement) String

func (fs *ForStatement) String() string

String returns a string representation of the for statement

func (*ForStatement) TokenLiteral

func (fs *ForStatement) TokenLiteral() string

TokenLiteral returns the token literal

type FunctionLiteral

type FunctionLiteral struct {
	Token      lexer.Token // The 'func' token
	Name       string      // Optional: named function
	Parameters []*Identifier
	Body       *BlockStatement
}

FunctionLiteral represents a function literal expression

func (*FunctionLiteral) String

func (fl *FunctionLiteral) String() string

String returns a string representation of the function literal

func (*FunctionLiteral) TokenLiteral

func (fl *FunctionLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type Identifier

type Identifier struct {
	Token lexer.Token // the token.IDENT token
	Value string
}

Identifier represents an identifier expression

func (*Identifier) String

func (i *Identifier) String() string

String returns a string representation of the identifier

func (*Identifier) TokenLiteral

func (i *Identifier) TokenLiteral() string

TokenLiteral returns the token literal

type IfStatement

type IfStatement struct {
	Token       lexer.Token // the 'if' token
	Condition   Expression
	Consequence *BlockStatement
	Alternative *BlockStatement
}

IfStatement represents an if statement

func (*IfStatement) String

func (is *IfStatement) String() string

String returns a string representation of the if statement

func (*IfStatement) TokenLiteral

func (is *IfStatement) TokenLiteral() string

TokenLiteral returns the token literal

type ImportStatement

type ImportStatement struct {
	Token lexer.Token    // The 'import' token
	Name  *Identifier    // Default import name (import math from ...)
	Path  *StringLiteral // Module path
	Alias *Identifier    // Namespace alias (import * as math from ...)
	Names []*Identifier  // Destructured names (import { add, sub } from ...)
}

ImportStatement represents an import statement

func (*ImportStatement) String

func (is *ImportStatement) String() string

String returns a string representation of the import statement

func (*ImportStatement) TokenLiteral

func (is *ImportStatement) TokenLiteral() string

TokenLiteral returns the token literal

type IndexExpression

type IndexExpression struct {
	Token lexer.Token // The '[' token
	Left  Expression
	Index Expression
}

IndexExpression represents an index expression

func (*IndexExpression) String

func (ie *IndexExpression) String() string

String returns a string representation of the index expression

func (*IndexExpression) TokenLiteral

func (ie *IndexExpression) TokenLiteral() string

TokenLiteral returns the token literal

type InfixExpression

type InfixExpression struct {
	Token    lexer.Token // The operator token, e.g. +, -
	Left     Expression
	Operator string
	Right    Expression
}

InfixExpression represents an infix expression

func (*InfixExpression) String

func (ie *InfixExpression) String() string

String returns a string representation of the infix expression

func (*InfixExpression) TokenLiteral

func (ie *InfixExpression) TokenLiteral() string

TokenLiteral returns the token literal

type IntegerLiteral

type IntegerLiteral struct {
	Token lexer.Token
	Value int64
}

IntegerLiteral represents an integer literal expression

func (*IntegerLiteral) String

func (il *IntegerLiteral) String() string

String returns a string representation of the integer literal

func (*IntegerLiteral) TokenLiteral

func (il *IntegerLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type MapLiteral

type MapLiteral struct {
	Token lexer.Token // the '{' token
	Pairs map[Expression]Expression
}

MapLiteral represents a map literal expression

func (*MapLiteral) String

func (ml *MapLiteral) String() string

String returns a string representation of the map literal

func (*MapLiteral) TokenLiteral

func (ml *MapLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type NewExpression

type NewExpression struct {
	Token     lexer.Token // The 'new' token
	Class     Expression
	Arguments []Expression
}

NewExpression represents a new expression for object instantiation

func (*NewExpression) String

func (ne *NewExpression) String() string

String returns a string representation of the new expression

func (*NewExpression) TokenLiteral

func (ne *NewExpression) TokenLiteral() string

TokenLiteral returns the token literal

type Node

type Node interface {
	TokenLiteral() string
	String() string
}

Node represents a node in the Abstract Syntax Tree

type NullLiteral

type NullLiteral struct {
	Token lexer.Token
}

NullLiteral represents a null literal expression

func (*NullLiteral) String

func (nl *NullLiteral) String() string

String returns a string representation of the null literal

func (*NullLiteral) TokenLiteral

func (nl *NullLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type Parser

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

Parser parses tokens into an AST

func New

func New(l *lexer.Lexer) *Parser

New creates a new Parser instance

func (*Parser) Errors

func (p *Parser) Errors() []string

Errors returns all parser errors

func (*Parser) ParseProgram

func (p *Parser) ParseProgram() *Program

ParseProgram parses the entire program

type PostfixExpression

type PostfixExpression struct {
	Token    lexer.Token // The operator token, e.g. ++, --
	Left     Expression
	Operator string
}

PostfixExpression represents a postfix expression (++, --)

func (*PostfixExpression) String

func (pe *PostfixExpression) String() string

String returns a string representation of the postfix expression

func (*PostfixExpression) TokenLiteral

func (pe *PostfixExpression) TokenLiteral() string

TokenLiteral returns the token literal

type PrefixExpression

type PrefixExpression struct {
	Token    lexer.Token // The prefix token, e.g. !, -
	Operator string
	Right    Expression
}

PrefixExpression represents a prefix expression

func (*PrefixExpression) String

func (pe *PrefixExpression) String() string

String returns a string representation of the prefix expression

func (*PrefixExpression) TokenLiteral

func (pe *PrefixExpression) TokenLiteral() string

TokenLiteral returns the token literal

type Program

type Program struct {
	Statements []Statement
}

Program is the root node of every AST

func (*Program) String

func (p *Program) String() string

String returns a string representation of the program

func (*Program) TokenLiteral

func (p *Program) TokenLiteral() string

TokenLiteral returns the token literal of the first statement

type ReturnStatement

type ReturnStatement struct {
	Token       lexer.Token // the 'return' token
	ReturnValue Expression
}

ReturnStatement represents a return statement

func (*ReturnStatement) String

func (rs *ReturnStatement) String() string

String returns a string representation of the return statement

func (*ReturnStatement) TokenLiteral

func (rs *ReturnStatement) TokenLiteral() string

TokenLiteral returns the token literal

type SpreadExpression

type SpreadExpression struct {
	Token      lexer.Token // The '...' token
	Expression Expression
}

SpreadExpression represents spread operator (...)

func (*SpreadExpression) String

func (se *SpreadExpression) String() string

String returns a string representation of the spread expression

func (*SpreadExpression) TokenLiteral

func (se *SpreadExpression) TokenLiteral() string

TokenLiteral returns the token literal

type Statement

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

Statement represents a statement node

type StringLiteral

type StringLiteral struct {
	Token lexer.Token
	Value string
}

StringLiteral represents a string literal expression

func (*StringLiteral) String

func (sl *StringLiteral) String() string

String returns a string representation of the string literal

func (*StringLiteral) TokenLiteral

func (sl *StringLiteral) TokenLiteral() string

TokenLiteral returns the token literal

type SuperCallExpression

type SuperCallExpression struct {
	Token  lexer.Token // The 'super' token
	Method string
	Args   []Expression
}

SuperCallExpression represents a super.method() call

func (*SuperCallExpression) String

func (sc *SuperCallExpression) String() string

func (*SuperCallExpression) TokenLiteral

func (sc *SuperCallExpression) TokenLiteral() string

type SuperExpression

type SuperExpression struct {
	Token lexer.Token // The 'super' token
}

SuperExpression represents the 'super' keyword

func (*SuperExpression) String

func (se *SuperExpression) String() string

func (*SuperExpression) TokenLiteral

func (se *SuperExpression) TokenLiteral() string

type SwitchStatement

type SwitchStatement struct {
	Token      lexer.Token // The 'switch' token
	Expression Expression
	Cases      []*CaseStatement
	Default    *DefaultStatement
}

SwitchStatement represents a switch statement

func (*SwitchStatement) String

func (ss *SwitchStatement) String() string

String returns a string representation of the switch statement

func (*SwitchStatement) TokenLiteral

func (ss *SwitchStatement) TokenLiteral() string

TokenLiteral returns the token literal

type TernaryExpression

type TernaryExpression struct {
	Token       lexer.Token // The '?' token
	Condition   Expression
	Consequent  Expression
	Alternative Expression
}

TernaryExpression represents a ternary conditional expression

func (*TernaryExpression) String

func (te *TernaryExpression) String() string

String returns a string representation of the ternary expression

func (*TernaryExpression) TokenLiteral

func (te *TernaryExpression) TokenLiteral() string

TokenLiteral returns the token literal

type ThisExpression

type ThisExpression struct {
	Token lexer.Token // The 'this' token
}

ThisExpression represents the 'this' keyword

func (*ThisExpression) String

func (te *ThisExpression) String() string

String returns a string representation of the this expression

func (*ThisExpression) TokenLiteral

func (te *ThisExpression) TokenLiteral() string

TokenLiteral returns the token literal

type ThrowStatement

type ThrowStatement struct {
	Token   lexer.Token // The 'throw' token
	ErrExpr Expression
}

ThrowStatement represents a throw statement

func (*ThrowStatement) String

func (ts *ThrowStatement) String() string

String returns a string representation of the throw statement

func (*ThrowStatement) TokenLiteral

func (ts *ThrowStatement) TokenLiteral() string

TokenLiteral returns the token literal

type TryStatement

type TryStatement struct {
	Token   lexer.Token // The 'try' token
	Block   *BlockStatement
	Catch   *CatchStatement
	Finally *FinallyStatement
}

TryStatement represents a try-catch-finally statement

func (*TryStatement) String

func (ts *TryStatement) String() string

String returns a string representation of the try statement

func (*TryStatement) TokenLiteral

func (ts *TryStatement) TokenLiteral() string

TokenLiteral returns the token literal

type VarStatement

type VarStatement struct {
	Token lexer.Token // the 'var' token
	Name  *Identifier
	Value Expression
}

VarStatement represents a variable declaration statement

func (*VarStatement) String

func (vs *VarStatement) String() string

String returns a string representation of the var statement

func (*VarStatement) TokenLiteral

func (vs *VarStatement) TokenLiteral() string

TokenLiteral returns the token literal

type WhileStatement

type WhileStatement struct {
	Token     lexer.Token // the 'while' token
	Condition Expression
	Body      *BlockStatement
}

WhileStatement represents a while statement

func (*WhileStatement) String

func (ws *WhileStatement) String() string

String returns a string representation of the while statement

func (*WhileStatement) TokenLiteral

func (ws *WhileStatement) TokenLiteral() string

TokenLiteral returns the token literal

Jump to

Keyboard shortcuts

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