/
commentparser.go
108 lines (70 loc) · 1.97 KB
/
commentparser.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package pbmeta
import (
"github.com/davyxu/golexer"
)
// 自定义的token id
const (
Token_EOF = iota
Token_LeftBrace
Token_RightBrace
Token_WhiteSpace
Token_LineEnd
Token_UnixStyleComment
Token_Identifier
Token_Unknown
)
type CommentParser struct {
*golexer.Parser
}
func parseTaggedComment(src string, commentArray []*TaggedComment) []*TaggedComment {
return NewCommentParser(src).Run(src, commentArray)
}
func (self *CommentParser) Run(src string, commentArray []*TaggedComment) (ret []*TaggedComment) {
defer golexer.ErrorCatcher(func(err error) {
log.Errorln(err, self.TokenID(), self.TokenValue())
ret = commentArray
})
self.Lexer().Start(src)
for {
self.NextToken()
//log.Debugln("#", self.TokenID(), self.TokenValue())
if self.TokenID() == Token_WhiteSpace {
continue
}
// 读取标头
if self.TokenID() == Token_LeftBrace {
self.NextToken()
var tc TaggedComment
tagNameToken := self.Expect(Token_Identifier)
self.Expect(Token_RightBrace)
tc.Name = tagNameToken.Value()
for {
//log.Debugln("#", self.TokenID(), self.TokenValue())
tc.Data += self.TokenValue()
self.NextToken()
if self.TokenID() == Token_LineEnd || self.TokenID() == Token_EOF {
break
}
}
commentArray = append(commentArray, &tc)
}
if self.TokenID() == Token_EOF {
break
}
}
return commentArray
}
func NewCommentParser(srcName string) *CommentParser {
l := golexer.NewLexer()
// 匹配顺序从高到低
l.AddMatcher(golexer.NewSignMatcher(Token_LeftBrace, "["))
l.AddMatcher(golexer.NewSignMatcher(Token_RightBrace, "]"))
l.AddMatcher(golexer.NewWhiteSpaceMatcher(Token_WhiteSpace))
l.AddMatcher(golexer.NewLineEndMatcher(Token_LineEnd))
l.AddIgnoreMatcher(golexer.NewUnixStyleCommentMatcher(Token_UnixStyleComment))
l.AddMatcher(golexer.NewIdentifierMatcher(Token_Identifier))
l.AddMatcher(golexer.NewUnknownMatcher(Token_Unknown))
return &CommentParser{
Parser: golexer.NewParser(l, srcName),
}
}