parsing

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: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcceptAction

type AcceptAction[S gr.TokenTyper] struct {
	// contains filtered or unexported fields
}

AcceptAction is the accept action.

func NewAcceptAction

func NewAcceptAction[S gr.TokenTyper](rule *Rule[S]) (*AcceptAction[S], error)

NewAcceptAction creates a new accept action.

Parameters:

  • rule: The rule to accept.

Returns:

  • *AcceptAction: The new accept action.
  • error: An error of type *common.ErrInvalidParameter if the rule is nil.

func (*AcceptAction[S]) String

func (a *AcceptAction[S]) String() string

String implements the Actioner interface.

type Actioner

type Actioner interface {
	fmt.Stringer
}

Actioner is an interface that defines the behavior of an action.

type DecisionFunc

type DecisionFunc[S gr.TokenTyper] func(parser *Parser[S], lookahead *gr.Token[S]) (Actioner, error)

DecisionFunc is the function that returns the decision of the parser.

Parameters:

  • parser: The parser.
  • lookahead: The lookahead token.

Returns:

  • Actioner: The action of the decision.
  • error: An error if the decision is invalid.

type ErrUnexpectedToken

type ErrUnexpectedToken[T gr.TokenTyper] struct {
	// Expecteds is the list of expected tokens.
	Expecteds []T

	// Got is the token that was encountered.
	Got *T

	// After is the token that was encountered after the expected token.
	After *T
}

ErrUnexpectedToken is an error that occurs when an unexpected token is encountered.

func NewErrUnexpectedToken

func NewErrUnexpectedToken[T gr.TokenTyper](after *T, got *T, expecteds ...T) *ErrUnexpectedToken[T]

NewErrUnexpectedToken creates a new unexpected token error.

Parameters:

  • after: The token that was encountered after the expected token.
  • got: The token that was encountered.
  • expecteds: The expected tokens.

Returns:

  • *ErrUnexpectedToken[T]: The new error. Never returns nil.

func (*ErrUnexpectedToken[T]) Error

func (e *ErrUnexpectedToken[T]) Error() string

Error implements the error interface.

Format:

"expected either <value 0>, <value 1>, <value 2>, ..., or <value n> after <after> instead, got <actual> instead"

type Parser

type Parser[S gr.TokenTyper] struct {

	// Err is the error reason of the parser.
	Err *displ.ErrParsing
	// contains filtered or unexported fields
}

Parser is the parser of the grammar.

func NewParser

func NewParser[S gr.TokenTyper](decision_func DecisionFunc[S]) *Parser[S]

NewParser creates a new parser.

Parameters:

  • decision_func: The function that returns the decision of the parser.

Returns:

  • *Parser: The new parser.

This function returns nil if the decision_func is nil.

func (*Parser[S]) Accept

func (p *Parser[S]) Accept()

Accept accepts all the tokens that were popped since the last call to Accept().

func (*Parser[S]) FullParse

func (p *Parser[S]) FullParse(tokens []*gr.Token[S]) []*gr.Token[S]

FullParse is just a wrapper around the Grammar.FullParse function.

Parameters:

  • tokens: The input stream of the parser.

Returns:

  • []*gr.Token[S]: The syntax forest of the input stream.

func (*Parser[S]) FullParseWithSteps

func (p *Parser[S]) FullParseWithSteps(tokens []*gr.Token[S], data []byte, tab_size int) []*gr.Token[S]

FullParseWithSteps is like FullParse but, for each step, it pauses and prints its debug state.

Parameters:

  • tokens: The input stream of the parser.

Returns:

  • []*gr.Token[S]: The syntax forest of the input stream.

func (*Parser[S]) GetPopped

func (p *Parser[S]) GetPopped() []*gr.Token[S]

GetPopped returns the popped tokens.

Returns:

  • []*Token[S]: The popped tokens.

func (*Parser[S]) Peek

func (p *Parser[S]) Peek() (*gr.Token[S], bool)

Peek pops a token from the stack without removing it.

Returns:

  • *Token[T]: The token if the stack is not empty, nil otherwise.
  • bool: True if the stack is not empty, false otherwise.

func (*Parser[S]) Pop

func (p *Parser[S]) Pop() (*gr.Token[S], bool)

Pop pops a token from the stack.

Returns:

  • *Token[T]: The token if the stack is not empty, nil otherwise.
  • bool: True if the stack is not empty, false otherwise.

func (*Parser[S]) Push

func (p *Parser[S]) Push(token *gr.Token[S])

Push pushes a token to the stack. Does nothing if the token is nil.

Parameters:

  • token: The token to push.

func (*Parser[S]) Refuse

func (p *Parser[S]) Refuse()

Refuse refuses all the tokens that were popped since the last call to Accept().

func (*Parser[S]) SetInputStream

func (p *Parser[S]) SetInputStream(tokens []*gr.Token[S])

SetInputStream sets the input stream of the parser.

Parameters:

  • tokens: The input stream of the parser.

func (*Parser[S]) Shift

func (p *Parser[S]) Shift() bool

Shift shifts a token from the input stream to the stack.

Returns:

  • bool: True if the input stream is not empty, false otherwise.

func (*Parser[S]) Step

func (p *Parser[S]) Step(title string, data []byte, tab_size int) error

Step is a function that pauses and prints the current state of the parser.

It is useful for debugging.

Parameters:

  • title: The title of the step. This is used for displaying the step title.

Returns:

  • error: Any error that might have occurred. This is used for fatal errors.

type ReduceAction

type ReduceAction[S gr.TokenTyper] struct {
	// contains filtered or unexported fields
}

ReduceAction is the reduce action.

func NewReduceAction

func NewReduceAction[S gr.TokenTyper](rule *Rule[S]) (*ReduceAction[S], error)

NewReduceAction creates a new reduce action.

Parameters:

  • rule: The rule to reduce.

Returns:

  • *ReduceAction: The new reduce action.
  • error: An error of type *common.ErrInvalidParameter if the rule is nil.

func (*ReduceAction[S]) String

func (r *ReduceAction[S]) String() string

String implements the Actioner interface.

type Rule

type Rule[S gr.TokenTyper] struct {
	// contains filtered or unexported fields
}

Rule is a struct that represents a rule of type S.

func NewRule

func NewRule[S gr.TokenTyper](lhs S, rhss []S) *Rule[S]

NewRule creates a new rule.

Parameters:

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

Returns:

  • *Rule[S]: The new rule.

Returns nil if the rhss is empty.

func (*Rule[S]) GetIndicesOfRhs

func (r *Rule[S]) GetIndicesOfRhs(rhs S) []int

GetIndicesOfRhs returns the ocurrence indices of the rhs in the rule.

Parameters:

  • rhs: The right-hand side to search.

Returns:

  • []int: The indices of the rhs in the rule.

func (*Rule[S]) GetLhs

func (r *Rule[S]) GetLhs() S

GetLhs returns the left-hand side of the rule.

Returns:

  • S: The left-hand side of the rule.

func (*Rule[S]) GetRhss

func (r *Rule[S]) GetRhss() []S

GetRhss returns the right-hand sides of the rule.

Returns:

  • []S: The right-hand sides of the rule.

func (*Rule[S]) Size

func (r *Rule[S]) Size() int

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

Returns:

  • int: The "size" of the rule.

func (*Rule[S]) String

func (r *Rule[S]) String() string

String implements the fmt.Stringer interface.

Format:

RHS(n) RHS(n-1) ... RHS(1) -> LHS .

type ShiftAction

type ShiftAction struct {
}

ShiftAction is the shift action.

func NewShiftAction

func NewShiftAction() *ShiftAction

NewShiftAction creates a new shift action.

Returns:

  • *ShiftAction: The new shift action. Never returns nil.

func (*ShiftAction) String

func (s *ShiftAction) String() string

String implements the Actioner interface.

Jump to

Keyboard shortcuts

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