parser

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2018 License: MPL-2.0 Imports: 4 Imported by: 2

Documentation

Overview

Package parser contains the main structs for parsing

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmptyLeftRecCtx

func EmptyLeftRecCtx() data.IntMap

EmptyLeftRecCtx creates an empty left recursion context

func NoCurtailingParsers

func NoCurtailingParsers() data.IntSet

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

func (Func) Parse

func (f Func) Parse(h *History, leftRecCtx data.IntMap, r reader.Reader) (data.IntSet, ResultSet, reader.Error)

Parse parses the next token and returns with an AST node and the updated reader

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

func NewHistory

func NewHistory() *History

NewHistory creates a history instance

func (*History) CallCount

func (h *History) CallCount() int

CallCount returns with the call count

func (*History) GetResults

func (h *History) GetResults(parserIndex int, pos int, leftRecCtx data.IntMap) (data.IntSet, ResultSet, reader.Error, bool)

GetResults return with a previously saved result

func (*History) RegisterCall

func (h *History) RegisterCall()

RegisterCall registers a call

func (*History) RegisterResults

func (h *History) RegisterResults(parserIndex int, pos int, curtailingParsers data.IntSet, resultSet ResultSet, err reader.Error, leftRecCtx data.IntMap)

RegisterResults registers a parser result for a certain position

type Parser

type Parser interface {
	Parse(h *History, leftRecCtx data.IntMap, r reader.Reader) (data.IntSet, ResultSet, reader.Error)
}

Parser defines a parser interface

type Result

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

Result represents one result of a parser

func NewResult

func NewResult(node ast.Node, reader reader.Reader) Result

NewResult creates a new result instance

func (Result) AsSet

func (r Result) AsSet() ResultSet

AsSet converts the result to a result set containing this result

func (Result) Node

func (r Result) Node() ast.Node

Node returns with the node

func (Result) Reader

func (r Result) Reader() reader.Reader

Reader returns with the reader

func (Result) String

func (r Result) String() string

type ResultSet

type ResultSet []Result

ResultSet is a set of results

func NewResultSet

func NewResultSet(results ...Result) ResultSet

NewResultSet creates a new result set

func (*ResultSet) Append

func (rs *ResultSet) Append(results ...Result)

Append adds a result to the parse result

Directories

Path Synopsis
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0

Jump to

Keyboard shortcuts

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