Parser

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Indentation string = "|  "
)

Variables

This section is empty.

Functions

func FormatSyntaxError added in v0.1.4

func FormatSyntaxError(root gr.Tokener, data []byte) string

FormatSyntaxError formats a syntax error in the data. The function returns a string with the faulty line and a caret pointing to the invalid token.

Parameters:

  • branch: The branch of tokens to search for.
  • data: The data to search in.

Returns:

  • string: The formatted syntax error.

Example:

data := []byte("Hello, word!")
branch := []gr.LeafToken{
  {Data: "Hello", At: 0},
  {Data: ",", At: 6},
  {Data: "word", At: 8}, // Invalid token (expected "world")
  {Data: "!", At: 12},
}

fmt.Println(FormatSyntaxError(branch, data))

Output:

Hello, word!
       ^

func FullParse

func FullParse(grammar *gr.Grammar, inputStream gr.TokenStream, 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

func ParseBranch(parser *Parser, inputStream gr.TokenStream) ([]gr.NonLeafToken, 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.

func ParseIS

func ParseIS(parser *Parser, branches []gr.TokenStream) ([]gr.NonLeafToken, error)

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.

func TokenerString added in v0.1.4

func TokenerString(root gr.Tokener) string

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 NewAcceptAction added in v0.1.4

func NewAcceptAction(ruleIndex int) Action

NewAcceptAction creates a new accept action.

Parameters:

  • ruleIndex: The index of the rule to reduce by.

Returns:

  • Action: The new accept action.

func NewErrorAction

func NewErrorAction(reason error) Action

NewErrorAction creates a new error action.

Parameters:

  • reason: The reason for the error.

Returns:

  • Action: The new error action.

func NewReduceAction

func NewReduceAction(ruleIndex int) Action

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

	// ActAccept represents the action of accepting the input.
	// (It must be a reduce action and not a shift action)
	ActAccept
)

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 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 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.Grammar) (Parser, error)

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

func (p *Parser) Parse() error

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

func (p *Parser) SetInputStream(inputStream gr.TokenStream) error

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.

Jump to

Keyboard shortcuts

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