json5 / json5-spec

The JSON5 Data Interchange Format
https://spec.json5.org
MIT License
49 stars 11 forks source link

Leading and/or trailing decimal points in literals #28

Open bford opened 4 years ago

bford commented 4 years ago

First, thanks for all your hard and continuing work on this spec!

I'm finding the specification of Number syntax a bit unclear, though, with respect to trailing decimal points in particular.

The JSON summary says "Numbers may have a leading or trailing decimal point", but I don't quite see that expressed in the Number syntax description in section 6. For example, it says "Decimal numbers contain an integer component...", then the next paragraph says, "A fraction part is a decimal point followed by one or more digits". But if a trailing decimal point is allowed, then shouldn't the fraction part be "a decimal point followed by zero or more digits"? Requiring the fraction part to be one or more digits would seem to disallow a trailing decimal point, wouldn't it?

The description is also confusing in that it defines what "a fraction part" is but skips over defining what "an integer part" is. Presumably an integer part should consist of "zero or more digits" as well given that a leading decimal point is allowed? What is the exact syntax of the "integer part"?

Further, is it valid for both the integer and fraction part to be empty? i.e., is just a single decimal point '.' alone a valid number? I wouldn't think so. Presumably the integer part OR the fraction part, but not both, may be empty (contain no decimal digits).

Since the JSON5NumericLiteral syntax definition refers to ECMAScript 5.1's NumericLiteral, I suppose it may be reasonable to interpret that as definitive, and that specification seems to confirm that either the integer part or the fraction part but not both may be empty. If that's the case, it would be nice if the JSON5 spec's description was aligned with that.

Finally, the description of hexadecimal numbers suggests but doesn't quite explicitly say that hexadecimal numbers can't have fraction or exponent parts. I would suggest making that explicit just for clarity.

To suggest a concrete fix, I would propose:

  1. Just before the paragraph "A fraction part is...", insert a paragraph saying "An integer part consists of zero or more digits. The first digit must be nonzero unless the integer part consists of only a single zero digit."

  2. After the sentence "A fraction part is...", insert "If the integer part contains no digits, then the fraction part must contain one or more digits." Or something like that.

  3. Although the "Examples" box is only informative, it would be nice if it included an example with a trailing decimal point, e.g., "123.".

  4. To the paragraph "Hexadecimal numbers contain...", append "Hexadecimal numbers can only represent integers and have no fraction or exponent parts."

Thanks!

jordanbtucker commented 4 years ago

Thanks for pointing this out. The spec should definitely be updated with your suggestions. I lifted most of the verbiage from the JSON RFC, which is why it doesn't quite match the rules of JSON5.