forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.go
99 lines (94 loc) · 2.31 KB
/
array.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
package php
import (
"github.com/stephens2424/php/ast"
"github.com/stephens2424/php/token"
)
func (p *Parser) parseArrayLookup(e ast.Expression) ast.Expression {
p.expectCurrent(token.ArrayLookupOperatorLeft, token.BlockBegin)
switch typ := p.peek().typ; typ {
case token.ArrayLookupOperatorRight, token.BlockBegin:
p.expect(token.ArrayLookupOperatorRight, token.BlockEnd)
return ast.ArrayAppendExpression{Array: e}
}
p.next()
expr := &ast.ArrayLookupExpression{
Array: e,
Index: p.parseExpression(),
}
p.expect(token.ArrayLookupOperatorRight, token.BlockEnd)
return expr
}
func (p *Parser) parseArrayDeclaration() ast.Expression {
var endType token.Token
pairs := make([]ast.ArrayPair, 0)
p.expectCurrent(token.Array, token.ArrayLookupOperatorLeft)
switch p.current.typ {
case token.Array:
p.expect(token.OpenParen)
endType = token.CloseParen
case token.ArrayLookupOperatorLeft:
endType = token.ArrayLookupOperatorRight
}
ArrayLoop:
for {
var key, val ast.Expression
switch p.peek().typ {
case endType:
break ArrayLoop
default:
val = p.parseNextExpression()
}
switch p.peek().typ {
case token.Comma:
p.expect(token.Comma)
case endType:
pairs = append(pairs, ast.ArrayPair{Key: key, Value: val})
break ArrayLoop
case token.ArrayKeyOperator:
p.expect(token.ArrayKeyOperator)
key = val
val = p.parseNextExpression()
if p.peek().typ == endType {
pairs = append(pairs, ast.ArrayPair{Key: key, Value: val})
break ArrayLoop
}
p.expect(token.Comma)
default:
p.errorf("expected => or ,")
return nil
}
pairs = append(pairs, ast.ArrayPair{Key: key, Value: val})
}
p.expect(endType)
return &ast.ArrayExpression{Pairs: pairs}
}
func (p *Parser) parseList() ast.Expression {
l := &ast.ListStatement{
Assignees: make([]ast.Assignable, 0),
}
p.expect(token.OpenParen)
for {
if p.accept(token.Comma) {
continue
}
if p.peek().typ == token.CloseParen {
break
}
p.next()
op, ok := p.parseOperand().(ast.Assignable)
if ok {
l.Assignees = append(l.Assignees, op)
} else {
p.errorf("%v list element is not assignable", op)
}
if p.peek().typ != token.Comma {
break
}
p.expect(token.Comma)
}
p.expect(token.CloseParen)
p.expect(token.AssignmentOperator)
l.Operator = p.current.val
l.Value = p.parseNextExpression()
return l
}