-
Notifications
You must be signed in to change notification settings - Fork 0
/
cif.g
352 lines (278 loc) · 8.59 KB
/
cif.g
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
/** CIF Version 1.1 Working specification grammar
Translated from the grammar defined at
http://www.iucr.org/resources/cif/spec/version1.1/cifsyntax#bnf
A compiled version of the parser, with C language target, but contains
C++ code in the actions, therefore the output files must be renamed to *.cpp
Richard Gildea
April 2010
*/
grammar cif;
options {
language=C;
}
@lexer::includes{
#include <ucif/builder.h>
}
@includes{
#include <string>
#include <vector>
#include <ucif/builder.h>
}
@parser::context
{
ucif::array_wrapper_base* errors;
}
@lexer::context
{
ucif::array_wrapper_base* errors;
}
@parser::apifuncs
{
PARSER->super = (void *)ctx;
}
@lexer::apifuncs
{
LEXER->super = (void *)ctx;
}
@members {
std::string to_std_string(pANTLR3_COMMON_TOKEN token) {
ANTLR3_MARKER start = token->start;
ANTLR3_MARKER stop = token->stop;
std::string str((const char*)start, stop-start+1);
if ((str[0] == '\'' && str[str.size()-1] == '\'') ||
(str[0] == '"' && str[str.size()-1] == '"'))
{ str = str.substr(1, str.size()-2); }
return str;
}
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
// The start rule
parse[ucif::builder_base* builder_, bool strict_]
scope {
ucif::builder_base* builder;
bool strict;
}
@init {
$parse::builder = builder_;
$parse::strict = strict_;
}
: cif (EOF | '\u001a' /*Ctrl-Z*/) ;
/*------------------------------------------------------------------
* BASIC STRUCTURE OF A CIF
*------------------------------------------------------------------*/
cif
: COMMENTS? data_block*
;
loop_body
scope {
unsigned column_cntr;
}
@init {
$loop_body::column_cntr = 0;
}
: v1=value
{
if (($data_items::curr_loop_values)->size() > 0) {
(*($data_items::curr_loop_values))[0]->push_back(to_std_string($v1.start));
}
}
( v2=value
{
$loop_body::column_cntr++;
if ($loop_body::column_cntr == ($data_items::curr_loop_headers)->size()) {
$loop_body::column_cntr = 0;
}
(*($data_items::curr_loop_values))[$loop_body::column_cntr]->push_back(to_std_string($v2.start));
}
)*
;
save_frame
: SAVE_FRAME_HEADING
{ ($parse::builder)->start_save_frame(to_std_string($SAVE_FRAME_HEADING)); }
( data_items )+ SAVE
{ ($parse::builder)->end_save_frame(); }
;
data_items
scope {
std::vector<ucif::array_wrapper_base*>* curr_loop_values;
ucif::array_wrapper_base* curr_loop_headers;
}
@init {
$data_items::curr_loop_values = new std::vector<ucif::array_wrapper_base*>();
$data_items::curr_loop_headers = ($parse::builder)->new_array();
}
@after {
for (std::size_t i=0; i<($data_items::curr_loop_values)->size(); i++) {
delete (*($data_items::curr_loop_values))[i];
}
delete $data_items::curr_loop_values;
delete $data_items::curr_loop_headers;
}
: TAG value
{
if (($value.start)->type != EOF) {
($parse::builder)->add_data_item(
to_std_string($TAG),
to_std_string($value.start));
}
}
| loop_header loop_body
{
std::vector<ucif::array_wrapper_base*> values = *($data_items::curr_loop_values);
std::size_t n_cols = $data_items::curr_loop_headers->size();
if (n_cols > 0) {
std::size_t n_rows = values[0]->size();
bool loop_is_good = true;
for (std::size_t i=1; i<n_cols; i++) {
if (values[i]->size() != n_rows) {
std::string msg = "Wrong number of data items for loop containing ";
msg += (*$data_items::curr_loop_headers)[0];
CTX->errors->push_back(msg);
loop_is_good = false;
break;
}
}
if (loop_is_good) {
($parse::builder)->add_loop(*$data_items::curr_loop_headers, values);
}
}
}
;
data_block
: ( DATA_BLOCK_HEADING
{ ($parse::builder)->add_data_block(to_std_string($DATA_BLOCK_HEADING)); }
( ( data_items | save_frame ) )*
)
| ( {!$parse::strict}?=>GLOBAL_ ( ( data_items | save_frame ) )* ) // global blocks are ignored
;
loop_header
: LOOP_ ( TAG
{
($data_items::curr_loop_headers)->push_back(to_std_string($TAG));
($data_items::curr_loop_values)->push_back(($parse::builder)->new_array());
}
)+
;
/*------------------------------------------------------------------
* TAGS AND VALUES
*------------------------------------------------------------------*/
value
: INAPPLICABLE | UNKNOWN | NUMERIC | char_string | text_field
;
char_string
: CHAR_STRING ;
text_field
: SEMI_COLON_TEXT_FIELD ;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
/*------------------------------------------------------------------
* CHARACTER SETS
*------------------------------------------------------------------*/
fragment EOL
: ( '\n' | '\r' | '\r\n' ) ;
fragment DOUBLE_QUOTE
: '"' ;
fragment SINGLE_QUOTE
: '\'' ;
fragment ORDINARY_CHAR
: '!' | '%' | '&' | '(' | ')' | '*' | '+' | ',' | '-' | '.' | '/' |
( '0'.. '9' ) | ':' | '<' | '=' | '>' | '?' | '@' | ('A'..'Z') | ('a'..'z') |
'\\' | '^' | '`' | '{' | '|' | '}' | '~'
;
fragment NON_BLANK_CHAR_
: ORDINARY_CHAR | DOUBLE_QUOTE | SINGLE_QUOTE | '#' | '$' | '_' | '[' | ']' | ';' ;
fragment TEXT_LEAD_CHAR
: ORDINARY_CHAR | DOUBLE_QUOTE | SINGLE_QUOTE | '#' | '$' | '_' | '[' | ']' | ' ' | '\t' ;
fragment ANY_PRINT_CHAR
: ORDINARY_CHAR | '#' | '$' | '_' | '[' | ']' | ' ' | '\t' | ';'
// temporarily disable quotes from any print char until
// I figure out how to do it properly...
// | DOUBLE_QUOTE | SINGLE_QUOTE
;
TAG : '_' (NON_BLANK_CHAR_)+ ;
/*------------------------------------------------------------------
* RESERVED WORDS - define these after semicolon text field
*------------------------------------------------------------------*/
fragment
DATA_ : ( 'D' | 'd' ) ( 'A' | 'a' ) ( 'T' | 't' ) ( 'A' | 'a' ) '_' ;
fragment
SAVE_ : ( 'S' | 's' ) ( 'A' | 'a' ) ( 'V' | 'v' ) ( 'E' | 'e' ) '_' ;
LOOP_ : ( 'L' | 'l' ) ( 'O' | 'o' ) ( 'O' | 'o' ) ( 'P' | 'p' ) '_' ;
GLOBAL_ : ( 'G' | 'g' ) ( 'L' | 'l' ) ( 'O' | 'o' ) ( 'B' | 'b' ) ( 'A' | 'a' ) ( 'L' | 'l' ) '_' ;
STOP_ : ( 'S' | 's' ) ( 'T' | 't' ) ( 'O' | 'o' ) ( 'P' | 'p' ) '_' ;
/*------------------------------------------------------------------
* SPECIAL KEY WORDS
*------------------------------------------------------------------*/
//VERSION : '#\\#CIF_' (DIGIT)+ '.' (DIGIT)+ ;
DATA_BLOCK_HEADING
: DATA_ (NON_BLANK_CHAR)+ ;
SAVE_FRAME_HEADING
: SAVE_ (NON_BLANK_CHAR)+ ;
SAVE : SAVE_ ;
/*------------------------------------------------------------------
* NUMERICS
*------------------------------------------------------------------*/
fragment DIGIT : '0'..'9' ;
fragment EXPONENT
: ( ( 'e' | 'E') | ( 'e' | 'E')( '+' | '-' ) ) (DIGIT)+ ;
fragment INTEGER
: ( '+' | '-' )? (DIGIT)+ ;
fragment FLOAT
: INTEGER EXPONENT | ( ( '+' | '-' )? ( (DIGIT)* '.' (DIGIT)+) | (DIGIT)+ '.' ) (EXPONENT)? ;
fragment UNSIGNED_INTEGER
: (DIGIT)+ ;
fragment NUMBER
: INTEGER | FLOAT;
NUMERIC : NUMBER | ( NUMBER '(' (UNSIGNED_INTEGER)+ ')' ) ;
/*------------------------------------------------------------------
* CHARACTER STRINGS AND FIELDS
*------------------------------------------------------------------*/
INAPPLICABLE
: '.' ;
UNKNOWN : '?' ;
fragment UNQUOTED_STRING
: (({ GETCHARPOSITIONINLINE() > 0 }?=> ';')
| ORDINARY_CHAR ) (NON_BLANK_CHAR_)* ;
// apparently a single quoted string such as 'a dog's life' is legal...
fragment SINGLE_QUOTED_STRING
: SINGLE_QUOTE
( ( (SINGLE_QUOTE NON_BLANK_CHAR_)=>SINGLE_QUOTE ) | ANY_PRINT_CHAR | DOUBLE_QUOTE )*
SINGLE_QUOTE
;
fragment DOUBLE_QUOTED_STRING
: DOUBLE_QUOTE
( ( (DOUBLE_QUOTE NON_BLANK_CHAR_)=>DOUBLE_QUOTE ) | ANY_PRINT_CHAR | SINGLE_QUOTE )*
DOUBLE_QUOTE
;
CHAR_STRING
: UNQUOTED_STRING | SINGLE_QUOTED_STRING | DOUBLE_QUOTED_STRING;
SEMI_COLON_TEXT_FIELD
: ( { GETCHARPOSITIONINLINE() == 0 }?=> ';')
( ( ANY_PRINT_CHAR | SINGLE_QUOTE | DOUBLE_QUOTE )* EOL
( (TEXT_LEAD_CHAR ( ANY_PRINT_CHAR | SINGLE_QUOTE | DOUBLE_QUOTE )* )? EOL)* )
{ $start += 1; EMIT(); } // strip semicolons
';'
;
/*------------------------------------------------------------------
* WHITE SPACE AND COMMENTS
*------------------------------------------------------------------*/
COMMENTS
: ( ( '#' (ANY_PRINT_CHAR | SINGLE_QUOTE | DOUBLE_QUOTE )*
( EOL | { LA(1) == EOF }? ) )+ )
{ $channel = HIDDEN; }
;
//TOKENIZED_COMMENTS
// : ( ' ' | '\t' | EOL )+ COMMENTS_
// { $channel = HIDDEN; }
// ;
// Redefine this as non-fragment so can be seen by the parser
NON_BLANK_CHAR
: NON_BLANK_CHAR_ ;
WHITESPACE
: ( '\t' | ' ' | EOL | '\u000C' )+
//{ $channel = HIDDEN; }
{ SKIP(); }
;