parser

package
v1.8.14 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package parser provides the CalcMark parser implementation.

The parser converts tokenized input into an Abstract Syntax Tree (AST) that represents the structure of CalcMark programs. It uses a recursive descent parsing strategy with operator precedence climbing.

Architecture

The parser consists of two main components:

  1. Recursive Descent Parser (recursive_descent.go): Implements the core parsing logic with support for expressions, operators, and precedence.

  2. Adapter (adapter.go): Provides a simple public API that wraps the recursive descent parser.

Usage

Basic parsing:

nodes, err := parser.Parse("x = 5 + 3\n")
if err != nil {
    log.Fatal(err)
}

Operator Precedence

From highest to lowest:

  1. Parentheses ()
  2. Exponentiation ^ (right-associative)
  3. Unary -, + (prefix)
  4. Multiplicative *, /, % (left-associative)
  5. Additive +, - (left-associative)
  6. Comparison >, <, >=, <=, ==, != (non-associative)

Grammar

See calcmark.bnf for the complete EBNF grammar specification.

Error Handling

Parse errors include position information (line and column) to help with error reporting and IDE integration:

parse error at line 1, column 5: expected '(' after function name

Performance

The parser is designed for speed and typically completes in microseconds:

  • Simple expressions: ~1-2μs
  • Complex expressions: ~5μs
  • Multi-line programs: ~50μs

See benchmark_test.go for detailed performance metrics.

Index

Constants

View Source
const (
	// MaxNestingDepth limits expression nesting to prevent stack overflow
	// Example: (((((...))))) with 100+ levels
	MaxNestingDepth = 100

	// MaxTokenCount limits total tokens to prevent "token bomb" attacks
	// Example: x1+x2+x3+...+x10000
	MaxTokenCount = 10000

	// MaxCompoundPeriods limits the number of periods in growth functions
	// to prevent CPU exhaustion from large exponent calculations.
	// compound(x, y, 10000) is the maximum allowed.
	MaxCompoundPeriods = 10_000
)

Security limits to prevent DOS attacks

Variables

This section is empty.

Functions

func Parse

func Parse(text string) ([]ast.Node, error)

Parse parses CalcMark source code into an AST Uses custom recursive descent parser NOTE: This does NOT perform semantic validation (like variable redefinition checks). Semantic validation happens during document evaluation where full environment context is available.

Types

type ParseError

type ParseError struct {
	Message string
	Line    int
	Column  int
}

ParseError represents a parsing error with position information

func (*ParseError) Error

func (e *ParseError) Error() string

type RecursiveDescentParser

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

RecursiveDescentParser implements a recursive descent parser for CalcMark. It uses the hand-written lexer and builds an AST directly.

func NewRecursiveDescentParser

func NewRecursiveDescentParser(source string) *RecursiveDescentParser

NewRecursiveDescentParser creates a new parser for the given source text.

func (*RecursiveDescentParser) Parse

func (p *RecursiveDescentParser) Parse() ([]ast.Node, error)

Parse parses the source and returns an AST.

type SecurityError

type SecurityError struct {
	Message string
	Limit   string
	Actual  int
}

SecurityError represents a security limit violation

func (*SecurityError) Error

func (e *SecurityError) Error() string

type SemanticError

type SemanticError struct {
	Message  string
	VarName  string
	Line     int
	Column   int
	FirstDef *struct {
		Line   int
		Column int
	}
}

SemanticError represents a semantic validation error

func (*SemanticError) Error

func (e *SemanticError) Error() string

Jump to

Keyboard shortcuts

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