Documentation ¶
Overview ¶
parsec 的部分代码实现参考了 https://github.com/sanyaade-buildtools/goparsec 和 https://github.com/prataprc/goparsec 但是我需要一个面向 unicode 的简洁实现,所以只好重写了自己的版本。
parser state 包参考了 golang 的内置包定义,部分代码模仿或来自 text/scanner ,其中有向 https://github.com/sanyaade-buildtools/goparsec 学习一部分设计思路
Index ¶
- Variables
- func AnyRune(st ParseState) (interface{}, error)
- func Eof(st ParseState) (interface{}, error)
- func ExtractString(input interface{}) string
- func Int(st ParseState) (interface{}, error)
- type ParseError
- type ParseState
- type Parser
- func Between(start, end, p Parser) Parser
- func Bind(parser Parser, fun func(interface{}) Parser) Parser
- func Bind_(parserx, parsery Parser) Parser
- func Binds(first Parser, then ...func(interface{}) Parser) Parser
- func Binds_(parsers ...Parser) Parser
- func Choice(parsers ...Parser) Parser
- func Either(parserx, parsery Parser) Parser
- func Fail(message string) Parser
- func Many(parser Parser) Parser
- func Many1(parser Parser) Parser
- func ManyTil(p, end Parser) Parser
- func Maybe(p Parser) Parser
- func NoneOf(runes string) Parser
- func OneOf(runes string) Parser
- func Option(v interface{}, parser Parser) Parser
- func Return(v interface{}) Parser
- func ReturnString(input interface{}) Parser
- func Rune(r rune) Parser
- func RuneChecker(checker func(rune) bool, expected string) Parser
- func SepBy(p, sep Parser) Parser
- func SepBy1(p, sep Parser) Parser
- func Skip(p Parser) Parser
- func String(s string) Parser
- func Try(parser Parser) Parser
- func Union(parsers ...Parser) Parser
- func UnionAll(parsers ...Parser) Parser
- type StateInMemory
- func (this *StateInMemory) Column() int
- func (this *StateInMemory) Line() int
- func (this *StateInMemory) Next(pred func(rune) bool) (r rune, match bool, err error)
- func (this *StateInMemory) Pos() int
- func (this *StateInMemory) SeekTo(pos int)
- func (this *StateInMemory) Trap(message string, args ...interface{}) error
Constants ¶
This section is empty.
Variables ¶
View Source
var Digit = RuneChecker(unicode.IsDigit, "digit")
View Source
var Eol = Either(Eof, NewLine)
View Source
var Float = Either(UnsignedFloat, Bind_(Rune('-'), func(st ParseState) (interface{}, error) { value, err := UnsignedFloat(st) if err == nil { return ExtractString(append([]interface{}{'-'}, value.([]interface{})...)), nil } else { return nil, err } }))
View Source
var Letter = RuneChecker(unicode.IsLetter, "letter")
View Source
var NewLine = OneOf(NewLineRunes)
View Source
var NewLineRunes = "\r\n"
View Source
var Number = RuneChecker(unicode.IsNumber, "number")
View Source
var Space = RuneChecker(unicode.IsSpace, "space")
View Source
var Spaces = Skip(Space)
View Source
var UnsignedFloat = Bind(Many(Digit), func(input interface{}) Parser { return func(st ParseState) (interface{}, error) { value, err := Bind_(Rune('.'), Many1(Digit))(st) if err != nil { return nil, err } ret := []interface{}{} ret = append(ret, input.([]interface{})...) ret = append(ret, '.') ret = append(ret, value.([]interface{})...) return ExtractString(ret), nil } })
Functions ¶
func AnyRune ¶
func AnyRune(st ParseState) (interface{}, error)
func Eof ¶
func Eof(st ParseState) (interface{}, error)
func ExtractString ¶
func ExtractString(input interface{}) string
func ExtraString 将 interface{} 转成 string,如果输入数据与前面提到的规范不符,会 panic
func Int ¶
func Int(st ParseState) (interface{}, error)
Types ¶
type ParseError ¶
func (ParseError) Error ¶
func (err ParseError) Error() string
type ParseState ¶
type ParseState interface { Next(pred func(rune) bool) (r rune, ok bool, err error) Line() int Column() int Pos() int SeekTo(int) Trap(message string, args ...interface{}) error }
func MemoryParseState ¶
func MemoryParseState(data string) ParseState
type Parser ¶
type Parser func(ParseState) (interface{}, error)
func Choice ¶
Choice 实现为以下逻辑的迭代版本:
func Choice(parsers ...Parser) Parser { switch len(parsers) { case 0: panic(errors.New("empty choice chain")) case 1: return parsers[0] case 2: return Either(Try(parsers[0]), Try(parsers[1])) default: return Either(Try(parsers[0]), Choice(parsers[1:])) } }
其实我比较希望把下面那个东西实现成上面这个样子,就是好像在golang里不太经济……
func ReturnString ¶
func ReturnString(input interface{}) Parser
func ReturnString 用 Return 包装 ExtraString,使其适用于 Bind 这样的组合子。
type StateInMemory ¶
type StateInMemory struct {
// contains filtered or unexported fields
}
func (*StateInMemory) Column ¶
func (this *StateInMemory) Column() int
func (*StateInMemory) Line ¶
func (this *StateInMemory) Line() int
func (*StateInMemory) Pos ¶
func (this *StateInMemory) Pos() int
func (*StateInMemory) SeekTo ¶
func (this *StateInMemory) SeekTo(pos int)
func (*StateInMemory) Trap ¶
func (this *StateInMemory) Trap(message string, args ...interface{}) error
Directories ¶
Path | Synopsis |
---|---|
parsec 的部分代码实现参考了 https://github.com/sanyaade-buildtools/goparsec 和 https://github.com/prataprc/goparsec 但是我需要一个面向 unicode 的简洁实现,所以只好重写了自己的版本。
|
parsec 的部分代码实现参考了 https://github.com/sanyaade-buildtools/goparsec 和 https://github.com/prataprc/goparsec 但是我需要一个面向 unicode 的简洁实现,所以只好重写了自己的版本。 |
Click to show internal directories.
Click to hide internal directories.