dsl

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package dsl provides facilities for parsing lisp-like domain-specific languages (DSL).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Index

type Index[E any] struct {
	atomic.Int32
}

Index is a Predicate that evaluates to true only on its N-th invocation.

func OnIndex

func OnIndex[E any](n int32) *Index[E]

OnIndex returns a Predicate that evaluates to true on its N-th call.

func (*Index[E]) Evaluate

func (p *Index[E]) Evaluate(E) bool

Evaluate implements Predicate.

func (*Index[E]) String

func (p *Index[E]) String() string

String implements fmt.Stringer.

type Parser

type Parser[T any] struct {
	// contains filtered or unexported fields
}

A Parser holds the rules and logic for parsing a DSL.

func NewParser

func NewParser[T any]() *Parser[T]

NewParser constructs a new Parser of a lisp-like DSL.

func NewPredicateParser

func NewPredicateParser[E any]() *Parser[Predicate[E]]

NewPredicateParser constructs a new Parser of a Lisp-like DSL, where the resulting type implements Predicate[E]. NewPredicateParser predefines a few useful functions: Not, And, Or, OnIndex.

func (*Parser[T]) DefineConstant

func (p *Parser[T]) DefineConstant(identifier string, instantiate func() T)

DefineConstant adds a new constant to the Parser's supported DSL. Whenever the provided identifier is used within a constant context, the provided closure is invoked to instantiate an appropriate AST value.

func (*Parser[T]) DefineFunc

func (p *Parser[T]) DefineFunc(identifier string, parseFunc func(*Parser[T], *Scanner) T)

DefineFunc adds a new func to the Parser's supported DSL. Whenever the provided identifier is used within a function invocation context, the provided closure is invoked to instantiate an appropriate AST value.

func (*Parser[T]) Parse

func (p *Parser[T]) Parse(d string) (ret T, err error)

Parse parses the provided input string.

func (*Parser[T]) ParseFromPos

func (p *Parser[T]) ParseFromPos(s *Scanner, tok Token) T

ParseFromPos parses from the provided current position and associated scanner. If the parser fails to parse, it panics. This function is intended to be used when composing Parsers of various types.

type Predicate

type Predicate[E any] interface {
	Evaluate(E) bool
	String() string
}

Predicate encodes conditional logic that yields a boolean.

func And

func And[E any](preds ...Predicate[E]) Predicate[E]

And returns a Predicate that evaluates to true if all its operands evaluate to true.

func Not

func Not[E any](p Predicate[E]) Predicate[E]

Not returns a Predicate that negates the provided predicate.

func Or

func Or[E any](preds ...Predicate[E]) Predicate[E]

Or returns a Predicate that evaluates to true if any of its operands evaluate true.

type Scanner

type Scanner struct {
	scanner.Scanner
}

A Scanner holds the scanner's internal state while processing a given text.

func (*Scanner) Consume

func (s *Scanner) Consume(expect token.Token) Token

Consume scans the next token. If the token is not of the provided token, it panics. It returns the token itself.

func (*Scanner) ConsumeString

func (s *Scanner) ConsumeString() string

ConsumeString scans the next token. It panics if the next token is not a string, or if unable to unquote the string. It returns the unquoted string contents.

func (*Scanner) Scan

func (s *Scanner) Scan() Token

Scan scans the next token and returns it.

type Token

type Token struct {
	Kind token.Token
	Lit  string
	// contains filtered or unexported fields
}

Token is a lexical token scanned from an input text.

func (*Token) String

func (t *Token) String() string

String implements fmt.Stringer.

Jump to

Keyboard shortcuts

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