Parser

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Indentation string = "|  "
)

Variables

This section is empty.

Functions

func FullParse

func FullParse(grammar *gr.ParserGrammar, source *cds.Stream[*gr.LeafToken], dt *cs.ConflictSolver) ([]*gr.TokenTree, error)

FullParse parses the input stream using the given grammar and decision function. It is a convenience function intended for simple parsing tasks.

Parameters:

  • grammar: The grammar that the parser will use.
  • inputStream: The input stream that the parser will parse.
  • decisionFunc: The decision function that the parser will use.

Returns:

  • []gr.NonLeafToken: The parse tree.
  • error: An error if the input stream could not be parsed.

func ParseBranch

func ParseBranch(parser *Parser, source *cds.Stream[*gr.LeafToken]) ([]*gr.TokenTree, error)

ParseBranch is a function that, given a parser and an input stream of tokens, returns a slice of non-leaf tokens.

Parameters:

  • parser: The parser to use.
  • inputStream: The input stream of tokens to parse.

Returns:

  • []gr.NonLeafToken: A slice of non-leaf tokens.
  • error: An error if the branch cannot be parsed.

Types

type CurrentEval added in v0.1.8

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

CurrentEval is a struct that represents the current evaluation of the parser.

func NewCurrentEval added in v0.1.8

func NewCurrentEval() *CurrentEval

NewCurrentEval creates a new current evaluation.

Returns:

  • *CurrentEval: A new current evaluation.

func (*CurrentEval) Accept added in v0.1.9

func (ce *CurrentEval) Accept() bool

Accept returns true if the current evaluation has accepted the input stream.

Returns:

  • bool: True if the current evaluation has accepted the input stream.

func (*CurrentEval) ActOnDecision added in v0.1.8

func (ce *CurrentEval) ActOnDecision(decision cs.HelperElem, source *cds.Stream[*gr.LeafToken]) error

ActOnDecision acts on a decision that the parser has made.

Parameters:

  • decision: The decision that the parser has made.
  • source: The source of the input stream.

Returns:

  • bool: True if the parser has accepted the input stream.
  • error: An error if the parser could not act on the decision.

func (*CurrentEval) Copy added in v0.1.8

func (ce *CurrentEval) Copy() uc.Copier

Copy creates a copy of the current evaluation.

Returns:

  • uc.Copier: A copy of the current evaluation.

func (*CurrentEval) GetParseTree added in v0.1.8

func (ce *CurrentEval) GetParseTree() ([]*gr.TokenTree, error)

GetParseTree returns the parse tree that the parser has generated.

Parse() must be called before calling this method. If it is not, an error will be returned.

Returns:

  • []*gr.TokenTree: A slice of parse trees.
  • error: An error if the parse tree could not be retrieved.

Errors:

  • *ue.ErrInvalidUsage: If Parse() has not been called.
  • *gr.ErrCycleDetected: A cycle is detected in the token tree.
  • *ue.ErrInvalidParameter: The top of the stack is nil.
  • *gr.ErrUnknowToken: The root is not a known token.

func (*CurrentEval) Parse added in v0.1.8

func (ce *CurrentEval) Parse(source *cds.Stream[*gr.LeafToken], dt *cs.ConflictSolver) ([]*CurrentEval, error)

Parse parses the input stream using the parser's decision table.

Parameters:

  • source: The source of the input stream.
  • dt: The decision table to use.

Returns:

  • []*CurrentEval: A slice of current evaluations.
  • error: An error if the input stream could not be parsed.

type ErrNoAccept added in v0.1.4

type ErrNoAccept struct{}

ErrNoAccept is an error that is returned when the parser reaches the end of the input stream without accepting the input stream.

func NewErrNoAccept added in v0.1.4

func NewErrNoAccept() *ErrNoAccept

NewErrNoAccept creates a new ErrNoAccept error.

Returns:

  • *ErrNoAccept: A pointer to the new ErrNoAccept error.

func (*ErrNoAccept) Error added in v0.1.4

func (e *ErrNoAccept) Error() string

Error is a method of the error interface.

Returns:

  • string: The error message.

type ErrNothingWasParsed added in v0.1.8

type ErrNothingWasParsed struct{}

ErrNothingWasParsed is an error that is returned when the parser does not parse anything.

func NewErrNothingWasParsed added in v0.1.8

func NewErrNothingWasParsed() *ErrNothingWasParsed

NewErrNothingWasParsed creates a new ErrNothingWasParsed error.

Returns:

  • *ErrNothingWasParsed: A pointer to the new ErrNothingWasParsed error.

func (*ErrNothingWasParsed) Error added in v0.1.8

func (e *ErrNothingWasParsed) Error() string

Error returns the error message: "nothing was parsed".

Returns:

  • string: The error message.

type ErrUnknownAction added in v0.1.8

type ErrUnknownAction struct {

	// Action is the action that was attempted.
	Action any
}

ErrUnknownAction is an error that is returned when the parser encounters an unknown action.

func NewErrUnknownAction added in v0.1.8

func NewErrUnknownAction(action any) *ErrUnknownAction

NewErrUnknownAction creates a new ErrUnknownAction error.

Parameters:

  • action: The action that was attempted.

Returns:

  • *ErrUnknownAction: A pointer to the new ErrUnknownAction error.

func (*ErrUnknownAction) Error added in v0.1.8

func (e *ErrUnknownAction) Error() string

Error is a method of the error interface.

Returns:

  • string: The error message.

type Parser

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

Parser is a parser that uses a stack to parse a stream of tokens.

func NewParser

func NewParser(grammar *gr.ParserGrammar) (*Parser, error)

NewParser creates a new parser with the given grammar.

Parameters:

  • grammar: The grammar that the parser will use.

Returns:

  • *Parser: A pointer to the new parser.
  • error: An error if the parser could not be created.

Errors:

  • *ers.ErrInvalidParameter: The grammar is nil.
  • *gr.ErrNoProductionRulesFound: No production rules are found in the grammar.

func (*Parser) GetParseTree

func (p *Parser) GetParseTree() ([]*gr.TokenTree, error)

GetParseTree returns the parse tree that the parser has generated.

Parse() must be called before calling this method. If it is not, an error will be returned.

Returns:

  • []*gr.TokenTree: A slice of parse trees.
  • error: An error if the parse tree could not be retrieved.

func (*Parser) Parse

func (p *Parser) Parse(source *cds.Stream[*gr.LeafToken]) error

Parse parses the input stream using the parser's decision function.

Returns:

  • error: An error if the input stream could not be parsed.

Jump to

Keyboard shortcuts

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