Documentation
¶
Index ¶
- func ExtractRhsAt[T internal.TokenTyper](rules []*Rule[T], at int) []T
- type ActiveParser
- func (ap ActiveParser[T]) Error() error
- func (ap ActiveParser[T]) Forest() []*tree.Tree[*gr.Token[T]]
- func (ap ActiveParser[T]) HasError() bool
- func (ap *ActiveParser[T]) NextEvents() []*Item[T]
- func (ap *ActiveParser[T]) Pop() (*gr.Token[T], bool)
- func (ap *ActiveParser[T]) WalkOne(item *Item[T]) bool
- type ConflictMap
- type DecisionFn
- type ErrParsing
- type ErrUnexpectedLookahead
- type Item
- func (item *Item[T]) Advance() (*Item[T], bool)
- func (item *Item[T]) AppendLookahead(ls *gccmp.Set[T]) error
- func (item *Item[T]) Equals(other *Item[T]) bool
- func (item *Item[T]) IncreaseLookbehind() bool
- func (item Item[T]) IsInConflictWith(other *Item[T]) bool
- func (item Item[T]) IsShift() bool
- func (item Item[T]) Lhs() T
- func (item Item[T]) LookaheadAt(pos int) (*gccmp.Set[T], bool)
- func (item Item[T]) Pos() int
- func (item Item[T]) Rhs() iter.Seq[T]
- func (item Item[T]) RhsAt(pos int) (T, bool)
- func (item Item[T]) String() string
- type Parser
- type Rule
- type RuleSet
- func (rs RuleSet[T]) Decision(p *ActiveParser[T]) ([]*Item[T], error)
- func (rs *RuleSet[T]) DetermineItems()
- func (rs RuleSet[T]) DetermineLookaheads(item *Item[T], offset int)
- func (rs *RuleSet[T]) DetermineSymbols()
- func (rs *RuleSet[T]) MustAddRule(rule *Rule[T])
- func (rs *RuleSet[T]) MustMakeRule(lhs T, rhss []T)
- func (rs RuleSet[T]) RulesWithLhs(lhs T) []*Rule[T]
- func (rs *RuleSet[T]) SolveConflicts() bool
- func (rs RuleSet[T]) String() string
- type State
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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsReduce checks if the item is a reduce.
Returns:
- bool: True if the item is a reduce, otherwise false.
func (Item[T]) IsInConflictWith ¶
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 ¶
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 ¶
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]) Rhs ¶
Rhs returns an iterator over the right hand side of the item.
Returns:
- iter.Seq[T]: The iterator. Never returns nil.
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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]) DetermineItems ¶
func (rs *RuleSet[T]) DetermineItems()
DetermineItems determines the items in the rule set.
func (RuleSet[T]) DetermineLookaheads ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
AddNext adds the next state to the state. Nil or already added states are ignored.
Parameters:
- next: The next state.
func (State[T]) IsOfSeed ¶
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 ¶
NextState returns an iterator over the next states.
Returns:
- iter.Seq[*State[T]]: The iterator. Never returns nil.