summaryrefslogtreecommitdiffstats
path: root/src/x-java.l
blob: ba0ae27300cd039b6f7b34e268fd56a2a4bea7f1 (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
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
/* xgettext Java backend.					-*- C -*-
   Copyright (C) 2001 Free Software Foundation, Inc.
   Written by Tommy Johansson <tommy.johansson@kanalen.org>, 2001.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   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-1307, USA.  */

%{
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "message.h"
#include "x-java.h"
#include "xgettext.h"
#include "system.h"

typedef enum
{
  JAVA_WORD,
  JAVA_STRING,
  JAVA_OPERATOR,
  JAVA_FLOW,
  JAVA_COMMENT,
} TOKEN_TYPE;

typedef struct
{
  char *word;
  char *string;
  char *operator;
  char *flow;
  char *comment;

  int line_no;
} PARSER_GLOBAL;

static PARSER_GLOBAL pg;
static PARSER_GLOBAL *parser_global = &pg;

typedef enum
{
  STATE_NONE,
  STATE_STRING,
  STATE_WORD,
  STATE_APPEND,
  STATE_INVOCATION,
  STATE_KEYWORD,
} PARSER_STATE;

typedef struct
{
  char *data;
  int len;
  int maxlen;
} char_buf;


/* Prototypes for local functions.  Needed to ensure compiler checking of
   function argument counts despite of K&R C function definition syntax.  */
static char_buf *create_char_buf PARAMS ((void));
static void append_char_buf PARAMS ((char_buf *b, int c));
static char *get_string PARAMS ((char_buf *b));
static void destroy_charbuf PARAMS ((char_buf *b));
static void update_line_no PARAMS ((int c));
static char *append_strings PARAMS ((char *a, char *b));
static inline bool isplus PARAMS ((char *s));
static inline bool isdot PARAMS ((char *s));
static char *translate_esc PARAMS ((char *s));
static bool do_compare PARAMS ((const char *s1, const char *s2));
static bool is_keyword PARAMS ((const char *s));
static void free_global PARAMS ((void));


#define INITIAL_CHARBUF_SIZE 500
#define CHARBUF_GROWTH 100
static char_buf *
create_char_buf ()
{
  char_buf *b = (char_buf *) xmalloc (sizeof (char_buf));
  b->data = (char *) xmalloc (INITIAL_CHARBUF_SIZE);
  b->len = 0;
  b->maxlen = INITIAL_CHARBUF_SIZE;
  return b;
}

static void
append_char_buf (b, c)
     char_buf *b;
     int c;
{
  if (b->len >= b->maxlen - 1)
    {
      b->data = (char *) xrealloc (b->data, b->maxlen + CHARBUF_GROWTH);
      b->maxlen += CHARBUF_GROWTH;
    }
  b->data[b->len++] = c;
  b->data[b->len] = '\0';
}

static char *
get_string (b)
     char_buf *b;
{
  return xstrdup (b->data);
}

static void
destroy_charbuf (b)
     char_buf *b;
{
  free (b->data);
  free (b);
}

static void
update_line_no (c)
     int c;
{
  if (c == '\n')
    parser_global->line_no++;
}

%}

%option noyywrap

NUM [0-9]
ID [a-zA-Z_][a-zA-Z0-9_]*

%%

"/*" {
  int c;
  int last;
  char *str;

  char_buf *charbuf = create_char_buf ();
  while (1)
    {
      c = input ();
      last = input ();
      update_line_no (c);
      if ((c == '*' && last == '/') || c == EOF)
	break;
      unput (last);
      append_char_buf (charbuf, c);
    }
  str = get_string (charbuf);
  destroy_charbuf (charbuf);
  parser_global->comment = str;
  return JAVA_COMMENT;
}

{NUM}| {NUM}+"."{NUM}*
\" {
  int c;
  char *str;
  char_buf *charbuf = create_char_buf ();
  while ((c = input ()) != '"')
    {
      update_line_no (c);
      append_char_buf (charbuf, c);
    }
  str = get_string (charbuf);
  destroy_charbuf (charbuf);
  parser_global->string = str;
  return JAVA_STRING;
}

{ID} {
  parser_global->word = yytext;
  return JAVA_WORD;
}

"."|"("|")"|";"|"{"|"}"|"["|"]"|","|":"|"\\"|"?"|"\'" {
  parser_global->flow = yytext;
  return JAVA_FLOW;
}

"="|"<"|">"|"+"|"-"|"*"|"/"|"!"|"&"|"|"|"%"|"^"|"~" {
  parser_global->operator = yytext;
  return JAVA_OPERATOR;
}

"#"|"@"|"\r"|"`" /* ignore whitespace */

"//"[^\n]* {
  parser_global->comment = xstrdup (yytext);
  return JAVA_COMMENT;
}
"\n"|"\r"|"\r\n" parser_global->line_no++;
[ \t]+
.
<<EOF>> return -1;
%%

static char *
append_strings (a, b)
     char *a;
     char *b;
{
  int total_size = strlen (a) + strlen (b) + 1;
  char *new_string = (char *) xmalloc (total_size);
  strcpy (new_string, a);
  strcat (new_string, b);
  return new_string;
}

static inline bool
isplus (s)
     char *s;
{
  return *s == '+';
}

static inline bool
isdot (s)
     char *s;
{
  return *s == '.';
}


static char *
translate_esc (s)
     char *s;
{
  char *n = (char *) xmalloc (strlen (s) + 1);
  int i;
  int j = 0;

  for (i = 0; i < strlen (s); i++)
    switch (s[i])
      {
      case '\\':
	if (s[i + 1] == 'n')
	  {
	    n[j++] = '\n';
	    i++;
	  }
	break;
      default:
	n[j++] = s[i];
      }
  n[j] = '\0';
  return n;
}

static string_list_ty *java_keywords = NULL;

/**
 * Try to match a string against the keyword. If substring_match is
 * true substring match is used.
 */
static bool
do_compare (s1, s2)
     const char *s1;
     const char *s2;
{
  if (substring_match)
    return strstr (s1, s2) != NULL;
  else
    return strcmp (s1, s2) == 0;
}

/**
 * Check if a string is a keyword or not.
 */
static bool
is_keyword (s)
     const char *s;
{
  int i;

  for (i = 0; i < java_keywords->nitems; i++)
    if (do_compare (java_keywords->item[i], s))
      return true;
  return false;
}

/**
 * Add a keyword to the list of possible keywords.
 */
void
x_java_keyword (keyword)
     const char *keyword;
{
  if (java_keywords == NULL)
    java_keywords = string_list_alloc ();

  string_list_append (java_keywords, keyword);
}


/**
 * Free any memory allocated by the tokenizer.
 */
static void
free_global ()
{
  /**
   * free memory used by strings and comments as they are strdup'ed
   * by the lexer.
   */
  if (parser_global->string != NULL)
    {
      free (parser_global->string);
      parser_global->string = NULL;
    }
  if (parser_global->comment != NULL)
    {
      free (parser_global->comment);
      parser_global->comment = NULL;
    }
}


/**
 * Main java keyword extract function.
 */
void
extract_java (f, real_filename, logical_filename, mdlp)
     FILE *f;
     const char *real_filename;
     const char *logical_filename;
     msgdomain_list_ty *mdlp;
{
  char *logical_file_name = xstrdup (logical_filename);
  int token;
  PARSER_STATE state = STATE_NONE;
  PARSER_STATE last_state = STATE_NONE;
  char *str;
  char *key;
  message_list_ty *mlp = mdlp->item[0]->messages;

  if (java_keywords == NULL)
    {
      /* ops, no standard keywords */
      x_java_keyword ("gettext");	/* GettextResource.gettext */
      x_java_keyword ("ngettext");	/* GettextResource.ngettext */
      x_java_keyword ("getString");	/* ResourceBundle.getString */
    }

  memset (parser_global, 0, sizeof (*parser_global));
  /* first line is 1 */
  parser_global->line_no = 1;

  yyin = f;
  do
    {
      token = yylex ();
      switch (token)
	{

	case JAVA_WORD:
	  if (state == STATE_INVOCATION)
	    {
	      char *k2;
	      k2 = append_strings (key, ".");
	      free (key);
	      key = append_strings (k2, parser_global->word);
	      state = STATE_NONE;
	    }
	  else
	    {
	      state = STATE_WORD;
	      key = xstrdup (parser_global->word);
	    }
	  /* For java we try to match both things like object.methodCall()
	     and methodCall(). */
	  if (is_keyword (key) || is_keyword (parser_global->word))
	    {
	      free (key);
	      state = STATE_KEYWORD;
	    }
	  break;

	case JAVA_STRING:
	  if (state == STATE_KEYWORD)
	    {
	      last_state = STATE_KEYWORD;
	    }
	  if (state == STATE_APPEND)
	    {
	      char *s2;
	      s2 = append_strings (str, translate_esc (parser_global->string));
	      free (str);
	      str = s2;
	      state = STATE_STRING;
	    }
	  else
	    {
	      state = STATE_STRING;
	      str = translate_esc (parser_global->string);
	    }
	  break;

	case JAVA_OPERATOR:
	  if (state == STATE_STRING && isplus (parser_global->operator))
	    {
	      state = STATE_APPEND;
	    }
	  else
	    {
	      state = STATE_NONE;
	    }
	  break;

	case JAVA_FLOW:
	  /* Did we get something? */
	  if (state == STATE_STRING && last_state == STATE_KEYWORD)
	    {
	      lex_pos_ty pos;
	      pos.file_name = logical_file_name;
	      pos.line_number = parser_global->line_no;
	      state = STATE_NONE;
	      last_state = STATE_NONE;

	      remember_a_message (mlp, str, &pos);
	    }

	  if (state == STATE_WORD && isdot (parser_global->flow))
	    {
	      state = STATE_INVOCATION;
	    }

	  break;

	case JAVA_COMMENT:
	  state = STATE_NONE;
	  xgettext_comment_add (parser_global->comment);
	  break;

	default:
	  state = STATE_NONE;
	}
      free_global ();
    }
  while (token != -1);
}