forked from brouhaha/tumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
309 lines (242 loc) · 6.8 KB
/
parser.y
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
/*
* tumble: build a PDF file from image files
*
* Parser
* Copyright 2001, 2002, 2003, 2017 Eric Smith <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*
* 2009-03-13 [JDB] Add support for blank pages, overlay images, color
* mapping, color-key masking, and push/pop of input
* contexts.
*/
%{
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "semantics.h"
extern int yylex (void);
%}
%define parse.error verbose
%union {
int integer;
char character;
double fp;
char *string;
page_size_t size;
range_t range;
page_label_t page_label;
overlay_t overlay;
rgb_t rgb;
rgb_range_t rgb_range;
}
%token <integer> INTEGER
%token <fp> FLOAT
%token <string> STRING
%token <character> CHARACTER
%token <size> PAGE_SIZE
%token ELIPSIS
%token CM
%token INCH
%token PORTRAIT
%token LANDSCAPE
%token FILE_KEYWORD
%token IMAGE
%token IMAGES
%token IMAGEMASK
%token ROTATE
%token CROP
%token SIZE
%token RESOLUTION
%token BLANK
%token INPUT
%token TRANSPARENT
%token COLORMAP
%token LABEL
%token OVERLAY
%token PAGE
%token PAGES
%token BOOKMARK
%token OUTPUT
%token AUTHOR
%token CREATOR
%token TITLE
%token SUBJECT
%token KEYWORDS
%type <range> range
%type <range> image_ranges
%type <range> page_ranges
%type <fp> unit
%type <fp> length
%type <integer> orientation
%type <size> page_size
%type <rgb> rgb
%type <rgb_range> rgb_range
%type <rgb_range> gray_range
%type <rgb_range> color_range
%%
statements:
statement
| statements statement ;
statement:
/* empty: null statement */
| input_statement
| output_statement ;
range:
INTEGER ELIPSIS INTEGER { $$.first = $1; $$.last = $3; }
| INTEGER { $$.first = $1; $$.last = $1; } ;
image_ranges:
range { input_images ($1); }
| image_ranges ',' range { input_images ($3); } ;
input_file_clause:
FILE_KEYWORD STRING { input_set_file ($2); } ;
image_clause:
IMAGE INTEGER { range_t range = { $2, $2 }; input_images (range); } ;
images_clause:
IMAGES image_ranges ;
rotate_clause:
ROTATE INTEGER { input_set_rotation ($2); } ;
unit:
/* empty */ /* default to INCH */ { $$ = 1.0; }
| CM { $$ = (1.0 / 25.4); }
| INCH { $$ = 1.0; } ;
length:
FLOAT unit { $$ = $1 * $2; } ;
crop_clause:
CROP PAGE_SIZE
| CROP PAGE_SIZE orientation
| CROP length ',' length
| CROP length ',' length ',' length ',' length ;
orientation:
PORTRAIT { $$ = 0; }
| LANDSCAPE { $$ = 1; } ;
page_size:
PAGE_SIZE { $$ = $1; }
| PAGE_SIZE orientation { if ($2)
{
$$.width = $1.height;
$$.height = $1.width;
}
else
$$ = $1;
}
| length ',' length { $$.width = $1; $$.height = $3; } ;
size_clause:
SIZE page_size { input_set_page_size ($2); } ;
resolution_clause:
RESOLUTION FLOAT unit ;
rgb_range:
'(' range range range ')' { $$.red = $2; $$.green = $3; $$.blue = $4; }
gray_range:
'(' range ')' { $$.red = $2; $$.green = $2; $$.blue = $2; } ;
color_range:
rgb_range
| gray_range;
transparency_clause:
TRANSPARENT color_range { input_set_transparency ($2); } ;
modifier_clause:
rotate_clause ';'
| crop_clause ';'
| size_clause ';'
| resolution_clause ';'
| transparency_clause ';' ;
modifier_clauses:
modifier_clause
| modifier_clauses modifier_clause ;
blank_page_clause:
BLANK { input_set_file (NULL); } size_clause;
input_clause:
input_file_clause ';'
| image_clause ';'
| images_clause ';'
| modifier_clauses ';'
| blank_page_clause ';'
| input_clause_list ;
input_clauses:
input_clause
| input_clauses input_clause ;
input_clause_list:
'{' { input_push_context (); }
input_clauses '}' { input_pop_context (); } ;
input_statement:
INPUT '{' input_clauses '}' ;
pdf_file_attribute:
AUTHOR STRING { output_set_author ($2); }
| CREATOR STRING { output_set_creator ($2); }
| TITLE STRING { output_set_title ($2); }
| SUBJECT STRING { output_set_subject ($2); }
| KEYWORDS STRING { output_set_keywords ($2); } ;
pdf_file_attribute_list:
pdf_file_attribute
| pdf_file_attribute_list pdf_file_attribute ;
pdf_file_attributes:
/* empty */
| pdf_file_attribute_list ;
output_file_clause:
FILE_KEYWORD STRING { output_set_file ($2); }
pdf_file_attributes ;
label_clause:
LABEL { page_label_t label = { NULL, '\0' }; output_set_page_label (label); }
| LABEL STRING { page_label_t label = { $2, '\0' }; output_set_page_label (label); }
| LABEL CHARACTER { page_label_t label = { NULL, $2 }; output_set_page_label (label); }
| LABEL STRING ',' CHARACTER { page_label_t label = { $2, $4 }; output_set_page_label (label); } ;
overlay_clause_list:
/* empty */
| '{' overlay_clauses '}' ;
overlay_clauses:
overlay_clause ';'
| overlay_clauses overlay_clause ';' ;
overlay_clause:
OVERLAY length ',' length { overlay_t overlay = { .imagemask = false,
.position.x = $2,
.position.y = $4 }; output_overlay (overlay); }
| IMAGEMASK rgb { output_imagemask($2); } ;
page_ranges:
range { output_pages ($1); }
| page_ranges ',' range { output_pages ($3); } ;
page_clause:
PAGE INTEGER { range_t range = { $2, $2 }; output_pages (range); } overlay_clause_list ;
pages_clause:
PAGES page_ranges ;
bookmark_name:
STRING { output_set_bookmark ($1); } ;
bookmark_name_list:
bookmark_name
| bookmark_name_list ',' bookmark_name ;
bookmark_clause:
BOOKMARK { output_push_context (); bookmark_level++; }
bookmark_name_list
output_clause_list { bookmark_level--; output_pop_context (); } ;
rgb:
'(' INTEGER INTEGER INTEGER ')' { $$.red = $2; $$.green = $3; $$.blue = $4; } ;
colormap_clause:
COLORMAP rgb ',' rgb { output_set_colormap ($2, $4); } ;
output_clause:
output_file_clause ';'
| colormap_clause ';'
| label_clause ';'
| page_clause ';'
| pages_clause ';'
| bookmark_clause ';'
| output_clause_list ;
output_clauses:
output_clause
| output_clauses output_clause ;
output_clause_list:
'{' { output_push_context (); }
output_clauses '}' { output_pop_context (); } ;
output_statement:
OUTPUT '{' output_clauses '}' ;