Skip to content

Commit d5d4f46

Browse files
committed
more cleanup and more tests
1 parent 1362646 commit d5d4f46

18 files changed

+73
-62
lines changed

draft-marchan-kdl2.md

+61-58
Original file line numberDiff line numberDiff line change
@@ -267,75 +267,78 @@ about how to interpret a value.
267267

268268
### Suffix Type Annotation
269269

270-
When a ({{value}}) is a ({{number}}), it's possible to attach the type
271-
annotation as a "suffix", instead of prepending it between `(` and `)`. This
272-
makes it possible to, for example, write `10px`, `10.5%`, `512GiB`, etc., which
273-
are equivalent to `(px)10`, `(%)5`, and `(GiB)512`, respectively.
274-
275-
Most suffixes can be appended directly to the number (a
276-
({{bare-suffix-type-annotation}})), as shown in the previous paragraph. To avoid
277-
parsing ambiguity, there are some restrictions on this; an
278-
({{explicit-suffix-type-annotation}}) avoids all these restrictions by using an
279-
additional `#` to explicitly indicate it. For example, `10.0u8` is invalid, but
280-
`10.0#u8` is valid and equivalent to `(u8)10.0`. See
281-
({{bare-suffix-type-annotation}}) for the full list of restrictions.
282-
283-
An implementation that finds BOTH a parenthesized and a suffix
284-
({{type-annotation}}) on the same ({{number}}) MUST yield a syntax error.
285-
286-
Suffixes MUST BE plain ({{identifier-string}})s. No other ({{string}}) is
287-
acceptable.
288-
289-
There are two kinds of ({{suffix-type-annotation}}) available:
290-
({{bare-suffix-type-annotation}})s and ({{explicit-suffix-type-annotation}}).
270+
When a Value ({{value}}) is a Number ({{number}}), it's possible to attach the
271+
type annotation as a "suffix", instead of prepending it between `(` and `)`.
272+
This makes it possible to, for example, write `10px`, `10.5%`, `512GiB`, etc.,
273+
which are equivalent to `(px)10`, `(%)5`, and `(GiB)512`, respectively.
274+
275+
There are two kinds of Suffix Type Annotations ({{suffix-type-annotation}})
276+
available: Bare Suffix Type Annotations ({{bare-suffix-type-annotation}})s and
277+
Explicit Suffix Type Annotations ({{explicit-suffix-type-annotation}}).
278+
279+
Most suffixes can be appended directly to the number (a Bare Suffix Type
280+
Annotation ({{bare-suffix-type-annotation}})), as shown in the previous
281+
paragraph. To avoid parsing ambiguity, there are some restrictions on this; an
282+
Explicit Suffix Type Annotation ({{explicit-suffix-type-annotation}}) avoids all
283+
these restrictions by using an additional `#` to explicitly indicate it. For
284+
example, `10.0u8` is invalid, but `10.0#u8` is valid and equivalent to
285+
`(u8)10.0`. See Bare Suffix Type Annotation ({{bare-suffix-type-annotation}})
286+
for the full list of restrictions.
287+
288+
An implementation that finds BOTH a parenthesized ({{type-annotation}}) and a
289+
Suffix Type Annotation ({{suffix-type-annotation}}) on the same Number
290+
({{number}}) MUST yield a syntax error.
291+
292+
Suffixes MUST BE plain Identifier Strings ({{identifier-string}}). No other
293+
String ({{string}}) syntax is acceptable.
291294

292295
#### Bare Suffix Type Annotation
293296

294-
When a ({{value}}) is a decimal ({{number}}) WITHOUT exponential syntax (`1e+5`
295-
etc) (and ONLY a decimal: that is, numbers which do NOT have a `0b`/`0o`/`0x`
296-
prefix), it's possible to attach the type annotation as a suffix directly to the
297-
number, without any additional syntax.
297+
When a Value ({{value}}) is a decimal Number ({{number}}) WITHOUT exponential
298+
syntax (`1e+5` etc) (and ONLY a decimal. That is, numbers which do NOT have a
299+
`0b`/`0o`/`0x` prefix with an optional sign), it's possible to append the type
300+
annotation as a suffix directly to the number, without any additional syntax.
298301

299-
They also come with some additional rules (like only being available for
300-
decimals), in order to prevent potential ambiguity or footguns with the syntax.
301-
This is generally acceptable, as type annotations in particular tend to be
302-
application-defined and limited in scope, rather than arbitrary user data. In
303-
designing this feature, it was determined that the value for various real-world
304-
DSLs outweighed the complexity of the following rules.
302+
To remove further ambiguity, on top of not being available for non-decimal
303+
prefixes, and for decimals with exponent parts, the suffix Identifier String
304+
({{identifier-string}}) itself MUST NOT start with any of `.`, `,`, or `_`, as
305+
well as `[eE][-+]?[0-9]?` as part of the exponential restriction above. Note the
306+
optional digit, which is added to prevent typo ambiguity.
305307

306-
As such, to remove ambiguity, the suffix ({{identifier-string}}) MUST NOT start
307-
with any of the following patterns, all of which MUST yield syntax errors (if
308-
they can be distinguished from other syntaxes at all):
308+
For example, the following are all illegal:
309309

310-
* `.`, `,`, or `_`
311-
* `[eE][+-]?[0-9]` (to disambiguate exponentials)
310+
* `10,000` (suffix would start with `,`)
311+
* `10e0n` (suffix on an exponential)
312+
* `0xyz` (starts with reserved hexadecimal prefix)
313+
* `0b` (starts with reserved binary prefix)
314+
* `5e+oops` (looks too close to an exponential)
312315

313-
For example, `10,000` is illegal. `10e0n` is illegal, but `10e0` is a legal
314-
*decimal number using exponential syntax*, __not__ equivalent to `(e0)10`.
315-
Additionally, note that since bare suffixes are only legal on _decimals_, `0u8`
316-
is legal, but `0xs` is _not_, since hexadecimals are determined by their
317-
prefixes. Similarly, `1xs` _is_ legal, and equivalent to `(xs)1`.
316+
Whereas the following are all legal:
318317

319-
All other ({{identifier-string}})s can be safely appended to decimal numbers, so
320-
long as the decimal does not include an exponential component.
318+
* `0u8` (aka `(u8)0`)
319+
* `5em` (aka `(em)5`. The `e` is not followed by a digit.)
320+
* `1xyz` (aka `(xyz)1`. No longer starts with `0` as above.)
321+
* `20b` (aka `(b)20`, "20 bytes". No longer starts with just `0` as above.)
321322

322323
If the desired suffix would violate any of the above rules, either regular
323-
parenthetical ({{type-annotation}})s or ({{explicit-suffix-type-annotation}})s
324-
may be used.
324+
parenthetical Type Annotations ({{type-annotation}}) or Explicit Suffix Type
325+
Annotations ({{explicit-suffix-type-annotation}}) may be used.
325326

326327
#### Explicit Suffix Type Annotation
327328

328-
Any ({{number}}) may have a `#` appended to it, followed by any valid
329-
({{identifier-string}}). This is an explicit ({{suffix-type-annotation}}) syntax
330-
without any of the relatively complex requirements of
331-
({{bare-suffix-type-annotation}}), which can be a useful escape hatch. For
332-
example: `0#b1` is invalid syntax without the `#` prefix.
329+
Any Number ({{number}}) may have a `#` appended to it, followed by any valid
330+
Identifier String ({{identifier-string}}). This is an Explicit Suffix Type
331+
Annotation ({{suffix-type-annotation}}) syntax without any of the added
332+
restrictions of Bare Suffix Type Annotations ({{bare-suffix-type-annotation}}),
333+
which can be a useful escape hatch. For example: `0#b` is invalid syntax without
334+
the `#` prefix.
333335

334-
Note again that, unlike ({{bare-suffix-type-annotation}})s, Explicit Suffixes
335-
may be used with ALL ({{number}}) formats (hexadecimal, decimal, octal, and
336-
binary). For example, `0x1234#u32` is valid.
336+
Note that, unlike Bare Suffix Type Annotations
337+
({{bare-suffix-type-annotation}}), Explicit Suffixes may be used with ALL Number
338+
({{number}}) formats (hexadecimal, decimal, octal, and binary). For example,
339+
`0x1234#u32` is valid.
337340

338-
### Reserved Type Annotations for Numbers Without Decimals
341+
### Reserved Type Annotations for Numbers Without Decimal Parts
339342

340343
Additionally, the following type annotations MAY be recognized by KDL parsers
341344
and, if used, SHOULD interpret these types as follows.
@@ -361,7 +364,7 @@ Platform-dependent integer types, both signed and unsigned:
361364
- `isize`
362365
- `usize`
363366

364-
### Reserved Type Annotations for Numbers With Decimals:
367+
### Reserved Type Annotations for Numbers With Decimal Parts
365368

366369
IEEE 754 floating point numbers, both single (32) and double (64) precision:
367370

@@ -373,7 +376,7 @@ IEEE 754-2008 decimal floating point numbers
373376
- `decimal64`
374377
- `decimal128`
375378

376-
### Reserved Type Annotations for Strings:
379+
### Reserved Type Annotations for Strings
377380

378381
- `date-time`: ISO8601 date/time format.
379382
- `time`: "Time" section of ISO8601.
@@ -404,8 +407,8 @@ IEEE 754-2008 decimal floating point numbers
404407
### Examples
405408

406409
~~~kdl
407-
node (u8)123
408-
node 123#i64
410+
node 123u8
411+
node 0#b 20b 50GiB
409412
node prop=(regex).*
410413
(published)date "1970-01-01"
411414
(contributor)person name="Foo McBar"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node (u8)123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 0b0101hi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 0b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 1234e+foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 0x123nope
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node 123xabc123
1+
node 0xohno

tests/test_cases/input/suffix_type_bare_non_decimal_fail.kdl

-1
This file was deleted.

tests/test_cases/input/suffix_type_bare_non_identifier_fail.kdl

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 0o123nope
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node #"foo"#bar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 123"astring"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 456#"rawstring"#

tests/test_cases/input/suffix_type_explicit_non_identifier_fail.kdl

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 123#"string"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 456##"rawstring"#

0 commit comments

Comments
 (0)