Finish and test Token combinator
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// 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, _ := gigaparsec.Run(tokenize, strings.NewReader("(alpha + 42) -5 "))
|
||||
for _, token := range tokens {
|
||||
fmt.Println(token)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// (
|
||||
// alpha
|
||||
// +
|
||||
// 42
|
||||
// )
|
||||
// -
|
||||
// 5
|
||||
}
|
||||
Reference in New Issue
Block a user