pogo

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: Apache-2.0 Imports: 9 Imported by: 3

README

pogo

Another parser combinator library for Go

How do I use it?

  • Describe your grammar using the provided combinators, or extend with your own
  • Provide an implementation of both parser and lexer interfaces
  • pogo.Do will now produce a parse tree
  • (Optional) Use the provided generator to produce an easy to use parse tree visitor, and implement it to translate your parse tree to a higher level representation.

TL;DR take a look at the provided json parser

Should I use it?

Sure, if you want. I can't guarantee maintainance, though!

Documentation

Index

Constants

View Source
const TOK_EOF = TokenType("")

Variables

View Source
var Debug = false
View Source
var EOF = Tok(TOK_EOF)
View Source
var ErrParsed = ErrParsedType{}

ErrParsed is returned when nothing could be parsed, and an error was raised

View Source
var NilItem = Item{}
View Source
var NilParsed = NilParsedType{}

NilParsed is returned when nothing could be parsed, but no error was raised

View Source
var VisitorTemplate = visitorTemplate{
	Productions: make(map[string]reflect.Type),
}

Functions

func Do

func Do(p interface{}, ps ParseState) (Parsed, ParseState)

Types

type BaseParseState

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

func (BaseParseState) CloneBase

func (b BaseParseState) CloneBase() BaseParseState

func (*BaseParseState) Errors

func (b *BaseParseState) Errors() []ParseError

func (*BaseParseState) Pos

func (b *BaseParseState) Pos() int

type ErrParsedType

type ErrParsedType struct{}

func (ErrParsedType) String

func (ErrParsedType) String() string

type Item

type Item struct {
	Token      TokenType
	Value      string
	Offset     Pos
	Line       int
	Column     int
	Index      int
	SourceFile string
}

func (Item) End

func (i Item) End() Pos

func (Item) Start

func (i Item) Start() Pos

func (Item) String

func (i Item) String() string

type LexerIface

type LexerIface interface {
	At(state ParseState) Item
}

type NamedValue

type NamedValue struct {
	Name  string
	Value Parsed
}

func (NamedValue) String

func (n NamedValue) String() string

type NilParsedType

type NilParsedType struct{}

func (NilParsedType) String

func (NilParsedType) String() string

type ParseError

type ParseError struct {
	Token Item
	Error error
}

type ParseState

type ParseState interface {
	Lexer() LexerIface
	Pos() int
	Errors() []ParseError
	Clone() ParseState
	// contains filtered or unexported methods
}

type Parsed

type Parsed interface {
	String() string
	// contains filtered or unexported methods
}

type Parser

type Parser func(p ParseState) (Parsed, ParseState)

func Char

func Char(c rune) Parser

Char is a shortcut for parsing single character tokens

func Many

func Many(p interface{}) Parser

func Many1

func Many1(p interface{}) Parser

func Maybe

func Maybe(p interface{}) Parser

Maybe executes a parser which may fail. If it fails no input is consumed and NilParsed is returned. Errors are not sent to the error handler.

func MaybePeek

func MaybePeek(p interface{}) Parser

MaybePeek parses p without consuming any input. If it fails, NilParsed is returned. Errors are not sent to the error handler.

func Named

func Named(ident string, p interface{}) Parser

func Or

func Or(p ...interface{}) Parser

Or accepts 1..n parsers and attempts them one by one. The first successful match is returned. If no parsers succeed, then the error of the most successful (by length) parser is raised.

func Peek

func Peek(p interface{}) Parser

Peek parses p without consuming any input. Errors are sent to the error handler.

func Prod

func Prod(ident string, p interface{}) Parser

Parses a named production

func Recover

func Recover(p interface{}, recoverfn RecoverFunc) Parser

Recover parses p. If any errors are raised while parsing p, recoverfn is called and its results become the result of Recover. Errors are sent to the error handler.

func RecoverTo

func RecoverTo(token TokenType, p interface{}) Parser

RecoverTo parses p. If any errors are raised while parsing p, the lexer is fast-fowarded until after the next instance of token. Errors are sent to the error handler.

func Root

func Root(p interface{}) Parser

Root parses p. If any errors are raised while parsing p, ErrParsed is returned. Errors are sent to the error handler.

func SepBy

func SepBy(p, delim interface{}) Parser

func SepBy1

func SepBy1(p, delim interface{}) Parser

func SepBy1Term

func SepBy1Term(p, delim, terminator interface{}) Parser

func SepByTerm

func SepByTerm(p, delim, terminator interface{}) Parser

func Seq

func Seq(p ...interface{}) Parser

Sequence accepts 1..n parsers and expects them to succeed in their defined sequence

func Tok

func Tok(token TokenType) Parser

Tok is a parser which matches a given TokenType. The token is only consumed if it matches.

func Try

func Try(p interface{}) Parser

Try parses p. If any errors are raised while parsing p, NilParsed is returned. Errors are not sent to the error handler.

func TryRecover

func TryRecover(p interface{}, recoverfn RecoverFunc) Parser

TryRecover parses p. If any errors are raised while parsing p, recoverfn is called and its results become the result of Recover. Errors are not sent to the error handler.

func TypedProd

func TypedProd(ident string, typ interface{}, p interface{}) Parser

Parses a named and typed production

func TypedProdIface

func TypedProdIface(ident string, typ interface{}, p interface{}) Parser

Parses a named and typed production. Special case for interface typed productions. Usage: TypedProdIface("prod", (*MyIface)(nil))

type Pos

type Pos token.Pos

type Production

type Production struct {
	Ident    string
	Children Sequence
}

func (Production) String

func (t Production) String() string

type RecoverFunc

type RecoverFunc func(start ParseState, end ParseState) (Parsed, ParseState)

type Sequence

type Sequence []Parsed

func (Sequence) Filter

func (s Sequence) Filter(tokens ...TokenType) Sequence

func (Sequence) Flatten

func (s Sequence) Flatten() []Parsed

func (Sequence) String

func (s Sequence) String() string

type TokenType

type TokenType string

Directories

Path Synopsis
Code generated by pogo DO NOT EDIT
Code generated by pogo DO NOT EDIT

Jump to

Keyboard shortcuts

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