Documentation
¶
Index ¶
- func FullParse(grammar *gr.Grammar, inputStream []gr.LeafToken, decisionFunc DecisionFunc) ([]gr.NonLeafToken, error)
- func ParseBranch(parser *Parser, branch []gr.LeafToken) ([]gr.NonLeafToken, error)
- func ParseIS(parser *Parser, branches [][]gr.LeafToken) ([]gr.NonLeafToken, error)
- type Action
- type ActionType
- type DecisionFunc
- type Parser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FullParse ¶
func FullParse(grammar *gr.Grammar, inputStream []gr.LeafToken, decisionFunc DecisionFunc) ([]gr.NonLeafToken, 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 a branch of tokens, returns a slice of non-leaf tokens.
Parameters:
- parser: The parser to use.
- branch: The branch of tokens to parse.
Returns:
- []gr.NonLeafToken: A slice of non-leaf tokens.
- error: An error if the branch cannot be parsed.
func ParseIS ¶
ParseIS is a function that, given a parser and a slice of branches of tokens, returns a slice of non-leaf tokens.
Parameters:
- parser: The parser to use.
- branches: The branches of tokens to parse.
Returns:
- []gr.NonLeafToken: A slice of non-leaf tokens.
- error: An error if the branches cannot be parsed.
Types ¶
type Action ¶
type Action struct { // Type represents the type of action that the parser will take. Type ActionType // Data represents the data associated with the action. Data any }
Action represents an action that the parser will take.
func NewErrorAction ¶
NewErrorAction creates a new error action.
Parameters:
- reason: The reason for the error.
Returns:
- Action: The new error action.
func NewReduceAction ¶
NewReduceAction creates a new reduce action.
If the rule index is less than 0, an error action will be returned instead.
Parameters:
- ruleIndex: The index of the rule to reduce by.
Returns:
- Action: The new reduce action.
func NewShiftAction ¶
func NewShiftAction() Action
NewShiftAction creates a new shift action.
Returns:
- Action: The new shift action.
type ActionType ¶
type ActionType int8
ActionType represents the type of action that the parser will take.
const ( // ActShift represents the action of shifting a token onto the stack. ActShift ActionType = iota // ActReduce represents the action of reducing the stack by a rule. ActReduce // ActError represents the action of encountering an error. ActError )
type DecisionFunc ¶
type DecisionFunc func(stack *ds.DoubleStack[gr.Tokener]) Action
DecisionFunc is a function that is used to determine the next action to take in the parser.
Parameters:
- stack: The stack that the parser is using.
- lookahead: The lookahead token.
Returns:
- Action: The action to take.
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 ¶
NewParser creates a new parser with the given grammar.
If the grammar is nil or has no productions that are of type *gr.Production, an error of type *ers.ErrInvalidParameter will be returned.
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.
func (*Parser) GetParseTree ¶
func (p *Parser) GetParseTree() ([]gr.NonLeafToken, 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.NonLeafToken: The parse tree.
- error: An error if the parse tree could not be retrieved.
func (*Parser) Parse ¶
Parse parses the input stream using the parser's decision function.
SetInputStream() and SetDecisionFunc() must be called before calling this method. If they are not, an error will be returned.
Returns:
- error: An error if the input stream could not be parsed.
func (*Parser) SetDecisionFunc ¶
func (p *Parser) SetDecisionFunc(decisionFunc DecisionFunc) error
SetDecisionFunc sets the decision function that the parser will use to determine the next action to take.
If the decision function is nil, an error of type *ers.ErrInvalidParameter will be returned.
Parameters:
- decisionFunc: The decision function that the parser will use.
Returns:
- error: An error if the decision function could not be set.
func (*Parser) SetInputStream ¶
SetInputStream sets the input stream that the parser will parse. It also adds an EOF token to the end of the input stream if it is not already present.
If the input stream is nil or empty, an error of type *ers.ErrInvalidParameter will be returned.
Parameters:
- inputStream: The input stream that the parser will parse.
Returns:
- error: An error if the input stream could not be set.