pego

package module
v0.0.0-...-a3d134c Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2014 License: MIT Imports: 5 Imported by: 1

README

pego

This is a pattern matching library for Go. It is based on lpeg, which uses a flavor of PEG.

This is the official continuation of the project of the same name started by Markus Jarderot. He wrote the implementation and I've updated the project to work with newer Go versions.

The original project page is located here: https://code.google.com/p/pego/

##Example

pat := Grm("S", map[string]*Pattern{
	"S": Ref("A").Clist(),
	"A": Seq(
		NegSet("()").Rep(0, -1),
		Seq(
			Ref("B"),
			NegSet("()").Rep(0, -1),
		).Rep(0, -1)).Csimple(),
	"B": Seq(
		"(", Ref("A"), ")"),
})

##More information

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match(program *Pattern, input string) (interface{}, error, int)

Main match function

Types

type CapStack

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

func NewCapStack

func NewCapStack() *CapStack

func (*CapStack) Close

func (s *CapStack) Close(end int) (*CaptureEntry, int)

Close and return the closest open capture

func (*CapStack) Mark

func (s *CapStack) Mark() int

Create and return a mark

func (*CapStack) Open

func (s *CapStack) Open(p int, start int) *CaptureEntry

Open and return an new capture

func (*CapStack) Pop

func (s *CapStack) Pop(count int) []*CaptureResult

Pop and return the top `count` captures

func (*CapStack) Rollback

func (s *CapStack) Rollback(mark int)

Rollback to a previous mark

func (*CapStack) String

func (s *CapStack) String() string

type CaptureEntry

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

type CaptureHandler

type CaptureHandler interface {
	Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)
}

Interface for all capture handlers

type CaptureResult

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

Used when returning the values Similar to CaptureEntry, but without some internal values

type ConstCapture

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

Captures a constant value

func (*ConstCapture) Process

func (h *ConstCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*ConstCapture) String

func (h *ConstCapture) String() string

type FunctionCapture

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

Calls a function with all sub-captures, and captures the return value. If functions reports an error, let it bubble up.

func (*FunctionCapture) Process

func (h *FunctionCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*FunctionCapture) String

func (h *FunctionCapture) String() string

type IAny

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

Match `count` of any character

func (*IAny) String

func (op *IAny) String() string

type IBackCommit

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

Backtracks to a fallback-point, and does a relative jump.

func (*IBackCommit) String

func (op *IBackCommit) String() string

type ICall

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

Push return address to stack, and do a relative jump.

func (*ICall) String

func (op *ICall) String() string

type IChar

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

Match a single character.

func (*IChar) String

func (op *IChar) String() string

type ICharset

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

Match a character from a set TODO(mizardx): Unicode?

func (*ICharset) Has

func (op *ICharset) Has(char byte) bool

Charset contains character?

func (*ICharset) String

func (op *ICharset) String() string

type IChoice

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

Add a fallback point at offset, and continue on next instruction.

func (*IChoice) String

func (op *IChoice) String() string

type ICloseCapture

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

Close the nearest open capture

func (*ICloseCapture) String

func (op *ICloseCapture) String() string

type ICommit

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

Pop a fallback point, and do a relative jump.

func (*ICommit) String

func (op *ICommit) String() string

type IEmptyCapture

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

Open and close a new empty capture

func (*IEmptyCapture) String

func (op *IEmptyCapture) String() string

type IEnd

type IEnd struct{}

End of program (Last instuction only)

func (*IEnd) String

func (op *IEnd) String() string

type IFail

type IFail struct{}

Roll back to closest fallback point.

func (*IFail) String

func (op *IFail) String() string

type IFailTwice

type IFailTwice struct{}

Roll back to the next closest fallback point.

func (*IFailTwice) String

func (op *IFailTwice) String() string

type IFullCapture

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

Open and close a new capture of fixed size

func (*IFullCapture) String

func (op *IFullCapture) String() string

type IGiveUp

type IGiveUp struct{}

Stop matching and return a negative result.

func (*IGiveUp) String

func (op *IGiveUp) String() string

type IJump

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

Relative jump.

func (*IJump) String

func (op *IJump) String() string

type IOpenCall

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

Unresolved call. Used by grammars.

func (*IOpenCall) String

func (op *IOpenCall) String() string

type IOpenCapture

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

Open a new capture

func (*IOpenCapture) String

func (op *IOpenCapture) String() string

type IPartialCommit

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

Update top stack entry, and do a relative jump.

func (*IPartialCommit) String

func (op *IPartialCommit) String() string

type IReturn

type IReturn struct{}

Pop a return address from the stack and jump to it.

func (*IReturn) String

func (op *IReturn) String() string

type ISpan

type ISpan struct {
	ICharset
}

Match zero or more characters from a set

func (*ISpan) String

func (op *ISpan) String() string

type Instruction

type Instruction interface {
	fmt.Stringer
}

Interface for instructions. XXX(mizardx): Make this more specific?

type ListCapture

type ListCapture struct{}

Captures a list of all sub-captures

func (*ListCapture) Process

func (h *ListCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*ListCapture) String

func (h *ListCapture) String() string

type Pattern

type Pattern []Instruction

func And

func And(p *Pattern) *Pattern

Positive look-ahead for the pattern.

func Any

func Any(n int) *Pattern

Matches `n` of any character.

func Cconst

func Cconst(value interface{}) *Pattern

Does a constant capture.

func Cfunc

func Cfunc(p *Pattern, f func([]*CaptureResult) (interface{}, error)) *Pattern

Does a function capture.

func Char

func Char(char byte) *Pattern

Matches `char`

func Clist

func Clist(p *Pattern) *Pattern

Does a list capture.

func Cposition

func Cposition() *Pattern

Does a position capture.

func Csimple

func Csimple(p *Pattern) *Pattern

Does a simple capture of the pattern.

func Cstring

func Cstring(p *Pattern, format string) *Pattern

Does a string capture.

func Csubst

func Csubst(p *Pattern) *Pattern

Does a substitution capture.

func Fail

func Fail() *Pattern

Always fails.

func Grm

func Grm(start string, grammar map[string]*Pattern) *Pattern

Match a grammar. start: name of the first pattern grammar: map of names to patterns

func Lit

func Lit(text string) *Pattern

Match the text litteraly.

func NegSet

func NegSet(chars string) *Pattern

Match a negated set of characters. Opposite of Set()

func Not

func Not(p *Pattern) *Pattern

Negative look-ahead for the pattern.

func Or

func Or(p1, p2 *Pattern) *Pattern

Ordered choice of p1 and p2

func Pat

func Pat(value interface{}) *Pattern

Resolve a value to a pattern. Patterns are return unmodified.

  • true gives a pattern that always succeeds. Equivalent to Succ().
  • false gives a pattern that always fails. Equivalent to Fail().
  • n < 0 asserts that there are at least -n characters. Equivalent to And(Any(n)).
  • n >= 0 matches n characters. Equivalent to Any(n).
  • A string matches itself. Equivalent to Lit(value)

func Ref

func Ref(name string) *Pattern

Open reference to a name. Use with grammars.

func Rep

func Rep(p *Pattern, min, max int) *Pattern

Repeat pattern between `min` and `max` times. max == -1 means unlimited.

func Seq

func Seq(args ...interface{}) *Pattern

A sequence of values, instructions and other patterns. (See Seq2)

func Seq2

func Seq2(args []interface{}) *Pattern

A sequence of values, instructions and other patterns Instructions that represent jumps are updated to match the sizes of the argument.

func Set

func Set(chars string) *Pattern

Match a set of characters.

func Succ

func Succ() *Pattern

Always succeeds (an empty pattern).

func (*Pattern) Cfunc

func (p *Pattern) Cfunc(f func([]*CaptureResult) (interface{}, error)) *Pattern

A function capture of this pattern.

func (*Pattern) Clist

func (p *Pattern) Clist() *Pattern

A list capture of this pattern.

func (*Pattern) Csimple

func (p *Pattern) Csimple() *Pattern

A simple capture of this pattern.

func (*Pattern) Cstring

func (p *Pattern) Cstring(format string) *Pattern

A string capture of this pattern.

func (*Pattern) Csubst

func (p *Pattern) Csubst() *Pattern

A substition capture of this pattern.

func (*Pattern) Exc

func (p *Pattern) Exc(pred *Pattern) *Pattern

Match this pattern, except for when `pred` matches.

func (*Pattern) Or

func (p *Pattern) Or(ps ...interface{}) *Pattern

List of alternatives.

func (*Pattern) Rep

func (p *Pattern) Rep(min, max int) *Pattern

Match this pattern between `min` and `max` times. max == -1 means unlimited.

func (*Pattern) Resolve

func (p *Pattern) Resolve(name string, target *Pattern) *Pattern

Resolve an open reference. Consider using a grammar instead.

func (*Pattern) String

func (p *Pattern) String() string

type PositionCapture

type PositionCapture struct{}

Captures the current input position

func (*PositionCapture) Process

func (h *PositionCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*PositionCapture) String

func (h *PositionCapture) String() string

type SimpleCapture

type SimpleCapture struct{}

Captures the matched substring

func (*SimpleCapture) Process

func (h *SimpleCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*SimpleCapture) String

func (h *SimpleCapture) String() string

type Stack

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

func (*Stack) At

func (s *Stack) At(i int) interface{}

func (*Stack) Len

func (s *Stack) Len() int

func (*Stack) Pop

func (s *Stack) Pop() interface{}

func (*Stack) Push

func (s *Stack) Push(i interface{})

func (*Stack) String

func (s *Stack) String() string

type StackEntry

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

type StringCapture

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

Capture a string created from a format applied to the sub-captures.

func (*StringCapture) Process

func (h *StringCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*StringCapture) String

func (h *StringCapture) String() string

type SubstCapture

type SubstCapture struct{}

Capture a string, with all sub-captures replaced by their string-representation. XXX(mizardx): Better explaination?

func (*SubstCapture) Process

func (h *SubstCapture) Process(input string, start, end int, captures *CapStack, subcaps int) (interface{}, error)

func (*SubstCapture) String

func (h *SubstCapture) String() string

Jump to

Keyboard shortcuts

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