forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop.go
261 lines (249 loc) · 6.19 KB
/
oop.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
package php
import (
"github.com/stephens2424/php/ast"
"github.com/stephens2424/php/lexer"
"github.com/stephens2424/php/token"
)
func (p *Parser) parseInstantiation() ast.Expression {
p.expectCurrent(token.NewOperator)
p.next()
p.instantiation = true
expr := &ast.NewExpression{}
expr.Class = p.parseOperand()
p.instantiation = false
if p.peek().Typ == token.OpenParen {
p.expect(token.OpenParen)
if p.peek().Typ != token.CloseParen {
expr.Arguments = append(expr.Arguments, p.parseNextExpression())
for p.peek().Typ == token.Comma {
p.expect(token.Comma)
expr.Arguments = append(expr.Arguments, p.parseNextExpression())
}
}
p.expect(token.CloseParen)
}
return expr
}
func (p *Parser) parseClass() *ast.Class {
if p.current.Typ == token.Abstract {
p.expect(token.Class)
}
if p.current.Typ == token.Final {
p.expect(token.Class)
}
switch p.next(); {
case p.current.Typ == token.Identifier:
case lexer.IsKeyword(p.current.Typ, p.current.Val):
default:
p.errorf("unexpected variable operand %s", p.current)
}
name := p.current.Val
if p.peek().Typ == token.Extends {
p.expect(token.Extends)
p.expect(token.Identifier)
}
if p.peek().Typ == token.Implements {
p.expect(token.Implements)
p.expect(token.Identifier)
for p.peek().Typ == token.Comma {
p.expect(token.Comma)
p.expect(token.Identifier)
}
}
p.expect(token.BlockBegin)
c := p.parseClassFields(&ast.Class{Name: name})
p.namespace.ClassesAndInterfaces[c.Name] = c
return c
}
func (p *Parser) parseObjectLookup(r ast.Expression) (expr ast.Expression) {
p.expectCurrent(token.ObjectOperator)
prop := &ast.PropertyExpression{
Receiver: r,
}
switch p.next(); p.current.Typ {
case token.BlockBegin:
prop.Name = p.parseNextExpression()
p.expect(token.BlockEnd)
case token.VariableOperator:
prop.Name = p.parseExpression()
case token.Identifier:
prop.Name = &ast.Identifier{Value: p.current.Val}
}
expr = prop
switch pk := p.peek(); pk.Typ {
case token.OpenParen:
expr = &ast.MethodCallExpression{
Receiver: r,
FunctionCallExpression: p.parseFunctionCall(prop.Name),
}
}
expr = p.parseOperation(p.parenLevel, expr)
return
}
func (p *Parser) parseVisibility() (vis ast.Visibility, found bool) {
switch p.peek().Typ {
case token.Private:
vis = ast.Private
case token.Public:
vis = ast.Public
case token.Protected:
vis = ast.Protected
default:
return ast.Public, false
}
p.next()
return vis, true
}
func (p *Parser) parseAbstract() bool {
if p.peek().Typ == token.Abstract {
p.next()
return true
}
return false
}
func (p *Parser) parseClassFields(c *ast.Class) *ast.Class {
// Starting on BlockBegin
c.Methods = make([]*ast.Method, 0)
c.Properties = make([]*ast.Property, 0)
for p.peek().Typ != token.BlockEnd {
vis, _, _, abstract := p.parseClassMemberSettings()
p.next()
switch p.current.Typ {
case token.Function:
if abstract {
f := p.parseFunctionDefinition()
m := &ast.Method{
Visibility: vis,
FunctionStmt: &ast.FunctionStmt{FunctionDefinition: f},
}
c.Methods = append(c.Methods, m)
p.expect(token.StatementEnd)
} else {
c.Methods = append(c.Methods, &ast.Method{
Visibility: vis,
FunctionStmt: p.parseFunctionStmt(),
})
}
case token.Var:
p.expect(token.VariableOperator)
fallthrough
case token.VariableOperator:
for {
p.expect(token.Identifier)
prop := &ast.Property{
Visibility: vis,
Name: "$" + p.current.Val,
}
if p.peek().Typ == token.AssignmentOperator {
p.expect(token.AssignmentOperator)
prop.Initialization = p.parseNextExpression()
}
c.Properties = append(c.Properties, prop)
if p.accept(token.StatementEnd) {
break
}
p.expect(token.Comma)
p.expect(token.VariableOperator)
}
case token.Const:
constant := &ast.Constant{}
p.expect(token.Identifier)
constant.Name = p.current.Val
if p.peek().Typ == token.AssignmentOperator {
p.expect(token.AssignmentOperator)
constant.Value = p.parseNextExpression()
}
c.Constants = append(c.Constants, constant)
p.expect(token.StatementEnd)
default:
p.errorf("unexpected class member %v", p.current)
return c
}
}
p.expect(token.BlockEnd)
return c
}
func (p *Parser) parseInterface() *ast.Interface {
i := &ast.Interface{
Inherits: make([]string, 0),
}
p.expect(token.Identifier)
i.Name = p.current.Val
p.namespace.ClassesAndInterfaces[i.Name] = i
if p.peek().Typ == token.Extends {
p.expect(token.Extends)
for {
p.expect(token.Identifier)
i.Inherits = append(i.Inherits, p.current.Val)
if p.peek().Typ != token.Comma {
break
}
p.expect(token.Comma)
}
}
p.expect(token.BlockBegin)
for p.peek().Typ != token.BlockEnd {
vis, _ := p.parseVisibility()
if p.peek().Typ == token.Static {
p.next()
}
p.next()
switch p.current.Typ {
case token.Function:
f := p.parseFunctionDefinition()
m := ast.Method{
Visibility: vis,
FunctionStmt: &ast.FunctionStmt{FunctionDefinition: f},
}
i.Methods = append(i.Methods, m)
p.expect(token.StatementEnd)
case token.Const:
constant := ast.Constant{}
p.expect(token.Identifier)
constant.Name = p.current.Val
if p.peek().Typ == token.AssignmentOperator {
p.expect(token.AssignmentOperator)
constant.Value = p.parseNextExpression()
}
i.Constants = append(i.Constants, constant)
p.expect(token.StatementEnd)
default:
p.errorf("unexpected interface member %v", p.current)
}
}
p.expect(token.BlockEnd)
return i
}
func (p *Parser) parseClassMemberSettings() (vis ast.Visibility, static, final, abstract bool) {
var foundVis bool
vis = ast.Public
for {
switch p.peek().Typ {
case token.Abstract:
if abstract {
p.errorf("found multiple abstract declarations")
}
abstract = true
p.next()
case token.Private, token.Public, token.Protected:
if foundVis {
p.errorf("found multiple visibility declarations")
}
vis, foundVis = p.parseVisibility()
case token.Final:
if final {
p.errorf("found multiple final declarations")
}
final = true
p.next()
case token.Static:
if static {
p.errorf("found multiple static declarations")
}
static = true
p.next()
default:
return
}
}
}