File: po-gram.y

package info (click to toggle)
potool 0.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 136 kB
  • ctags: 60
  • sloc: ansic: 327; yacc: 160; sh: 95; makefile: 74
file content (192 lines) | stat: -rw-r--r-- 3,376 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
%{
/*
 * potool is a program aiding editing of po files
 * Copyright (C) 1999-2000 Zbigniew Chyla
 *
 * 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 "i18n.h"

gint polineno;
gint polex(void);
void poerror(char *s);

static GList *po_entry_list = NULL;
static gchar *concat_strings(GList *slist);
static void free_strings(GList *slist);


%}

%union {
	gint intval;
	gchar *strval;
	GList *glistval;
	po_entry *poval;
	po_comments commentsval;
}

%token MSGID MSGSTR INVALID
%token <strval> STRING
%token <strval> COMMENT_STD
%token <strval> COMMENT_POS
%token <strval> COMMENT_SPECIAL
%token <strval> COMMENT_RESERVED

%type <glistval> string_list
%type <commentsval> comments
%type <poval> msg

%start translation_unit
%%

translation_unit
	: msg_list comments
	;

msg_list
	: msg
	{
		po_entry_list = g_list_append(NULL, $1);
	}
	| msg_list msg
	{
		po_entry_list = g_list_prepend(po_entry_list, $2);
	}
	;

comments
	: /* empty */
	{
		$$.comments_std = NULL;
		$$.comments_pos = NULL;
		$$.comments_spec = NULL;
		$$.comments_res = NULL;
	}
	| comments COMMENT_STD
	{
		$$ = $1;
		$$.comments_std =  g_list_append($$.comments_std, $2);
	}
	| comments COMMENT_POS
	{
		$$ = $1;
		$$.comments_pos =  g_list_append($$.comments_pos, $2);
	}
	| comments COMMENT_SPECIAL
	{
		$$ = $1;
		$$.comments_spec =  g_list_append($$.comments_spec, $2);
	}
	| comments COMMENT_RESERVED
	{
		$$ = $1;
		$$.comments_res =  g_list_append($$.comments_res, $2);
	}
	;

msg
	: comments MSGID string_list MSGSTR string_list
	{
		GList *l;

		$$ = g_new(po_entry, 1);
		$$->id = concat_strings($3);
		$$->str = concat_strings($5);
		$$->comments_std = $1.comments_std;
		$$->comments_pos = $1.comments_pos;
		$$->comments_res = $1.comments_res;
		$$->comments_spec = $1.comments_spec;
		$$->is_fuzzy = $$->is_c_format = 0;
		for ( l = $$->comments_spec; l != NULL; l = g_list_next(l) ) {
			gchar *s = (gchar *) l->data;
			if ( strstr(s, " fuzzy") != NULL ) {
				$$->is_fuzzy = 1;
			}
			if ( strstr(s, " c-format") != NULL ) {
				$$->is_c_format = 1;
			}
		}
		free_strings($3);
		free_strings($5);
	}
	;

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

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

%%
#include <stdio.h>

extern char potext[];
extern int column;

void po_init_parser(void)
{
}

static gchar *concat_strings(GList *slist)
{
	gint total_len;
	gchar *s, *p;
	GList *l;

	for ( total_len = 0, l = slist; l != NULL; l = g_list_next(l) ) {
		total_len += strlen((gchar *) l->data);
	}
	s = g_malloc(total_len + 1);
	for ( p = s, l = slist; l != NULL; l = g_list_next(l) ) {
		int len = strlen((gchar *) l->data);

		if ( len > 0 ) {
			(void) g_memmove(p, (gchar *) l->data, len);
			p += len;
		}
	}
	s[total_len] = '\0';
	return s;
}

static void free_strings(GList *slist)
{
	GList *l;

	for ( l = slist; l != NULL; l = g_list_next(l) ) {
		g_free((gchar *) l->data);
	}
	g_list_free(slist);
}

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

GList *po_read(gchar *fn)
{
	po_scan_open_file(fn);
	po_init_parser();
	poparse();
	po_scan_close_file();
	return g_list_reverse(po_entry_list);
}