Skip to content

Commit

Permalink
add stricter check for quoteless strings
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Aug 8, 2016
1 parent 996e723 commit db7c332
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/HJSON/HJSONParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function parseWsc($source, $options=[])
return $this->parse($source, array_merge($options, ['keepWsc' => true]));
}

private function isPunctuatorChar($c) {
return $c === '{' || $c === '}' || $c === '[' || $c === ']' || $c === ',' || $c === ':';
}

private function checkExit($result)
{
$this->white();
Expand Down Expand Up @@ -331,7 +335,7 @@ private function keyname()
if (!$this->ch) $this->error("Found EOF while looking for a key name (check your syntax)");
else if ($space < 0) $space = mb_strlen($name);
}
else if ($this->ch === '{' || $this->ch === '}' || $this->ch === '[' || $this->ch === ']' || $this->ch === ',') {
else if ($this->isPunctuatorChar($this->ch)) {
$this->error("Found '{$this->ch}' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)");
}
else $name .= $this->ch;
Expand All @@ -343,6 +347,10 @@ private function tfnns()
{
// Hjson strings can be quoteless
// returns string, true, false, or null.

if ($this->isPunctuatorChar($this->ch))
$this->error("Found a punctuator character '{$this->ch}' when excpecting a quoteless string (check your syntax)");

$value = $this->ch;
while (true) {
$isEol = $this->next() === null;
Expand Down
2 changes: 1 addition & 1 deletion tests/HJSONParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testAll()
$isJson = $name[1] === "json";
$name = $name[0];

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

$this->runEach($name, $file, $isJson, false, false);
Expand Down
2 changes: 2 additions & 0 deletions tests/assets/fail34_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A quoteless string is OK,
but two must be contained in an array.
4 changes: 4 additions & 0 deletions tests/assets/failStr1_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ]
}
4 changes: 4 additions & 0 deletions tests/assets/failStr2_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: }
}
4 changes: 4 additions & 0 deletions tests/assets/failStr3_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: {
}
4 changes: 4 additions & 0 deletions tests/assets/failStr4_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: [
}
4 changes: 4 additions & 0 deletions tests/assets/failStr5_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: :
}
4 changes: 4 additions & 0 deletions tests/assets/failStr6_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ,
}
2 changes: 1 addition & 1 deletion tests/assets/passSingle_result.hjson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo=bar
allow quoteless strings
2 changes: 1 addition & 1 deletion tests/assets/passSingle_result.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"foo=bar"
"allow quoteless strings"
2 changes: 1 addition & 1 deletion tests/assets/passSingle_test.hjson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo=bar
allow quoteless strings
4 changes: 3 additions & 1 deletion tests/assets/strings_result.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
text1: This is a valid string value.
text2: a \ is just a \
text3: "You need quotes\tfor escapes"
text4: " untrimmed "
text4a: " untrimmed "
text4b: " untrimmed"
text4c: "untrimmed "
multiline1:
'''
first line
Expand Down
4 changes: 3 additions & 1 deletion tests/assets/strings_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"text1": "This is a valid string value.",
"text2": "a \\ is just a \\",
"text3": "You need quotes\tfor escapes",
"text4": " untrimmed ",
"text4a": " untrimmed ",
"text4b": " untrimmed",
"text4c": "untrimmed ",
"multiline1": "first line\n indented line\nlast line",
"multiline2": "first line\n indented line\nlast line",
"multiline3": "first line\n indented line\nlast line\n",
Expand Down
4 changes: 3 additions & 1 deletion tests/assets/strings_test.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

text3: "You need quotes\tfor escapes"

text4: " untrimmed "
text4a: " untrimmed "
text4b: " untrimmed"
text4c: "untrimmed "

# multiline string

Expand Down
54 changes: 54 additions & 0 deletions tests/assets/testlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
charset_test.hjson
comments_test.hjson
empty_test.hjson
fail10_test.json
fail11_test.json
fail12_test.json
fail13_test.json
fail14_test.json
fail15_test.json
fail16_test.json
fail17_test.json
fail19_test.json
fail20_test.json
fail21_test.json
fail22_test.json
fail23_test.json
fail24_test.json
fail26_test.json
fail28_test.json
fail29_test.json
fail2_test.json
fail30_test.json
fail31_test.json
fail32_test.json
fail33_test.json
fail34_test.json
fail5_test.json
fail6_test.json
fail7_test.json
fail8_test.json
failKey1_test.hjson
failKey2_test.hjson
failKey3_test.hjson
failObj1_test.hjson
failObj2_test.hjson
failObj3_test.hjson
failStr1_test.hjson
failStr2_test.hjson
failStr3_test.hjson
failStr4_test.hjson
failStr5_test.hjson
failStr6_test.hjson
kan_test.hjson
keys_test.hjson
oa_test.hjson
pass1_test.json
pass2_test.json
pass3_test.json
pass4_test.json
passSingle_test.hjson
root_test.hjson
stringify1_test.hjson
strings_test.hjson
trail_test.hjson

0 comments on commit db7c332

Please sign in to comment.