Documentation
¶
Index ¶
- Constants
- func FullParse(grammar *ParserGrammar, source *cds.Stream[*gr.LeafToken], ...) ([]*gr.TokenTree, error)
- func ParseBranch(parser *Parser, source *cds.Stream[*gr.LeafToken]) ([]*gr.TokenTree, error)
- type CurrentEval
- func (ce *CurrentEval) Accept() bool
- func (ce *CurrentEval) ActOnDecision(decision cs.HelperElem, source *cds.Stream[*gr.LeafToken]) error
- func (ce *CurrentEval) Copy() uc.Copier
- func (ce *CurrentEval) GetParseTree() ([]*gr.TokenTree, error)
- func (ce *CurrentEval) Parse(source *cds.Stream[*gr.LeafToken], dt *cs.ConflictSolver) ([]*CurrentEval, error)
- type ErrNoAccept
- type ErrNothingWasParsed
- type ErrUnknownAction
- type Parser
- type ParserGrammar
Constants ¶
const (
Indentation string = "| "
)
Variables ¶
This section is empty.
Functions ¶
func FullParse ¶
func FullParse(grammar *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 ¶
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 *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 ¶
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.
type ParserGrammar ¶ added in v0.1.13
type ParserGrammar struct {
// contains filtered or unexported fields
}
ParserGrammar represents a context-free grammar.
func NewParserGrammar ¶ added in v0.1.13
func NewParserGrammar(rules string) (*ParserGrammar, error)
NewParserGrammar 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 (*ParserGrammar) GetProductions ¶ added in v0.1.13
func (g *ParserGrammar) GetProductions() []*gr.Production
GetProductions returns a slice of Production in the grammar.
Returns:
- []*Production: A slice of Production in the grammar.
func (*ParserGrammar) GetSymbols ¶ added in v0.1.13
func (g *ParserGrammar) GetSymbols() []string
GetSymbols returns a slice of symbols in the grammar.
Returns:
- []string: A slice of symbols in the grammar.
func (*ParserGrammar) ProductionMatch ¶ added in v0.1.13
func (g *ParserGrammar) ProductionMatch(at int, stack *ds.DoubleStack[gr.Tokener]) []*gr.MatchedResult[*gr.NonLeafToken]
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.