File: po-gram.y

package info (click to toggle)
potool 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 248 kB
  • sloc: ansic: 718; yacc: 452; sh: 290; perl: 51; makefile: 49
file content (510 lines) | stat: -rw-r--r-- 9,479 bytes parent folder | download
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
%{
/*
 * potool is a program aiding editing of po files
 * Copyright (C) 1999-2002 Zbigniew Chyla
 * Copyright (C) 2000-2019 Marcin Owsiany <porridge@debian.org>
 *
 * see LICENSE for licensing info
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "po-gram.h"
#include "common.h"
#include "i18n.h"

extern int polineno;
int polex (void);
void poerror (const char *s);

static GSList *entries = NULL, *obsolete_entries = NULL;
static StringBlock *concat_strings (GSList *slist);

%}

%define parse.error verbose

%union {
	int int_val;
	char *str_val;
	StringBlock *stringblock_val;
	GSList *gslist_val;
	PoEntry *entry_val;
	PoComments comments_val;
	PoPrevious previous_val;
	MsgStrX *msgstrx_val;
}

%token MSGCTXT PREVIOUS_MSGCTXT OBSOLETE_MSGCTXT OBSOLETE_PREVIOUS_MSGCTXT
%token MSGID MSGID_PLURAL PREVIOUS_MSGID PREVIOUS_MSGID_PLURAL
%token OBSOLETE_MSGID OBSOLETE_MSGID_PLURAL OBSOLETE_PREVIOUS_MSGID OBSOLETE_PREVIOUS_MSGID_PLURAL
%token MSGSTR OBSOLETE_MSGSTR INVALID
%token <str_val> MSGSTR_X
%token <str_val> STRING
%token <str_val> OBSOLETE_STRING
%token <str_val> COMMENT_STD
%token <str_val> COMMENT_POS
%token <str_val> COMMENT_SPECIAL
%token <str_val> COMMENT_RESERVED

%type <stringblock_val> msgctx
%type <stringblock_val> obsolete_msgctx
%type <stringblock_val> previous_ctx
%type <stringblock_val> obsolete_previous_ctx
%type <stringblock_val> previous_id
%type <stringblock_val> obsolete_previous_id
%type <stringblock_val> previous_id_plural
%type <stringblock_val> obsolete_previous_id_plural
%type <gslist_val> string_list
%type <gslist_val> obsolete_string_list
%type <gslist_val> really_obsolete_string_list
%type <gslist_val> msg_list
%type <gslist_val> obsolete_msg_list
%type <gslist_val> msgstr_x_list
%type <gslist_val> obsolete_msgstr_x_list
%type <msgstrx_val> msgstr_x
%type <msgstrx_val> obsolete_msgstr_x
%type <comments_val> comments
%type <previous_val> previous
%type <previous_val> obsolete_previous
%type <entry_val> msg
%type <entry_val> obsolete_msg

%start translation_unit
%%

translation_unit
	: msg_list
	{
		entries = g_slist_reverse ($1);
		obsolete_entries = NULL;
	}
	| msg_list obsolete_msg_list
	{
		entries = g_slist_reverse ($1);
		obsolete_entries = g_slist_reverse ($2);
	}
	;

msg_list
	: msg
	{
		$$ = g_slist_append (NULL, $1);
	}
	| msg_list msg
	{
		$$ = g_slist_prepend ($1, $2);
	}
	;

obsolete_msg_list
	: obsolete_msg
	{
		$$ = g_slist_append (NULL, $1);
	}
	| obsolete_msg_list obsolete_msg
	{
		$$ = g_slist_prepend ($1, $2);
	}
	;

comments
	: /* empty */
	{
		$$.std = NULL;
		$$.pos = NULL;
		$$.spec = NULL;
		$$.res = NULL;
	}
	| comments COMMENT_STD
	{
		$$ = $1;
		$$.std =  g_slist_append ($$.std, $2);
	}
	| comments COMMENT_POS
	{
		$$ = $1;
		$$.pos =  g_slist_append ($$.pos, $2);
	}
	| comments COMMENT_SPECIAL
	{
		$$ = $1;
		$$.spec =  g_slist_append ($$.spec, $2);
	}
	| comments COMMENT_RESERVED
	{
		$$ = $1;
		$$.res =  g_slist_append ($$.res, $2);
	}
	;

previous_ctx
	: /* empty */
	{
		$$ = NULL;
	}
	| PREVIOUS_MSGCTXT string_list
	{
		$$ = concat_strings($2);
		g_slist_free_custom ($2, g_free);
	}
	;

previous_id
	: /* empty */
	{
		$$ = NULL;
	}
	| PREVIOUS_MSGID string_list
	{
		$$ = concat_strings($2);
		g_slist_free_custom ($2, g_free);
	}
	;

previous_id_plural
	: /* empty */
	{
		$$ = NULL;
	}
	| PREVIOUS_MSGID_PLURAL string_list
	{
		$$ = concat_strings($2);
		g_slist_free_custom ($2, g_free);
	}
	;

previous
	: previous_ctx previous_id previous_id_plural
	{
		$$.ctx = $1;
		$$.id = $2;
		$$.id_plural = $3;
	}
	;

obsolete_previous_ctx
	: /* empty */
	{
		$$ = NULL;
	}
	| OBSOLETE_PREVIOUS_MSGCTXT string_list
	{
		$$ = concat_strings($2);
		g_slist_free_custom ($2, g_free);
	}
	;

obsolete_previous_id
	: /* empty */
	{
		$$ = NULL;
	}
	| OBSOLETE_PREVIOUS_MSGID string_list
	{
		$$ = concat_strings($2);
		g_slist_free_custom ($2, g_free);
	}
	;

obsolete_previous_id_plural
	: /* empty */
	{
		$$ = NULL;
	}
	| OBSOLETE_PREVIOUS_MSGID_PLURAL string_list
	{
		$$ = concat_strings($2);
		g_slist_free_custom ($2, g_free);
	}
	;

obsolete_previous
	: obsolete_previous_ctx obsolete_previous_id obsolete_previous_id_plural
	{
		$$.ctx = $1;
		$$.id = $2;
		$$.id_plural = $3;
	}
	;

msgstr_x
	: MSGSTR MSGSTR_X string_list
	{
		$$ = g_new(MsgStrX, 1);
		$$->n = atoi($2);
		$$->str = concat_strings ($3);
		g_free ($2);
		g_slist_free_custom ($3, g_free);
	}
	;

obsolete_msgstr_x
	: OBSOLETE_MSGSTR MSGSTR_X obsolete_string_list
	{
		$$ = g_new(MsgStrX, 1);
		$$->n = atoi($2);
		$$->str = concat_strings ($3);
		g_free ($2);
		g_slist_free_custom ($3, g_free);
	}
	;

msgstr_x_list
	: msgstr_x
	{
		$$ = g_slist_append (NULL, $1);
	}
	| msgstr_x_list msgstr_x
	{
		$$ = g_slist_append ($1, $2);
	}
	;

obsolete_msgstr_x_list
	: obsolete_msgstr_x
	{
		$$ = g_slist_append (NULL, $1);
	}
	| obsolete_msgstr_x_list obsolete_msgstr_x
	{
		$$ = g_slist_append ($1, $2);
	}
	;

msgctx
	: /* empty */
	{
		$$ = NULL;
	}
	| MSGCTXT string_list
	{
		$$ = concat_strings ($2);
		g_slist_free_custom ($2, g_free);
	}
	;

obsolete_msgctx
	: /* empty */
	{
		$$ = NULL;
	}
	| OBSOLETE_MSGCTXT obsolete_string_list
	{
		$$ = concat_strings ($2);
		g_slist_free_custom ($2, g_free);
	}
	;

msg
	: comments previous msgctx MSGID string_list MSGSTR string_list
	{
		GSList *l;

		$$ = g_new (PoEntry, 1);
		$$->ctx = $3;
		$$->id = concat_strings ($5);
		$$->id_plural = NULL;
		$$->str = concat_strings ($7);
		$$->msgstrxs = NULL;
		$$->comments = $1;
		$$->previous = $2;
		$$->is_fuzzy = $$->is_c_format = 0;
		for (l = $$->comments.spec; l != NULL; l = l->next) {
			char *s = l->data;

			if (strstr (s, " fuzzy") != NULL) {
				$$->is_fuzzy = 1;
			}
			if (strstr (s, " c-format") != NULL) {
				$$->is_c_format = 1;
			}
		}
		g_slist_free_custom ($5, g_free);
		g_slist_free_custom ($7, g_free);
	}
	| comments previous msgctx MSGID string_list MSGID_PLURAL string_list msgstr_x_list
	{
		GSList *l;

		$$ = g_new (PoEntry, 1);
		$$->ctx = $3;
		$$->id = concat_strings ($5);
		$$->id_plural = concat_strings ($7);
		$$->str = NULL;
		$$->msgstrxs = $8;
		$$->comments = $1;
		$$->previous = $2;
		$$->is_fuzzy = $$->is_c_format = 0;
		for (l = $$->comments.spec; l != NULL; l = l->next) {
			char *s = l->data;

			if (strstr (s, " fuzzy") != NULL) {
				$$->is_fuzzy = 1;
			}
			if (strstr (s, " c-format") != NULL) {
				$$->is_c_format = 1;
			}
		}
		g_slist_free_custom ($5, g_free);
		g_slist_free_custom ($7, g_free);
	}
	;

obsolete_msg
	: comments obsolete_previous obsolete_msgctx OBSOLETE_MSGID obsolete_string_list OBSOLETE_MSGSTR obsolete_string_list
	{
		GSList *l;

		$$ = g_new (PoEntry, 1);
		$$->ctx = $3;
		$$->id = concat_strings ($5);
		$$->id_plural = NULL;
		$$->str = concat_strings ($7);
		$$->msgstrxs = NULL;
		$$->comments = $1;
		$$->previous = $2;
		$$->is_fuzzy = $$->is_c_format = 0;
		for (l = $$->comments.spec; l != NULL; l = l->next) {
			char *s = l->data;

			if (strstr (s, " fuzzy") != NULL) {
				$$->is_fuzzy = 1;
			}
			if (strstr (s, " c-format") != NULL) {
				$$->is_c_format = 1;
			}
		}
		g_slist_free_custom ($5, g_free);
		g_slist_free_custom ($7, g_free);
	}
	| comments obsolete_previous obsolete_msgctx OBSOLETE_MSGID obsolete_string_list OBSOLETE_MSGID_PLURAL obsolete_string_list obsolete_msgstr_x_list
	{
		GSList *l;

		$$ = g_new (PoEntry, 1);
		$$->ctx = $3;
		$$->id = concat_strings ($5);
		$$->id_plural = concat_strings ($7);
		$$->str = NULL;
		$$->msgstrxs = $8;
		$$->comments = $1;
		$$->previous = $2;
		$$->is_fuzzy = $$->is_c_format = 0;
		for (l = $$->comments.spec; l != NULL; l = l->next) {
			char *s = l->data;

			if (strstr (s, " fuzzy") != NULL) {
				$$->is_fuzzy = 1;
			}
			if (strstr (s, " c-format") != NULL) {
				$$->is_c_format = 1;
			}
		}
		g_slist_free_custom ($5, g_free);
		g_slist_free_custom ($7, g_free);
	}
	;

string_list
	: STRING
	{
		$$ = g_slist_append (NULL, $1);
	}
	| string_list STRING
	{
		$$ = g_slist_append ($1, $2);
	}
	;

obsolete_string_list
	: STRING
	{
		$$ = g_slist_append (NULL, $1);
	}
	| STRING really_obsolete_string_list
	{
		$$ = g_slist_prepend ($2, $1);
	}
	;

really_obsolete_string_list
	: OBSOLETE_STRING
	{
		$$ = g_slist_append (NULL, $1);
	}
	| really_obsolete_string_list OBSOLETE_STRING
	{
		$$ = g_slist_append ($1, $2);
	}
	;

/* ---------- ---------- */

%%
#include <stdio.h>

extern char potext[];
extern int column;

void po_init_parser (void)
{
}

static StringBlock*
concat_strings (GSList *slist)
{
	GSList *l;
	int total_len = 0, i = 0;
	char *p;
	StringBlock *ret = g_new(StringBlock, 1);
	ret->num_lines = 0;

	for (l = slist; l != NULL; l = l->next) {
		total_len += strlen (l->data);
		ret->num_lines++;
	}
	ret->str = g_malloc (total_len + 1);
	ret->line_lengths = g_malloc (sizeof(int) * ret->num_lines);
	p = ret->str;
	for (l = slist; l != NULL; l = l->next) {
		char *s = l->data;
		int len = strlen (s);
		if (len > 0) {
			memmove (p, s, len);
			p += len;
		}
		ret->line_lengths[i++] = len;
	}
	ret->str[total_len] = '\0';
	return ret;
}

void
poerror (const char *s)
{
	fflush (stdout);
	po_error (_("Parse error at line %d: %s\n"), polineno, s);
}

PoFile *
po_read (char *fn)
{
	PoFile *pof;

	po_scan_open_file (fn);
	po_init_parser ();
	poparse ();
	po_scan_close_file ();

	pof = g_new (PoFile, 1);
	pof->entries = entries;
	pof->obsolete_entries = obsolete_entries;

	return pof;
}