parser

package
v1.10.3 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package parser implements R7RS Scheme syntax parsing.

The parser converts a token stream into syntax values with source location:

Features

  • All Scheme datums: literals, symbols, lists, vectors, bytevectors
  • Full numeric tower: integers, floats, rationals, complex, big numbers
  • Quote forms: ', `, ,, ,@ and syntax variants #', #`, #,, #,@
  • Datum labels: #n= and #n# for shared/circular structures (R7RS 2.4)
  • Case folding: #!fold-case and #!no-fold-case directives
  • Symbol interning at parse time

Usage

p := parser.NewParserWithFile(env, true, reader, "example.scm")
for {
    stx, err := p.ReadSyntax(ctx)
    if err == io.EOF {
        break
    }
    // process stx
}

Error Handling

Parse errors are wrapped in ParserError with source location from the offending token. The parser accumulates no state across expressions.

Index

Constants

View Source
const (
	ConstQuote            = "quote"
	ConstQuasiquote       = "quasiquote"
	ConstUnquote          = "unquote"
	ConstUnquoteSplicing  = "unquote-splicing"
	ConstSyntax           = "syntax"
	ConstQuasisyntax      = "quasisyntax"
	ConstUnsyntax         = "unsyntax"
	ConstUnsyntaxSplicing = "unsyntax-splicing"
)

Quote form identifiers.

View Source
const (
	ParserNumberDefaultBase = 10
)

Variables

View Source
var (
	// ErrUnknownTokenType is returned when the parser encounters an unrecognized token.
	ErrUnknownTokenType = werr.NewStaticError("unknown token type")
	ErrAlreadyClosed    = werr.NewStaticError("parser already closed")
)

Functions

func ParseComplexStringNumber added in v1.5.0

func ParseComplexStringNumber(s string) (values.Number, bool)

ParseComplexStringNumber parses a rectangular complex number string ending in 'i'. Handles "3+4i", "1.5-2.5i", "1+inf.0i", "0+3/4i", etc. Returns (nil, false) if s cannot be parsed as a complex number.

func ParseImaginaryStringNumber added in v1.5.0

func ParseImaginaryStringNumber(s string) (values.Number, bool)

ParseImaginaryStringNumber parses a pure imaginary string (ending in 'i') and returns the resulting complex number. Handles "+3i", "-2.5i", "+i", "-i", "+inf.0i", "-nan.0i", etc. Returns (nil, false) if s cannot be parsed as a pure imaginary number.

func ParseSpecialFloat added in v1.5.0

func ParseSpecialFloat(s string) (*values.Float, bool)

ParseSpecialFloat checks if s is +inf.0, -inf.0, +nan.0, or -nan.0 and returns the corresponding Float value. Returns (nil, false) if s is not a special-value string.

Types

type Parser

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

Parser represents a R7RS compliant Scheme syntax parser.

func NewParser

func NewParser(env *environment.EnvironmentFrame, skipComments bool, rdr io.RuneReader) *Parser

NewParser creates a new parser for the given reader and environment.

func NewParserWithFile

func NewParserWithFile(env *environment.EnvironmentFrame, skipComments bool, rdr io.RuneReader, file string) *Parser

NewParserWithFile creates a new parser with a specified source filename.

func (*Parser) Close

func (p *Parser) Close() error

Close closes the parser and releases resources.

func (*Parser) ReadSyntax

func (p *Parser) ReadSyntax(_ context.Context) (syntax.SyntaxValue, error)

ReadSyntax reads and returns the next syntax value from the input.

func (*Parser) Text

func (p *Parser) Text() string

Text returns the current text being parsed.

type ParserError

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

ParserError represents an error that occurred during parsing.

func NewParserError

func NewParserError(tok tokenizer.Token, mess string) *ParserError

NewParserError creates a new parser error for the given token.

func NewParserErrorWithWrap

func NewParserErrorWithWrap(err error, tok tokenizer.Token, mess string) *ParserError

NewParserErrorWithWrap creates a new parser error wrapping another error.

func NewParserErrorWithWrapf

func NewParserErrorWithWrapf(err error, tok tokenizer.Token, mess string, vs ...any) *ParserError

NewParserErrorWithWrapf creates a new parser error wrapping another error.

func NewParserErrorf

func NewParserErrorf(tok tokenizer.Token, mess string, vs ...any) *ParserError

NewParserErrorf creates a new parser error for the given token.

func (*ParserError) EqualTo

func (p *ParserError) EqualTo(v values.Value) bool

func (*ParserError) Error

func (p *ParserError) Error() string

func (*ParserError) Is

func (p *ParserError) Is(err error) bool

Is implements errors.Is for ParserError.

func (*ParserError) IsVoid

func (p *ParserError) IsVoid() bool

func (*ParserError) SchemeString

func (p *ParserError) SchemeString() string

func (*ParserError) Unwrap

func (p *ParserError) Unwrap() error

Jump to

Keyboard shortcuts

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