parsley

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2018 License: MPL-2.0 Imports: 5 Imported by: 14

Documentation

Index

Examples

Constants

View Source
const NilPos = Pos(0)

NilPos represents an invalid position

View Source
const NilPosition = nilPosition(0)

NilPosition represents an invalid position

Variables

This section is empty.

Functions

func Parse

func Parse(h History, r Reader, p Parser) (Node, Error)

Parse parses the given input and returns with the root node of the AST. It expects a reader and the root parser. If there are multiple possible parse trees only the first one is returned.

Example

Let's define a simple parser which is able to parse adding two integers.

sum := ast.InterpreterFunc(func(ctx interface{}, nodes []parsley.Node) (interface{}, parsley.Error) {
	value0, _ := nodes[0].Value(ctx)
	value1, _ := nodes[2].Value(ctx)
	return value0.(int) + value1.(int), nil
})

p := combinator.Seq("ADD", "addition",
	terminal.Integer(),
	terminal.Rune('+'),
	terminal.Integer(),
).Bind(sum)

r := text.NewReader(text.NewFile("example.file", []byte("1+2")))
node, err := parsley.Parse(parser.NewHistory(), r, combinator.Sentence(p))
if err != nil {
	panic(err)
}

value, err2 := node.Value(nil)
if err2 != nil {
	panic(err2)
}
fmt.Printf("Result: %d\n", value.(int))
Output:
Result: 3

Types

type Error added in v0.6.0

type Error interface {
	error
	Cause() error
	Pos() Pos
}

Error is an error with a position

func Evaluate

func Evaluate(h History, r Reader, p Parser, ctx interface{}) (interface{}, Error)

Evaluate parses the given input and evaluates it. It expects a reader, the root parser and the evaluation context. If there are multiple possible parse trees only the first one is used for evaluation.

Example

Let's define a simple parser which is able to parse adding two integers.

sum := ast.InterpreterFunc(func(ctx interface{}, nodes []parsley.Node) (interface{}, parsley.Error) {
	value0, _ := nodes[0].Value(ctx)
	value1, _ := nodes[2].Value(ctx)
	return value0.(int) + value1.(int), nil
})

p := combinator.Seq("ADD", "addition",
	terminal.Integer(),
	terminal.Rune('+'),
	terminal.Integer(),
).Bind(sum)

r := text.NewReader(text.NewFile("example.file", []byte("1+2")))
value, err := parsley.Evaluate(parser.NewHistory(), r, combinator.Sentence(p), nil)
if err != nil {
	panic(err)
}
fmt.Printf("Result: %d\n", value.(int))
Output:
Result: 3

func NewError added in v0.6.0

func NewError(pos Pos, cause error) Error

NewError creates a new error with the given position If the passed error is already a parsley.Error it returns the original error as it should have already the correct position.

func NewErrorf added in v0.7.0

func NewErrorf(pos Pos, format string, values ...interface{}) Error

NewErrorf creates a new error with the given position and message

func WrapError added in v0.6.0

func WrapError(e Error, format string, values ...interface{}) Error

WrapError wraps the given error in a error If format contains the "{{err}}" placeholder it will be replaced with the original error message

type File added in v0.6.0

type File interface {
	Position(int) Position
	Pos(int) Pos
	Len() int
	SetOffset(int)
}

File is an interface to translate a byte offset in a file to a position object

type FileSet added in v0.6.0

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

FileSet contains multiple files

func NewFileSet added in v0.6.0

func NewFileSet(files ...File) *FileSet

NewFileSet creates a new file set

func (*FileSet) AddFile added in v0.6.0

func (fs *FileSet) AddFile(f File)

AddFile adds a new file

func (*FileSet) ErrorWithPosition added in v0.6.0

func (fs *FileSet) ErrorWithPosition(err Error) error

ErrorWithPosition creates an error with a human-readable position

func (*FileSet) Position added in v0.6.0

func (fs *FileSet) Position(pos Pos) Position

Position returns with a position object for a given global position

type History added in v0.6.0

type History interface {
	SaveResult(parserIndex int, pos Pos, result *Result)
	GetResult(parserIndex int, pos Pos, leftRecCtx data.IntMap) (*Result, bool)
	RegisterCall()
	CallCount() int
}

History records information about parser calls

type Interpreter added in v0.6.0

type Interpreter interface {
	Eval(ctx interface{}, nodes []Node) (interface{}, Error)
}

Interpreter defines an interface to evaluate AST nodes

type Node added in v0.6.0

type Node interface {
	Token() string
	Value(ctx interface{}) (interface{}, Error)
	Pos() Pos
	ReaderPos() Pos
}

Node represents an AST node

type Parser added in v0.6.0

type Parser interface {
	Parse(h History, leftRecCtx data.IntMap, r Reader, pos Pos) (Node, Error, data.IntSet)
	Name() string
}

Parser defines a parser interface

type Pos added in v0.6.0

type Pos int

Pos is a global offset in a file set which can be translated into a concrete file position

type Position added in v0.6.0

type Position interface {
	fmt.Stringer
}

Position is an interface to translate a file position to a string

type Reader added in v0.6.0

type Reader interface {
	Pos(int) Pos
	Remaining(Pos) int
	IsEOF(Pos) bool
}

Reader is a reader interface for parsing

type Result added in v0.6.0

type Result struct {
	LeftRecCtx        data.IntMap
	CurtailingParsers data.IntSet
	Node              Node
	Err               Error
}

Result is a stored parser result

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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