Commit 0943bd9 1 parent 3e96dbb commit 0943bd9 Copy full SHA for 0943bd9
File tree 2 files changed +76
-20
lines changed
2 files changed +76
-20
lines changed Original file line number Diff line number Diff line change 29
29
grammar ANTLRv3;
30
30
31
31
grammarDef
32
- : DOC_COMMENT ? (' lexer' | ' parser' | ' tree' )
32
+ : DOC_COMMENT ? (' lexer' | ' parser' | ' tree' )? ' grammar ' id ' ; ' optionsSpec? tokensSpec? attrScope* action* rule_+
33
33
;
34
34
35
35
tokensSpec
@@ -67,7 +67,7 @@ optionValue
67
67
| STRING_LITERAL
68
68
| CHAR_LITERAL
69
69
| INT
70
- | s = ' *'
70
+ | ' *'
71
71
;
72
72
73
73
rule_
93
93
;
94
94
95
95
altList
96
- : alternative rewrite (' |' alternative rewrite)*
96
+ : alternative rewrite? (' |' alternative rewrite? )*
97
97
;
98
98
99
99
alternative
232
232
| RULE_REF
233
233
;
234
234
235
-
236
- SL_COMMENT
237
- : ' //' (' $ANTLR ' SRC | ~ (' \r ' | ' \n ' )*) ' \r ' ? ' \n ' -> skip
238
- ;
239
-
240
-
241
- ML_COMMENT
242
- : ' /*' ()*? ' */'
243
- ;
244
-
245
-
246
235
CHAR_LITERAL
247
236
: ' \' ' LITERAL_CHAR ' \' '
248
237
;
@@ -342,12 +331,6 @@ fragment SRC
342
331
: ' src' ' ' ACTION_STRING_LITERAL ' ' INT
343
332
;
344
333
345
-
346
- WS
347
- : (' ' | ' \t ' | ' \r ' ? ' \n ' ) + -> skip
348
- ;
349
-
350
-
351
334
fragment WS_LOOP
352
335
: (WS | SL_COMMENT | ML_COMMENT )*
353
336
;
@@ -542,3 +525,18 @@ RANGE2
542
525
REWRITE
543
526
: ' ->'
544
527
;
528
+
529
+
530
+ SL_COMMENT
531
+ : ' //' ~[\r\n]* -> skip
532
+ ;
533
+
534
+
535
+ ML_COMMENT
536
+ : ' /*' .*? ' */' -> skip
537
+ ;
538
+
539
+ WS
540
+ : (' ' | ' \t ' | ' \r ' ? ' \n ' ) + -> skip
541
+ ;
542
+
Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Copyright
2011 by Nathaniel Harward
< [email protected] >
4
+
5
+ ANTLRv3 grammar for CSV files.
6
+
7
+ * No trimming of spaces (disallowed in RFC4180)
8
+ * No trimming of double- quotes, either to define/ end a quoted field or
9
+ when embedded inside one
10
+ * Handles all/ mixed newline formats (MSDOS/ Windows; Unix; Mac OS)
11
+
12
+ If you find any issues please email me so I can correct it.
13
+
14
+ */
15
+
16
+ grammar csv;
17
+
18
+ file
19
+ : record (NEWLINE record)* EOF
20
+ ;
21
+
22
+ record
23
+ : (quoted_field | unquoted_field) (COMMA (quoted_field | unquoted_field))*
24
+ ;
25
+
26
+ quoted_field
27
+ : DQUOTE
28
+ ( CHAR
29
+ | COMMA
30
+ | DQUOTE DQUOTE
31
+ | NEWLINE
32
+ )* DQUOTE
33
+ ;
34
+
35
+ unquoted_field
36
+ : CHAR*
37
+ ;
38
+
39
+ CHAR
40
+ : ' \u 0000' .. ' \u 0009'
41
+ | ' \u 000b' .. ' \u 000c'
42
+ | ' \u 000e' .. ' \u 0021'
43
+ | ' \u 0023' .. ' \u 002b'
44
+ | ' \u 002d' .. ' \u ffff'
45
+ ;
46
+
47
+ COMMA
48
+ : ' \u 002c'
49
+ ;
50
+
51
+ DQUOTE
52
+ : ' \u 0022'
53
+ ;
54
+
55
+ NEWLINE
56
+ : ' \u 000d' ? ' \u 000a' // DOS/ Windows(\r\n) + Unix(\n)
57
+ | ' \u 000d' // Mac OS 9 and earlier(\r)
58
+ ;
You can’t perform that action at this time.
0 commit comments