Documentation
¶
Overview ¶
Package parser contains the main structs for parsing
Index ¶
- func EmptyLeftRecCtx() data.IntMap
- func NoCurtailingParsers() data.IntSet
- type Func
- type FuncFactory
- type History
- func (h *History) CallCount() int
- func (h *History) GetResults(parserIndex int, pos int, leftRecCtx data.IntMap) (data.IntSet, ResultSet, reader.Error, bool)
- func (h *History) RegisterCall()
- func (h *History) RegisterResults(parserIndex int, pos int, curtailingParsers data.IntSet, resultSet ResultSet, ...)
- type Parser
- type Result
- type ResultSet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EmptyLeftRecCtx ¶
EmptyLeftRecCtx creates an empty left recursion context
func NoCurtailingParsers ¶
NoCurtailingParsers returns with an empty int set
Types ¶
type Func ¶
type Func func(h *History, leftRecCtx data.IntMap, r reader.Reader) (data.IntSet, ResultSet, reader.Error)
Func defines a helper to implement the Parser interface with functions
func Empty ¶
func Empty() Func
Empty always matches and returns with an empty node result When using Empty you should not forget to handle for nil nodes in your node builders and/or interpreters.
Example ¶
Using the Empty parser you can match expressions optionally. Note: there is an Optional combinator for this purpose.
package main
import (
"fmt"
"github.com/opsidian/parsley/ast"
"github.com/opsidian/parsley/ast/builder"
"github.com/opsidian/parsley/combinator"
"github.com/opsidian/parsley/parser"
"github.com/opsidian/parsley/parsley"
"github.com/opsidian/parsley/reader"
"github.com/opsidian/parsley/text"
"github.com/opsidian/parsley/text/terminal"
)
func main() {
concat := ast.InterpreterFunc(func(ctx interface{}, nodes []ast.Node) (interface{}, reader.Error) {
var res string
for _, node := range nodes {
if node != nil {
val, _ := node.Value(ctx)
res += string(val.(rune))
}
}
return res, nil
})
p := combinator.Seq(builder.All("ABC", concat),
terminal.Rune('a', "a"),
combinator.Choice("b or nothing", terminal.Rune('b', "b"), parser.Empty()),
terminal.Rune('c', "c"),
)
s := parsley.NewSentence(p)
value, _, _ := s.Evaluate(text.NewReader([]byte("ac"), "", true), nil)
fmt.Printf("%T %v\n", value, value)
}
Output: string ac
func End ¶
func End() Func
End matches the end of the input
Example ¶
Using the End parser you can make sure you fully match the input
package main
import (
"fmt"
"github.com/opsidian/parsley/ast/builder"
"github.com/opsidian/parsley/combinator"
"github.com/opsidian/parsley/parser"
"github.com/opsidian/parsley/text"
"github.com/opsidian/parsley/text/terminal"
)
func main() {
s := combinator.Seq(builder.Select(0), terminal.Float(), parser.End())
_, rs, _ := s.Parse(parser.NewHistory(), parser.EmptyLeftRecCtx(), text.NewReader([]byte("1.23"), "", true))
value, _ := rs[0].Node().Value(nil)
fmt.Printf("%T %v\n", value, value)
}
Output: float64 1.23
type FuncFactory ¶
type FuncFactory interface {
CreateParser() Func
}
FuncFactory defines an interface for creating parser functions
type History ¶
type History struct {
// contains filtered or unexported fields
}
History records information about parser calls
type Parser ¶
type Parser interface {
Parse(h *History, leftRecCtx data.IntMap, r reader.Reader) (data.IntSet, ResultSet, reader.Error)
}
Parser defines a parser interface