Grammar

package
v0.2.19 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LeftToRight is the direction of a production from left to right.
	LeftToRight   string = "->"
	StartSymbolID string = "source"
	EndSymbolID   string = "EOF"
)

Variables

This section is empty.

Functions

func IsTerminal added in v0.2.18

func IsTerminal(identifier string) bool

Types

type Grammar

type Grammar struct {
	// Productions is a slice of Productions in the grammar.
	Productions []Productioner

	// LhsToSkip is a slice of productions to skip.
	LhsToSkip []string

	// Symbols is a slice of Symbols in the grammar.
	Symbols []string
}

Grammar represents a context-free grammar.

A context-free grammar is a set of productions, each of which consists of a non-terminal symbol and a sequence of symbols.

The non-terminal symbol is the left-hand side of the production, and the sequence of symbols is the right-hand side of the production.

The grammar also contains a set of symbols, which are the non-terminal and terminal symbols in the grammar.

func (*Grammar) Match added in v0.2.18

func (g *Grammar) Match(at int, b any) []MatchedResult

func (*Grammar) String

func (g *Grammar) String() string

String is a method of Grammar that returns a string representation of a Grammar.

Returns:

  • string: A string representation of a Grammar.

type GrammarBuilder

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

GrammarBuilder represents a builder for a grammar.

The default direction of the productions is LeftToRight.

func (*GrammarBuilder) AddProduction

func (b *GrammarBuilder) AddProduction(p Productioner)

AddProduction is a method of GrammarBuilder that adds a production to the GrammarBuilder.

Parameters:

  • p: The production to add to the GrammarBuilder.

func (*GrammarBuilder) Build

func (b *GrammarBuilder) Build() (*Grammar, error)

Build is a method of GrammarBuilder that builds a Grammar from the GrammarBuilder.

Returns:

  • *Grammar: A Grammar built from the GrammarBuilder.

func (*GrammarBuilder) Reset

func (b *GrammarBuilder) Reset()

Reset is a method of GrammarBuilder that resets a GrammarBuilder.

func (*GrammarBuilder) SetToSkip added in v0.2.19

func (b *GrammarBuilder) SetToSkip(lhss ...string)

func (*GrammarBuilder) String

func (b *GrammarBuilder) String() string

String is a method of GrammarBuilder that returns a string representation of a GrammarBuilder.

Returns:

  • string: A string representation of a GrammarBuilder.

type LeafToken added in v0.2.18

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

func NewLeafToken added in v0.2.18

func NewLeafToken(id string, data string, at int) *LeafToken

func (*LeafToken) GetData added in v0.2.18

func (t *LeafToken) GetData() any

func (*LeafToken) GetID added in v0.2.18

func (t *LeafToken) GetID() string

func (*LeafToken) GetPos added in v0.2.18

func (t *LeafToken) GetPos() int

func (*LeafToken) String added in v0.2.18

func (t *LeafToken) String() string

type MatchedResult added in v0.2.18

type MatchedResult struct {
	Matched   Tokener
	RuleIndex int
}

func NewMatchResult added in v0.2.18

func NewMatchResult(matched Tokener, ruleIndex int) MatchedResult

type NonLeafToken added in v0.2.18

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

func NewNonLeafToken added in v0.2.18

func NewNonLeafToken(id string, at int, data ...Tokener) *NonLeafToken

func (*NonLeafToken) GetData added in v0.2.18

func (t *NonLeafToken) GetData() any

func (*NonLeafToken) GetID added in v0.2.18

func (t *NonLeafToken) GetID() string

func (*NonLeafToken) GetPos added in v0.2.18

func (t *NonLeafToken) GetPos() int

func (*NonLeafToken) String added in v0.2.18

func (t *NonLeafToken) String() string

type Production

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

Production represents a production in a grammar.

func NewProduction

func NewProduction(lhs string, rhs ...string) *Production

NewProduction is a function that returns a new Production with the given left-hand side and right-hand side.

Parameters:

  • lhs: The left-hand side of the production.
  • rhs: The right-hand side of the production.

Returns:

  • *Production: A new Production with the given left-hand side and right-hand side.

func (*Production) Equals added in v0.2.18

func (p *Production) Equals(other Productioner) bool

Equals is a method of Production that returns whether the production is equal to another production. Two productions are equal if their left-hand sides are equal and their right-hand sides are equal.

Parameters:

  • other: The other production to compare to.

Returns:

  • bool: Whether the production is equal to the other production.

func (*Production) GetLhs added in v0.2.18

func (p *Production) GetLhs() string

GetLhs is a method of Production that returns the left-hand side of the production.

Returns:

  • string: The left-hand side of the production.

func (*Production) GetRhsAt

func (p *Production) GetRhsAt(index int) (string, error)

GetRhsAt is a method of Production that returns the symbol at the given index in the right-hand side of the production.

Parameters:

  • index: The index of the symbol to get.

Returns:

  • string: The symbol at the given index in the right-hand side of the production.
  • error: An error of type *ErrInvalidParameter if the index is invalid.

func (*Production) GetSymbols

func (p *Production) GetSymbols() []string

GetSymbols is a method of Production that returns a slice of symbols in the production. The slice contains the left-hand side of the production and the right-hand side of the production, with no duplicates.

Returns:

  • []string: A slice of symbols in the production.

func (*Production) IndexOfRhs added in v0.2.18

func (p *Production) IndexOfRhs(rhs string) int

func (*Production) Iterator

func (p *Production) Iterator() itf.Iterater[string]

Iterator is a method of Production that returns an iterator for the production that iterates over the right-hand side of the production.

Returns:

  • itf.Iterater[string]: An iterator for the production.

func (*Production) Match added in v0.2.18

func (p *Production) Match(at int, b any) Tokener

b must be of Stack.Stacker[Tokener]

func (*Production) ReverseIterator added in v0.2.18

func (p *Production) ReverseIterator() itf.Iterater[string]

ReverseIterator is a method of Production that returns a reverse iterator for the production that iterates over the right-hand side of the production in reverse.

Returns:

  • itf.Iterater[string]: A reverse iterator for the production.

func (*Production) Size

func (p *Production) Size() int

Size is a method of Production that returns the number of symbols in the right-hand side of the production.

Returns:

  • int: The number of symbols in the right-hand side of the production.

func (*Production) String

func (p *Production) String() string

String is a method of Production that returns a string representation of a Production.

Returns:

  • string: A string representation of a Production.

type Productioner added in v0.2.18

type Productioner interface {
	fmt.Stringer
	Equals(Productioner) bool
	GetLhs() string
	GetSymbols() []string
	Match(int, any) Tokener
}

type RegProduction added in v0.2.18

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

func NewRegProduction added in v0.2.18

func NewRegProduction(lhs string, regex string) *RegProduction

func (*RegProduction) Equals added in v0.2.18

func (p *RegProduction) Equals(other Productioner) bool

func (*RegProduction) GetLhs added in v0.2.18

func (p *RegProduction) GetLhs() string

func (*RegProduction) GetSymbols added in v0.2.18

func (p *RegProduction) GetSymbols() []string

func (*RegProduction) Match added in v0.2.18

func (p *RegProduction) Match(at int, b any) Tokener

return nil if no match

func (*RegProduction) String added in v0.2.18

func (r *RegProduction) String() string

type Tokener added in v0.2.18

type Tokener interface {
	fmt.Stringer

	GetID() string
	GetData() any
	GetPos() int
}

Jump to

Keyboard shortcuts

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