parser

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package parser implements the syntactic analyzer for the Monkey programming language.

The parser takes a stream of tokens from the lexer and constructs an Abstract Syntax Tree (AST) that represents the structure of the program. It implements a recursive descent parser with Pratt parsing (precedence climbing) for expressions.

Key features:

  • Top-down parsing of statements and expressions
  • Precedence-based expression parsing
  • Error reporting for syntax errors
  • Support for all language constructs (statements, expressions, literals, etc.)

The main entry point is the New function, which creates a new Parser instance, and the Parser.ParseProgram method, which parses a complete Monkey program and returns an AST.

Index

Constants

View Source
const (

	// Lowest represents the lowest possible precedence for parsing expressions in the syntax tree.
	Lowest int

	// Equals is the precedence for the equality operator.
	Equals // ==

	// LessGreater is the precedence for the less-than and greater-than operators.
	LessGreater // > or <

	// Sum is the precedence for the sum operator.
	Sum // +

	// Product is the precedence for the product operator.
	Product // *

	// Prefix is the precedence for prefix operators.
	Prefix // -x or !x

	// Call is the precedence for function calls.
	Call // myFunc(x)

	// Index is the precedence for array indexing.
	Index // array[index]
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

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

Parser represents a Monkey parser.

func New

func New(l *lexer.Lexer) *Parser

New creates a new Parser with the given lexer.Lexer.

func (*Parser) Errors

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

Errors return the list of errors encountered during parsing.

func (*Parser) ParseProgram

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

ParseProgram parses a complete Monkey program and returns its AST representation. It processes tokens until it reaches the end of the input, building a list of statements.

Check Parser.Errors after calling this method to see if any parsing errors occurred.

Jump to

Keyboard shortcuts

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