Documentation
¶
Index ¶
- Variables
- type Input
- type Match
- type ParseError
- type Parser
- func All[T any](parsers ...Parser[T]) Parser[[]T]
- func Any[T any](parsers ...Parser[T]) Parser[T]
- func AtLeast[T any](min int, p Parser[T]) Parser[[]T]
- func AtMost[T any](max int, p Parser[T]) Parser[[]T]
- func Convert[A, B any](parser Parser[A], converter func(a A) (B, error)) Parser[B]
- func EOF[T any]() Parser[T]
- func Func[T any](f func(in *Input) (item T, ok bool, err error)) Parser[T]
- func MustRegexp(exp string) (p Parser[string])
- func OneOrMore[T any](p Parser[T]) Parser[[]T]
- func Optional[T any](parser Parser[T]) Parser[Match[T]]
- func Or[A any, B any](a Parser[A], b Parser[B]) Parser[Tuple2[Match[A], Match[B]]]
- func Regexp(exp string) (p Parser[string], err error)
- func Repeat[T any](min, max int, p Parser[T]) Parser[[]T]
- func Rune(r rune) Parser[string]
- func RuneIn(s string) Parser[string]
- func RuneInRanges(ranges ...*unicode.RangeTable) Parser[string]
- func RuneNotIn(s string) Parser[string]
- func RuneWhere(predicate func(r rune) bool) Parser[string]
- func SequenceOf2[A, B any](a Parser[A], b Parser[B]) Parser[Tuple2[A, B]]
- func SequenceOf3[A, B, C any](a Parser[A], b Parser[B], c Parser[C]) Parser[Tuple3[A, B, C]]
- func SequenceOf4[A, B, C, D any](a Parser[A], b Parser[B], c Parser[C], d Parser[D]) Parser[Tuple4[A, B, C, D]]
- func SequenceOf5[A, B, C, D, E any](a Parser[A], b Parser[B], c Parser[C], d Parser[D], e Parser[E]) Parser[Tuple5[A, B, C, D, E]]
- func SequenceOf6[A, B, C, D, E, F any](a Parser[A], b Parser[B], c Parser[C], d Parser[D], e Parser[E], f Parser[F]) Parser[Tuple6[A, B, C, D, E, F]]
- func SequenceOf7[A, B, C, D, E, F, G any](a Parser[A], b Parser[B], c Parser[C], d Parser[D], e Parser[E], f Parser[F], ...) Parser[Tuple7[A, B, C, D, E, F, G]]
- func SequenceOf8[A, B, C, D, E, F, G, H any](a Parser[A], b Parser[B], c Parser[C], d Parser[D], e Parser[E], f Parser[F], ...) Parser[Tuple8[A, B, C, D, E, F, G, H]]
- func SequenceOf9[A, B, C, D, E, F, G, H, I any](a Parser[A], b Parser[B], c Parser[C], d Parser[D], e Parser[E], f Parser[F], ...) Parser[Tuple9[A, B, C, D, E, F, G, H, I]]
- func String(s string) Parser[string]
- func StringFrom[T any](parsers ...Parser[T]) Parser[string]
- func StringInsensitive(s string) Parser[string]
- func StringUntil[T any](delimiter Parser[T]) Parser[string]
- func StringUntilEOF[T any](delimiter Parser[T]) Parser[string]
- func Then[A any, B any](a Parser[A], b Parser[B]) Parser[Tuple2[A, B]]
- func Times[T any](n int, p Parser[T]) Parser[[]T]
- func Until[T, D any](parser Parser[T], delimiter Parser[D]) Parser[[]T]
- func UntilEOF[T, D any](parser Parser[T], delimiter Parser[D]) Parser[[]T]
- func ZeroOrMore[T any](p Parser[T]) Parser[[]T]
- type Position
- type Tuple2
- type Tuple3
- type Tuple4
- type Tuple5
- type Tuple6
- type Tuple7
- type Tuple8
- type Tuple9
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var AnyRune = RuneWhere(func(r rune) bool { return true })
AnyRune matches any single rune.
var CR = Rune('\r')
CR is a carriage return.
var CRLF = String("\r\n")
CRLF parses a carriage returned, followed by a line feed, used by Windows systems as the newline.
var LF = Rune('\n')
CR parses a line feed, used by Unix systems as the newline.
var Letter = RuneInRanges(unicode.Letter)
Letter returns a parser which accepts a rune within the Letter Unicode range.
var NewLine = Any(CRLF, LF)
NewLine matches either a Windows or Unix line break character.
var OptionalWhitespace = Func(func(in *Input) (output string, ok bool, err error) { output, ok, err = Whitespace.Parse(in) if err != nil { return } return output, true, nil })
OptionalWhitespace parses optional whitespace.
Functions ¶
This section is empty.
Types ¶
type Input ¶
type Input struct {
// contains filtered or unexported fields
}
InputString is an input used by parsers. It stores the current location and character positions.
func (*Input) Position ¶
Position returns the zero-bound index, line and column number of the current position within the stream.
func (*Input) PositionAt ¶
Position returns the zero-bound index, line and column number of the current position within the stream.
type ParseError ¶
func Error ¶
func Error(msg string, pos Position) ParseError
func (ParseError) Error ¶
func (e ParseError) Error() string
type Parser ¶
Parser is implemented by all parsers.
Example ¶
package main import ( "fmt" "strconv" "github.com/a-h/parse" ) func main() { type GotoStatement struct { Line int64 } gotoParser := parse.Func(func(in *parse.Input) (item GotoStatement, ok bool, err error) { start := in.Index() if _, ok, err = parse.String("GOTO ").Parse(in); err != nil || !ok { // Rollback, and return. in.Seek(start) return } // Read until the next newline or the EOF. until := parse.Any(parse.NewLine, parse.EOF[string]()) var lineNumber string if lineNumber, ok, err = parse.StringUntil(until).Parse(in); err != nil || !ok { err = parse.Error("Syntax error: GOTO is missing line number", in.Position()) return } // We must have a valid line number now, or there is a syntax error. item.Line, err = strconv.ParseInt(lineNumber, 10, 64) if err != nil { return item, false, parse.Error("Syntax error: GOTO has invalid line number", in.Position()) } // Chomp the newline we read up to. _, _, _ = until.Parse(in) return item, true, nil }) inputs := []string{ "GOTO 10", "GOTO abc", "FOR i = 0", } for _, input := range inputs { stmt, ok, err := gotoParser.Parse(parse.NewInput(input)) fmt.Printf("%+v, ok=%v, err=%v\n", stmt, ok, err) } }
Output: {Line:10}, ok=true, err=<nil> {Line:0}, ok=false, err=Syntax error: GOTO has invalid line number: line 0, col 8 {Line:0}, ok=false, err=<nil>
var Whitespace Parser[string] = StringFrom(OneOrMore(RuneInRanges(unicode.White_Space)))
Whitespace parses whitespace.
ZeroToNine matches a single rune from 0123456789.
func All ¶
All parses all of the parsers in the list in sequence and combines the result.
Example ¶
package main import ( "fmt" "github.com/a-h/parse" ) func main() { abcParser := parse.All(parse.String("A"), parse.String("B"), parse.String("C")) fmt.Println(abcParser.Parse(parse.NewInput("ABC"))) fmt.Println(abcParser.Parse(parse.NewInput("AB"))) fmt.Println(abcParser.Parse(parse.NewInput("A"))) }
Output: [A B C] true <nil> [A B] false <nil> [A] false <nil>
func Any ¶
Any parses any one of the parsers in the list.
Example ¶
package main import ( "fmt" "github.com/a-h/parse" ) func main() { abParser := parse.Any(parse.String("A"), parse.String("B")) fmt.Println(abParser.Parse(parse.NewInput("A"))) fmt.Println(abParser.Parse(parse.NewInput("B"))) fmt.Println(abParser.Parse(parse.NewInput("C"))) }
Output: A true <nil> B true <nil> false <nil>
func MustRegexp ¶
MustRegexp creates a parse that parses from the input's current position. Passing in a regular expression that doesn't compile will result in a panic.
func Optional ¶
Optional converts the given parser into an optional parser.
Example ¶
package main import ( "fmt" "github.com/a-h/parse" ) func main() { abcParser := parse.StringFrom( parse.StringFrom(parse.Optional(parse.String("A"))), parse.String("B"), ) fmt.Println(abcParser.Parse(parse.NewInput("ABC"))) fmt.Println(abcParser.Parse(parse.NewInput("B"))) fmt.Println(abcParser.Parse(parse.NewInput("A"))) }
Output: AB true <nil> B true <nil> false <nil>
func Or ¶
Or returns a success if either a or b can be parsed. If both a and b match, a takes precedence.
func RuneInRanges ¶
func RuneInRanges(ranges ...*unicode.RangeTable) Parser[string]
RuneInRanges matches a single rune when the rune is withig one of the given Unicode ranges.
func SequenceOf3 ¶
func SequenceOf4 ¶
func SequenceOf5 ¶
func SequenceOf6 ¶
func SequenceOf7 ¶
func SequenceOf8 ¶
func SequenceOf9 ¶
func String ¶
String matches a given string constant.
Example ¶
package main import ( "fmt" "github.com/a-h/parse" ) func main() { abParser := parse.Any(parse.String("A")) fmt.Println(abParser.Parse(parse.NewInput("A"))) fmt.Println(abParser.Parse(parse.NewInput("B"))) }
Output: A true <nil> false <nil>
func StringFrom ¶
StringFrom returns the string range captured by the given parsers.
func StringInsensitive ¶
StringInsensitive matches a given string constant using Unicode case folding.
func StringUntil ¶
StringUntil matches until the delimiter is reached.
func StringUntilEOF ¶
StringUntilEOF matches until the delimiter or the end of the file is reached.
func Then ¶
Then matches a sequence of two parsers. For multiples of the same type, use Times, Repeat, AtLeast, AtMost, ZeroOrMore, OneOrMore.
func ZeroOrMore ¶
ZeroOrMore matches the given parser zero or more times.
Source Files
¶
- all.go
- any.go
- convert.go
- digits.go
- eof.go
- error.go
- input.go
- optional.go
- or.go
- regexp.go
- rune.go
- sequenceof2.go
- sequenceof3.go
- sequenceof4.go
- sequenceof5.go
- sequenceof6.go
- sequenceof7.go
- sequenceof8.go
- sequenceof9.go
- string.go
- stringfrom.go
- stringuntil.go
- then.go
- times.go
- tuples.go
- types.go
- until.go
- whitespace.go