parser

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LOWEST      = 0
	LAMBDA      = 1  // \x. (lowest precedence)
	LogicalOr   = 2  // ||
	LogicalAnd  = 3  // &&
	BitwiseXor  = 5  // ^   (4 reserved for future bitwise OR |)
	BitwiseAnd  = 6  // &
	EQUALS      = 7  // ==, !=
	LESSGREATER = 8  // >, <, >=, <=
	SHIFT       = 9  // <<, >>
	CONS        = 10 // :: (list cons - right associative)
	APPEND      = 11 // ++
	SUM         = 12 // +, -
	PRODUCT     = 13 // *, /, %
	PREFIX      = 14 // -x, !x, ~x (unary)
	CALL        = 15 // f(x) (application)
	DotAccess   = 16 // r.field (field access - highest)
	HIGHEST     = 17
)

Precedence levels — C-standard dedicated bands. These MUST match the values returned by token.Precedence().

Variables

This section is empty.

Functions

func AssertDeclCount

func AssertDeclCount(t *testing.T, file *ast.File, expected int)

AssertDeclCount verifies that a file has the expected number of declarations.

Example:

file := p.ParseFile()
AssertDeclCount(t, file, 3)

func AssertErrorCount

func AssertErrorCount(t *testing.T, p *Parser, expected int)

AssertErrorCount verifies that the parser has exactly the expected number of errors.

Example:

p := New(lexer.New("invalid syntax"))
p.ParseFile()
AssertErrorCount(t, p, 1)

func AssertFuncCall

func AssertFuncCall(t *testing.T, expr ast.Expr) *ast.FuncCall

AssertFuncCall verifies that an expression is a function call and returns it for further inspection.

Example:

expr := p.parseExpression(LOWEST)
call := AssertFuncCall(t, expr)
if call != nil {
	AssertIdentifier(t, call.Func, "add")
	if len(call.Args) != 2 { ... }
}

func AssertFuncDecl

func AssertFuncDecl(t *testing.T, node ast.Node, expectedName string) *ast.FuncDecl

AssertFuncDecl verifies that a node is a function declaration and returns it.

Example:

file := p.ParseFile()
fn := AssertFuncDecl(t, file.Decls[0], "factorial")
if fn != nil {
	// Check function properties
}

func AssertIdentifier

func AssertIdentifier(t *testing.T, expr ast.Expr, expectedName string)

AssertIdentifier verifies that an expression is an identifier with the expected name.

Example:

expr := p.parseExpression(LOWEST)
AssertIdentifier(t, expr, "x")

func AssertList

func AssertList(t *testing.T, expr ast.Expr) *ast.List

AssertList verifies that an expression is a list and returns it for further inspection.

Example:

expr := p.parseExpression(LOWEST)
list := AssertList(t, expr)
if list != nil {
	if len(list.Elements) != 3 { ... }
}

func AssertListLength

func AssertListLength(t *testing.T, expr ast.Expr, expectedLen int) *ast.List

AssertListLength verifies that an expression is a list with the expected length.

func AssertListType

func AssertListType(t *testing.T, typ ast.Type) ast.Type

AssertListType verifies that a type is a list type and returns the element type. DX-17 Phase 2: [T] syntax now parses to TypeApp("list", [T])

func AssertLiteralBool

func AssertLiteralBool(t *testing.T, expr ast.Expr, expected bool)

AssertLiteralBool verifies that an expression is a boolean literal with the expected value.

func AssertLiteralFloat

func AssertLiteralFloat(t *testing.T, expr ast.Expr, expected float64)

AssertLiteralFloat verifies that an expression is a float literal with the expected value.

func AssertLiteralInt

func AssertLiteralInt(t *testing.T, expr ast.Expr, expected int)

AssertLiteralInt verifies that an expression is an integer literal with the expected value.

Example:

expr := p.parseExpression(LOWEST)
AssertLiteralInt(t, expr, 42)

func AssertLiteralString

func AssertLiteralString(t *testing.T, expr ast.Expr, expected string)

AssertLiteralString verifies that an expression is a string literal with the expected value.

func AssertNoErrors

func AssertNoErrors(t *testing.T, p *Parser)

AssertNoErrors verifies that the parser has no errors. If there are errors, it prints them before failing the test.

Example:

p := New(lexer.New(input))
file := p.ParseFile()
AssertNoErrors(t, p)

func AssertSimpleType

func AssertSimpleType(t *testing.T, typ ast.Type, expectedName string)

AssertSimpleType verifies that a type is a simple type with the expected name.

func AssertTokenPosition

func AssertTokenPosition(t *testing.T, p *Parser, expectedCur, expectedPeek lexer.TokenType)

AssertTokenPosition helps debug token position issues in tests. It verifies that the parser is at the expected current and peek tokens.

Example:

p.nextToken()
expr := p.parseExpression(LOWEST)
AssertTokenPosition(t, p, lexer.INT, lexer.COMMA)  // cur=INT, peek=COMMA

func AssertTypeDecl

func AssertTypeDecl(t *testing.T, node ast.Node, expectedName string) *ast.TypeDecl

AssertTypeDecl verifies that a node is a type declaration and returns it.

Types

type Parser

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

Parser parses AILANG source code into an AST

func New

func New(l *lexer.Lexer) *Parser

New creates a new Parser

func (*Parser) Errors

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

Errors returns parser errors

func (*Parser) Parse

func (p *Parser) Parse() *ast.Program

Parse parses the input and returns an AST

func (*Parser) ParseFile

func (p *Parser) ParseFile() (file *ast.File)

ParseFile parses a complete AILANG source file

func (*Parser) SetStrictSyntaxMode

func (p *Parser) SetStrictSyntaxMode(strict bool)

SetStrictSyntaxMode enables or disables strict syntax mode. When strict mode is enabled, all syntactic sugar is rejected with helpful errors.

func (*Parser) SugarUsed

func (p *Parser) SugarUsed() bool

SugarUsed returns true if any syntactic sugar was used during parsing. This is used by the REPL to show "(desugared)" feedback to users.

type ParserError

type ParserError struct {
	Code        string
	Message     string
	Pos         ast.Pos
	NearToken   lexer.Token
	Expected    []lexer.TokenType
	Fix         string // Deprecated: use Suggestions instead
	Suggestions []string
	HelpURL     string
	Confidence  float64
}

ParserError represents a structured parser error with fix suggestions

func NewParserError

func NewParserError(code string, pos ast.Pos, nearToken lexer.Token, message string, expected []lexer.TokenType, fix string) *ParserError

NewParserError creates a structured parser error with fix suggestion

func NewSuggestionError

func NewSuggestionError(code string, pos ast.Pos, nearToken lexer.Token, message string, suggestions []string, helpURL string) *ParserError

NewSuggestionError creates a parser error with multiple suggestions and optional help URL

func (*ParserError) Error

func (e *ParserError) Error() string

Jump to

Keyboard shortcuts

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