Documentation
¶
Index ¶
Examples ¶
Constants ¶
const NilPos = Pos(0)
NilPos represents an invalid position
const NilPosition = nilPosition(0)
NilPosition represents an invalid position
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
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
Error is an error with a position
func Evaluate ¶
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
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.
type File ¶ added in v0.6.0
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
NewFileSet creates a new file set
func (*FileSet) ErrorWithPosition ¶ added in v0.6.0
ErrorWithPosition creates an error with a human-readable 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
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