-
Notifications
You must be signed in to change notification settings - Fork 31
/
string.go
101 lines (94 loc) · 1.8 KB
/
string.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
package tokenizer
func lexPhpStringConst(l *Lexer) lexState {
st_type := l.next() // " or '
if st_type == '"' {
// too lazy to work this out, let's switch to the other lexer
l.emit(Rune('"'))
l.push(lexPhpStringWhitespace)
return l.base
}
if st_type == '`' {
l.emit(Rune('`'))
l.push(lexPhpStringWhitespaceBack)
return l.base
}
for {
c := l.next()
if c == st_type {
// end of string
l.emit(T_CONSTANT_ENCAPSED_STRING)
return l.base
}
if c == '\\' {
// advance (ignore) one
l.next()
continue
}
}
}
func lexPhpStringWhitespace(l *Lexer) lexState {
for {
c := l.peek()
switch c {
case eof:
l.emit(T_ENCAPSED_AND_WHITESPACE)
l.error("unexpected eof in string")
return nil
case '"':
// end of string
if l.pos > l.start {
l.emit(T_ENCAPSED_AND_WHITESPACE)
}
l.next() // "
l.emit(Rune(c))
l.pop() // return to previous context
return l.base
case '\\':
// advance (ignore) one
l.next() // \
l.next() // the escaped char
case '$':
// this is a variable
if l.pos > l.start {
l.emit(T_ENCAPSED_AND_WHITESPACE)
}
// meh :(
return lexPhpVariable
default:
l.next()
}
}
}
func lexPhpStringWhitespaceBack(l *Lexer) lexState {
for {
c := l.peek()
switch c {
case eof:
l.emit(T_ENCAPSED_AND_WHITESPACE)
l.error("unexpected eof in string")
return nil
case '`':
// end of string
if l.pos > l.start {
l.emit(T_ENCAPSED_AND_WHITESPACE)
}
l.next() // `
l.emit(Rune('`'))
l.pop() // return to previous context
return l.base
case '\\':
// advance (ignore) one
l.next() // \
l.next() // the escaped char
case '$':
// this is a variable
if l.pos > l.start {
l.emit(T_ENCAPSED_AND_WHITESPACE)
}
// meh :(
return lexPhpVariable
default:
l.next()
}
}
}