Documentation ¶
Index ¶
- Constants
- type Capture
- type Cursor
- type ErrorStack
- type InvalidTypeError
- type NoMatchError
- type Node
- type Operator
- type Parser
- func (p *Parser) IgnoreDisabled() bool
- func (p *Parser) Match(v any) (Cursor, error)
- func (p *Parser) MatchEOF(v any) (Cursor, error)
- func (p *Parser) NewNoMatchError(v any, start, end Cursor) *NoMatchError
- func (p *Parser) Parse(v any) (*Node, error)
- func (p *Parser) ParseEOF(v any) (*Node, error)
- func (p *Parser) Reset() *Parser
- func (p *Parser) SetIgnoreList(ignore []any)
- func (p *Parser) ToggleIgnore(disable bool)
- type Reader
Examples ¶
Constants ¶
const ReaderDone = rune(-1)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Capture ¶
Capture is the interface that wraps the Parse method. Parse parses the input and returns a node.
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
Cursor represents the current position in the input.
func (Cursor) LastNewLine ¶
LastNewLine returns the last end of line absolute position.
type ErrorStack ¶
type ErrorStack struct {
Errors []error
}
ErrorStack is a stack of errors.
func NewErrorStack ¶
func NewErrorStack(context, err error) *ErrorStack
NewErrorStack returns a new ErrorStack.
func (*ErrorStack) AddError ¶
func (e *ErrorStack) AddError(err error) *ErrorStack
AddError adds an error to the stack.
type InvalidTypeError ¶
type InvalidTypeError struct {
V any
}
InvalidTypeError is returned when an invalid type is passed to a function.
Example ¶
package main import ( "fmt" "github.com/0x51-dev/upeg/parser" ) func main() { fmt.Println(parser.NewInvalidTypeError('0')) }
Output: invalid type: int32
func NewInvalidTypeError ¶
func NewInvalidTypeError(v any) *InvalidTypeError
NewInvalidTypeError returns a new InvalidTypeError.
func (*InvalidTypeError) Error ¶
func (e *InvalidTypeError) Error() string
Error returns the error message.
type NoMatchError ¶
type NoMatchError struct { // Operator, string, or rune. V any // Start cursor of the rule. Start Cursor // End cursor of the rule. End Cursor // contains filtered or unexported fields }
NoMatchError is returned when a rule does not match.
Example ¶
package main import ( "fmt" "github.com/0x51-dev/upeg/parser" "github.com/0x51-dev/upeg/parser/op" ) func main() { p, _ := parser.New([]rune("test")) _, err := p.Match(op.And{'t', 'e', 's', 't', 'i', 'f', 'y'}) fmt.Println(err) }
Output: error stack: 2) [1:1/1:5] '�' | no match: ('t' 'e' 's' 't' 'i' 'f' 'y') test ----^ 1) [1:5/1:5] '�' | no match: 'i' test ----^
type Node ¶
type Node struct { // Name is the name of the node. Name string // contains filtered or unexported fields }
Node is a node in the parse tree.
func NewParentNode ¶
NewParentNode creates a new parent node.
type Operator ¶
type Operator interface { // Match the given value. Returns a cursor to the next character if matched, the cursor of the input will be moved // to the end of the match. Otherwise, returns an error, the cursor of the input will not be moved. // If an error is returned, the returned cursor is the last matched cursor. Match(start Cursor, p *Parser) (end Cursor, err error) fmt.Stringer }
Operator is the interface that wraps the Match method. March check whether the interface matches the input.
type Parser ¶
type Parser struct { // Reader is the input reader. Reader *Reader Rules map[string]Operator // contains filtered or unexported fields }
Parser is the parser.
func (*Parser) IgnoreDisabled ¶ added in v0.1.1
func (*Parser) Match ¶
Match the given value. Returns the end cursor if the match was successful. Returns an error if the match failed.
func (*Parser) MatchEOF ¶ added in v0.1.3
MatchEOF matches the given value and ensures that the end of the input is reached.
func (*Parser) NewNoMatchError ¶
func (p *Parser) NewNoMatchError(v any, start, end Cursor) *NoMatchError
NewNoMatchError returns a new NoMatchError.
func (*Parser) ParseEOF ¶ added in v0.1.3
ParseEOF parses the given value and ensures that the end of the input is reached.
func (*Parser) SetIgnoreList ¶ added in v0.1.1
func (*Parser) ToggleIgnore ¶ added in v0.1.1
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is the input reader.
func (*Reader) GetInputRange ¶
GetInputRange returns the input range from start to end (excl).
func (*Reader) GetLine ¶
GetLine returns the line from the given cursor starting at the last newline until the end cursor.