Skip to content

Commit 78076e5

Browse files
committed
glayzzle#41 rewrite class & interface bodies
1 parent 2e043c5 commit 78076e5

File tree

1 file changed

+68
-116
lines changed

1 file changed

+68
-116
lines changed

src/parser/class.js

Lines changed: 68 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -62,47 +62,29 @@ module.exports = {
6262
* </ebnf>
6363
*/
6464
,read_class_body: function() {
65-
var result = {
66-
'constants': []
67-
,'properties': []
68-
,'methods': []
69-
,'use': {
70-
// list of traits
71-
traits: [],
72-
// list of alias
73-
adaptations: []
74-
}
75-
}, startAt = null, node = null, comment = false;
76-
77-
65+
var result = [];
7866

7967
while(this.token !== this.EOF && this.token !== '}') {
8068

8169
if (this.token === this.tok.T_COMMENT) {
82-
comment = this.read_comment();
70+
result.push(this.read_comment());
8371
continue;
8472
}
8573

8674
if (this.token === this.tok.T_DOC_COMMENT) {
87-
comment = this.read_doc_comment();
75+
result.push(this.read_doc_comment());
8876
continue;
8977
}
9078

9179
// check T_USE trait
9280
if (this.token === this.tok.T_USE) {
93-
comment = false; // flush comments
94-
this.next().read_trait_use_statement(result['use']);
81+
result = result.concat(
82+
this.next().read_trait_use_statement()
83+
);
9584
continue;
9685
}
9786

98-
// prepare here position (to avoid bad position on locations)
99-
if (this.locations) {
100-
startAt = [
101-
this.lexer.yylloc.first_line,
102-
this.lexer.yylloc.first_column,
103-
this.length - this.lexer._input.length - this.lexer.yytext.length
104-
];
105-
}
87+
10688
// read member flags
10789
var flags = this.read_member_flags(false);
10890

@@ -115,59 +97,48 @@ module.exports = {
11597
for(var i = 0; i < constants.length; i++) {
11698
var constant = constants[i];
11799
(this.locations ? constant[3] : constant).push(flags);
118-
if (comment) {
119-
var buffer = comment.slice(0);
120-
(this.locations ? buffer[3] : buffer).push(constant);
121-
constant = buffer;
122-
}
123-
result.constants.push(constant);
100+
result.push(constant);
124101
}
125102
continue;
126103
}
127104

128105
// jump over T_VAR then land on T_VARIABLE
129106
if (this.token === this.tok.T_VAR) {
130107
this.next().expect(this.tok.T_VARIABLE);
108+
flags[0] = flags[1] = 0; // public & non static var
131109
}
132110

133-
// reads a variable
134111
if (this.token === this.tok.T_VARIABLE) {
112+
113+
// reads a variable
135114
var variables = this.read_variable_list(flags);
136115
this.expect(';').nextWithComments();
116+
137117
for(var i = 0; i < variables.length; i++) {
138118
var variable = variables[i];
139119
(this.locations ? variable[3] : variable).push(flags);
140-
if (comment) {
141-
var buffer = comment.slice(0);
142-
(this.locations ? buffer[3] : buffer).push(variable);
143-
variable = buffer;
144-
}
145-
result.properties.push(variable);
120+
result.push(variable);
146121
}
147-
comment = false;
122+
148123
} else if (this.token === this.tok.T_FUNCTION) {
124+
149125
// reads a function
150126
var method = this.read_function(false, flags[2] === 1);
151-
if (this.locations) {
152-
method[1] = startAt;
153-
method[3].push(flags);
154-
} else {
155-
method.push(flags);
156-
}
157-
if (comment) {
158-
(this.locations ? comment[3] : comment).push(method);
159-
method = comment;
160-
comment = false;
161-
}
162-
result.methods.push(method);
127+
(this.locations ? method[3] : method).push(flags);
128+
result.push(method);
129+
163130
} else {
131+
164132
// raise an error
165-
this.error([
166-
this.tok.T_CONST,
167-
this.tok.T_VARIABLE,
168-
this.tok.T_FUNCTION
169-
]);
133+
result.push(
134+
this.error([
135+
this.tok.T_CONST,
136+
this.tok.T_VARIABLE,
137+
this.tok.T_FUNCTION
138+
])
139+
);
170140
this.next(); // ignore token
141+
171142
}
172143
}
173144
this.expect('}').nextWithComments();
@@ -310,10 +281,8 @@ module.exports = {
310281
* </ebnf>
311282
*/
312283
,read_interface_body: function() {
313-
var result = {
314-
'constants': []
315-
,'methods': []
316-
}, startAt = null, comment = false;
284+
var result = [];
285+
317286
while(this.token !== this.EOF && this.token !== '}') {
318287

319288
if (this.token === this.tok.T_COMMENT) {
@@ -326,16 +295,6 @@ module.exports = {
326295
continue;
327296
}
328297

329-
330-
// prepare here position (to avoid bad position on locations)
331-
if (this.locations) {
332-
startAt = [
333-
this.lexer.yylloc.first_line,
334-
this.lexer.yylloc.first_column,
335-
this.length - this.lexer._input.length - this.lexer.yytext.length
336-
];
337-
}
338-
339298
// read member flags
340299
var flags = this.read_member_flags(true);
341300

@@ -347,38 +306,25 @@ module.exports = {
347306
for(var i = 0; i < constants.length; i++) {
348307
var constant = constants[i];
349308
(this.locations ? constant[3] : constant).push(flags);
350-
if (comment) {
351-
var buffer = comment.slice(0);
352-
(this.locations ? buffer[3] : buffer).push(constant);
353-
constant = buffer;
354-
}
355-
result.constants.push(constant);
309+
result.push(constant);
356310
}
357311

358312
}
359313

360314
// reads a function
361315
else if (this.token === this.tok.T_FUNCTION) {
362-
// reads a function
363-
var method = this.read_function_declaration().concat(
364-
[flags]
365-
);
366-
if (this.locations) {
367-
method[1] = startAt;
368-
}
369-
if (comment) {
370-
(this.locations ? comment[3] : comment).push(method);
371-
method = comment;
372-
comment = false;
373-
}
374-
result.methods.push(method);
316+
var method = this.read_function_declaration();
317+
(this.locations ? method[3] : method).push(flags);
318+
result.push(method);
375319
this.expect(';').nextWithComments();
376320
} else {
377321
// raise an error
378-
this.error([
379-
this.tok.T_CONST,
380-
this.tok.T_FUNCTION
381-
]);
322+
result.push(
323+
this.error([
324+
this.tok.T_CONST,
325+
this.tok.T_FUNCTION
326+
])
327+
);
382328
this.next();
383329
}
384330
}
@@ -422,23 +368,28 @@ module.exports = {
422368
* trait_use_statement ::= namespace_name (',' namespace_name)* ('{' trait_use_alias '}')?
423369
* </ebnf>
424370
*/
425-
,read_trait_use_statement: function(result) {
426-
result.traits.push(this.read_namespace_name());
371+
,read_trait_use_statement: function() {
372+
// defines use statements
373+
var node = this.node('use');
374+
var name = this.read_namespace_name();
375+
var result = [node(name)];
427376
while(this.token === ',') {
428-
result.traits.push(
429-
this.next().read_namespace_name()
430-
);
377+
node = this.node('use');
378+
name = this.next().read_namespace_name();
379+
result.push(node(name));
431380
}
432381
if (this.token === '{') {
382+
// defines alias statements
433383
while(this.next()) {
434384
if (this.token === '}') break;
435-
result.adaptations.push(this.read_trait_use_alias());
385+
result.push(this.read_trait_use_alias());
436386
this.expect(';');
437387
}
438388
this.expect('}').nextWithComments();
439389
} else {
440390
this.expect(';').nextWithComments();
441391
}
392+
return result;
442393
}
443394
/**
444395
* Reading trait alias
@@ -447,42 +398,43 @@ module.exports = {
447398
* </ebnf>
448399
*/
449400
,read_trait_use_alias: function() {
450-
var result = {
451-
origin: this.read_namespace_name(),
452-
act: false,
453-
target: false
454-
};
401+
var node = this.node('alias');
402+
var origin = this.read_namespace_name();
403+
var act = false;
404+
var target = false;
405+
var flags = false;
406+
455407
if (this.token === this.tok.T_DOUBLE_COLON) {
456-
result.origin = [
457-
result.origin,
408+
origin = [
409+
'static',
410+
'get',
411+
origin,
458412
this.next().expect(this.tok.T_STRING).text()
459413
];
460414
this.next();
461415
}
462416

463417
if (this.token === this.tok.T_INSTEADOF) {
464-
result.act = 'instead';
465-
result.target = this.next().read_namespace_name();
418+
act = 'insteadof';
419+
target = this.next().read_namespace_name();
466420
} else if (this.token === this.tok.T_AS) {
467-
result.act = 'as';
421+
act = 'as';
468422
if (this.next().is('T_MEMBER_FLAGS')) {
469-
result.flags = this.read_member_flags();
470-
} else {
471-
result.flags = null;
423+
flags = this.read_member_flags();
472424
}
473425
if (this.token === this.tok.T_STRING) {
474-
result.target = this.text();
426+
target = this.text();
475427
this.next();
476-
} else if (result.flags === null) {
428+
} else if (flags === false) {
477429
// no visibility flags and no name => too bad
478-
this.expect(this.tok.T_STRING)
430+
this.expect(this.tok.T_STRING);
479431
}
480432
} else {
481433
this.expect([
482434
this.tok.T_AS,
483435
this.tok.T_INSTEADOF
484436
]);
485437
}
486-
return result;
438+
return node(origin, act, target, flags);
487439
}
488440
};

0 commit comments

Comments
 (0)