Skip to content

Commit e9162ba

Browse files
authored
Merge pull request #9 from pug-php/1.x
1.x
2 parents f96d61f + e91ef41 commit e9162ba

File tree

4 files changed

+87
-35
lines changed

4 files changed

+87
-35
lines changed

examples/indent.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
for (i = 9; i; i--) {
2+
if ((function (tiny) {
3+
return tiny < 3;
4+
})(i)) {
5+
continue;
6+
}
7+
break;
8+
}

examples/indent.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
for ($i = 9; $i; $i--) {
2+
if (call_user_func((function ($tiny) {
3+
return $tiny < 3;
4+
}), $i)) {
5+
continue;
6+
}
7+
break;
8+
}

src/JsPhpize/Compiler/Compiler.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ protected function visitAssignation(Assignation $assignation, $indent)
9494

9595
protected function visitBlock(Block $block, $indent)
9696
{
97-
$head = $block->type . ' ' . ($block->value
98-
? $this->visitNode($block->value, $indent)
97+
$head = $block->type . ($block->value
98+
? ' ' . $this->visitNode($block->value, $indent)
9999
: ''
100100
);
101101

@@ -105,7 +105,7 @@ protected function visitBlock(Block $block, $indent)
105105

106106
$letVariables = $this->visitNodesArray($block->getLetVariables(), $indent, '', $indent . "unset(%s);\n");
107107

108-
return $head . "{\n" .
108+
return $head . " {\n" .
109109
$this->compile($block, ' ' . $indent) .
110110
$letVariables .
111111
$indent . '}';
@@ -218,7 +218,18 @@ protected function visitHooksArray(HooksArray $array, $indent)
218218

219219
protected function visitInstruction(Instruction $group, $indent)
220220
{
221-
return $this->visitNodesArray($group->instructions, $indent, '', $indent . "%s;\n");
221+
$visitNode = array($this, 'visitNode');
222+
223+
return implode('', array_map(function ($instruction) use ($visitNode, $indent) {
224+
$value = call_user_func($visitNode, $instruction, $indent);
225+
226+
return $indent .
227+
($instruction instanceof Block && $instruction->handleInstructions()
228+
? $value
229+
: $value . ';'
230+
) .
231+
"\n";
232+
}, $group->instructions));
222233
}
223234

224235
public function visitNode(Node $node, $indent)
@@ -233,12 +244,12 @@ public function visitNode(Node $node, $indent)
233244
$php = $node->getBefore() . $php . $node->getAfter();
234245
}
235246

236-
return $indent . $php;
247+
return $php;
237248
}
238249

239250
protected function visitParenthesis(Parenthesis $parenthesis, $indent)
240251
{
241-
return '(' . $this->visitNodesArray($parenthesis->nodes, $indent, $parenthesis->separator) . ')';
252+
return '(' . $this->visitNodesArray($parenthesis->nodes, $indent, $parenthesis->separator . ' ') . ')';
242253
}
243254

244255
protected function visitTernary(Ternary $ternary, $indent)

src/JsPhpize/Parser/Parser.php

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ protected function parseHooksArray()
175175
throw new Exception('Missing ] to match ' . $exceptionInfos, 6);
176176
}
177177

178+
protected function getBracketsArrayItemKeyFromToken($token)
179+
{
180+
$type = null;
181+
182+
if ($token->is('keyword')) {
183+
$type = 'string';
184+
$value = var_export($token->value, true);
185+
} elseif ($token->isValue()) {
186+
$type = $token->type;
187+
$value = $token->value;
188+
if ($type === 'variable') {
189+
$type = 'string';
190+
$value = var_export($value, true);
191+
}
192+
}
193+
194+
if ($type) {
195+
$token = $this->next();
196+
if (!$token) {
197+
throw new Exception('Missing value after ' . $value . $this->exceptionInfos(), 12);
198+
}
199+
if (!$token->is(':')) {
200+
$this->unexpected($token);
201+
}
202+
$key = new Constant($type, $value);
203+
$value = $this->expectValue($this->next());
204+
205+
return array($key, $value);
206+
}
207+
}
208+
178209
protected function parseBracketsArray()
179210
{
180211
$array = new BracketsArray();
@@ -192,28 +223,8 @@ protected function parseBracketsArray()
192223
}
193224
$this->unexpected($token);
194225
}
195-
$type = null;
196-
if ($token->is('keyword')) {
197-
$type = 'string';
198-
$value = var_export($token->value, true);
199-
} elseif ($token->isValue()) {
200-
$type = $token->type;
201-
$value = $token->value;
202-
if ($type === 'variable') {
203-
$type = 'string';
204-
$value = var_export($value, true);
205-
}
206-
}
207-
if ($type) {
208-
$token = $this->next();
209-
if (!$token) {
210-
throw new Exception('Missing value after ' . $value . $this->exceptionInfos(), 12);
211-
}
212-
if (!$token->is(':')) {
213-
$this->unexpected($token);
214-
}
215-
$key = new Constant($type, $value);
216-
$value = $this->expectValue($this->next());
226+
if ($pair = $this->getBracketsArrayItemKeyFromToken($token)) {
227+
list($key, $value) = $pair;
217228
$expectComma = true;
218229
$array->addItem($key, $value);
219230

@@ -440,7 +451,7 @@ protected function getValueFromToken($token)
440451
return $value;
441452
}
442453

443-
protected function parseKeyword($token)
454+
protected function parseKeywordStatement($token)
444455
{
445456
$name = $token->value;
446457
$keyword = new Block($name);
@@ -477,6 +488,13 @@ protected function parseKeyword($token)
477488
throw new Exception("'" . $keyword->type . "' block need parentheses.", 17);
478489
}
479490
}
491+
492+
return $keyword;
493+
}
494+
495+
protected function parseKeyword($token)
496+
{
497+
$keyword = $this->parseKeywordStatement($token);
480498
if ($keyword->handleInstructions()) {
481499
$this->parseBlock($keyword);
482500
}
@@ -494,6 +512,17 @@ protected function parseLet($token)
494512
return $letVariable->value;
495513
}
496514

515+
protected function getInstructionFromToken($token)
516+
{
517+
if ($token->is('keyword')) {
518+
return $this->parseKeyword($token);
519+
}
520+
521+
if ($value = $this->getValueFromToken($token)) {
522+
return $value;
523+
}
524+
}
525+
497526
protected function parseInstructions($block, $waitForClosure)
498527
{
499528
while ($token = $this->next()) {
@@ -507,12 +536,8 @@ protected function parseInstructions($block, $waitForClosure)
507536
$block->let($this->parseLet($token));
508537
continue;
509538
}
510-
if ($token->is('keyword')) {
511-
$block->addInstruction($this->parseKeyword($token));
512-
continue;
513-
}
514-
if ($value = $this->getValueFromToken($token)) {
515-
$block->addInstruction($value);
539+
if ($instruction = $this->getInstructionFromToken($token)) {
540+
$block->addInstruction($instruction);
516541
continue;
517542
}
518543
if ($token->is(';')) {

0 commit comments

Comments
 (0)