-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte.go
82 lines (69 loc) · 2.08 KB
/
byte.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package bytes
import (
"unicode"
"github.com/flier/gocombine/pkg/parser"
"github.com/flier/gocombine/pkg/parser/choice"
"github.com/flier/gocombine/pkg/parser/repeat"
"github.com/flier/gocombine/pkg/parser/token"
"github.com/flier/gocombine/pkg/stream"
)
// Any parses any byte.
func Any() parser.Func[byte, byte] {
return parser.Expected(func(input []byte) (byte, []byte, error) {
return stream.Uncons(input)
}, "any")
}
// Byte parses a byte and succeeds if the byte is equal to `b`.
func Byte(b byte) parser.Func[byte, byte] {
return token.Token(b).Expected("byte")
}
// Space parse a single whitespace.
func Space() parser.Func[byte, byte] {
return token.Satisfy(func(b byte) bool {
return unicode.IsSpace(rune(b))
}).Expected("whitespace")
}
// Spaces parse zero or more spaces.
func Spaces() parser.Func[byte, []byte] {
return repeat.Many(Space()).Expected("whitespaces")
}
// NewLine parses a newline character (`'\n'`).
func NewLine() parser.Func[byte, byte] {
return token.Token[byte]('\n').Expected("newline")
}
// CrLf parses carriage return and newline (`"\r\n"`), returning the newline character.
func CrLf() parser.Func[byte, []byte] {
return choice.And(
token.Token[byte]('\r'),
token.Token[byte]('\n'),
).Expected("crlf")
}
// Tab parses a tab character (`'\t'`).
func Tab() parser.Func[byte, byte] {
return token.Token[byte]('\t').Expected("tab")
}
// Upper parses an uppercase letter.
func Upper() parser.Func[byte, byte] {
return token.Satisfy(func(b byte) bool {
return unicode.IsUpper(rune(b))
}).Expected("uppercase letter")
}
// Lower parses a lowercase letter.
func Lower() parser.Func[byte, byte] {
return token.Satisfy(func(b byte) bool {
return unicode.IsLower(rune(b))
}).Expected("lowercase letter")
}
// Letter parses an alphabet letter.
func Letter() parser.Func[byte, byte] {
return token.Satisfy(func(b byte) bool {
return unicode.IsLetter(rune(b))
}).Expected("letter")
}
// AlphaNum parses either an alphabet letter or digit.
func AlphaNum() parser.Func[byte, byte] {
return choice.Or(
Letter(),
Digit(),
).Expected("letter or digit")
}