Documentation ¶
Index ¶
- Variables
- func Cat(result *Result) error
- func Cut(state *State, result *Result) error
- func EOL(state *State, result *Result) error
- func End(state *State, result *Result) error
- func Epsilon(state *State, result *Result) error
- func Head(state *State, result *Result) error
- func Int(state *State, result *Result) error
- func Line(state *State, result *Result) error
- func NewError(what string, pos Position) error
- func NewNestedError(name string, err error) error
- func Next(state *State) (byte, error)
- func Number(state *State, result *Result) error
- func Skip(state *State, n int) error
- func Spaces(state *State, result *Result) error
- func ToString(result *Result) error
- func Trail(state *State) ([]byte, error)
- type BoundError
- type Error
- type Map
- type NestedError
- type Parser
- func Any(qs ...interface{}) Parser
- func AsParser(q interface{}) Parser
- func AsParsers(qs ...interface{}) []Parser
- func Between(l, r byte) Parser
- func Byte(p ...byte) Parser
- func ByteRange(begin, end byte) Parser
- func Bytes(p []byte) Parser
- func Count(q interface{}, n int) Parser
- func Delim(q, s interface{}) Parser
- func Dry(q interface{}) Parser
- func Exact(q interface{}) Parser
- func Filter(filter ascii.Filter) Parser
- func Many(q interface{}) Parser
- func Maybe(q interface{}) Parser
- func Quoted(c byte) Parser
- func Rune(rs ...rune) Parser
- func RuneRange(begin, end rune) Parser
- func Runes(rs []rune) Parser
- func Seq(qs ...interface{}) Parser
- func String(s string) Parser
- func Until(q interface{}) Parser
- func Word(filter ascii.Filter) Parser
- type Position
- type Reader
- type Result
- type State
- func (s *State) Advance()
- func (s State) Buffer() []byte
- func (s *State) Clear()
- func (s *State) Drop()
- func (s State) Dump() []byte
- func (s State) Offset() int
- func (s *State) Pop()
- func (s State) Position() Position
- func (s *State) Push()
- func (s State) Pushed() bool
- func (s *State) Read(p []byte) (int, error)
- func (s *State) ReadByte() (byte, error)
- func (s *State) Request(n int) error
Constants ¶
This section is empty.
Variables ¶
var ( Null = Byte(0) Graphic = ByteRange(32, 126) Control = Any(ByteRange(0, 31), Byte(127)) Space = Byte(ascii.Space...) Upper = ByteRange('A', 'Z') Lower = ByteRange('a', 'z') Letter = Any(Upper, Lower) Digit = ByteRange('0', '9') Latin = Any(Letter, Digit) )
Parsers for matching ASCII character patterns.
var Void = &Result{}
Void is a global Result for storing unused parsing results.
Functions ¶
func EOL ¶ added in v1.0.1
EOL matches the end of a line. The end of a line is one of the following:
a carriage return (CR, '\r') a line feed (LF, '\n') a carriage return + line feed (CRLF, "\r\n") end of state
func End ¶ added in v1.0.1
End will match if the state buffer has been exhausted and no more bytes can be read from the io.Reader object.
func Int ¶ added in v1.0.1
Int will match an integer string and return its numerical representation. An integer is defined to be as follows in EBNF:
zero = `0` non-zero = `1` | `2` | `3` | `4` | `5` | `6` | `7` | `8` | `9` digit = zero | non-zero integer = [ ( `-` | `+` ) ], zero | digit, { digit }
This implementation is optimized so the parser will first scan as far as possible to match a valid integer and then retrieve a block of bytes and convert it to an `int` via strconv.Atoi.
func NewNestedError ¶ added in v1.0.1
NewNestedError creates a new NestedError.
func Number ¶
Number will match a floating point number. A number is defined to be as follows in EBNF:
zero = `0` non-zero = `1` | `2` | `3` | `4` | `5` | `6` | `7` | `8` | `9` digit = zero | non-zero integer = [ ( `-` | `+` ) ], zero | digit, { digit } fraction = `.`, digit, { digit } exponent = ( `e` | `E` ), [ ( `-` | `+` ) ], integer number = [ ( `-` | `+` ) ], integer, [ fraction ], [ exponent ]
This implementation is optimized so the parser will first scan as far as possible to match a valid number and then retrieve a block of bytes and convert it to a `float64` via strconv.ParseFloat.
Types ¶
type BoundError ¶ added in v1.0.1
type BoundError struct {
// contains filtered or unexported fields
}
BoundError is an error bound to a parser.
func (BoundError) Error ¶ added in v1.0.1
func (e BoundError) Error() string
Error satisfies the error interface.
func (BoundError) Unwrap ¶ added in v1.0.1
func (e BoundError) Unwrap() error
Unwrap returns the internal error value.
type Error ¶ added in v1.0.1
type Error struct {
// contains filtered or unexported fields
}
Error is a generic parser error.
type Map ¶
Map is the function signature for a result mapper.
type NestedError ¶ added in v1.0.1
type NestedError struct {
// contains filtered or unexported fields
}
NestedError is a nested error type.
func (NestedError) Error ¶ added in v1.0.1
func (e NestedError) Error() string
Error satisfies the error interface.
func (NestedError) Unwrap ¶ added in v1.0.1
func (e NestedError) Unwrap() error
Unwrap returns the internal error value.
type Parser ¶
Parser is the function signature of a parser.
func Any ¶
func Any(qs ...interface{}) Parser
Any creates a Parser which will attempt to match any of the given Parsers. If all of the given Parsers fail to match, the state will attempt to backtrack to the position before any of the given Parsers were applied. An error from the parser will be returned immediately if the state cannot be backtracked. Otherwise, the error from the last Parser will be returned.
func AsParser ¶
func AsParser(q interface{}) Parser
AsParser attempts to create a Parser for a given argument.
func AsParsers ¶
func AsParsers(qs ...interface{}) []Parser
AsParsers applies the AsParser function to each argument.
func Between ¶ added in v1.0.1
Between creates a Parser which will attempt to match a sequence of bytes between the given bytes. If a backslash appears in the middle of the string, the byte immediately following will be skipped.
func Byte ¶
Byte creates a Parser which will attempt to match the next single byte. If no bytes are given, it will match any byte. Otherwise, the given bytes will be tested for a match.
func ByteRange ¶
ByteRange creates a Parser which will attempt to match a byte between the given range inclusively.
func Count ¶
Count creates a Parser which will attempt to match the given Parser exactly N-times, where N is the given number.
func Delim ¶
func Delim(q, s interface{}) Parser
Delim creates a Parser which will attempt to match the first Parser multiple times like Many, but with the second Parser in between.
func Dry ¶
func Dry(q interface{}) Parser
Dry creates a Parser which will attempt to match the given Parser, but will restore the pre-matching state even if the given Parser matches.
func Exact ¶ added in v1.0.1
func Exact(q interface{}) Parser
Exact creates a Parser which will only match if the state is both at the beginning of the state and the given Parser exhausts the entire state after matching. This is equivalent to the following parser:
`pars.Seq(pars.Head, parser, pars.End).Map(Child(1))`
func Filter ¶ added in v1.0.1
Filter creates a Parser which will attempt to match the given ascii.Filter.
func Many ¶
func Many(q interface{}) Parser
Many creates a Parser which will attempt to match the given Parser as many times as possible.
func Maybe ¶ added in v1.0.1
func Maybe(q interface{}) Parser
Maybe creates a Parser which will attempt to match the given Parser but will not return an error upon a mismatch unless the state cannot be backtracked.
func Quoted ¶
Quoted creates a Parser which will attempt to match a sequence of bytes flanked by the given byte. This Parser is equivalent to the following:
Between(c, c)
func Rune ¶
Rune creates a Parser which will attempt to match the next single rune. If no runes are given, it will match any rune. Otherwise, the given runes will be tested for a match.
func RuneRange ¶
RuneRange creates a Parser which will attempt to match any rune within the given range inclusively.
func Seq ¶
func Seq(qs ...interface{}) Parser
Seq creates a Parser which will attempt to match all of the given Parsers in the given order. If any of the given Parsers fail to match, the state will attempt to backtrack to the position before any of the given Parsers were applied.
func Until ¶
func Until(q interface{}) Parser
Until creates a Parser which will advance the state until the given Parser matches, and return extent of the buffer up to the matching state position.
func Word ¶
Word creates a Parser which will attempt to match a group of bytes which satisfy the given filter.
func (Parser) Children ¶ added in v1.0.1
Children will keep the children associated to the given indices.
func (Parser) Error ¶ added in v1.0.1
Error will modify the Parser to return the given error if the Parser returns an error.
type Position ¶ added in v1.0.1
Position represents the line and byte numbers.
type Reader ¶ added in v1.0.1
type Reader struct {
// contains filtered or unexported fields
}
Reader is a special io.Reader that will skip all whitespaces unless it is a part of a string literal (quoted by a ", ', or `).
type Result ¶
Result is the output of a parser. One of three fields will be set:
Token: a byte sequence matching for a primitive parser. Value: any value, useful for constructing complex objects. Children: results for individual child parsers.
Use one of the Set* methods to mutually set fields.
func AsResult ¶ added in v1.0.1
func AsResult(arg interface{}) *Result
AsResult creates a new result based on the given argument type.
func AsResults ¶ added in v1.0.1
func AsResults(args ...interface{}) *Result
AsResults transforms a given list of arguments into a result with Children with each argument as is result.
func NewChildrenResult ¶ added in v1.0.1
NewChildrenResult creates a new result with the given children.
func NewTokenResult ¶ added in v1.0.1
NewTokenResult creates a new result with the given token.
func NewValueResult ¶ added in v1.0.1
func NewValueResult(v interface{}) *Result
NewValueResult creates a new result with the given value.
func (*Result) SetChildren ¶ added in v1.0.1
SetChildren sets the children and clears other fields.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State represents a parser state, which is a convenience wrapper for an io.Reader object with buffering and backtracking.
func FromString ¶
FromString creates a new state from the given string.
func (*State) Advance ¶
func (s *State) Advance()
Advance the state by the amount given in a previous Request call.
func (*State) Clear ¶
func (s *State) Clear()
Clear will discard the buffer contents prior to the current state offset and drop all pushed states.
func (*State) Drop ¶ added in v1.0.1
func (s *State) Drop()
Drop will discard the most recently pushed state.
func (State) Dump ¶ added in v1.0.1
Dump returns the entire remaining buffer content. Note that the returned byte slice will not always contain the entirety of the bytes that can be read by the io.Reader object.
func (*State) Pop ¶ added in v1.0.1
func (s *State) Pop()
Pop will backtrack to the most recently pushed state.
func (*State) Push ¶ added in v1.0.1
func (s *State) Push()
Push the current state position for backtracking.
func (*State) Request ¶ added in v1.0.1
Request checks if the state contains at least the given number of bytes, additionally reading from the io.Reader object as necessary when the internal buffer is exhausted. If the call to Read for the io.Reader object returns an error, Request will return the corresponding error. A subsequent call to Advance will advance the state offset as far as possible.