Skip to content

Commit

Permalink
syntax: allowed tab as separator for dashed-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 27, 2023
1 parent 5715e17 commit 457bfbf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Neon/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
} elseif ($item->key !== null && $this->tokens->isNext('-')) { // special dash subblock
$item->value = $this->parseBlock($indent, onlyBullets: true);
}
} elseif ($item->key === null) {
$item->value = $this->parseBlock($indent . ' '); // open new block after dash

} elseif ($item->key === null) { // open new block after dash
$save = $this->tokens->getPos();
try {
$item->value = $this->parseBlock($indent . "\t");
} catch (Exception) {
$this->tokens->seek($save);
$item->value = $this->parseBlock($indent . ' ');
}
} elseif ($this->tokens->isNext()) {
$item->value = $this->parseValue();
if ($this->tokens->isNext() && !$this->tokens->isNext(Token::Newline)) {
Expand Down
6 changes: 6 additions & 0 deletions src/Neon/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function getPos(): int
}


public function seek(int $position): void
{
$this->pos = $position;
}


/** @return Token[] */
public function getTokens(): array
{
Expand Down
51 changes: 51 additions & 0 deletions tests/Neon/Decoder.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,57 @@ Assert::same(
);


Assert::same(
[
[
'a' => 'b',
'c' => 'd',
],
],
Neon::decode(<<<'XX'
- a: b
c: d
XX),
);


Assert::same(
[
[
'a' => 'b',
[
'c' => 'd',
['e' => 'f'],
],
],
],
Neon::decode(<<<'XX'
- a: b
- c: d
- e: f
XX),
);


Assert::same(
[
[
'a' => 'b',
[
'c' => 'd',
'e' => 'f',
],
],
],
Neon::decode(<<<'XX'
- a: b
- c: d
e: f
XX),
);



Assert::same(
[
'root' => [['key1' => null, 'key3' => 123]],
Expand Down
2 changes: 1 addition & 1 deletion tests/Neon/fixtures/Parser.nodes.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ third:
b: 2

-
- c
- c

dash subblock:
- a
Expand Down
2 changes: 1 addition & 1 deletion tests/Neon/fixtures/Parser.nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ Nette\Neon\Node\BlockArrayNode
| | key: null
| | value: Nette\Neon\Node\BlockArrayNode
| | | code: '- c'
| | | indentation: ' '
| | | indentation: '\t '
| | | items: array (1)
| | | | 0 => Nette\Neon\Node\ArrayItemNode
| | | | | code: '- c'
Expand Down

0 comments on commit 457bfbf

Please sign in to comment.