Skip to content
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

Support quoted and typed keys #11

Merged
merged 6 commits into from
Nov 4, 2023
Merged
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
12 changes: 6 additions & 6 deletions src/main/php/org/yaml/Input.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private function escape($in, &$offset) {
* @param string $end
* @return var[]
*/
private function token(&$in, &$offset, $end= '#') {
public function token(&$in, &$offset, $end= '#') {
$l= strlen($in);
$offset+= strspn($in, ' ', $offset);
if ($offset >= $l) return null;
Expand All @@ -186,7 +186,7 @@ private function token(&$in, &$offset, $end= '#') {

$c= $in[$offset];
if ('"' === $c) {
$offset+= 1;
$offset++;
$string= '';
do {
$p= strcspn($in, '\\"', $offset);
Expand All @@ -206,7 +206,7 @@ private function token(&$in, &$offset, $end= '#') {
$offset+= $p;
} while (true);
} else if ("'" === $c) {
$offset+= 1;
$offset++;
$string= '';
do {
$p= strcspn($in, "'", $offset);
Expand All @@ -231,7 +231,7 @@ private function token(&$in, &$offset, $end= '#') {
$in.= $this->nextLine();
return $this->token($in, $offset, $end);
} else if ('*' === $c) {
$offset+= 1;
$offset++;
$p= strcspn($in, $end, $offset);
$literal= trim(substr($in, $offset, $p));
$offset+= strlen($literal);
Expand Down Expand Up @@ -270,10 +270,10 @@ private function token(&$in, &$offset, $end= '#') {
$offset++;
return $c;
} else if (0 === substr_compare($in, '!!', $offset, 2)) {
$p= strcspn($in, ' ', $offset);
$p= strcspn($in, ' '.$end, $offset);
$tag= substr($in, $offset + 2, $p - 2);
$offset+= $p + 1;
if ($offset >= $l) return [$tag, null];
if ($offset >= $l || $end === $in[$offset]) return [$tag, null];
$token= $this->token($in, $offset, $end);
return [$tag, $token[1]];
} else {
Expand Down
40 changes: 23 additions & 17 deletions src/main/php/org/yaml/YamlParser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,31 @@ public function valueOf($reader, $value, $level= 0) {
} else {
$r[$key]= $this->valueOf($reader, substr($line, $spaces + 2), $spaces);
}
} else if (!strpos('#!?{[', $line[$spaces]) && strpos($line, ':')) {
$key= $value= null;
sscanf($line, "%[^:]: %[^\r]", $key, $value);
$key= trim(substr($key, $spaces), ' ');

// The “<<” merge key is used to indicate that all the keys of one or more specified
// maps should be inserted into the current map. If the value associated with the key
// is a single mapping node, each of its key/value pairs is inserted into the current
// mapping, unless the key already exists in it.
if ('<<' === $key) {
$merge= $this->valueOf($reader, $value, $spaces);
foreach (0 === key($merge) ? $merge : [$merge] as $map) {
$r+= $map;
}
} else {
$offset= $spaces;
$token= $reader->token($line, $offset, ':');

// After reading the token, if we encounter a colon, construct a map
if (false === ($p= strpos($line, ':', $offset))) {
$r= $this->tokenValue($token);
} else {
$r[$key]= $this->valueOf($reader, $value, $spaces);
$key= trim($this->tokenValue($token));
$p++;
$value= $p + 1 < $l ? substr($line, $p + strspn($line, ' ', $p)) : null;

// The “<<” merge key is used to indicate that all the keys of one or more specified
// maps should be inserted into the current map. If the value associated with the key
// is a single mapping node, each of its key/value pairs is inserted into the current
// mapping, unless the key already exists in it.
if ('<<' === $key) {
$merge= $this->valueOf($reader, $value, $spaces);
foreach (0 === key($merge) ? $merge : [$merge] as $map) {
$r+= $map;
}
} else {
$r[$key]= $this->valueOf($reader, $value, $spaces);
}
}
} else {
$r= $this->valueOf($reader, $line, $spaces);
}

} while (null !== ($line= $reader->nextLine()));
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/org/yaml/unittest/EmptyNodesTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace org\yaml\unittest;

use test\{Assert, Ignore, Test};
use test\{Assert, Test};

/**
* 7.2 Empty Nodes
Expand All @@ -18,7 +18,7 @@ public function empty_value() {
Assert::equals(['key' => null], $this->parse('key: '));
}

#[Test, Ignore('Key types not yet supported')]
#[Test]
public function empty_string_key() {
Assert::equals(['' => 'value'], $this->parse('!!str : value'));
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/php/org/yaml/unittest/YamlParserTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public function parse_sequence() {
);
}

#[Test, Values([['key', 'key'], ['"key"', 'key'], ["'key'", 'key'], ['key b', 'key b'], ['"key:"', 'key:'], ['"key::b"', 'key::b'], ['"key \"b\""', 'key "b"'], ["'key ''b'''", "key 'b'"], ['!!str key', 'key']])]
public function string_keys($declaration, $expected) {
Assert::equals([$expected => 1], $this->parse("{$declaration}: 1"));
}

#[Test]
public function mapping_scalars_to_sequences() {
Assert::equals(
Expand Down
Loading