Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editorial: Define + use StringToNumber and NumericValue
Browse files Browse the repository at this point in the history
Formerly, the procedure for applying ToNumber to the String type
was spread over three widely-separated prose paragraphs
in two different clauses.

This commit brings all that together, expresses it as an actual algorithm,
and gives it the name StringToNumber.

Similarly for the value of a NumericLiteral, here expressed as
the syntax-directed operation NumericValue.

This commit should provide better integration-points for PR #1515 (BigInt).
jmdyck committed May 28, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a5375bd commit 68d8da5
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions spec.html
Original file line number Diff line number Diff line change
@@ -3637,7 +3637,7 @@ <h1>ToNumber ( _argument_ )</h1>
String
</td>
<td>
See grammar and conversion algorithm below.
Return StringToNumber(_argument_).
</td>
</tr>
<tr>
@@ -3666,10 +3666,7 @@ <h1>ToNumber ( _argument_ )</h1>

<emu-clause id="sec-tonumber-applied-to-the-string-type">
<h1>ToNumber Applied to the String Type</h1>
<p>ToNumber applied to Strings applies the following grammar to the input String interpreted as a sequence of UTF-16 encoded code points (<emu-xref href="#sec-ecmascript-language-types-string-type"></emu-xref>). If the grammar cannot interpret the String as an expansion of |StringNumericLiteral|, then the result of ToNumber is *NaN*.</p>
<emu-note>
<p>The terminal symbols of this grammar are all composed of characters in the Unicode Basic Multilingual Plane (BMP). Therefore, the result of ToNumber will be *NaN* if the string contains any <emu-xref href="#leading-surrogate"></emu-xref> or <emu-xref href="#trailing-surrogate"></emu-xref> code units, whether paired or unpaired.</p>
</emu-note>
<p>The abstract operation StringToNumber specifies how to convert a String value to a Number value. Overall, the process is similar to the determination of the NumericValue of a numeric literal (see <emu-xref href="#sec-literals-numeric-literals"></emu-xref>), but some of the details are different.</p>
<h2>Syntax</h2>
<emu-grammar type="definition">
StringNumericLiteral :::
@@ -3724,7 +3721,6 @@ <h2>Syntax</h2>

<emu-clause id="sec-runtime-semantics-mv-s">
<h1>Runtime Semantics: MV</h1>
<p>The conversion of a String to a Number value is similar overall to the determination of the Number value for a numeric literal (see <emu-xref href="#sec-literals-numeric-literals"></emu-xref>), but some of the details are different, so the process for converting a String numeric literal to a value of Number type is given here. This value is determined in two steps: first, a mathematical value (MV) is derived from the String numeric literal; second, this mathematical value is rounded as described below. The MV on any grammar symbol, not provided below, is the MV for that symbol defined in <emu-xref href="#sec-static-semantics-mv"></emu-xref>.</p>
<ul>
<li>
The MV of <emu-grammar>StringNumericLiteral ::: [empty]</emu-grammar> is 0.
@@ -3784,7 +3780,30 @@ <h1>Runtime Semantics: MV</h1>
The MV of <emu-grammar>StrUnsignedDecimalLiteral ::: DecimalDigits ExponentPart</emu-grammar> is the MV of |DecimalDigits| times 10<sup>_e_</sup>, where _e_ is the MV of |ExponentPart|.
</li>
</ul>
<p>Once the exact MV for a String numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is *+0* unless the first non white space code point in the String numeric literal is `"-"`, in which case the rounded value is *-0*. Otherwise, the rounded value must be the Number value for the MV (in the sense defined in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>), unless the literal includes a |StrUnsignedDecimalLiteral| and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit and then incrementing the literal at the 20th digit position. A digit is significant if it is not part of an |ExponentPart| and</p>
<p>For the MV of any production not shown above, see <emu-xref href="#sec-static-semantics-mv"></emu-xref>.</p>
</emu-clause>

<emu-clause id="sec-stringtonumber" aoid="StringToNumber">
<h1>StringToNumber ( _str_ )</h1>
<emu-alg>
1. Assert: Type(_str_) is String.
1. Let _text_ be the sequence of Unicode code points that results from interpreting _str_ as UTF-16 encoded Unicode text as described in <emu-xref href="#sec-ecmascript-language-types-string-type"></emu-xref>.
1. Let _literal_ be the result of parsing _text_ using the goal symbol |StringNumericLiteral|. If _text_ does not conform to the grammar, or if any elements of _text_ were not matched by the parse, return *NaN*.
1. NOTE: The terminal symbols of the |StringNumericLiteral| grammar are all code points in the Unicode Basic Multilingual Plane (BMP). Therefore, this operation will return *NaN* if _str_ contains any <emu-xref href="#leading-surrogate"></emu-xref> or <emu-xref href="#trailing-surrogate"></emu-xref> code units, whether paired or unpaired.
1. If _literal_ contains a |StrUnsignedDecimalLiteral| and _literal_ has more than 20 significant digits, then
1. Let _lit_ be an implementation-dependent choice of:
* _literal_
* a literal produced from _literal_ by replacing each significant digit after the 20th with a `0` digit
* a literal produced from _literal_ by replacing each significant digit after the 20th with a `0` digit and then incrementing the literal at the 20th significant digit position
1. Else,
1. Let _lit_ be _literal_.
1. Let _mv_ be the MV of _lit_.
1. If _mv_ is 0, then
1. If the first non white space code point in _text_ is `"-"`, return *-0*; else return *+0*.
1. Return the Number value for _mv_ (as specified in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>).
</emu-alg>

<p>A digit is significant if it is not part of an |ExponentPart| and</p>
<ul>
<li>
it is not `0`; or
@@ -10067,7 +10086,7 @@ <h2>Syntax</h2>

<emu-clause id="sec-static-semantics-mv">
<h1>Static Semantics: MV</h1>
<p>A numeric literal stands for a value of the Number type. This value is determined in two steps: first, a mathematical value (MV) is derived from the literal; second, this mathematical value is rounded as described below.</p>
<p>A numeric literal stands for a value of the Number type, as specified by the syntax-directed operation NumericValue. Determining this value involves deriving a mathematical value (MV) from the literal, according to the following rules.</p>
<ul>
<li>
The MV of <emu-grammar>NumericLiteral :: DecimalLiteral</emu-grammar> is the MV of |DecimalLiteral|.
@@ -10217,7 +10236,37 @@ <h1>Static Semantics: MV</h1>
The MV of <emu-grammar>HexDigits :: HexDigits HexDigit</emu-grammar> is (the MV of |HexDigits| &times; 16) plus the MV of |HexDigit|.
</li>
</ul>
<p>Once the exact MV for a numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is *+0*; otherwise, the rounded value must be the Number value for the MV (as specified in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>), unless the literal is a |DecimalLiteral| and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a `0` digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a `0` digit and then incrementing the literal at the 20th significant digit position. A digit is <em>significant</em> if it is not part of an |ExponentPart| and</p>
</emu-clause>

<emu-clause id="sec-numericvalue">
<h1>Static Semantics: NumericValue</h1>
<emu-grammar>
NumericLiteral ::
BinaryIntegerLiteral
OctalIntegerLiteral
HexIntegerLiteral
</emu-grammar>
<emu-alg>
1. Let _mv_ be the MV of |NumericLiteral|.
1. If _mv_ is 0, return *+0*.
1. Return the Number value for _mv_ (as specified in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>).
</emu-alg>

<emu-grammar>NumericLiteral :: DecimalLiteral</emu-grammar>
<emu-alg>
1. If |DecimalLiteral| has more than 20 significant digits, then
1. Let _lit_ be an implementation-dependent choice of:
* |DecimalLiteral|
* a literal produced from |DecimalLiteral| by replacing each significant digit after the 20th with a `0` digit
* a literal produced from |DecimalLiteral| by replacing each significant digit after the 20th with a `0` digit and then incrementing the literal at the 20th significant digit position
1. Else,
1. Let _lit_ be |DecimalLiteral|.
1. Let _mv_ be the MV of _lit_.
1. If _mv_ is 0, return *+0*.
1. Return the Number value for _mv_ (as specified in <emu-xref href="#sec-ecmascript-language-types-number-type"></emu-xref>).
</emu-alg>

<p>A digit is <em>significant</em> if it is not part of an |ExponentPart| and</p>
<ul>
<li>
it is not `0`; or
@@ -11456,7 +11505,7 @@ <h1>Runtime Semantics: Evaluation</h1>
</emu-alg>
<emu-grammar>Literal : NumericLiteral</emu-grammar>
<emu-alg>
1. Return the number whose value is MV of |NumericLiteral| as defined in <emu-xref href="#sec-literals-numeric-literals"></emu-xref>.
1. Return NumericValue of |NumericLiteral| as defined in <emu-xref href="#sec-literals-numeric-literals"></emu-xref>.
</emu-alg>
<emu-grammar>Literal : StringLiteral</emu-grammar>
<emu-alg>
@@ -11729,7 +11778,7 @@ <h1>Static Semantics: PropName</h1>
</emu-alg>
<emu-grammar>LiteralPropertyName : NumericLiteral</emu-grammar>
<emu-alg>
1. Let _nbr_ be the result of forming the value of the |NumericLiteral|.
1. Let _nbr_ be NumericValue of |NumericLiteral|.
1. Return ! ToString(_nbr_).
</emu-alg>
<emu-grammar>ComputedPropertyName : `[` AssignmentExpression `]`</emu-grammar>
@@ -11780,7 +11829,7 @@ <h1>Runtime Semantics: Evaluation</h1>
</emu-alg>
<emu-grammar>LiteralPropertyName : NumericLiteral</emu-grammar>
<emu-alg>
1. Let _nbr_ be the result of forming the value of the |NumericLiteral|.
1. Let _nbr_ be NumericValue of |NumericLiteral|.
1. Return ! ToString(_nbr_).
</emu-alg>
<emu-grammar>ComputedPropertyName : `[` AssignmentExpression `]`</emu-grammar>

0 comments on commit 68d8da5

Please sign in to comment.