Skip to content

Commit c29e8ec

Browse files
committed
refactoring: update ast structure of "Class", "Interface" and "Trait" nodes
1 parent 2990f0c commit c29e8ec

File tree

17 files changed

+1744
-2082
lines changed

17 files changed

+1744
-2082
lines changed

internal/php5/parser_test.go

Lines changed: 278 additions & 318 deletions
Large diffs are not rendered by default.

internal/php5/php5.go

Lines changed: 477 additions & 451 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/php5/php5.y

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,48 +1280,74 @@ unticked_class_declaration_statement:
12801280
{
12811281
switch n := $1.(type) {
12821282
case *ast.StmtClass :
1283-
n.Position = yylex.(*Parser).builder.NewNodeTokenPosition($1, $7)
1284-
n.ClassName = &ast.Identifier{
1285-
Position: yylex.(*Parser).builder.NewTokenPosition($2),
1283+
className := &ast.Identifier{
1284+
Position: yylex.(*Parser).builder.NewTokenPosition($2),
12861285
IdentifierTkn: $2,
12871286
Value: $2.Value,
12881287
}
1289-
n.Extends = $3
1290-
n.Implements = $4
1291-
n.OpenCurlyBracketTkn = $5
1292-
n.Stmts = $6
1288+
1289+
n.Position = yylex.(*Parser).builder.NewNodeTokenPosition($1, $7)
1290+
n.ClassName = className
1291+
n.OpenCurlyBracketTkn = $5
1292+
n.Stmts = $6
12931293
n.CloseCurlyBracketTkn = $7
1294+
1295+
if $3 != nil {
1296+
n.ExtendsTkn = $3.(*ast.StmtClass).ExtendsTkn
1297+
n.Extends = $3.(*ast.StmtClass).Extends
1298+
}
1299+
1300+
if $4 != nil {
1301+
n.ImplementsTkn = $4.(*ast.StmtClass).ImplementsTkn
1302+
n.Implements = $4.(*ast.StmtClass).Implements
1303+
n.ImplementsSeparatorTkns = $4.(*ast.StmtClass).ImplementsSeparatorTkns
1304+
}
12941305
case *ast.StmtTrait :
1295-
n.Position = yylex.(*Parser).builder.NewNodeTokenPosition($1, $7)
1296-
n.TraitName = &ast.Identifier{
1297-
Position: yylex.(*Parser).builder.NewTokenPosition($2),
1306+
traitName := &ast.Identifier{
1307+
Position: yylex.(*Parser).builder.NewTokenPosition($2),
12981308
IdentifierTkn: $2,
12991309
Value: $2.Value,
13001310
}
1301-
n.Extends = $3
1302-
n.Implements = $4
1303-
n.OpenCurlyBracketTkn = $5
1304-
n.Stmts = $6
1311+
1312+
n.Position = yylex.(*Parser).builder.NewNodeTokenPosition($1, $7)
1313+
n.TraitName = traitName
1314+
n.OpenCurlyBracketTkn = $5
1315+
n.Stmts = $6
13051316
n.CloseCurlyBracketTkn = $7
1317+
1318+
if $3 != nil {
1319+
yylex.(*Parser).errHandlerFunc(errors.NewError("A trait cannot extend a class. Traits can only be composed from other traits with the 'use' keyword", $3.(*ast.StmtClass).Position))
1320+
}
1321+
1322+
if $4 != nil {
1323+
yylex.(*Parser).errHandlerFunc(errors.NewError("A trait cannot implement an interface", $4.(*ast.StmtClass).Position))
1324+
}
13061325
}
13071326

13081327
$$ = $1
13091328
}
13101329
| interface_entry T_STRING interface_extends_list '{' class_statement_list '}'
13111330
{
1312-
$$ = &ast.StmtInterface{
1331+
iface := &ast.StmtInterface{
13131332
Position: yylex.(*Parser).builder.NewTokensPosition($1, $6),
13141333
InterfaceTkn: $1,
13151334
InterfaceName: &ast.Identifier{
13161335
Position: yylex.(*Parser).builder.NewTokenPosition($2),
13171336
IdentifierTkn: $2,
13181337
Value: $2.Value,
13191338
},
1320-
Extends: $3,
13211339
OpenCurlyBracketTkn: $4,
13221340
Stmts: $5,
13231341
CloseCurlyBracketTkn: $6,
13241342
}
1343+
1344+
if $3 != nil {
1345+
iface.ExtendsTkn = $3.(*ast.StmtInterface).ExtendsTkn
1346+
iface.Extends = $3.(*ast.StmtInterface).Extends
1347+
iface.ExtendsSeparatorTkns = $3.(*ast.StmtInterface).ExtendsSeparatorTkns
1348+
}
1349+
1350+
$$ = iface
13251351
}
13261352
;
13271353

@@ -1378,10 +1404,10 @@ extends_from:
13781404
}
13791405
| T_EXTENDS fully_qualified_class_name
13801406
{
1381-
$$ = &ast.StmtClassExtends{
1382-
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
1383-
ExtendTkn: $1,
1384-
ClassName: $2,
1407+
$$ = &ast.StmtClass{
1408+
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
1409+
ExtendsTkn: $1,
1410+
Extends: $2,
13851411
}
13861412
}
13871413
;
@@ -1400,11 +1426,11 @@ interface_extends_list:
14001426
}
14011427
| T_EXTENDS interface_list
14021428
{
1403-
$$ = &ast.StmtInterfaceExtends{
1404-
Position: yylex.(*Parser).builder.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items),
1405-
ExtendsTkn: $1,
1406-
InterfaceNames: $2.(*ast.ParserSeparatedList).Items,
1407-
SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns,
1429+
$$ = &ast.StmtInterface{
1430+
Position: yylex.(*Parser).builder.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items),
1431+
ExtendsTkn: $1,
1432+
Extends: $2.(*ast.ParserSeparatedList).Items,
1433+
ExtendsSeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns,
14081434
};
14091435
}
14101436
;
@@ -1416,11 +1442,11 @@ implements_list:
14161442
}
14171443
| T_IMPLEMENTS interface_list
14181444
{
1419-
$$ = &ast.StmtClassImplements{
1420-
Position: yylex.(*Parser).builder.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items),
1421-
ImplementsTkn: $1,
1422-
InterfaceNames: $2.(*ast.ParserSeparatedList).Items,
1423-
SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns,
1445+
$$ = &ast.StmtClass{
1446+
Position: yylex.(*Parser).builder.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items),
1447+
ImplementsTkn: $1,
1448+
Implements: $2.(*ast.ParserSeparatedList).Items,
1449+
ImplementsSeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns,
14241450
};
14251451
}
14261452
;

0 commit comments

Comments
 (0)