-
Notifications
You must be signed in to change notification settings - Fork 108
/
php.go
64 lines (53 loc) · 1.22 KB
/
php.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
// SPDX-License-Identifier: MIT
package lang
const (
phpHerodoc int8 = iota + 1
phpNowdoc
)
type phpDocBlock struct {
token1 string
token2 string
doctype int8
}
// herodoc 和 nowdoc 的实现。
//
// http://php.net/manual/zh/language.types.string.php#language.types.string.syntax.heredoc
func newPHPDocBlock() blocker {
return &phpDocBlock{
doctype: phpHerodoc,
}
}
func (b *phpDocBlock) beginFunc(l *parser) bool {
prev := l.Current()
if !l.Match("<<<") {
return false
}
token, found := l.Delim('\n', true)
if !found || len(token) <= 1 { // <<< 之后直接是换行符,则应该退回 <<< 字符
l.Move(prev)
return false
}
token = token[:len(token)-1] // l.delim 会带上换行符,需要去掉
if token[0] == '\'' && token[len(token)-1] == '\'' ||
token[0] == '"' && token[len(token)-1] == '"' {
b.doctype = phpNowdoc
token = token[1 : len(token)-1]
}
b.token1 = "\n" + string(token) + "\n"
b.token2 = "\n" + string(token) + ";\n"
return true
}
func (b *phpDocBlock) endFunc(l *parser) (data []byte, ok bool) {
for {
switch {
case l.AtEOF():
return nil, false
case l.Match(b.token1):
return nil, true
case l.Match(b.token2):
return nil, true
default:
l.Next(1)
}
}
}