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:
Recursive Descent Parser (recursive_descent.go): Implements the core parsing logic with support for expressions, operators, and precedence.
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:
- Parentheses ()
- Exponentiation ^ (right-associative)
- Unary -, + (prefix)
- Multiplicative *, /, % (left-associative)
- Additive +, - (left-associative)
- 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 ¶
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 ¶
Types ¶
type ParseError ¶
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.
type SecurityError ¶
SecurityError represents a security limit violation
func (*SecurityError) Error ¶
func (e *SecurityError) Error() string