parser

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractRhsAt

func ExtractRhsAt[T internal.TokenTyper](rules []*Rule[T], at int) []T

ExtractRhsAt returns a slice of the right-hand side at the given index.

Parameters:

  • at: The index of the right-hand side.

Returns:

  • []T: The slice of the right-hand side.

Types

type ActiveParser

type ActiveParser[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

ActiveParser is the active parser (i.e., the one that is currently parsing).

func (ActiveParser[T]) Error

func (ap ActiveParser[T]) Error() error

Error returns the error if any.

Returns:

  • error: An error if any.

func (ActiveParser[T]) Forest

func (ap ActiveParser[T]) Forest() []*tree.Tree[*gr.Token[T]]

Forest returns the tree that were parsed.

Returns:

  • []*uttr.Tree[*grammar.Token[T]]: The forest.

func (ActiveParser[T]) HasError

func (ap ActiveParser[T]) HasError() bool

HasError checks if the error is not nil.

Returns:

  • bool: True if the error is not nil.

func (*ActiveParser[T]) NextEvents

func (ap *ActiveParser[T]) NextEvents() []*Item[T]

exec executes the active parser.

Parameters:

  • history: The history of the parser.

Returns:

  • []*Item[T]: The possible paths.

func (*ActiveParser[T]) Pop

func (ap *ActiveParser[T]) Pop() (*gr.Token[T], bool)

Pop pops a token from the stack.

Returns:

  • *Token[T]: The popped token.
  • bool: True if the token was popped, false otherwise.

func (*ActiveParser[T]) WalkOne

func (ap *ActiveParser[T]) WalkOne(item *Item[T]) bool

apply is a helper function that applies the action to the stack.

Parameters:

  • item: The item to apply.

Returns:

  • bool: True if the action is accepted. False otherwise.

type ConflictMap

type ConflictMap[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

ConflictMap is the conflict map.

func NewConflictMap

func NewConflictMap[T internal.TokenTyper]() *ConflictMap[T]

NewConflictMap creates a new conflict map.

Returns:

  • *ConflictMap[T]: The new conflict map. Never returns nil.

func (ConflictMap[T]) All

func (cm ConflictMap[T]) All() iter.Seq2[T, iter.Seq[*Item[T]]]

All returns an iterator over the conflict map where the key is the token symbol and the value is an iterator over the conflicting items.

Returns:

  • iter.Seq2[T, iter.Seq[*Item[T]]]: The iterator. Never returns nil.

func (*ConflictMap[T]) Cleanup

func (cm *ConflictMap[T]) Cleanup()

Cleanup cleans up the conflict map for the garbage collector.

func (ConflictMap[T]) Entry

func (cm ConflictMap[T]) Entry() iter.Seq2[T, *Item[T]]

Entry returns an iterator over the conflict map where the key is the token symbol and the value is the conflicting items.

Returns:

  • iter.Seq2[T, *Item[T]]: The iterator. Never returns nil.

func (*ConflictMap[T]) Init

func (cm *ConflictMap[T]) Init(items map[T][]*Item[T])

Init initializes the conflict map.

Parameters:

  • items: The items.

func (ConflictMap[T]) Len

func (cm ConflictMap[T]) Len() int

Len returns the number of items in the conflict map.

Returns:

  • int: The number of items in the conflict map.

func (*ConflictMap[T]) Reset

func (cm *ConflictMap[T]) Reset()

Reset resets the conflict map.

type DecisionFn

type DecisionFn[T internal.TokenTyper] func(ap *ActiveParser[T]) ([]*Item[T], error)

DecisionFn is the decision function.

Parameters:

  • ap: The active parser.

Returns:

  • []*Item[T]: The list of items.
  • error: An error.

type ErrParsing

type ErrParsing struct {
	// Err is the error.
	Err error

	// PossibleCause is the possible cause of the error.
	PossibleCause error
}

ErrParsing is the error for parsing errors.

func NewErrParsing

func NewErrParsing(err error, possible_cause error) *ErrParsing

NewErrParsing creates a new ErrParsing.

Parameters:

  • err: The error.
  • possibleCause: The possible cause of the error.

Returns:

  • *ErrParsing: A pointer to the new ErrParsing. Never returns nil.

func (ErrParsing) Error

func (e ErrParsing) Error() string

Error implements the error interface.

Message: "<err>, possible cause: <possible cause>".

func (ErrParsing) Unwrap

func (e ErrParsing) Unwrap() error

Unwrap returns the underlying error.

Returns:

  • error: The underlying error.

type ErrUnexpectedLookahead

type ErrUnexpectedLookahead[T internal.TokenTyper] struct {
	// Expected is the expected token.
	Expecteds []T

	// Prev is the previous token.
	Prev T

	// Got is the actual token.
	Got *T
}

ErrUnexpectedLookahead is the error for unexpected tokens.

func NewErrUnexpectedLookahead

func NewErrUnexpectedLookahead[T internal.TokenTyper](prev T, got *T, expecteds ...T) *ErrUnexpectedLookahead[T]

NewErrUnexpectedLookahead creates a new ErrUnexpectedLookahead.

Parameters:

  • prev: The previous token.
  • got: The actual token.
  • expecteds: The expected tokens.

Returns:

  • *ErrUnexpectedLookahead[T]: A pointer to the new ErrUnexpectedLookahead. Never returns nil.

func (ErrUnexpectedLookahead[T]) Error

func (e ErrUnexpectedLookahead[T]) Error() string

Error implements the error interface.

Message: "expected {expected} after {prev}, got {got} instead".

type Item

type Item[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

Item is an item in the parsing table.

func NewItem

func NewItem[T internal.TokenTyper](rule *Rule[T], pos int) (*Item[T], error)

NewItem creates a new item.

Parameters:

  • rule: The rule.
  • pos: The position.

Returns:

  • *Item[T]: The created item.
  • error: An error if the position is invalid or the rule is nil.

func (*Item[T]) Advance

func (item *Item[T]) Advance() (*Item[T], bool)

Advance advances the position of the item in the rule by one.

Returns:

  • bool: True if the position was advanced, otherwise false.

func (*Item[T]) AppendLookahead

func (item *Item[T]) AppendLookahead(ls *gccmp.Set[T]) error

AppendLookahead appends the given lookahead set to the item.

Parameters:

  • ls: The lookahead set.

Returns:

  • error: An error if the lookahead set is nil.

func (*Item[T]) Equals

func (item *Item[T]) Equals(other *Item[T]) bool

Equals implements the pkg.Type interface.

Two items are equal if their rules are equal, they are not nil, and their positions are equal.

func (*Item[T]) IncreaseLookbehind

func (item *Item[T]) IncreaseLookbehind() bool

IsReduce checks if the item is a reduce.

Returns:

  • bool: True if the item is a reduce, otherwise false.

func (Item[T]) IsInConflictWith

func (item Item[T]) IsInConflictWith(other *Item[T]) bool

IsInConflictWith checks if the item is in conflict with another item.

Returns:

  • bool: True if the item is in conflict with another item, otherwise false.

func (Item[T]) IsShift

func (item Item[T]) IsShift() bool

IsShift checks if the item is a shift.

Returns:

  • bool: True if the item is a shift, otherwise false.

func (Item[T]) Lhs

func (item Item[T]) Lhs() T

Lhs returns the left hand side of the item.

Returns:

  • T: The left hand side.

func (Item[T]) LookaheadAt

func (item Item[T]) LookaheadAt(pos int) (*gccmp.Set[T], bool)

LookaheadAt returns the lookahead set at the given position.

Parameters:

  • pos: The position.

Returns:

  • *gccmp.Set[T]: The lookahead set.
  • bool: True if the lookahead set exists, otherwise false.

func (Item[T]) Pos

func (item Item[T]) Pos() int

Pos returns the position of the item in the rule.

Returns:

  • int: The position.

func (Item[T]) Rhs

func (item Item[T]) Rhs() iter.Seq[T]

Rhs returns an iterator over the right hand side of the item.

Returns:

  • iter.Seq[T]: The iterator. Never returns nil.

func (Item[T]) RhsAt

func (item Item[T]) RhsAt(pos int) (T, bool)

RhsAt returns the right hand side at the given position.

Parameters:

  • pos: The position.

Returns:

  • T: The right hand side.
  • bool: True if the right hand side exists, otherwise false.

func (Item[T]) String

func (item Item[T]) String() string

String implements the fmt.Stringer interface.

type Parser

type Parser[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

Parser is the grammar parser.

func NewParser

func NewParser[T internal.TokenTyper](rule_set *RuleSet[T]) (*Parser[T], error)

NewParser creates a new parser with the given rule set.

Parameters:

  • rule_set: The rule set.

Returns:

  • *Parser[T]: The new parser.
  • error: An error of type *errors.ErrInvalidParameter if rule_set is nil.

func NewParserWithFunc

func NewParserWithFunc[T internal.TokenTyper](decision_fn DecisionFn[T]) (*Parser[T], error)

NewParserWithFunc creates a new parser with the given rule set.

Parameters:

  • decision_fn: The decision function.

Returns:

  • *Parser[T]: The new parser.
  • error: An error of type *errors.ErrInvalidParameter if rule_set is nil.

func (*Parser[T]) Parse

func (p *Parser[T]) Parse(tokens []*gr.Token[T]) iter.Seq[*ActiveParser[T]]

Parse is the main function of the parser.

Parameters:

  • tokens: The tokens to be parsed.

Returns:

  • *ActiveParser[T]: The parser.
  • error: An error if any.

type Rule

type Rule[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

Rule is a grammar rule.

func NewRule

func NewRule[T internal.TokenTyper](lhs T, rhss []T) (*Rule[T], error)

NewRule creates a new rule with the given left-hand side and right-hand side.

Parameters:

  • lhs: The left-hand side of the rule.
  • rhss: The right-hand side of the rule.

Returns:

  • *Rule[T]: The new rule.
  • error: An error of type *errors.ErrInvalidParameter if 'rhss' is empty.

func (Rule[T]) Backwards

func (r Rule[T]) Backwards() iter.Seq[T]

Backwards returns an iterator over the right-hand side of the rule in reverse order.

Returns:

  • iter.Seq[T]: The iterator. Never returns nil.

func (*Rule[T]) Equals

func (r *Rule[T]) Equals(other *Rule[T]) bool

Equals checks if the given rule is equal to the current rule.

Parameters:

  • other: The other rule.

Returns:

  • bool: True if the given rule is equal to the current rule, otherwise false.

If 'other' is nil, it always returns false.

func (Rule[T]) IndicesOf

func (r Rule[T]) IndicesOf(rhs T) []int

IndicesOf returns a slice of indices of the given right-hand side in the rule.

Parameters:

  • rhs: The right-hand side of the rule.

Returns:

  • []int: The slice of indices.

func (Rule[T]) Lhs

func (r Rule[T]) Lhs() T

Lhs returns the left-hand side of the rule.

Returns:

  • T: The left-hand side.

func (Rule[T]) Rhs

func (r Rule[T]) Rhs() iter.Seq[T]

Rhs returns an iterator over the right-hand side of the rule.

Returns:

  • iter.Seq[T]: The iterator. Never returns nil.

func (Rule[T]) RhsAt

func (r Rule[T]) RhsAt(idx int) (T, bool)

RhsAt returns the right-hand side at the given index.

Parameters:

  • idx: The index of the right-hand side.

Returns:

  • T: The right-hand side.
  • bool: True if the right-hand side exists, otherwise false.

func (Rule[T]) Size

func (r Rule[T]) Size() int

Size returns the amount of right-hand sides of the rule.

Returns:

  • int: The amount of right-hand sides of the rule.

This function never returns 0.

func (Rule[T]) Symbols

func (r Rule[T]) Symbols() *utst.Set[T]

Symbols returns the set of symbols in the rule.

Returns:

  • *utst.Set[T]: The set of symbols. Never returns nil.

type RuleSet

type RuleSet[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

RuleSet is the rule set data structure.

func NewRuleSet

func NewRuleSet[T internal.TokenTyper]() *RuleSet[T]

NewRuleSet creates a new RuleSet.

Returns:

  • *RuleSet[T]: The created RuleSet. Never returns nil.

func (RuleSet[T]) Decision

func (rs RuleSet[T]) Decision(p *ActiveParser[T]) ([]*Item[T], error)

func (*RuleSet[T]) DetermineItems

func (rs *RuleSet[T]) DetermineItems()

DetermineItems determines the items in the rule set.

func (RuleSet[T]) DetermineLookaheads

func (rs RuleSet[T]) DetermineLookaheads(item *Item[T], offset int)

DetermineLookaheads determines the lookaheads with a specific offset of the specified item.

Parameters:

  • item: The item to determine the lookaheads for.
  • offset: The offset to determine the lookaheads for.

Note: The offset must be greater than 0.

func (*RuleSet[T]) DetermineSymbols

func (rs *RuleSet[T]) DetermineSymbols()

DetermineSymbols determines the symbols in the rule set.

func (*RuleSet[T]) MustAddRule

func (rs *RuleSet[T]) MustAddRule(rule *Rule[T])

MustAddRule adds a new rule to the rule set.

Parameters:

  • rule: The rule to add.

func (*RuleSet[T]) MustMakeRule

func (rs *RuleSet[T]) MustMakeRule(lhs T, rhss []T)

MustMakeRule adds a new rule to the rule set.

Panics if the rule already exists or if the rhss is empty.

Parameters:

  • lhs: The left hand side of the rule.
  • rhss: The right hand side of the rule.

func (RuleSet[T]) RulesWithLhs

func (rs RuleSet[T]) RulesWithLhs(lhs T) []*Rule[T]

RulesWithLhs returns the rules with the specified left hand side.

Parameters:

  • lhs: The left hand side of the rules to return.

Returns:

  • []*internal.Rule[T]: The rules with the specified left hand side.

func (*RuleSet[T]) SolveConflicts

func (rs *RuleSet[T]) SolveConflicts() bool

SolveConflicts solves the conflicts in the rule set.

Returns:

  • bool: True if all conflicts were solved. False otherwise.

If conflicts are not solved, this function will print out the conflicts.

func (RuleSet[T]) String

func (rs RuleSet[T]) String() string

String implements the fmt.Stringer interface.

Format:

// {rule}
// {rule}
...

type State

type State[T internal.TokenTyper] struct {
	// contains filtered or unexported fields
}

State is the state of the goto graph.

func NewState

func NewState[T internal.TokenTyper](seed *Item[T], closure []*Item[T]) *State[T]

NewState creates a new state.

Parameters:

  • seed: The seed item.
  • closure: The closure of the state.

Returns:

  • *State[T]: The created state. Never returns nil.

func (*State[T]) AddNext

func (s *State[T]) AddNext(next *State[T])

AddNext adds the next state to the state. Nil or already added states are ignored.

Parameters:

  • next: The next state.

func (State[T]) IsOfSeed

func (s State[T]) IsOfSeed(item *Item[T]) bool

IsOfSeed checks if the state is of the seed item.

Parameters:

  • item: The item to check.

Returns:

  • bool: True if the state is of the seed item, false otherwise.

func (State[T]) NextState

func (s State[T]) NextState() iter.Seq[*State[T]]

NextState returns an iterator over the next states.

Returns:

  • iter.Seq[*State[T]]: The iterator. Never returns nil.

func (*State[T]) Rule

func (s *State[T]) Rule() iter.Seq2[int, *Item[T]]

Rule returns an iterator over the rules in the state. This excludes the seed item.

Returns:

  • iter.Seq2[int, *Item[T]]: The iterator. Never returns nil.

func (State[T]) Seed

func (s State[T]) Seed() *Item[T]

Seed returns the seed item.

Returns:

  • *Item[T]: The seed item. Never returns nil.

Jump to

Keyboard shortcuts

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