Skip to content

Commit db7c332

Browse files
committed
add stricter check for quoteless strings
1 parent 996e723 commit db7c332

16 files changed

Lines changed: 102 additions & 8 deletions

src/HJSON/HJSONParser.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function parseWsc($source, $options=[])
4141
return $this->parse($source, array_merge($options, ['keepWsc' => true]));
4242
}
4343

44+
private function isPunctuatorChar($c) {
45+
return $c === '{' || $c === '}' || $c === '[' || $c === ']' || $c === ',' || $c === ':';
46+
}
47+
4448
private function checkExit($result)
4549
{
4650
$this->white();
@@ -331,7 +335,7 @@ private function keyname()
331335
if (!$this->ch) $this->error("Found EOF while looking for a key name (check your syntax)");
332336
else if ($space < 0) $space = mb_strlen($name);
333337
}
334-
else if ($this->ch === '{' || $this->ch === '}' || $this->ch === '[' || $this->ch === ']' || $this->ch === ',') {
338+
else if ($this->isPunctuatorChar($this->ch)) {
335339
$this->error("Found '{$this->ch}' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)");
336340
}
337341
else $name .= $this->ch;
@@ -343,6 +347,10 @@ private function tfnns()
343347
{
344348
// Hjson strings can be quoteless
345349
// returns string, true, false, or null.
350+
351+
if ($this->isPunctuatorChar($this->ch))
352+
$this->error("Found a punctuator character '{$this->ch}' when excpecting a quoteless string (check your syntax)");
353+
346354
$value = $this->ch;
347355
while (true) {
348356
$isEol = $this->next() === null;

tests/HJSONParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testAll()
6767
$isJson = $name[1] === "json";
6868
$name = $name[0];
6969

70-
// skip empty test
70+
// skip empty test, empty keys are not supported by PHP
7171
if ($name === "empty") continue;
7272

7373
$this->runEach($name, $file, $isJson, false, false);

tests/assets/fail34_test.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A quoteless string is OK,
2+
but two must be contained in an array.

tests/assets/failStr1_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
# invalid quoteless string
3+
ql: ]
4+
}

tests/assets/failStr2_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
# invalid quoteless string
3+
ql: }
4+
}

tests/assets/failStr3_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
# invalid quoteless string
3+
ql: {
4+
}

tests/assets/failStr4_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
# invalid quoteless string
3+
ql: [
4+
}

tests/assets/failStr5_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
# invalid quoteless string
3+
ql: :
4+
}

tests/assets/failStr6_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
# invalid quoteless string
3+
ql: ,
4+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
foo=bar
1+
allow quoteless strings

0 commit comments

Comments
 (0)