Skip to content

Commit

Permalink
Merge pull request #181 from adobe/newissues
Browse files Browse the repository at this point in the history
New Issues
  • Loading branch information
JohnBrinkman authored Jul 10, 2024
2 parents 1dba1a8 + 301324a commit aa9722a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
7 changes: 4 additions & 3 deletions doc/spec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,11 @@ fragment EXP
;
----

Number literals follow the same syntax rules as numeric values in JSON with two exceptions:
Number literals follow the same syntax rules as numeric values in JSON with three exceptions:

1. Number literals may omit a leading zero. For example, `.123` is not valid JSON, but is allowed as a number literal.
2. The grammar construction for a number literal does not include a minus sign. Literal expressions are made negative by prefixing them with a unary minus.
1. Number literals may omit a leading zero before the decimal point. For example, `.123` is not valid JSON, but is allowed as a number literal.
2. Number literals may include leading zeros ahead of the integral part of the number. For example, `0123` is not valid JSON, but is allowed as a number literal.
3. The grammar construction for a number literal does not include a minus sign. Literal expressions are made negative by prefixing them with a unary minus.

Note that number literals (and JSON numbers) allow scientific notation.

Expand Down
2 changes: 1 addition & 1 deletion src/TreeInterpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class TreeInterpreter {
return null;
}

visit(n, v) {
visit(n, v = null) {
const visitFunctions = {
Identifier: this.field.bind(this),
QuotedIdentifier: this.field.bind(this),
Expand Down
6 changes: 3 additions & 3 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2181,9 +2181,9 @@ export default function functions(
const base = resolvedArgs.length > 1 ? toInteger(resolvedArgs[1]) : 10;
if (getType(num) === TYPE_STRING && base !== 10) {
let digitCheck;
if (base === 2) digitCheck = /^[01.]+$/;
else if (base === 8) digitCheck = /^[0-7.]+$/;
else if (base === 16) digitCheck = /^[0-9A-Fa-f.]+$/;
if (base === 2) digitCheck = /^\s*(\+|-)?[01.]+\s*$/;
else if (base === 8) digitCheck = /^\s*(\+|-)?[0-7.]+\s*$/;
else if (base === 16) digitCheck = /^\s*(\+|-)?[0-9A-Fa-f.]+\s*$/;
else throw evaluationError(`Invalid base: "${base}" for toNumber()`);

if (num === '') return 0;
Expand Down
1 change: 1 addition & 0 deletions src/stringToNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ governing permissions and limitations under the License.
import { typeError } from './errors.js';

export default function stringToNumber(n) {
if (!/^\s*(-|\+)?(\d*)(\.\d+)?(e(\+|-)?\d+)?\s*$/i.test(n)) throw typeError(`Failed to convert "${n}" to number`);
const ret = +n;
if (Number.isNaN(ret)) {
throw typeError(`Failed to convert "${n}" to number`);
Expand Down
22 changes: 22 additions & 0 deletions test/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,28 @@
"expression": "toNumber(toNum.n11, 16)",
"result": 11
},
{
"expression": "toNumber(\"0x11\")",
"result": null
},
{
"expression": "toNumber(\"0o11\")",
"result": null
},
{
"expression": "toNumber(\"0b11\")",
"result": null
},
{ "expression": "toNumber(\" 01e01\\n\")", "result": 10},
{ "expression": "toNumber(\" 011\\n\", 2)", "result": 3},
{ "expression": "toNumber(\" -011\\n\", 2)", "result": -3},
{ "expression": "toNumber(\" +011\\n\", 2)", "result": 3},
{ "expression": "toNumber(\" 011\\n\", 16)", "result": 17},
{ "expression": "toNumber(\" -011\\n\", 16)", "result": -17},
{ "expression": "toNumber(\" +011\\n\", 16)", "result": 17},
{ "expression": "toNumber(\" 011\\n\", 8)", "result": 9},
{ "expression": "toNumber(\" -011\\n\", 8)", "result": -9},
{ "expression": "toNumber(\" +011\\n\", 8)", "result": 9},
{
"expression": "'toString'(`1.0`)",
"error": "SyntaxError"
Expand Down
10 changes: 10 additions & 0 deletions test/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@
{ "expression": "sum([2,\"4\",6,`true`])", "result": 13 },
{ "expression": "(3 + 2) * 10 / 2 - 5", "result": 20 },
{ "expression": "2 * \"$2\" + `null`", "error": "TypeError" },
{ "expression": "\"0x11\" * 1", "error": "TypeError"},
{ "expression": "\"0b11\" * 1", "error": "TypeError"},
{ "expression": "\"0o11\" * 1", "error": "TypeError"},
{ "expression": "1+\"01e01\"", "result": 11},
{ "expression": "1*\" +011e+01\\n\"", "result": 110},
{ "expression": "1*\" -011e-01\\n\"", "result": -1.1},
{ "expression": "1 < 2", "result": true },
{ "expression": "1 > 2", "result": false },
{ "expression": "12 >= 12", "result": true },
Expand Down Expand Up @@ -1573,6 +1579,10 @@
"expression": "merge(register(\"_p1\", &42), register(\"_p2\", &43), {r: _p1() + _p2()})",
"result": { "r": 85 }
},
{
"expression": "register(\"_identity\", &@) || _identity()",
"result": null
},
{
"expression": "register(\"\", &42)",
"error": "FunctionError"
Expand Down

0 comments on commit aa9722a

Please sign in to comment.