summaryrefslogtreecommitdiffstats
path: root/tksao/fitsy++/parser.Y
blob: b57d23d1156f0c67b7564765e14631c1641dec3f (plain)
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
// Copyright (C) 1999-2017
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

%pure-parser
%parse-param {FitsFile* ff}
%lex-param {ffFlexLexer* ll}
%parse-param {ffFlexLexer* ll}

%{
#define YYDEBUG 1

#define GOTOFILT(x) {yyclearin; ffFilter(x);}
#define GOTOARR(x) {yyclearin; ffArray(x);}

#include "file.h"
#include "hpx.h"

#undef yyFlexLexer
#define yyFlexLexer ffFlexLexer
#include <FlexLexer.h>

extern int fflex(void*, ffFlexLexer*);
extern void fferror(FitsFile*, ffFlexLexer*, const char*);

char ff_filter[512];
extern void ffFilter(int);
extern void ffArray(int);

%}

%union {
  float real;
  int integer;
  char str[256];
  void* ptr;
}

%type <integer> endian

%token <integer> INT
%token <str> STRING

%token ARCH_
%token ARRAY_
%token BIG_
%token BIGENDIAN_
%token BIN_
%token BINKEY_
%token BINCOL_
%token BITPIX_
%token COL_
%token DIM_
%token DIMS_
%token ECLIPTIC_
%token ENDIAN_
%token EQUATORIAL_
%token GALACTIC_
%token KEY_
%token LAYOUT_
%token LITTLE_
%token LITTLEENDIAN_
%token NESTED_
%token NORTH_
%token ORDER_
%token QUAD_
%token RING_
%token SKIP_
%token SOUTH_
%token SYSTEM_
%token UNKNOWN_
%token XDIM_
%token YDIM_
%token ZDIM_

%%

 //start: {yydebug=1;} command

// assume any error is the start of a filter
command	: filename
	| filename ext
 	| filename ext sect
 	| filename sect
 	| filename '[' extb sectb ']'

	| filename ext bin
 	| filename ext bin sect
 	| filename ext sect bin
	| filename bin
	| filename bin sect
	| filename sect bin
 	| filename '[' extb binb ']'

	| filename ext hpxs
	| filename ext hpxs sect
	| filename ext sect hpxs
	| filename hpxs
	| filename hpxs sect
	| filename sect hpxs
	| filename '[' extb hpxs ']'

        | filename arrs
        | filename arrs sect
        | filename sect arrs
	| filename '[' ARRAY_ {GOTOARR(0)} '(' array ')' ']'

	| error {GOTOFILT(0)} STRING 
	  {ff->setValid(1);ff->setpFilter(ff_filter);}
	;

filename : /* empty */
        |  STRING {ff->setpName($1);}
	;

// we must do it this way so that a bare filter will be accepted
//ext	: '[' extb ']'
//      ;
//extb	: STRING {ff->setpExt($1);}
//	| INT {ff->setpIndex($1);}
//	;

ext	: '[' STRING ']' {ff->setpExt($2);}
        | '[' INT ']' {ff->setpIndex($2);}
        ;

extb	: STRING ',' {ff->setpExt($1);}
        | INT ',' {ff->setpIndex($1);}
	;

sect	: '[' sectb physical ']'

sectb	: rangex ',' rangey
        | rangex ',' rangey ',' block
        | rangex ',' rangey ',' rangez
        | rangex ',' rangey ',' block ',' rangez
        | rangex ',' block
        | rangexy
        | rangexy ',' block
        | rangexy ',' rangez
        | rangexy ',' block ',' rangez
	;

physical : /* empty */ {ff->setpcoord(0);}
        | 'p' {ff->setpcoord(1);}
        | 'P' {ff->setpcoord(1);}
        ;

rangex	: INT ':' INT 
	{
	  // this is not a mistake
	  // need to fool parser into processing "xy0:xy1,block"
	  ff->setpxmin($1); ff->setpxmax($3); ff->setpxvalid(1);
	  ff->setpymin($1); ff->setpymax($3); ff->setpyvalid(1);
	}
	| INT '@' INT 
	  {ff->setpxmin($3-$1/2); ff->setpxmax($3+$1/2); ff->setpxvalid(1);}
	| '*'
	{
	  // this is not a mistake
	  // need to fool parser into processing "*,block"
	  ff->setpxvalid(0); ff->setpyvalid(0);
	}
	;

rangey	: INT ':' INT
	  {ff->setpymin($1); ff->setpymax($3); ff->setpyvalid(1);}
	| INT '@' INT
	  {ff->setpymin($3-$1/2); ff->setpymax($3+$1/2); ff->setpyvalid(1);}
	| '*' {ff->setpyvalid(0);}
	;

rangexy : INT '@' INT '@' INT
	{
	  ff->setpxmin($3-$1/2); ff->setpxmax($3+$1/2); ff->setpxvalid(1);
	  ff->setpymin($5-$1/2); ff->setpymax($5+$1/2); ff->setpyvalid(1);
 	}
        ;

rangez	: INT ':' INT
	  {ff->setpzmin($1); ff->setpzmax($3); ff->setpzvalid(1);}
	| INT '@' INT
	  {ff->setpzmin($3-$1/2); ff->setpzmax($3+$1/2); ff->setpzvalid(1);}
	| '*' {ff->setpzvalid(0);}
	;

block   : INT {ff->setpblock($1); ff->setpbvalid(1);}
        ;

bin	: '[' binb ']'

binb	: binword '=' binkey
	| binword binkey
	;

binword	: BIN_
	| BINKEY_
	| BINCOL_
	| KEY_
	;

binkey	: '(' STRING ',' STRING ')' {ff->setpBinXY($2,$4);}
	| STRING ',' STRING {ff->setpBinXY($1,$3);}
	| '(' STRING ',' STRING ',' STRING ')' {ff->setpBinXYZ($2,$4,$6);}
	| STRING ',' STRING ',' STRING {ff->setpBinXYZ($1,$3,$5);}
	| '(' STRING ')' {ff->setpBinZ($2);}
	| STRING {ff->setpBinZ($1);}
	;

arrs    : '[' arrsb ']'
        ;

arrsb	: arrsb ',' arr
	| arr
	;

arr	: XDIM_ '=' INT {ff->setpWidth($3);}
	| YDIM_ '=' INT {ff->setpHeight($3);}
	| ZDIM_ '=' INT {ff->setpDepth($3);}
	| DIM_ '=' INT {ff->setpWidth($3);ff->setpHeight($3);}
	| DIMS_ '=' INT {ff->setpWidth($3);ff->setpHeight($3);}
	| BITPIX_ '=' INT {ff->setpBitpix($3);}
	| SKIP_ '=' INT {ff->setpSkip($3);}
        | ARCH_ '=' endian {ff->setpArch((FitsFile::ArchType)$3);} 
	| ENDIAN_ '=' endian {ff->setpArch((FitsFile::ArchType)$3);} 
	| endian {ff->setpArch((FitsFile::ArchType)$1);} 
	;

endian	: BIG_ {$$ = FitsFile::BIG;}
	| BIGENDIAN_ {$$ = FitsFile::BIG;}
	| LITTLE_ {$$ = FitsFile::LITTLE;}
	| LITTLEENDIAN_ {$$ = FitsFile::LITTLE;}
        ;

array	: atype adims askip aendian
	;

atype	: 'b' {ff->setpBitpix(8);}
	| 's' {ff->setpBitpix(16);}
	| 'u' {ff->setpBitpix(-16);}
	| 'i' {ff->setpBitpix(32);}
	| 'l' {ff->setpBitpix(64);}
	| 'r' {ff->setpBitpix(-32);}
	| 'f' {ff->setpBitpix(-32);}
	| 'd' {ff->setpBitpix(-64);}
	;

adims	: INT {ff->setpWidth($1);ff->setpHeight($1);}
	| INT '.' INT {ff->setpWidth($1);ff->setpHeight($3);}
	| INT '.' INT '.' INT 
	  {ff->setpWidth($1);ff->setpHeight($3);ff->setpDepth($5);}
	;

askip	: /* empty */
	| ':' INT {ff->setpSkip($2);}
	;

aendian	: /* empty */
	| 'l' {ff->setpArch(FitsFile::LITTLE);}
	| 'b' {ff->setpArch(FitsFile::BIG);}
	;

hpxs    : '[' hpxsb ']'
        ;

hpxsb	: hpxsb ',' hpx
	| hpx
	;

hpx	: SYSTEM_ '=' hpxSystem
	| ORDER_ '=' hpxOrder
	| LAYOUT_ '=' hpxLayout
	| COL_ '=' INT {ff->setpHPXColumn($3);}
	| QUAD_ '=' INT {ff->setpHPXQuad($3);}
	;

hpxSystem : EQUATORIAL_ {ff->setpHPXSystem(FitsHPX::EQU);}
	  | GALACTIC_ {ff->setpHPXSystem(FitsHPX::GAL);}
	  | ECLIPTIC_ {ff->setpHPXSystem(FitsHPX::ECL);}
	  | UNKNOWN_ {ff->setpHPXSystem(FitsHPX::UNKNOWN);}
	  ;

hpxOrder : RING_ {ff->setpHPXOrder(FitsHPX::RING);}
	 | NESTED_ {ff->setpHPXOrder(FitsHPX::NESTED);}
	 ;

hpxLayout : EQUATORIAL_ {ff->setpHPXLayout(FitsHPX::EQUATOR);}
	  | NORTH_ {ff->setpHPXLayout(FitsHPX::NORTH);}
	  | SOUTH_ {ff->setpHPXLayout(FitsHPX::SOUTH);}
	  ;

%%