forked from hohle/ion-rfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ion-rfc-appendix.nroff
646 lines (534 loc) · 12.6 KB
/
ion-rfc-appendix.nroff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
.tm 9. Appendix A: Antlr v4 Grammar for Ion 1.0 Text .................. \n%
.ti 0
9. Appendix A: Antlr v4 Grammar for Ion 1.0 Text
.in 0
.nf
// Ion Text 1.0 ANTLR v4 Grammar
//
// The following grammar does not encode all of the Ion semantics, in particular:
//
// * Timestamps are syntactically defined but the rules of ISO 8601 need to be
// applied (especially regarding day rules with months and leap years).
// * Non $ion_1_0 version markers are not trapped (e.g. $ion_1_1, $ion_2_0)
// * Edge cases around Unicode semantics:
// - ANTLR specifies only four hex digit Unicode escapes and on Java operates
// on UTF-16 code units (this is a flaw in ANTLR).
// - The grammar doesn't validate unpaired surrogate escapes in symbols or strings
// (e.g. "\udc00")
grammar IonText;
// note that EOF is a concept for the grammar, technically Ion streams
// are infinite
top_level
: (ws* top_level_value)* ws* value? EOF
;
top_level_value
: annotation+ top_level_value
| delimiting_entity
// numeric literals (if followed by something), need to be followed by
// whitespace or a token that is either quoted (e.g. string) or
// starts with punctuation (e.g. clob, struct, list)
| numeric_entity ws
| numeric_entity quoted_annotation value
| numeric_entity delimiting_entity
// literals that are unquoted symbols or keywords have a similar requirement
// as the numerics above, they have different productions because the
// rules for numerics are the same in s-expressions, but keywords
// have different rules between top-level and s-expressions.
| keyword_entity ws
| keyword_entity quoted_annotation value
| keyword_entity keyword_delimiting_entity
;
// TODO let's make sure this terminology
// is consistent with our specification documents
value
: annotation* entity
;
entity
: numeric_entity
| delimiting_entity
| keyword_entity
;
delimiting_entity
: quoted_text
| SHORT_QUOTED_CLOB
| LONG_QUOTED_CLOB
| BLOB
| list
| sexp
| struct
;
keyword_delimiting_entity
: delimiting_entity
| numeric_entity
;
keyword_entity
: any_null
| BOOL
| SPECIAL_FLOAT
| IDENTIFIER_SYMBOL
// note that this is because we recognize the type names for null
// they are ordinary symbols on their own
| TYPE
;
numeric_entity
: BIN_INTEGER
| DEC_INTEGER
| HEX_INTEGER
| TIMESTAMP
| FLOAT
| DECIMAL
;
annotation
: symbol ws* COLON COLON ws*
;
quoted_annotation
: QUOTED_SYMBOL ws* COLON COLON ws*
;
list
: L_BRACKET ws* value ws* (COMMA ws* value)* ws* (COMMA ws*)? R_BRACKET
| L_BRACKET ws* R_BRACKET
;
sexp
: L_PAREN (ws* sexp_value)* ws* value? R_PAREN
;
sexp_value
: annotation+ sexp_value
| sexp_delimiting_entity
| operator
// much like at the top level, numeric/identifiers/keywords
// have similar delimiting rules
| numeric_entity ws
| numeric_entity quoted_annotation value
| numeric_entity sexp_delimiting_entity
| sexp_keyword_entity ws
| sexp_keyword_entity quoted_annotation value
| sexp_keyword_entity sexp_keyword_delimiting_entity
| NULL ws
| NULL quoted_annotation value
| NULL sexp_null_delimiting_entity
;
sexp_delimiting_entity
: delimiting_entity
;
sexp_keyword_delimiting_entity
: sexp_delimiting_entity
| numeric_entity
| operator
;
sexp_null_delimiting_entity
: delimiting_entity
| NON_DOT_OPERATOR+
;
sexp_keyword_entity
: typed_null
| BOOL
| SPECIAL_FLOAT
| IDENTIFIER_SYMBOL
// note that this is because we recognize the type names for null
// they are ordinary symbols on their own
| TYPE
;
operator
: (DOT | NON_DOT_OPERATOR)+
;
struct
: L_CURLY ws* field (ws* COMMA ws* field)* ws* (COMMA ws*)? R_CURLY
| L_CURLY ws* R_CURLY
;
field
: field_name ws* COLON ws* annotation* entity
;
any_null
: NULL
| typed_null
;
typed_null
: NULL DOT NULL
| NULL DOT TYPE
;
field_name
: symbol
| SHORT_QUOTED_STRING
| (ws* LONG_QUOTED_STRING)+
;
quoted_text
: QUOTED_SYMBOL
| SHORT_QUOTED_STRING
| (ws* LONG_QUOTED_STRING)+
;
symbol
: IDENTIFIER_SYMBOL
// note that this is because we recognize the type names for null
// they are ordinary symbols on their own
| TYPE
| QUOTED_SYMBOL
;
ws
: WHITESPACE
| INLINE_COMMENT
| BLOCK_COMMENT
;
////////////////////////////////////////////////////////////////////////
// Ion Punctuation
////////////////////////////////////////////////////////////////////////
L_BRACKET : '[';
R_BRACKET : ']';
L_PAREN : '(';
R_PAREN : ')';
L_CURLY : '{';
R_CURLY : '}';
COMMA : ',';
COLON : ':';
DOT : '.';
NON_DOT_OPERATOR
: [!#%&*+\-/;<=>?@^`|~]
;
////////////////////////////////////////////////////////////////////////
// Ion Whitespace / Comments
////////////////////////////////////////////////////////////////////////
WHITESPACE
: WS+
;
INLINE_COMMENT
: '//' .*? (NL | EOF)
;
BLOCK_COMMENT
: '/*' .*? '*/'
;
////////////////////////////////////////////////////////////////////////
// Ion Null
////////////////////////////////////////////////////////////////////////
NULL
: 'null'
;
TYPE
: 'bool'
| 'int'
| 'float'
| 'decimal'
| 'timestamp'
| 'symbol'
| 'string'
| 'clob'
| 'blob'
| 'list'
| 'sexp'
| 'struct'
;
////////////////////////////////////////////////////////////////////////
// Ion Bool
////////////////////////////////////////////////////////////////////////
BOOL
: 'true'
| 'false'
;
////////////////////////////////////////////////////////////////////////
// Ion Timestamp
////////////////////////////////////////////////////////////////////////
TIMESTAMP
: DATE ('T' TIME?)?
| YEAR '-' MONTH 'T'
| YEAR 'T'
;
fragment
DATE
: YEAR '-' MONTH '-' DAY
;
fragment
YEAR
: '000' [1-9]
| '00' [1-9] DEC_DIGIT
| '0' [1-9] DEC_DIGIT DEC_DIGIT
| [1-9] DEC_DIGIT DEC_DIGIT DEC_DIGIT
;
fragment
MONTH
: '0' [1-9]
| '1' [0-2]
;
fragment
DAY
: '0' [1-9]
| [1-2] DEC_DIGIT
| '3' [0-1]
;
fragment
TIME
: HOUR ':' MINUTE (':' SECOND)? OFFSET
;
fragment
OFFSET
: 'Z'
| PLUS_OR_MINUS HOUR ':' MINUTE
;
fragment
HOUR
: [01] DEC_DIGIT
| '2' [0-3]
;
fragment
MINUTE
: [0-5] DEC_DIGIT
;
// note that W3C spec requires a digit after the '.'
fragment
SECOND
: [0-5] DEC_DIGIT ('.' DEC_DIGIT+)?
;
////////////////////////////////////////////////////////////////////////
// Ion Int
////////////////////////////////////////////////////////////////////////
BIN_INTEGER
: '-'? '0' [bB] BINARY_DIGIT (UNDERSCORE? BINARY_DIGIT)*
;
DEC_INTEGER
: '-'? DEC_UNSIGNED_INTEGER
;
HEX_INTEGER
: '-'? '0' [xX] HEX_DIGIT (UNDERSCORE? HEX_DIGIT)*
;
////////////////////////////////////////////////////////////////////////
// Ion Float
////////////////////////////////////////////////////////////////////////
SPECIAL_FLOAT
: PLUS_OR_MINUS 'inf'
| 'nan'
;
FLOAT
: DEC_INTEGER DEC_FRAC? FLOAT_EXP
;
fragment
FLOAT_EXP
: [Ee] PLUS_OR_MINUS? DEC_DIGIT+
;
////////////////////////////////////////////////////////////////////////
// Ion Decimal
////////////////////////////////////////////////////////////////////////
DECIMAL
: DEC_INTEGER DEC_FRAC? DECIMAL_EXP?
;
fragment
DECIMAL_EXP
: [Dd] PLUS_OR_MINUS? DEC_DIGIT+
;
////////////////////////////////////////////////////////////////////////
// Ion Symbol
////////////////////////////////////////////////////////////////////////
QUOTED_SYMBOL
: SYMBOL_QUOTE SYMBOL_TEXT SYMBOL_QUOTE
;
fragment
SYMBOL_TEXT
: (TEXT_ESCAPE | SYMBOL_TEXT_ALLOWED)*
;
// non-control Unicode and not single quote or backslash
fragment
SYMBOL_TEXT_ALLOWED
: '\u0020'..'\u0026' // no C1 control characters and no U+0027 single quote
| '\u0028'..'\u005B' // no U+005C backslash
| '\u005D'..'\uFFFF' // should be up to U+10FFFF
| WS_NOT_NL
;
IDENTIFIER_SYMBOL
: [$_a-zA-Z] ([$_a-zA-Z] | DEC_DIGIT)*
;
////////////////////////////////////////////////////////////////////////
// Ion String
////////////////////////////////////////////////////////////////////////
SHORT_QUOTED_STRING
: SHORT_QUOTE STRING_SHORT_TEXT SHORT_QUOTE
;
LONG_QUOTED_STRING
: LONG_QUOTE STRING_LONG_TEXT LONG_QUOTE
;
fragment
STRING_SHORT_TEXT
: (TEXT_ESCAPE | STRING_SHORT_TEXT_ALLOWED)*
;
fragment
STRING_LONG_TEXT
: (TEXT_ESCAPE | STRING_LONG_TEXT_ALLOWED)*?
;
// non-control Unicode and not double quote or backslash
fragment
STRING_SHORT_TEXT_ALLOWED
: '\u0020'..'\u0021' // no C1 control characters and no U+0022 double quote
| '\u0023'..'\u005B' // no U+005C backslash
| '\u005D'..'\uFFFF' // FIXME should be up to U+10FFFF
| WS_NOT_NL
;
// non-control Unicode (newlines are OK)
fragment
STRING_LONG_TEXT_ALLOWED
: '\u0020'..'\u005B' // no C1 control characters and no U+005C backslash
| '\u005D'..'\uFFFF' // FIXME should be up to U+10FFFF
| WS
;
fragment
TEXT_ESCAPE
: COMMON_ESCAPE | HEX_ESCAPE | UNICODE_ESCAPE
;
////////////////////////////////////////////////////////////////////////
// Ion CLOB
////////////////////////////////////////////////////////////////////////
SHORT_QUOTED_CLOB
: LOB_START WS* SHORT_QUOTE CLOB_SHORT_TEXT SHORT_QUOTE WS* LOB_END
;
LONG_QUOTED_CLOB
: LOB_START (WS* LONG_QUOTE CLOB_LONG_TEXT*? LONG_QUOTE)+ WS* LOB_END
;
fragment
CLOB_SHORT_TEXT
: (CLOB_ESCAPE | CLOB_SHORT_TEXT_ALLOWED)*
;
fragment
CLOB_LONG_TEXT
: CLOB_LONG_TEXT_NO_QUOTE
| '\'' CLOB_LONG_TEXT_NO_QUOTE
| '\'\'' CLOB_LONG_TEXT_NO_QUOTE
;
fragment
CLOB_LONG_TEXT_NO_QUOTE
: (CLOB_ESCAPE | CLOB_LONG_TEXT_ALLOWED)
;
// non-control ASCII and not double quote or backslash
fragment
CLOB_SHORT_TEXT_ALLOWED
: '\u0020'..'\u0021' // no U+0022 double quote
| '\u0023'..'\u005B' // no U+005C backslash
| '\u005D'..'\u007F'
| WS_NOT_NL
;
// non-control ASCII (newlines are OK)
fragment
CLOB_LONG_TEXT_ALLOWED
: '\u0020'..'\u0026' // no U+0027 single quote
| '\u0028'..'\u005B' // no U+005C backslash
| '\u005D'..'\u007F'
| WS
;
fragment
CLOB_ESCAPE
: COMMON_ESCAPE | HEX_ESCAPE
;
////////////////////////////////////////////////////////////////////////
// Ion BLOB
////////////////////////////////////////////////////////////////////////
BLOB
: LOB_START (BASE_64_QUARTET | WS)* BASE_64_PAD? WS* LOB_END
;
fragment
BASE_64_PAD
: BASE_64_PAD1
| BASE_64_PAD2
;
fragment
BASE_64_QUARTET
: BASE_64_CHAR WS* BASE_64_CHAR WS* BASE_64_CHAR WS* BASE_64_CHAR
;
fragment
BASE_64_PAD1
: BASE_64_CHAR WS* BASE_64_CHAR WS* BASE_64_CHAR WS* '='
;
fragment
BASE_64_PAD2
: BASE_64_CHAR WS* BASE_64_CHAR WS* '=' WS* '='
;
fragment
BASE_64_CHAR
: [0-9a-zA-Z+/]
;
////////////////////////////////////////////////////////////////////////
// Common Lexer Primitives
////////////////////////////////////////////////////////////////////////
fragment LOB_START : '{{';
fragment LOB_END : '}}';
fragment SYMBOL_QUOTE : '\'';
fragment SHORT_QUOTE : '"';
fragment LONG_QUOTE : '\'\'\'';
// Ion does not allow leading zeros for base-10 numbers
fragment
DEC_UNSIGNED_INTEGER
: '0'
| [1-9] (UNDERSCORE? DEC_DIGIT)*
;
fragment
DEC_FRAC
: '.'
| '.' DEC_DIGIT (UNDERSCORE? DEC_DIGIT)*
;
fragment
DEC_DIGIT
: [0-9]
;
fragment
HEX_DIGIT
: [0-9a-fA-F]
;
fragment
BINARY_DIGIT
: [01]
;
fragment
PLUS_OR_MINUS
: [+\-]
;
fragment
COMMON_ESCAPE
: '\\' COMMON_ESCAPE_CODE
;
fragment
COMMON_ESCAPE_CODE
: 'a'
| 'b'
| 't'
| 'n'
| 'f'
| 'r'
| 'v'
| '?'
| '0'
| '\''
| '"'
| '/'
| '\\'
| NL
;
fragment
HEX_ESCAPE
: '\\x' HEX_DIGIT HEX_DIGIT
;
fragment
UNICODE_ESCAPE
: '\\u' HEX_DIGIT_QUARTET
| '\\U000' HEX_DIGIT_QUARTET HEX_DIGIT
| '\\U0010' HEX_DIGIT_QUARTET
;
fragment
HEX_DIGIT_QUARTET
: HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
fragment
WS
: WS_NOT_NL
| '\u000A' // line feed
| '\u000D' // carriage return
;
fragment
NL
: '\u000D\u000A' // carriage return + line feed
| '\u000D' // carriage return
| '\u000A' // line feed
;
fragment
WS_NOT_NL
: '\u0009' // tab
| '\u000B' // vertical tab
| '\u000C' // form feed
| '\u0020' // space
;
fragment
UNDERSCORE
: '_'
;
.in 3