parser

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 3 Imported by: 14

Documentation

Index

Examples

Constants

View Source
const ReaderDone = rune(-1)

Variables

This section is empty.

Functions

This section is empty.

Types

type Capture

type Capture interface {
	Parse(p *Parser) (end *Node, err error)

	Operator
	fmt.Stringer
}

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) Character

func (c Cursor) Character() rune

Character returns the current character rune.

func (Cursor) LastNewLine

func (c Cursor) LastNewLine() int

LastNewLine returns the last end of line absolute position.

func (Cursor) Line

func (c Cursor) Line() (int, int)

Line returns the current line.

func (Cursor) Position

func (c Cursor) Position() int

Position returns the absolute position in the input.

func (Cursor) String

func (c Cursor) String() string

String returns the string representation of the cursor.

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.

func (*ErrorStack) Error

func (e *ErrorStack) Error() string

Error returns the error message.

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
----^

func (*NoMatchError) Error

func (e *NoMatchError) Error() string

Error returns the error message.

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 NewNode

func NewNode(name, value string) *Node

NewNode creates a new node.

func NewParentNode

func NewParentNode(name string, children []*Node) *Node

NewParentNode creates a new parent node.

func (*Node) AddChild

func (n *Node) AddChild(child *Node)

AddChild adds a child to the node.

func (*Node) Children

func (n *Node) Children() []*Node

Children returns the children of the node.

func (*Node) Value

func (n *Node) Value() string

Value returns the value of the 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 New

func New(input []rune) (*Parser, error)

New creates a new parser.

func (*Parser) IgnoreDisabled added in v0.1.1

func (p *Parser) IgnoreDisabled() bool

func (*Parser) Match

func (p *Parser) Match(v any) (Cursor, error)

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

func (p *Parser) MatchEOF(v any) (Cursor, error)

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) Parse

func (p *Parser) Parse(v any) (*Node, error)

Parse the given value.

func (*Parser) ParseEOF added in v0.1.3

func (p *Parser) ParseEOF(v any) (*Node, error)

ParseEOF parses the given value and ensures that the end of the input is reached.

func (*Parser) Reset

func (p *Parser) Reset() *Parser

Reset the parser. All state is lost.

func (*Parser) SetIgnoreList added in v0.1.1

func (p *Parser) SetIgnoreList(ignore []any)

func (*Parser) ToggleIgnore added in v0.1.1

func (p *Parser) ToggleIgnore(disable bool)

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is the input reader.

func NewReader

func NewReader(input []rune) (*Reader, error)

NewReader creates a new reader.

func (*Reader) Cursor

func (r *Reader) Cursor() Cursor

Cursor the current cursor.

func (*Reader) Done

func (r *Reader) Done() bool

Done returns true if the reader is done.

func (*Reader) GetInputRange

func (r *Reader) GetInputRange(start Cursor, end Cursor) []rune

GetInputRange returns the input range from start to end (excl).

func (*Reader) GetLine

func (r *Reader) GetLine(end Cursor) []rune

GetLine returns the line from the given cursor starting at the last newline until the end cursor.

func (*Reader) Jump

func (r *Reader) Jump(marker Cursor)

Jump to the given cursor. Ignore if cursor is nil or the reader is done.

func (*Reader) Next

func (r *Reader) Next() *Reader

Next reads the next rune.

func (*Reader) Rune

func (r *Reader) Rune() rune

Rune returns the current rune.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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