38 lines
720 B
Go
38 lines
720 B
Go
// SPDX-License-Identifier: Unlicense
|
|
|
|
package bytes_test
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
|
"git.codemonkeysoftware.net/b/gigaparsec/bytes"
|
|
)
|
|
|
|
func ExampleToken() {
|
|
ptokens := gigaparsec.Choose(
|
|
bytes.MatchString("("),
|
|
bytes.MatchString(")"),
|
|
bytes.MatchString("+"),
|
|
bytes.MatchString("-"),
|
|
bytes.Regexp(`[a-zA-Z][a-zA-Z0-9_]*`).Label("identifier"),
|
|
bytes.Regexp(`[0-9]+`).Label("number"),
|
|
)
|
|
|
|
tokenize := gigaparsec.Repeat(0, bytes.Token[string](bytes.WhitespaceASCII())(ptokens))
|
|
tokens, _ := tokenize.Run(strings.NewReader("(alpha + 42) -5 "))
|
|
for _, token := range tokens {
|
|
fmt.Println(token)
|
|
}
|
|
|
|
// Output:
|
|
// (
|
|
// alpha
|
|
// +
|
|
// 42
|
|
// )
|
|
// -
|
|
// 5
|
|
}
|