token

package
v0.0.0-...-1dacd80 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[T stream.Token]() parser.Func[T, T]

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

func EOF[T stream.Token]() parser.Func[T, bool]

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

func NoneOf[T stream.Token](tokens []T) parser.Func[T, T]

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

func OneOf[T stream.Token](tokens []T) parser.Func[T, T]

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

func Produce[T stream.Token, O any](f func() O) parser.Func[T, O]

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

func Satisfy[T stream.Token](predicate func(T) bool) parser.Func[T, T]

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

func SatisfyMap[T stream.Token, O any](predicate func(T) (O, error)) parser.Func[T, O]

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

func Token[T stream.Token](tok T) parser.Func[T, T]

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

func Tokens[T stream.Token](cmp func(lhs, rhs T) bool, expected, tokens []T) parser.Func[T, []T]

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>

func Value

func Value[T stream.Token, O any](v O) parser.Func[T, O]

Value always returns the value `v` without consuming any input.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/token"
)

func main() {
	p := token.Value[rune]('a')

	fmt.Println(p([]rune("pple")))

}
Output:

97 [112 112 108 101] <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

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