parser

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 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 (
	RuneAlarm       = rune('\a')
	RuneSpace       = rune(' ')
	RuneBackspace   = rune('\b')
	RuneFormFeed    = rune('\f')
	RuneRubout      = rune(127)
	RuneEscape      = rune(27)
	RuneNewline     = rune('\n')
	RuneNull        = rune(0)
	RuneReturn      = rune('\r')
	RuneTab         = rune('\t')
	RuneVerticalTab = rune('\v')
)

Character mnemonic runes.

View Source
const (
	ParserNumberDefaultBase = 10
)

Variables

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

Functions

func TrimPrefixFolded

func TrimPrefixFolded(s, prefix string) string

TrimPrefixFolded performs a case-insensitive trim of the specified prefix from the string s. If s does not start with the prefix (case-insensitive), s is returned unchanged. functionally,its identical to strings.TrimPrefix but case-insensitive.

func TrimSuffixFolded

func TrimSuffixFolded(s, suffix string) string

TrimSuffixFolded performs a case-insensitive trim of the specified suffix from the string s. If s does not end with the suffix (case-insensitive), s is returned unchanged. functionally,its identical to strings.TrimSuffix but case-insensitive.

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