-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Return error for string literal beginning with single quote #2964
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
Changes from 4 commits
90546b0
e57701e
d3295f1
35ae8ba
4b60de1
a8adc84
52ee3db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
flase | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -512,6 +512,12 @@ static pfunc check_literal(struct jv_parser* p) { | |
switch (p->tokenbuf[0]) { | ||
case 't': pattern = "true"; plen = 4; v = jv_true(); break; | ||
case 'f': pattern = "false"; plen = 5; v = jv_false(); break; | ||
case '\'': | ||
return "Invalid string literal; expected \", but got '"; | ||
case '/': | ||
case '*': | ||
case '&': | ||
return "Invalid literal"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In @wader's comment above, he found other character's that were triggering the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should it say "Invalid literal" instead of "Invalid numeric literal" for those three characters? How is that better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make more sense for those to trigger There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Invalid numeric literal" means that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. I guess if we got to this point, then we've tried parsing it as a string, boolean, and null. So all that's left is parsing it as a number, hence that message. Maybe the better error message would be something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that ppl search and find #501 (it's the most visited issue) because we mention numeric in many error cases and it's confusing Had a go at compring jq to $ for i in ident truea falsea nana aa1 1aa + - / "&" "*" '(' "'aa'"; do echo "$i:\t$(jq 2>&1 <<< "$i")"; echo " $(node -p "JSON.parse(\"$i\")" 2>&1 | grep Error | head -n1)"; done
ident: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token 'i', "ident" is not valid JSON
truea: jq: parse error: Invalid literal at line 2, column 0
SyntaxError: Unexpected non-whitespace character after JSON at position 4 (line 1 column 5)
falsea: jq: parse error: Invalid literal at line 2, column 0
SyntaxError: Unexpected non-whitespace character after JSON at position 5 (line 1 column 6)
nana: jq: parse error: Invalid literal at line 2, column 0
SyntaxError: Unexpected token 'a', "nana" is not valid JSON
aa1: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token 'a', "aa1" is not valid JSON
1aa: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected non-whitespace character after JSON at position 1 (line 1 column 2)
+: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token '+', "+" is not valid JSON
-: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)
/: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token '/', "/" is not valid JSON
&: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token '&', "&" is not valid JSON
*: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token '*', "*" is not valid JSON
(: jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token '(', "(" is not valid JSON
'aa': jq: parse error: Invalid numeric literal at line 2, column 0
SyntaxError: Unexpected token ''', "'aa'" is not valid JSON There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me it's ok changing the default error to Should we just change the default error to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be happy to implement a general |
||
case 'n': | ||
// if it starts with 'n', it could be a literal "nan" | ||
if (p->tokenpos != 3) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2095,3 +2095,19 @@ null | |
try ["ok", setpath([1]; 1)] catch ["ko", .] | ||
{"hi":"hello"} | ||
["ko","Cannot index object with number"] | ||
|
||
try fromjson catch . | ||
"{'a': 123}" | ||
"Invalid string literal; expected \", but got ' at line 1, column 5 (while parsing '{'a': 123}')" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
|
||
try fromjson catch . | ||
"{\"a\": /}" | ||
"Invalid literal at line 1, column 8 (while parsing '{\"a\": /}')" | ||
|
||
try fromjson catch . | ||
"{\"a\": *}" | ||
"Invalid literal at line 1, column 8 (while parsing '{\"a\": *}')" | ||
|
||
try fromjson catch . | ||
"{\"a\": &}" | ||
"Invalid literal at line 1, column 8 (while parsing '{\"a\": &}')" |
Uh oh!
There was an error while loading. Please reload this page.