Documentation ¶
Index ¶
- func Any[T stream.Token]() parser.Func[T, T]
- func EOF[T stream.Token]() parser.Func[T, bool]
- func NoneOf[T stream.Token](tokens []T) parser.Func[T, T]
- func OneOf[T stream.Token](tokens []T) parser.Func[T, T]
- func Produce[T stream.Token, O any](f func() O) parser.Func[T, O]
- func Satisfy[T stream.Token](predicate func(T) bool) parser.Func[T, T]
- func SatisfyMap[T stream.Token, O any](predicate func(T) (O, error)) parser.Func[T, O]
- func Token[T stream.Token](tok T) parser.Func[T, T]
- func Tokens[T stream.Token](cmp func(lhs, rhs T) bool, expected, tokens []T) parser.Func[T, []T]
- func Value[T stream.Token, O any](v O) parser.Func[T, O]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Any ¶
Any parses any token.
Example ¶
p := token.Any[rune]() fmt.Println(p([]rune("apple"))) fmt.Println(p(nil))
Output: 97 [112 112 108 101] <nil> 0 [] unexpected EOF
func EOF ¶
EOF succeeds only if the stream is at end of input, fails otherwise.
Example ¶
p := token.EOF[rune]() fmt.Println(p(nil)) fmt.Println(p([]rune("foobar")))
Output: true [] <nil> false [102 111 111 98 97 114] eof, expected end of input, actual "foobar", unexpected
func NoneOf ¶
NoneOf extract one token and succeeds if it is not part of `tokens`.
Example ¶
p := token.NoneOf([]rune("abc")) fmt.Println(p([]rune("apple"))) fmt.Println(p([]rune("foobar")))
Output: 97 [112 112 108 101] none of "abc", satisfy, actual 'a', unexpected 102 [111 111 98 97 114] <nil>
func OneOf ¶
OneOf extract one token and succeeds if it is part of `tokens`.
Example ¶
p := token.OneOf([]rune("abc")) fmt.Println(p([]rune("apple"))) fmt.Println(p([]rune("foobar")))
Output: 97 [112 112 108 101] <nil> 102 [111 111 98 97 114] one of "abc", satisfy, actual 'f', unexpected
func Produce ¶
Produce always returns the value produced by calling `f`.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/token" ) func main() { p := token.Produce[rune](func() string { return "foo" }) fmt.Println(p([]rune("bar"))) }
Output: foo [98 97 114] <nil>
func Satisfy ¶
Satisfy parses a token and succeeds depending on the result of `predicate`.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/token" ) func main() { p := token.Satisfy(func(c rune) bool { return c == '!' || c == '?' }) fmt.Println(p([]rune("!"))) fmt.Println(p([]rune("?"))) fmt.Println(p([]rune("#"))) }
Output: 33 [] <nil> 63 [] <nil> 35 [] satisfy, actual '#', unexpected
func SatisfyMap ¶
SatisfyMap parses a token and passes it to `predicate`. If `predicate` succeeds and returns the value. If `predicate` returns error the parser fails without consuming any input.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser" "github.com/flier/gocombine/pkg/parser/token" ) func main() { p := token.SatisfyMap(func(c rune) (bool, error) { switch c { case 'y', 'Y': return true, nil case 'n', 'N': return false, nil default: return false, parser.UnexpectedRange([]rune{'y', 'Y', 'n', 'N'}, []rune{c}) } }) fmt.Println(p([]rune("y"))) fmt.Println(p([]rune("N"))) fmt.Println(p([]rune("#"))) }
Output: true [] <nil> false [] <nil> false [] satisfy map, actual '#', unexpected
func Token ¶
Token parses a character and succeeds if the character is equal to `tok`.
Example ¶
p := token.Token('a') fmt.Println(p([]rune("apple"))) fmt.Println(p([]rune("foobar")))
Output: 97 [112 112 108 101] <nil> 102 [111 111 98 97 114] expected 'a', actual 'f', unexpected
func Tokens ¶
Tokens parses multiple tokens.
Consumes items from the input and compares them to the values from `tokens` using the / comparison function `cmp`. Succeeds if all the items from `tokens` are matched in the input / stream and fails otherwise with `expected` used as part of the error.
Example ¶
p := token.Tokens(func(l, r rune) bool { return unicode.ToLower(l) == unicode.ToLower(r) }, []rune("foo"), []rune("foo")) fmt.Println(p([]rune("apple"))) fmt.Println(p([]rune("FooBar")))
Output: [97] [112 112 108 101] expected "foo", actual "a", unexpected [70 111 111] [66 97 114] <nil>
Types ¶
This section is empty.