parser

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package parser implements the syntactic analyzer for the Monke 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 ParseProgram method, which parses a complete Monke 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 Monke parser.

func New

func New(l *lexer.Lexer) *Parser

New creates a new Parser with the given lexer. It initializes the parser, registers prefix and infix parsing functions, and reads the first two tokens to set up currentToken and peekToken.

func (*Parser) Errors

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

Errors returns the list of errors encountered during parsing. If the list is empty, parsing was successful.

func (*Parser) ParseProgram

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

ParseProgram parses a complete Monke program and returns its AST representation. It processes tokens until it reaches the end of the input, building a list of statements. Check 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