Skip to content

Commit

Permalink
C块注释
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Dec 28, 2021
1 parent cc7c685 commit cc0bfcf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ func (self *Lexer) Read(m Matcher) *Token {

type Hooker func(lex *Lexer) *Token

func (self *Lexer) SetPreHook(hook Hooker) {
func (self *Lexer) SetPreHook(hook Hooker) (pre Hooker) {
pre = self.preHooker
self.preHooker = hook
return
}

func NewLexer(s string) *Lexer {
Expand Down
53 changes: 53 additions & 0 deletions matcher_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,56 @@ func (self *unixLineCommentMatcher) Read(lex *Lexer) (tk *Token) {

return
}

// C块注释
func CBlockComment() Matcher {
return (*cBlockCommentMatcher)(nil)
}

func (*cBlockCommentMatcher) TokenType() string {
return "CBlockComment"
}

type cBlockCommentMatcher struct{}

func (self *cBlockCommentMatcher) Read(lex *Lexer) (tk *Token) {

if lex.Peek(0) != '/' || lex.Peek(1) != '*' {
return nil
}

lex.Consume(2)

var (
count int
endPos int
)
for {
c := lex.Peek(count)

switch c {
case '*':
endPos = count
case '/':
if endPos != 0 && count-endPos == 1 {
count++
goto EndOfParse
}
case 0: // EOF
goto EndOfParse
}
count++
}

EndOfParse:
if count == 0 {
return nil
}

// */ 不算到注释内容
tk = lex.NewToken(count-2, self)

lex.Consume(count)

return
}
2 changes: 1 addition & 1 deletion matcher_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (*fileEndMatcher) TokenType() string {

func (self *fileEndMatcher) Read(lex *Lexer) (tk *Token) {
if lex.EOF() {
return lex.NewTokenLiteral(0, self, "EOF")
return lex.NewTokenLiteral(0, self, "@EOF")
}
return nil
}
13 changes: 13 additions & 0 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ func TestUnixLineComment(t *testing.T) {
}).MustNoError(t).MustEOF(t)
}

// C块注释
func TestCBlockComment(t *testing.T) {

new(TestLexer).Try(`/*** abc
***/123`, func(lex *Lexer) {

Expect(lex, CBlockComment())

Expect(lex, Numeral())

}).MustNoError(t).MustEOF(t)
}

func TestSvcID(t *testing.T) {

new(TestLexer).Try("game#1@dev", func(lex *Lexer) {
Expand Down

0 comments on commit cc0bfcf

Please sign in to comment.