Skip to content

Commit c6c4157

Browse files
committed
Update printer to match AST changes
1 parent 97773e4 commit c6c4157

File tree

1 file changed

+134
-5
lines changed

1 file changed

+134
-5
lines changed

lib/Rascal/RascalPrinter.php

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class RascalPrinter extends BasePrinter
3333

3434
private $currentMethod = "";
3535

36+
private $currentProperty = "";
37+
3638
private $currentNamespace = "";
3739

3840
private $inAssignExpr = false;
@@ -334,6 +336,11 @@ public function pprintArrayItem(\PhpParser\Node\ArrayItem $node)
334336
return $fragment;
335337
}
336338

339+
public function pprintArrayItemExpr(\PhpParser\Node\Expr\ArrayItem $node)
340+
{
341+
return $this->pprintArrayItem($node);
342+
}
343+
337344
public function pprintAssignExpr(\PhpParser\Node\Expr\Assign $node)
338345
{
339346
$assignExpr = $this->pprint($node->expr);
@@ -731,6 +738,11 @@ public function pprintClosureUse(\PhpParser\Node\ClosureUse $node)
731738
return $fragment;
732739
}
733740

741+
public function pprintClosureUseExpr(\PhpParser\Node\Expr\ClosureUse $node)
742+
{
743+
return $this->pprintClosureUse($node);
744+
}
745+
734746
public function pprintConstFetchExpr(\PhpParser\Node\Expr\ConstFetch $node)
735747
{
736748
$fragment = "fetchConst(" . $this->pprint($node->name);
@@ -983,6 +995,8 @@ public function pprintShellExecExpr(\PhpParser\Node\Expr\ShellExec $node)
983995
$parts[] = $this->pprint($item);
984996
} elseif ($item instanceof \PhpParser\Node\InterpolatedStringPart) {
985997
$parts[] = $this->pprint($item);
998+
} elseif ($item instanceof \PhpParser\Node\Scalar\EncapsedStringPart) {
999+
$parts[] = $this->pprint($item);
9861000
} else {
9871001
$parts[] = "scalar(string(\"" . $this->rascalizeString($item) . "\"))";
9881002
}
@@ -1122,6 +1136,47 @@ public function pprintIdentifier(\PhpParser\Node\Identifier $node)
11221136
return $node->name;
11231137
}
11241138

1139+
public function pprintPropertyHook(\PhpParser\Node\PropertyHook $node)
1140+
{
1141+
// TODO: We should add current/prior function tracking here...
1142+
$name = $this->pprint($node->name);
1143+
1144+
// TODO: Are there other modifiers that could apply here?
1145+
$modifiers = array();
1146+
if ($node->isFinal())
1147+
$modifiers[] = "final()";
1148+
1149+
$byRef = $node->returnsByRef();
1150+
1151+
$params = array();
1152+
foreach ($node->getParams() as $param)
1153+
$params[] = $this->pprint($param);
1154+
1155+
$body = array();
1156+
if ($node->body instanceof Expr) {
1157+
$body[] = $this->pprint($node->body);
1158+
} elseif(null !== $node->body) {
1159+
foreach ($node->body as $stmt) {
1160+
$body[] = $this->pprint($stmt);
1161+
}
1162+
}
1163+
1164+
$attrs = array();
1165+
foreach ($node->getAttrGroups() as $attr) {
1166+
$attrs[] = $this->pprint($attr);
1167+
}
1168+
1169+
if ($node->body instanceof Expr) {
1170+
$fragment = sprintf("propertyHookExpr(\"%s\",[%s],%s,[%s],%s,[%s]", $name, $modifiers, $byRef, implode(",",$params),$body[0],implode(",",$attrs));
1171+
} else {
1172+
$fragment = sprintf("propertyHookStmts(\"%s\",[%s],%s,[%s],[%s],[%s]", $name, $modifiers, $byRef, implode(",",$params),implode(",",$body),implode(",",$attrs));
1173+
}
1174+
$fragment .= $this->annotateASTNode($node);
1175+
$fragment .= ")";
1176+
1177+
return $fragment;
1178+
}
1179+
11251180
public function pprintFullyQualifiedName(\PhpParser\Node\Name\FullyQualified $node)
11261181
{
11271182
return "\\" . $this->pprintName($node);
@@ -1149,8 +1204,6 @@ public function pprintNullableType(\PhpParser\Node\NullableType $node)
11491204

11501205
public function pprintParam(\PhpParser\Node\Param $node)
11511206
{
1152-
// TODO: Add support for property hoooks
1153-
11541207
$type = $this->formatType($node->type);
11551208

11561209
$varName = $node->var->name;
@@ -1195,8 +1248,13 @@ public function pprintParam(\PhpParser\Node\Param $node)
11951248
foreach ($node->attrGroups as $attr) {
11961249
$attrs[] = $this->pprint($attr);
11971250
}
1251+
1252+
$hooks = array();
1253+
foreach ($node->hooks as $hook) {
1254+
$hooks[] = $this->pprint($hook);
1255+
}
11981256

1199-
$fragment = sprintf("param(\"%s\",%s,%s,%s,%s,{%s},[%s]", $varName, $default, $byRef, $variadic, $type, $mods, implode(",",$attrs));
1257+
$fragment = sprintf("param(\"%s\",%s,%s,%s,%s,{%s},[%s]", $varName, $default, $byRef, $variadic, $type, $mods, implode(",",$attrs), implode(",",$hooks));
12001258
$fragment .= $this->annotateASTNode($node);
12011259
$fragment .= ")";
12021260

@@ -1217,6 +1275,11 @@ public function pprintFloatScalar(\PhpParser\Node\Scalar\Float_ $node)
12171275
return $fragment;
12181276
}
12191277

1278+
public function pprintDNumberScalar(\PhpParser\Node\Scalar\DNumber $node)
1279+
{
1280+
return $this->pprintFloatScalar($node);
1281+
}
1282+
12201283
public function pprintInterpolatedStringScalar(\PhpParser\Node\Scalar\InterpolatedString $node)
12211284
{
12221285
$parts = array();
@@ -1225,6 +1288,8 @@ public function pprintInterpolatedStringScalar(\PhpParser\Node\Scalar\Interpolat
12251288
$parts[] = $this->pprint($item);
12261289
} elseif ($item instanceof \PhpParser\Node\InterpolatedStringPart) {
12271290
$parts[] = $this->pprint($item);
1291+
} elseif ($item instanceof \PhpParser\Node\Scalar\EncapsedStringPart) {
1292+
$parts[] = $this->pprint($item);
12281293
} else {
12291294
// TODO: This may no longer be reachable because of the addition of
12301295
// the EncapsedStringPart class, verify this...
@@ -1238,6 +1303,11 @@ public function pprintInterpolatedStringScalar(\PhpParser\Node\Scalar\Interpolat
12381303
return $fragment;
12391304
}
12401305

1306+
public function pprintEncapsedScalar(\PhpParser\Node\Scalar\Encapsed $node)
1307+
{
1308+
return $this->pprintInterpolatedStringScalar($node);
1309+
}
1310+
12411311
public function pprintInterpolatedStringPart(\PhpParser\Node\InterpolatedStringPart $node)
12421312
{
12431313
$fragment = "string(\"" . $this->rascalizeString($node->value) . "\")";
@@ -1248,6 +1318,11 @@ public function pprintInterpolatedStringPart(\PhpParser\Node\InterpolatedStringP
12481318
return $fragment;
12491319
}
12501320

1321+
public function pprintEncapsedStringPartScalar(\PhpParser\Node\Scalar\EncapsedStringPart $node)
1322+
{
1323+
return $this->pprintInterpolatedStringPart($node);
1324+
}
1325+
12511326
public function pprintIntScalar(\PhpParser\Node\Scalar\Int_ $node)
12521327
{
12531328
$fragment = "integer(" . sprintf('%d', $node->value) . ")";
@@ -1258,6 +1333,11 @@ public function pprintIntScalar(\PhpParser\Node\Scalar\Int_ $node)
12581333
return $fragment;
12591334
}
12601335

1336+
public function pprintLNumberScalar(\PhpParser\Node\Scalar\LNumber $node)
1337+
{
1338+
return $this->pprintIntScalar($node);
1339+
}
1340+
12611341
public function pprintClassMagicConstScalar(\PhpParser\Node\Scalar\MagicConst\Class_ $node)
12621342
{
12631343
// If we are inside a trait and find __CLASS__, we have no clue what it should
@@ -1303,6 +1383,12 @@ public function pprintMethodMagicConstScalar(\PhpParser\Node\Scalar\MagicConst\M
13031383
$this->currentMethod ? $this->currentClass . "::" . $this->currentMethod : "");
13041384
}
13051385

1386+
public function pprintPropertyMagicConstScalar(\PhpParser\Node\Scalar\MagicConst\Property $node)
1387+
{
1388+
return $this->handleMagicConstExpression($node, "propertyConstant",
1389+
$this->currentProperty ? $this->currentClass . "::" . $this->currentProperty : "");
1390+
}
1391+
13061392
public function pprintNamespaceMagicConstScalar(\PhpParser\Node\Scalar\MagicConst\Namespace_ $node)
13071393
{
13081394
return $this->handleMagicConstExpression($node, "namespaceConstant", $this->currentNamespace);
@@ -1551,7 +1637,12 @@ public function pprintConstStmt(\PhpParser\Node\Stmt\Const_ $node)
15511637
foreach ($node->consts as $const)
15521638
$consts[] = $this->pprint($const);
15531639

1554-
$fragment = "const([" . implode(",", $consts) . "]";
1640+
$attrs = array();
1641+
foreach ($node->attrGroups as $attr) {
1642+
$attrs[] = $this->pprint($attr);
1643+
}
1644+
1645+
$fragment = "const([" . implode(",", $consts) . ",[" . implode(",", $attrs) . "]";
15551646
$fragment .= $this->annotateASTNode($node);
15561647
$fragment .= ")";
15571648

@@ -1602,6 +1693,11 @@ public function pprintDeclareItem(\PhpParser\Node\DeclareItem $node)
16021693
return $fragment;
16031694
}
16041695

1696+
public function pprintDeclareDeclareStmt(\PhpParser\Node\Stmt\DeclareDeclare $node)
1697+
{
1698+
return $this->pprintDeclareItem($node);
1699+
}
1700+
16051701
public function pprintDoStmt(\PhpParser\Node\Stmt\Do_ $node)
16061702
{
16071703
$stmts = array();
@@ -1999,8 +2095,13 @@ public function pprintPropertyStmt(\PhpParser\Node\Stmt\Property $node)
19992095
$attrs[] = $this->pprint($attr);
20002096
}
20012097

2098+
$hooks = array();
2099+
foreach ($node->hooks as $hook) {
2100+
$hooks[] = $this->pprint($hook);
2101+
}
2102+
20022103
$fragment = "property({" . implode(",", $modifiers) . "},[" . implode(",", $props) . "]," . $this->formatType($node->type);
2003-
$fragment .= ",[" . implode(",",$attrs) . "]";
2104+
$fragment .= ",[" . implode(",",$attrs) . "],[" . implode(",",$hooks) ."]";
20042105
$fragment .= $this->annotateASTNode($node);
20052106
$fragment .= ")";
20062107

@@ -2022,6 +2123,11 @@ public function pprintPropertyItem(\PhpParser\Node\PropertyItem $node)
20222123
return $fragment;
20232124
}
20242125

2126+
public function pprintPropertyPropertyStmt(\PhpParser\Node\Stmt\PropertyProperty $node)
2127+
{
2128+
return $this->pprintPropertyItem($node);
2129+
}
2130+
20252131
public function pprintReturnStmt(\PhpParser\Node\Stmt\Return_ $node)
20262132
{
20272133
if (null !== $node->expr)
@@ -2048,6 +2154,19 @@ public function pprintStaticStmt(\PhpParser\Node\Stmt\Static_ $node)
20482154
return $fragment;
20492155
}
20502156

2157+
public function pprintBlockStmt(\PhpParser\Node\Stmt\Block $node)
2158+
{
2159+
$statements = array();
2160+
foreach ($node->stmts as $stmt)
2161+
$statements[] = $this->pprint($stmt);
2162+
2163+
$fragment = "block([" . implode(",", $statements) . "]";
2164+
$fragment .= $this->annotateASTNode($node);
2165+
$fragment .= ")";
2166+
2167+
return $fragment;
2168+
}
2169+
20512170
public function pprintStaticVar(\PhpParser\Node\StaticVar $node)
20522171
{
20532172
$default = "noExpr()";
@@ -2065,6 +2184,11 @@ public function pprintStaticVar(\PhpParser\Node\StaticVar $node)
20652184
return $fragment;
20662185
}
20672186

2187+
public function pprintStaticVarStmt(\PhpParser\Node\Stmt\StaticVar $node)
2188+
{
2189+
return $this->pprintStaticVar($node);
2190+
}
2191+
20682192
public function pprintSwitchStmt(\PhpParser\Node\Stmt\Switch_ $node)
20692193
{
20702194
$cases = array();
@@ -2287,6 +2411,11 @@ public function pprintUseItem(\PhpParser\Node\UseItem $node)
22872411
return $fragment;
22882412
}
22892413

2414+
public function pprintUseUseStmt(\PhpParser\Node\Stmt\UseUse $node)
2415+
{
2416+
return $this->pprintUseItem($node);
2417+
}
2418+
22902419
public function pprintWhileStmt(\PhpParser\Node\Stmt\While_ $node)
22912420
{
22922421
$stmts = array();

0 commit comments

Comments
 (0)