forked from BadIdeasMadeWorse/php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexers.go
More file actions
265 lines (234 loc) · 5.07 KB
/
Copy pathlexers.go
File metadata and controls
265 lines (234 loc) · 5.07 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package php
import (
"fmt"
"strings"
"unicode"
"github.com/stephens2424/php/token"
)
const shortPHPBegin = "<?"
const longPHPBegin = "<?php"
const phpEnd = "?>"
const eof = -1
// lexHTML consumes and emits an html t until it
// finds a php begin
func lexHTML(l *lexer) stateFn {
for {
if strings.HasPrefix(l.input[l.pos:], shortPHPBegin) {
if l.pos > l.start {
l.emit(token.HTML)
}
return lexPHPBegin
}
if l.next() == eof {
break
}
}
if l.pos > l.start {
l.emit(token.HTML)
}
l.emit(token.EOF)
return nil
}
func lexPHPBegin(l *lexer) stateFn {
if strings.HasPrefix(l.input[l.pos:], longPHPBegin) {
l.pos += len(longPHPBegin)
}
if strings.HasPrefix(l.input[l.pos:], shortPHPBegin) {
l.pos += len(shortPHPBegin)
}
l.emit(token.PHPBegin)
return lexPHP
}
func lexPHP(l *lexer) stateFn {
l.skipSpace()
if r := l.peek(); unicode.IsDigit(r) {
return lexNumberLiteral
} else if r == '.' {
l.next() // must advance because we only peeked before
secondR := l.peek()
l.backup()
if unicode.IsDigit(secondR) {
return lexNumberLiteral
}
}
if strings.HasPrefix(l.input[l.pos:], "<<<") {
return lexDoc
}
if strings.HasPrefix(l.input[l.pos:], "?>") {
return lexPHPEnd
}
if strings.HasPrefix(l.input[l.pos:], "#") {
return lexLineComment
}
if strings.HasPrefix(l.input[l.pos:], "//") {
return lexLineComment
}
if strings.HasPrefix(l.input[l.pos:], "/*") {
return lexBlockComment
}
if l.peek() == eof {
l.emit(token.EOF)
return nil
}
if l.peek() == '`' {
return lexShellCommand
}
if l.peek() == '\'' {
return lexSingleQuotedStringLiteral
}
if l.peek() == '"' {
return lexDoubleQuotedStringLiteral
}
for _, tokenString := range token.TokenList {
t := token.TokenMap[tokenString]
potentialToken := l.input[l.pos:]
if len(potentialToken) > len(tokenString) {
potentialToken = potentialToken[:len(tokenString)]
}
if strings.HasPrefix(strings.ToLower(potentialToken), tokenString) {
prev := l.previous()
if isKeyword(t, tokenString) && prev == '$' {
break
}
l.pos += len(tokenString)
if isKeyword(t, tokenString) && l.accept(alphabet+underscore+digits) {
l.backup() // to account for the character consumed by accept
l.pos -= len(tokenString)
break
}
l.emit(t)
return lexPHP
}
}
l.acceptRun(alphabet + underscore + digits + "\\")
l.emit(token.Identifier)
return lexPHP
}
func lexNumberLiteral(l *lexer) stateFn {
if l.accept("0") {
// binary?
if l.accept("b") {
l.acceptRun("01")
l.emit(token.NumberLiteral)
return lexPHP
}
// hexadecimal?
if l.accept("xX") {
l.acceptRun(digits + "abcdefABCDEF")
l.emit(token.NumberLiteral)
return lexPHP
}
}
// is decimal?
l.acceptRun(digits)
if l.accept(".") {
l.acceptRun(digits)
}
if l.accept("E") {
l.acceptRun(digits)
}
l.emit(token.NumberLiteral)
return lexPHP
}
func lexShellCommand(l *lexer) stateFn {
l.next()
for {
switch l.next() {
case '`':
l.emit(token.ShellCommand)
return lexPHP
case eof:
l.emit(token.ShellCommand)
return nil
}
}
}
func lexSingleQuotedStringLiteral(l *lexer) stateFn {
l.next()
for {
switch l.next() {
case '\\':
l.next()
continue
case '\'':
l.emit(token.StringLiteral)
return lexPHP
}
}
}
func lexDoubleQuotedStringLiteral(l *lexer) stateFn {
l.next()
for {
switch l.next() {
case '\\':
l.next()
continue
case '"':
l.emit(token.StringLiteral)
return lexPHP
}
}
}
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const digits = "0123456789"
const underscore = "_"
func lexIdentifier(l *lexer) stateFn {
l.accept("$")
l.accept(underscore + alphabet)
l.acceptRun(underscore + alphabet + digits)
l.emit(token.VariableOperator)
return lexPHP
}
// lexPHPEnd lexes the end of a PHP section returning the context to HTML
func lexPHPEnd(l *lexer) stateFn {
l.pos += len(phpEnd)
l.emit(token.PHPEnd)
return lexHTML
}
func lexLineComment(l *lexer) stateFn {
lineLength := strings.Index(l.input[l.pos:], "\n") + 1
if lineLength == 0 {
// this is the last line, so lex until the end
lineLength = len(l.input[l.pos:])
}
// don't lex php end
if phpEndLength := strings.Index(l.input[l.pos:l.pos+lineLength], phpEnd); phpEndLength >= 0 && phpEndLength < lineLength {
lineLength = phpEndLength
}
l.pos += lineLength
l.ignore()
return lexPHP
}
func lexBlockComment(l *lexer) stateFn {
commentLength := strings.Index(l.input[l.pos:], "*/") + 2
if commentLength == 1 {
// the file ends before we find */
commentLength = len(l.input[l.pos:])
}
l.pos += commentLength
l.ignore()
return lexPHP
}
func lexDoc(l *lexer) stateFn {
var nowDoc bool
l.pos += len("<<<")
l.skipSpace()
if strings.HasPrefix(l.input[l.pos:], "'") {
nowDoc = true
l.pos += len("'")
}
labelPos := l.pos
l.accept(underscore + alphabet)
l.acceptRun(underscore + alphabet + digits)
endMarker := fmt.Sprintf("\n%s", l.input[labelPos:l.pos])
if nowDoc {
l.accept("'")
}
l.accept("\n")
for !strings.HasPrefix(l.input[l.pos:], endMarker) {
l.next()
}
l.pos += len(endMarker)
l.emit(token.StringLiteral)
return lexPHP
}