Parser

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Indentation string = "|  "
)

Variables

This section is empty.

Functions

func FullParse

func FullParse[T gr.TokenTyper](grammar *Grammar[T], source *cds.Stream[*gr.Token[T]], dt *cs.ConflictSolver[T]) ([]*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 Parse added in v0.1.18

func Parse[T gr.TokenTyper](p *Parser[T], source *cds.Stream[*gr.Token[T]]) error

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

Parameters:

  • p: The parser to use.
  • source: The input stream to parse.

Returns:

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

func ParseBranch

func ParseBranch[T gr.TokenTyper](parser *Parser[T], source *cds.Stream[*gr.Token[T]]) ([]*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[T gr.TokenTyper] 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[T gr.TokenTyper]() *CurrentEval[T]

NewCurrentEval creates a new current evaluation.

Returns:

  • *CurrentEval: A new current evaluation.

func (*CurrentEval[T]) Accept added in v0.1.9

func (ce *CurrentEval[T]) 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[T]) ActOnDecision added in v0.1.8

func (ce *CurrentEval[T]) ActOnDecision(decision cs.HelperElem[T], source *cds.Stream[*gr.Token[T]]) 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[T]) Copy added in v0.1.8

func (ce *CurrentEval[T]) Copy() uc.Copier

Copy creates a copy of the current evaluation.

Returns:

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

func (*CurrentEval[T]) GetParseTree added in v0.1.8

func (ce *CurrentEval[T]) 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:

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

func (*CurrentEval[T]) Parse added in v0.1.8

func (ce *CurrentEval[T]) Parse(source *cds.Stream[*gr.Token[T]], dt *cs.ConflictSolver[T]) ([]*CurrentEval[T], 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 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 Grammar added in v0.1.14

type Grammar[T gr.TokenTyper] struct {
	// contains filtered or unexported fields
}

Grammar represents a context-free grammar.

func NewGrammar added in v0.1.14

func NewGrammar[T gr.TokenTyper]() (*Grammar[T], error)

NewGrammar is a constructor of an empty ParserGrammar.

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.

Returns:

  • *ParserGrammar: A new empty ParserGrammar.

func (*Grammar[T]) AddRule added in v0.1.19

func (g *Grammar[T]) AddRule(lhs T, rhss []T) error

AddRule adds a new rule to the grammar.

Parameters:

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

Returns:

  • error: An error if there was a problem adding the rule.

func (*Grammar[T]) GetProductions added in v0.1.14

func (g *Grammar[T]) GetProductions() []*gr.Production[T]

GetProductions returns a slice of Production in the grammar.

Returns:

  • []*Production: A slice of Production in the grammar.

func (*Grammar[T]) GetSymbols added in v0.1.14

func (g *Grammar[T]) GetSymbols() []T

GetSymbols returns a slice of symbols in the grammar.

Returns:

  • []T: A slice of symbols in the grammar.

func (*Grammar[T]) ProductionMatch added in v0.1.14

func (g *Grammar[T]) ProductionMatch(at int, stack *ud.History[lls.Stacker[*gr.Token[T]]]) []*gr.MatchedResult[T]

ProductionMatch returns a slice of MatchedResult that match the input token.

Parameters:

  • at: The position in the input string.
  • stack: The input stream to match. Refers to Productioner.Match.

Returns:

  • []MatchedResult: A slice of MatchedResult that match the input token.

type Parser

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

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

func NewParser

func NewParser[T gr.TokenTyper](grammar *Grammar[T]) (*Parser[T], 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:

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

func (*Parser[T]) GetParseTree

func (p *Parser[T]) 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.

Jump to

Keyboard shortcuts

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