Skip to content

Fix mixed indents in block strings #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.3
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.3
coverage: none

- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
- run: composer install --no-progress --prefer-dist
- run: vendor/bin/tester tests -s -C
- if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: output
name: output-${{ matrix.php }}
path: tests/**/output


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.4-dev"
"dev-master": "3.5-dev"
}
}
}
4 changes: 2 additions & 2 deletions src/Neon/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function parseToNode(string $input): Node
{
$lexer = new Lexer;
$parser = new Parser;
$tokens = $lexer->tokenize($input);
return $parser->parse($tokens);
$stream = $lexer->tokenize($input);
return $parser->parse($stream);
}
}
4 changes: 2 additions & 2 deletions src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
final class Encoder
{
/** @deprecated */
#[\Deprecated]
public const BLOCK = true;

public bool $blockMode = false;
Expand All @@ -29,7 +29,7 @@ final class Encoder
public function encode(mixed $val): string
{
$node = $this->valueToNode($val, $this->blockMode);
return $node->toString();
return $node->toString($this->indentation);
}


Expand Down
9 changes: 9 additions & 0 deletions src/Neon/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@
*/
class Exception extends \Exception
{
public ?Position /*readonly*/ $position = null;


public function __construct(string $message, ?Position $position = null, ?\Throwable $previous = null)
{
$message .= $position ? ' ' . $position : '';
$this->position = $position;
parent::__construct($message, 0, $previous);
}
}
32 changes: 26 additions & 6 deletions src/Neon/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,45 @@ public function tokenize(string $input): TokenStream
}

$types = array_keys(self::Patterns);
$offset = 0;
$position = new Position;

$tokens = [];
foreach ($matches as $match) {
$type = $types[count($match) - 2];
$tokens[] = new Token($match[0], $type === Token::Char ? $match[0] : $type);
$offset += strlen($match[0]);
$tokens[] = new Token($type === Token::Char ? $match[0] : $type, $match[0], $position);
$position = $this->advance($position, $match[0]);
}

$tokens[] = new Token(Token::End, '', $position);

$stream = new TokenStream($tokens);
if ($offset !== strlen($input)) {
$s = str_replace("\n", '\n', substr($input, $offset, 40));
$stream->error("Unexpected '$s'", count($tokens));
if ($position->offset !== strlen($input)) {
$s = str_replace("\n", '\n', substr($input, $position->offset, 40));
throw new Exception("Unexpected '$s'", $position);
}

return $stream;
}


private function advance(Position $position, string $str): Position
{
if ($lines = substr_count($str, "\n")) {
return new Position(
$position->line + $lines,
strlen($str) - strrpos($str, "\n"),
$position->offset + strlen($str),
);
} else {
return new Position(
$position->line,
$position->column + strlen($str),
$position->offset + strlen($str),
);
}
}


public static function requiresDelimiters(string $s): bool
{
return preg_match('~[\x00-\x1F]|^[+-.]?\d|^(true|false|yes|no|on|off|null)$~Di', $s)
Expand Down
4 changes: 2 additions & 2 deletions src/Neon/Neon.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ final class Neon
{
public const Chain = '!!chain';

/** @deprecated use Neon::Chain */
#[\Deprecated('use Neon::Chain')]
public const CHAIN = self::Chain;

/** @deprecated use parameter $blockMode */
#[\Deprecated('use parameter $blockMode')]
public const BLOCK = Encoder::BLOCK;


Expand Down
6 changes: 3 additions & 3 deletions src/Neon/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ abstract class Node implements \IteratorAggregate
{
public ?int $startTokenPos = null;
public ?int $endTokenPos = null;
public ?int $startLine = null;
public ?int $endLine = null;
public ?Position $start = null;
public ?Position $end = null;


abstract public function toValue(): mixed;


abstract public function toString(): string;
abstract public function toString(string $indentation): string;


public function &getIterator(): \Generator
Expand Down
14 changes: 7 additions & 7 deletions src/Neon/Node/ArrayItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ public static function itemsToArray(array $items): array


/** @param self[] $items */
public static function itemsToInlineString(array $items): string
public static function itemsToInlineString(array $items, string $indentation): string
{
$res = '';
foreach ($items as $item) {
$res .= ($res === '' ? '' : ', ')
. ($item->key ? $item->key->toString() . ': ' : '')
. $item->value->toString();
. ($item->key ? $item->key->toString($indentation) . ': ' : '')
. $item->value->toString($indentation);
}

return $res;
}


/** @param self[] $items */
public static function itemsToBlockString(array $items): string
public static function itemsToBlockString(array $items, string $indentation): string
{
$res = '';
foreach ($items as $item) {
$v = $item->value->toString();
$res .= ($item->key ? $item->key->toString() . ':' : '-')
$v = $item->value->toString($indentation);
$res .= ($item->key ? $item->key->toString($indentation) . ':' : '-')
. ($item->value instanceof BlockArrayNode && $item->value->items
? "\n" . $v . (substr($v, -2, 1) === "\n" ? '' : "\n")
: ' ' . $v . "\n");
Expand All @@ -74,7 +74,7 @@ public function toValue(): mixed
}


public function toString(): string
public function toString(string $indentation): string
{
throw new \LogicException;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Neon/Node/BlockArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public function __construct(
}


public function toString(): string
public function toString(string $indentation): string
{
if (count($this->items) === 0) {
return '[]';
}

$res = ArrayItemNode::itemsToBlockString($this->items);
$res = ArrayItemNode::itemsToBlockString($this->items, $indentation);
return preg_replace('#^(?=.)#m', $this->indentation, $res);
}
}
4 changes: 2 additions & 2 deletions src/Neon/Node/EntityChainNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function toValue(): Neon\Entity
}


public function toString(): string
public function toString(string $indentation): string
{
return implode('', array_map(fn($entity) => $entity->toString(), $this->chain));
return implode('', array_map(fn($entity) => $entity->toString($indentation), $this->chain));
}


Expand Down
6 changes: 3 additions & 3 deletions src/Neon/Node/EntityNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function toValue(): Entity
}


public function toString(): string
public function toString(string $indentation): string
{
return $this->value->toString()
return $this->value->toString($indentation)
. '('
. ($this->attributes ? ArrayItemNode::itemsToInlineString($this->attributes) : '')
. ($this->attributes ? ArrayItemNode::itemsToInlineString($this->attributes, $indentation) : '')
. ')';
}

Expand Down
4 changes: 2 additions & 2 deletions src/Neon/Node/InlineArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function __construct(
}


public function toString(): string
public function toString(string $indentation): string
{
return $this->bracket
. ArrayItemNode::itemsToInlineString($this->items)
. ArrayItemNode::itemsToInlineString($this->items, $indentation)
. ['[' => ']', '{' => '}', '(' => ')'][$this->bracket];
}
}
2 changes: 1 addition & 1 deletion src/Neon/Node/LiteralNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function baseConvert(string $number, int $base): string|int
}


public function toString(): string
public function toString(string $indentation): string
{
if ($this->value instanceof \DateTimeInterface) {
return $this->value->format('Y-m-d H:i:s O');
Expand Down
18 changes: 10 additions & 8 deletions src/Neon/Node/StringNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function toValue(): string
}


public static function parse(string $s): string
public static function parse(string $s, Nette\Neon\Position $position): string
{
if (preg_match('#^...\n++([\t ]*+)#', $s, $m)) { // multiline
$res = substr($s, 3, -3);
Expand All @@ -51,23 +51,25 @@ public static function parse(string $s): string
}

return preg_replace_callback(
'#\\\\(?:ud[89ab][0-9a-f]{2}\\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|.)#i',
function (array $m): string {
'#\\\(?:ud[89ab][0-9a-f]{2}\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|.)#i',
function (array $m) use ($position): string {
$sq = $m[0];
if (isset(self::EscapeSequences[$sq[1]])) {
return self::EscapeSequences[$sq[1]];
} elseif ($sq[1] === 'u' && strlen($sq) >= 6) {
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq");
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq", $position);
} else {
throw new Nette\Neon\Exception("Invalid escaping sequence $sq");
throw new Nette\Neon\Exception("Invalid escaping sequence $sq", $position);
}
},
$res,
);


}


public function toString(): string
public function toString(string $indentation): string
{
if (!str_contains($this->value, "\n")) {
return preg_match('~[\x00-\x08\x0B-\x1F]~', $this->value)
Expand All @@ -77,7 +79,7 @@ public function toString(): string
} elseif (preg_match('~[\x00-\x08\x0B-\x1F]|\n[\t ]+\'{3}~', $this->value)) {
$s = substr(json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 1, -1);
$s = preg_replace_callback(
'#[^\\\\]|\\\\(.)#s',
'#[^\\\]|\\\(.)#s',
fn($m) => ['n' => "\n", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0],
$s,
);
Expand All @@ -89,7 +91,7 @@ public function toString(): string
$delim = "'''";
}

$s = preg_replace('#^(?=.)#m', "\t", $s);
$s = preg_replace('#^(?=.)#m', $indentation, $s);
return $delim . "\n" . $s . "\n" . $delim;
}
}
Loading