-
Notifications
You must be signed in to change notification settings - Fork 2
/
matcher_dummy.go
91 lines (64 loc) · 1.41 KB
/
matcher_dummy.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
83
84
85
86
87
88
89
90
91
package ulexer
import "unicode"
// 匹配空白符, 回车符属于空白符
func WhiteSpace() Matcher {
return (*whiteSpaceMatcher)(nil)
}
type whiteSpaceMatcher struct{}
func (*whiteSpaceMatcher) TokenType() string {
return "WhiteSpace"
}
func (self *whiteSpaceMatcher) Read(lex *Lexer) (tk *Token) {
var count int
for {
c := lex.Peek(count)
if !unicode.In(c, unicode.White_Space) {
break
}
count++
}
if count == 0 {
return nil
}
tk = lex.NewToken(count, self)
lex.Consume(count)
return
}
// 匹配行结尾 Mac OS 9 以及之前的系统的换行符是 CR,从 Mac OS X (后来改名为“OS X”)开始的换行符是 LF即‘\n',和Unix/Linux统一了。
func LineEnd() Matcher {
return (*lineEndMatcher)(nil)
}
type lineEndMatcher struct{}
func (*lineEndMatcher) TokenType() string {
return "LineEnd"
}
func (self *lineEndMatcher) Read(lex *Lexer) (tk *Token) {
var count int
for {
c := lex.Peek(count)
if c != '\n' && c != '\r' {
break
}
count++
}
if count == 0 {
return nil
}
tk = lex.NewToken(count, self)
lex.Consume(count)
return
}
// 流结束, TokenString: EOF
func FileEnd() Matcher {
return (*fileEndMatcher)(nil)
}
type fileEndMatcher struct{}
func (*fileEndMatcher) TokenType() string {
return "FilEnd"
}
func (self *fileEndMatcher) Read(lex *Lexer) (tk *Token) {
if lex.EOF() {
return lex.NewTokenLiteral(0, self, "@EOF")
}
return nil
}