forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
407 lines (375 loc) · 10.3 KB
/
expression.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package php
import (
"strings"
"github.com/stephens2424/php/ast"
"github.com/stephens2424/php/lexer"
"github.com/stephens2424/php/token"
)
var operatorPrecedence = map[token.Token]int{
token.ArrayLookupOperatorLeft: 19,
token.UnaryOperator: 18,
token.BitwiseNotOperator: 18,
token.CastOperator: 18,
token.InstanceofOperator: 17,
token.NegationOperator: 16,
token.MultOperator: 15,
token.AdditionOperator: 14,
token.SubtractionOperator: 14,
token.ConcatenationOperator: 14,
token.BitwiseShiftOperator: 13,
token.ComparisonOperator: 12,
token.EqualityOperator: 11,
token.AmpersandOperator: 10,
token.BitwiseXorOperator: 9,
token.BitwiseOrOperator: 8,
token.AndOperator: 7,
token.OrOperator: 6,
token.TernaryOperator1: 5,
token.TernaryOperator2: 5,
/*
PHP's documentation would have this operator be at 4, but it also notes:
Although = has a lower precedence than most other operators, PHP will
still allow expressions similar to the following: if (!$a = foo()), in
which case the return value of foo() is put into $a.
Thus, we put it at 17, pending further testing.
*/
token.AssignmentOperator: 17,
token.WrittenAndOperator: 3,
token.WrittenXorOperator: 2,
token.WrittenOrOperator: 1,
}
func (p *Parser) parseExpression() (expr ast.Expression) {
originalParenLev := p.parenLevel
switch p.current.Typ {
case token.IgnoreErrorOperator:
return p.parseNextExpression()
case token.List:
expr = p.parseList()
case token.AmpersandOperator, token.SubtractionOperator:
expr := p.parseUnaryExpressionRight(p.parseOperand(), p.current)
return p.parseOperation(originalParenLev, expr)
case token.UnaryOperator,
token.NegationOperator,
token.CastOperator,
token.BitwiseNotOperator,
token.ArrayLookupOperatorLeft,
token.Function,
token.NewOperator,
token.VariableOperator,
token.Array,
token.Identifier,
token.StringLiteral,
token.NumberLiteral,
token.BooleanLiteral,
token.Null,
token.Self,
token.Static,
token.Parent,
token.Include,
token.Exit,
token.ShellCommand:
expr = p.parseOperation(originalParenLev, p.parseOperand())
case token.OpenParen:
// check for a cast operator that happens to have had spaces in it, and was thus lexed incorrectly
if op := p.checkForCast(); op != nil {
expr = p.parseUnaryExpressionRight(p.parseNextExpression(), *op)
break
}
p.parenLevel += 1
p.next()
expr = p.parseExpression()
p.expect(token.CloseParen)
p.parenLevel -= 1
expr = p.parseOperation(originalParenLev, expr)
default:
p.errorf("Expected expression. Found %s", p.current)
}
if p.parenLevel != originalParenLev {
p.errorf("unbalanced parens: %d prev: %d", p.parenLevel, originalParenLev)
return
}
return
}
func (p *Parser) checkForCast() *token.Item {
if t := p.peek(); p.isCastType(t.Val) {
p.next()
if p.accept(token.CloseParen) {
t.Val = "(" + t.Val + ")"
t.Typ = token.CastOperator
return &t
}
p.backup()
}
return nil
}
func (p *Parser) isCastType(s string) bool {
switch strings.ToLower(s) {
case "int":
case "integer":
case "float":
case "bool":
case "boolean":
case "string":
case "null":
case "array":
case "object":
default:
return false
}
return true
}
func (p *Parser) parseOperation(originalParenLevel int, lhs ast.Expression) (expr ast.Expression) {
p.next()
switch operationTypeForToken(p.current.Typ) {
case ignoreErrorOperation:
return p.parseOperation(originalParenLevel, lhs)
case unaryOperation:
expr = p.parseUnaryExpressionLeft(lhs, p.current)
case assignmentOperation, binaryOperation:
expr = p.parseBinaryOperation(lhs, p.current, originalParenLevel)
case ternaryOperation:
expr = p.parseTernaryOperation(lhs)
case subexpressionEndOperation:
if p.parenLevel == originalParenLevel {
p.backup()
return lhs
}
p.parenLevel -= 1
expr = lhs
case subexpressionBeginOperation:
// Check if we have a paren directly after a literal
if _, ok := lhs.(*ast.Literal); ok {
// If we do, we might be in a particular case of a function call with NULL as the function name. Let callers handle this
p.backup()
return lhs
}
p.parenLevel += 1
expr = p.parseNextExpression()
default:
p.backup()
return lhs
}
return p.parseOperation(originalParenLevel, expr)
}
func (p *Parser) parseAssignmentOperation(lhs, rhs ast.Expression, operator token.Item) (expr ast.Expression) {
assignee, ok := lhs.(ast.Assignable)
if !ok {
p.errorf("%s is not assignable", lhs)
}
expr = ast.AssignmentExpression{
Assignee: assignee,
Operator: operator.Val,
Value: rhs,
}
return expr
}
// parseOperand takes the current token and returns it as the simplest
// expression for that token. That means an expression with no operators
// except for the object operator.
func (p *Parser) parseOperand() (expr ast.Expression) {
// These cases must come first and not repeat
switch p.current.Typ {
case token.IgnoreErrorOperator:
p.next()
return p.parseOperand()
case
token.UnaryOperator,
token.NegationOperator,
token.CastOperator,
token.SubtractionOperator,
token.AmpersandOperator,
token.BitwiseNotOperator:
op := p.current
p.next()
return p.parseUnaryExpressionRight(p.parseExpression(), op)
case token.OpenParen:
if op := p.checkForCast(); op != nil {
p.next()
return p.parseUnaryExpressionRight(p.parseExpression(), *op)
}
case token.Include:
return p.parseInclude()
case token.Function:
return p.parseAnonymousFunction()
case token.NewOperator:
return p.parseInstantiation()
case token.ArrayLookupOperatorLeft:
return p.parseArrayDeclaration()
}
switch p.current.Typ {
case token.ShellCommand:
return &ast.ShellCommand{Command: p.current.Val}
case
token.StringLiteral,
token.BooleanLiteral,
token.NumberLiteral,
token.Null:
return p.parseLiteral()
case token.UnaryOperator:
expr = p.parseUnaryExpressionLeft(expr, p.current)
p.next()
return
case token.Array:
expr = p.parseArrayDeclaration()
p.next()
case token.VariableOperator:
expr = p.parseVariableOperand()
case token.ObjectOperator:
expr = p.parseObjectLookup(expr)
p.next()
case token.ArrayLookupOperatorLeft, token.BlockBegin:
expr = p.parseArrayLookup(expr)
p.next()
case token.Identifier, token.Exit:
expr = p.parseIdentifier()
case token.Self, token.Static, token.Parent:
expr = p.parseScopeResolutionFromKeyword()
default:
p.backup()
return
}
return p.parseOperandComponent(expr)
}
func (p *Parser) parseOperandComponent(lhs ast.Expression) (expr ast.Expression) {
expr = lhs
for {
switch p.current.Typ {
case token.UnaryOperator:
expr = p.parseUnaryExpressionRight(expr, p.current)
return
case token.ObjectOperator:
expr = p.parseObjectLookup(expr)
p.next()
case token.ArrayLookupOperatorLeft, token.BlockBegin:
expr = p.parseArrayLookup(expr)
p.next()
case token.OpenParen:
p.backup()
if p.instantiation {
return
}
expr = p.parseFunctionCall(expr)
p.next()
default:
p.backup()
return
}
}
}
func (p *Parser) parseLiteral() ast.Expression {
switch p.current.Typ {
case token.StringLiteral:
return &ast.Literal{Type: ast.String, Value: p.current.Val}
case token.BooleanLiteral:
return &ast.Literal{Type: ast.Boolean, Value: p.current.Val}
case token.NumberLiteral:
return &ast.Literal{Type: ast.Float, Value: p.current.Val}
case token.Null:
if p.peek().Typ == token.OpenParen {
expr := p.parseIdentifier()
p.backup()
return expr
}
return &ast.Literal{Type: ast.Null, Value: p.current.Val}
}
p.errorf("Unknown literal type")
return nil
}
func (p *Parser) parseVariable() ast.Expression {
var expr *ast.Variable
p.expectCurrent(token.VariableOperator)
switch p.next(); {
case lexer.IsKeyword(p.current.Typ, p.current.Val):
// keywords are all valid variable names
fallthrough
case p.current.Typ == token.Identifier:
expr = ast.NewVariable(p.current.Val)
case p.current.Typ == token.BlockBegin:
expr = &ast.Variable{Name: p.parseNextExpression()}
p.expect(token.BlockEnd)
case p.current.Typ == token.VariableOperator:
expr = &ast.Variable{Name: p.parseVariable()}
default:
p.errorf("unexpected variable operand %s", p.current)
return nil
}
p.scope.Variable(expr)
return expr
}
func (p *Parser) parseInclude() ast.Expression {
inc := ast.Include{Expressions: make([]ast.Expression, 0)}
for {
inc.Expressions = append(inc.Expressions, p.parseNextExpression())
if p.peek().Typ != token.Comma {
break
}
p.expect(token.Comma)
}
return inc
}
func (p *Parser) parseIgnoreError() ast.Expression {
p.next()
return p.parseExpression()
}
func (p *Parser) parseNew(originalParenLev int) ast.Expression {
expr := p.parseInstantiation()
expr = p.parseOperation(originalParenLev, expr)
return expr
}
func (p *Parser) parseIdentifier() (expr ast.Expression) {
switch p.peek().Typ {
case token.OpenParen:
// Function calls are okay here because we know they came with
// a non-dynamic identifier.
expr = p.parseFunctionCall(&ast.Identifier{Value: p.current.Val})
p.next()
case token.ScopeResolutionOperator:
classIdent := p.current.Val
p.next() // get onto ::, then we get to the next expr
p.next()
expr = ast.NewClassExpression(classIdent, p.parseOperand())
p.next()
default:
name := p.current.Val
v := ast.NewVariable(p.current.Val)
expr = ast.ConstantExpression{
Variable: v,
}
p.namespace.Constants[name] = append(p.namespace.Constants[name], v)
p.next()
}
return expr
}
// parseScopeResolutionFromKeyword specifically parses self::, static::, and parent::
func (p *Parser) parseScopeResolutionFromKeyword() ast.Expression {
if p.peek().Typ == token.ScopeResolutionOperator {
r := p.current.Val
p.expect(token.ScopeResolutionOperator)
p.next()
expr := ast.NewClassExpression(r, p.parseOperand())
p.next()
return expr
}
// TODO Error
p.next()
return nil
}
func (p *Parser) parseVariableOperand() ast.Expression {
expr := p.parseVariable()
p.next()
// Array lookup with curly braces is a special case that is only supported by PHP in
// simple contexts.
switch p.current.Typ {
case token.BlockBegin:
expr = p.parseArrayLookup(expr)
p.next()
case token.ScopeResolutionOperator:
expr = &ast.ClassExpression{Receiver: expr, Expression: p.parseNextExpression()}
p.next()
case token.OpenParen:
p.backup()
expr = p.parseFunctionCall(expr)
p.next()
}
return expr
}