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
|
/**********************************************************************
** Copyright (C) 2002 Detlev Offenbach <[email protected]>
**
** This is a modified version of lupdate. The original is part of TQt-Linguist.
** The copyright of the original file can be found below.
**
** This version is modified to handle python sources.
**
** The file is provided AS IS with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE.
**
**********************************************************************/
/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
**
** fetchtr.cpp
**
** This file is part of TQt Linguist.
**
** See the file LICENSE included in the distribution for the usage
** and distribution terms.
**
** The file is provided AS IS with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE.
**
**********************************************************************/
#include <tqfile.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqtextstream.h>
#include <ctype.h>
#include <errno.h>
#include <metatranslator.h>
#include <stdio.h>
#include <string.h>
/*#include <tqxml.h>*/
static const char MagicComment[] = "TRANSLATOR ";
/*
The first part of this source file is the python tokenizer. We skip
most of python; the only tokens that interest us are defined here.
*/
enum { Tok_Eof, Tok_class, Tok_return, Tok_tr,
Tok_trUtf8, Tok_translate, Tok_Ident,
Tok_Comment, Tok_Dot, Tok_String,
Tok_LeftParen, Tok_RightParen,
Tok_Comma};
/*
The tokenizer maintains the following global variables. The names
should be self-explanatory.
*/
static TQCString yyFileName;
static int yyCh;
static char yyIdent[128];
static size_t yyIdentLen;
static char yyComment[65536];
static size_t yyCommentLen;
static char yyString[16384];
static size_t yyStringLen;
static int yyParenDepth;
static int yyLineNo;
static int yyCurLineNo;
// the file to read from (if reading from a file)
static FILE *yyInFile;
// the string to read from and current position in the string (otherwise)
static TQString yyInStr;
static int yyInPos;
static int buf;
static int (*getChar)();
static int (*peekChar)();
static int getCharFromFile()
{
int c;
if ( buf < 0 )
c = getc( yyInFile );
else {
c = buf;
buf = -1;
}
if ( c == '\n' )
yyCurLineNo++;
return c;
}
static int peekCharFromFile()
{
int c = getc( yyInFile );
buf = c;
return c;
}
static void startTokenizer( const char *fileName, int (*getCharFunc)(),
int (*peekCharFunc)() )
{
yyInPos = 0;
buf = -1;
getChar = getCharFunc;
peekChar = peekCharFunc;
yyFileName = fileName;
yyCh = getChar();
yyParenDepth = 0;
yyCurLineNo = 1;
}
static int getToken()
{
const char tab[] = "abfnrtv";
const char backTab[] = "\a\b\f\n\r\t\v";
uint n;
yyIdentLen = 0;
yyCommentLen = 0;
yyStringLen = 0;
while ( yyCh != EOF ) {
yyLineNo = yyCurLineNo;
if ( isalpha(yyCh) || yyCh == '_' ) {
do {
if ( yyIdentLen < sizeof(yyIdent) - 1 )
yyIdent[yyIdentLen++] = (char) yyCh;
yyCh = getChar();
} while ( isalnum(yyCh) || yyCh == '_' );
yyIdent[yyIdentLen] = '\0';
switch ( yyIdent[0] ) {
case 'Q':
if ( strcmp(yyIdent + 1, "T_TR_NOOP") == 0 ) {
return Tok_tr;
} else if ( strcmp(yyIdent + 1, "T_TRANSLATE_NOOP") == 0 ) {
return Tok_translate;
}
break;
case 'c':
if ( strcmp(yyIdent + 1, "lass") == 0 )
return Tok_class;
break;
case 'r':
if ( strcmp(yyIdent + 1, "eturn") == 0 )
return Tok_return;
break;
case 't':
if ( strcmp(yyIdent + 1, "r") == 0 )
return Tok_tr;
else if ( strcmp(yyIdent + 1, "rUtf8") == 0 )
return Tok_trUtf8;
else if ( strcmp(yyIdent + 1, "ranslate") == 0 )
return Tok_translate;
case '_':
if ( strcmp(yyIdent + 1, "_tr") == 0 )
return Tok_tr;
else if ( strcmp(yyIdent + 1, "_trUtf8") == 0 )
return Tok_trUtf8;
}
return Tok_Ident;
} else {
switch ( yyCh ) {
case '#':
yyCh = getChar();
do {
yyCh = getChar();
} while ( yyCh != EOF && yyCh != '\n' );
break;
case '"':
case '\'':
int quoteChar;
int trippelQuote, singleQuote;
int in;
quoteChar = yyCh;
trippelQuote = 0;
singleQuote = 1;
in = 0;
yyCh = getChar();
while ( yyCh != EOF ) {
if ( singleQuote && (yyCh == '\n' || (in && yyCh == quoteChar)) )
break;
if ( yyCh == quoteChar ) {
if (peekChar() == quoteChar) {
yyCh = getChar();
if (!trippelQuote) {
trippelQuote = 1;
singleQuote = 0;
in = 1;
yyCh = getChar();
} else {
yyCh = getChar();
if (yyCh == quoteChar) {
trippelQuote = 0;
break;
}
}
} else if (trippelQuote) {
if ( yyStringLen < sizeof(yyString) - 1 )
yyString[yyStringLen++] = (char) yyCh;
yyCh = getChar();
continue;
} else
break;
} else
in = 1;
if ( yyCh == '\\' ) {
yyCh = getChar();
if ( yyCh == 'x' ) {
TQCString hex = "0";
yyCh = getChar();
while ( isxdigit(yyCh) ) {
hex += (char) yyCh;
yyCh = getChar();
}
sscanf( hex, "%x", &n );
if ( yyStringLen < sizeof(yyString) - 1 )
yyString[yyStringLen++] = (char) n;
} else if ( yyCh >= '0' && yyCh < '8' ) {
TQCString oct = "";
do {
oct += (char) yyCh;
yyCh = getChar();
} while ( yyCh >= '0' && yyCh < '8' );
sscanf( oct, "%o", &n );
if ( yyStringLen < sizeof(yyString) - 1 )
yyString[yyStringLen++] = (char) n;
} else {
const char *p = strchr( tab, yyCh );
if ( yyStringLen < sizeof(yyString) - 1 )
yyString[yyStringLen++] = ( p == 0 ) ?
(char) yyCh : backTab[p - tab];
yyCh = getChar();
}
} else {
if ( yyStringLen < sizeof(yyString) - 1 )
yyString[yyStringLen++] = (char) yyCh;
yyCh = getChar();
}
}
yyString[yyStringLen] = '\0';
if ( yyCh != quoteChar ) {
printf("%c\n", yyCh);
tqWarning( "%s:%d: Unterminated string",
(const char *) yyFileName, yyLineNo );
}
if ( yyCh == EOF ) {
return Tok_Eof;
} else {
yyCh = getChar();
return Tok_String;
}
break;
case '(':
yyParenDepth++;
yyCh = getChar();
return Tok_LeftParen;
case ')':
yyParenDepth--;
yyCh = getChar();
return Tok_RightParen;
case ',':
yyCh = getChar();
return Tok_Comma;
case '.':
yyCh = getChar();
return Tok_Dot;
default:
yyCh = getChar();
}
}
}
return Tok_Eof;
}
/*
The second part of this source file is the parser. It accomplishes
a very easy task: It finds all strings inside a tr() or translate()
call, and possibly finds out the context of the call. It supports
three cases:
(1) the context is specified, as in FunnyDialog.tr("Hello") or
translate("FunnyDialog", "Hello");
(2) the call appears within an inlined function;
(3) the call appears within a function defined outside the class definition.
*/
static int yyTok;
static bool match( int t )
{
bool matches = ( yyTok == t );
if ( matches )
yyTok = getToken();
return matches;
}
static bool matchString( TQCString *s )
{
bool matches = ( yyTok == Tok_String );
*s = "";
while ( yyTok == Tok_String ) {
*s += yyString;
yyTok = getToken();
}
return matches;
}
static bool matchEncoding( bool *utf8 )
{
if ( yyTok == Tok_Ident ) {
if ( strcmp(yyIdent, "TQApplication") == 0 ) {
yyTok = getToken();
}
*utf8 = TQString( yyIdent ).endsWith( TQString("UTF8") );
yyTok = getToken();
return TRUE;
} else {
return FALSE;
}
}
static void parse( MetaTranslator *tor, const char *initialContext,
const char *defaultContext )
{
TQMap<TQCString, TQCString> qualifiedContexts;
TQCString context;
TQCString text;
TQCString com;
TQCString functionContext = initialContext;
TQCString prefix;
bool utf8 = FALSE;
yyTok = getToken();
while ( yyTok != Tok_Eof ) {
switch ( yyTok ) {
case Tok_class:
yyTok = getToken();
functionContext = yyIdent;
yyTok = getToken();
break;
case Tok_tr:
case Tok_trUtf8:
utf8 = ( yyTok == Tok_trUtf8 );
yyTok = getToken();
if ( match(Tok_LeftParen) && matchString(&text) ) {
com = "";
if ( match(Tok_RightParen) || (match(Tok_Comma) &&
matchString(&com) && match(Tok_RightParen)) ) {
if ( prefix.isNull() ) {
context = defaultContext;
} else if ( qstrcmp(prefix, "self") == 0 ) {
context = functionContext;
} else {
context = prefix;
}
prefix = (const char *) 0;
if ( qualifiedContexts.contains(context) )
context = qualifiedContexts[context];
tor->insert( MetaTranslatorMessage(context, text, com,
TQString::null, utf8) );
}
}
break;
case Tok_translate:
utf8 = FALSE;
yyTok = getToken();
if ( match(Tok_LeftParen) &&
matchString(&context) &&
match(Tok_Comma) &&
matchString(&text) ) {
com = "";
if ( match(Tok_RightParen) ||
(match(Tok_Comma) &&
matchString(&com) &&
(match(Tok_RightParen) ||
match(Tok_Comma) &&
matchEncoding(&utf8) &&
match(Tok_RightParen))) )
tor->insert( MetaTranslatorMessage(context, text, com,
TQString::null, utf8) );
}
break;
case Tok_Ident:
if ( !prefix.isNull() )
prefix += ".";
prefix += yyIdent;
yyTok = getToken();
if ( yyTok != Tok_Dot )
prefix = (const char *) 0;
break;
case Tok_Comment:
com = yyComment;
com = com.simplifyWhiteSpace();
if ( com.left(sizeof(MagicComment) - 1) == MagicComment ) {
com.remove( 0, sizeof(MagicComment) - 1 );
int k = com.find( ' ' );
if ( k == -1 ) {
context = com;
} else {
context = com.left( k );
com.remove( 0, k + 1 );
tor->insert( MetaTranslatorMessage(context, "", com,
TQString::null, FALSE) );
}
}
yyTok = getToken();
break;
default:
yyTok = getToken();
}
}
if ( yyParenDepth != 0 )
tqWarning( "%s: Unbalanced parentheses in Python code",
(const char *) yyFileName );
}
void fetchtr_py( const char *fileName, MetaTranslator *tor,
const char *defaultContext, bool mustExist )
{
yyInFile = fopen( fileName, "r" );
if ( yyInFile == 0 ) {
if ( mustExist )
tqWarning( "pytqlupdate error: cannot open Python source file '%s': %s",
fileName, strerror(errno) );
return;
}
startTokenizer( fileName, getCharFromFile, peekCharFromFile );
parse( tor, 0, defaultContext );
fclose( yyInFile );
}
|