1 /* xgettext Smalltalk backend.
2 Copyright (C) 2002-2003, 2005-2009, 2011 Free Software Foundation, Inc.
4 This file was written by Bruno Haible <haible@clisp.cons.org>, 2002.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "x-smalltalk.h"
36 #define _(s) gettext(s)
38 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
41 /* The relevant parts of the Smalltalk syntax are:
43 stringliteral ::= string | stringconst | symconst
44 stringconst ::= "#"string
45 string ::= "'"[char]*"'"
46 symconst ::= "#"symbol
47 symbol ::= id | binsel | keysel[keysel]*
49 id ::= letter[letter|digit]*
50 letter ::= "A".."Z" | "a".."z"
52 binsel ::= selchar[selchar]
53 selchar ::= "+" | "-" | "*" | "/" | "~" | "|" | "," | "<" | ">"
54 | "=" | "&" | "@" | "?" | "%" | "\"
56 Strings can contain any characters; to include the string delimiter itself,
57 it must be duplicated.
59 Character constants are written "$"char
61 Comments are enclosed within double quotes.
63 In well-formed expressions, {} and [] and () are balanced.
67 /* ======================== Reading of characters. ======================== */
70 /* Real filename, used in error messages about the input file. */
71 static const char *real_file_name;
73 /* Logical filename and line number, used to label the extracted messages. */
74 static char *logical_file_name;
75 static int line_number;
77 /* The input file stream. */
81 /* 1. line_number handling. */
91 error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
102 /* Supports only one pushback character. */
104 phase1_ungetc (int c)
116 /* Accumulating comments. */
119 static size_t bufmax;
120 static size_t buflen;
131 if (buflen >= bufmax)
133 bufmax = 2 * bufmax + 10;
134 buffer = xrealloc (buffer, bufmax);
136 buffer[buflen++] = c;
143 && (buffer[buflen - 1] == ' ' || buffer[buflen - 1] == '\t'))
145 if (buflen >= bufmax)
147 bufmax = 2 * bufmax + 10;
148 buffer = xrealloc (buffer, bufmax);
150 buffer[buflen] = '\0';
151 savable_comment_add (buffer);
155 /* These are for tracking whether comments count as immediately before
157 static int last_comment_line;
158 static int last_non_comment_line;
161 /* ========================== Reading of tokens. ========================== */
167 token_type_uniq, /* # */
168 token_type_symbol, /* symbol */
169 token_type_string_literal, /* string, stringconst, symbolconst */
170 token_type_other /* misc. operator */
172 typedef enum token_type_ty token_type_ty;
174 typedef struct token_ty token_ty;
178 char *string; /* for token_type_string_literal, token_type_symbol */
183 /* 2. Combine characters into tokens. Discard comments and whitespace. */
185 static token_ty phase2_pushback[1];
186 static int phase2_pushback_length;
189 phase2_get (token_ty *tp)
196 if (phase2_pushback_length)
198 *tp = phase2_pushback[--phase2_pushback_length];
206 tp->line_number = line_number;
211 tp->type = token_type_eof;
220 lineno = line_number;
224 if (c == '"' || c == EOF)
233 /* We skip all leading white space, but not EOLs. */
234 if (!(buflen == 0 && (c == ' ' || c == '\t')))
239 last_comment_line = lineno;
244 if (last_non_comment_line > last_comment_line)
245 savable_comment_reset ();
250 /* Ignore whitespace. */
254 last_non_comment_line = tp->line_number;
259 /* String literal. */
275 if (bufpos >= bufmax)
277 bufmax = 2 * bufmax + 10;
278 buffer = xrealloc (buffer, bufmax);
280 buffer[bufpos++] = c;
282 if (bufpos >= bufmax)
284 bufmax = 2 * bufmax + 10;
285 buffer = xrealloc (buffer, bufmax);
288 tp->type = token_type_string_literal;
289 tp->string = xstrdup (buffer);
309 int c2 = phase1_getc ();
326 name = XNMALLOC (3, char);
330 tp->type = token_type_symbol;
337 name = XNMALLOC (2, char);
340 tp->type = token_type_symbol;
345 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
346 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
347 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
348 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
350 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
351 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
352 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
353 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
355 /* Recognize id or id":"[id":"]* or id":"[id":"]*id. */
359 if (bufpos >= bufmax)
361 bufmax = 2 * bufmax + 10;
362 buffer = xrealloc (buffer, bufmax);
364 buffer[bufpos++] = c;
368 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
369 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
370 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
371 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
373 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
374 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
375 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
376 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
378 case '0': case '1': case '2': case '3': case '4':
379 case '5': case '6': case '7': case '8': case '9':
382 if (bufpos >= bufmax)
384 bufmax = 2 * bufmax + 10;
385 buffer = xrealloc (buffer, bufmax);
387 buffer[bufpos++] = c;
391 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
392 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
393 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
394 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
396 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
397 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
398 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
399 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
413 if (bufpos >= bufmax)
415 bufmax = 2 * bufmax + 10;
416 buffer = xrealloc (buffer, bufmax);
418 buffer[bufpos] = '\0';
419 tp->string = xstrdup (buffer);
420 tp->type = token_type_symbol;
424 /* Uniquification operator. */
425 tp->type = token_type_uniq;
430 tp->type = token_type_other;
434 tp->type = token_type_other;
440 /* Supports only one pushback token. */
442 phase2_unget (token_ty *tp)
444 if (tp->type != token_type_eof)
446 if (phase2_pushback_length == SIZEOF (phase2_pushback))
448 phase2_pushback[phase2_pushback_length++] = *tp;
453 /* 3. Combine "# string_literal" and "# symbol" to a single token. */
456 x_smalltalk_lex (token_ty *tp)
459 if (tp->type == token_type_uniq)
463 phase2_get (&token2);
464 if (token2.type == token_type_symbol
465 || token2.type == token_type_string_literal)
467 tp->type = token_type_string_literal;
468 tp->string = token2.string;
471 phase2_unget (&token2);
476 /* ========================= Extracting strings. ========================== */
478 /* The file is broken into tokens. Scan the token stream, looking for the
482 NLS at: <string> plural: <string>
483 where <string> is one of
490 extract_smalltalk (FILE *f,
491 const char *real_filename, const char *logical_filename,
492 flag_context_list_table_ty *flag_table,
493 msgdomain_list_ty *mdlp)
495 message_list_ty *mlp = mdlp->item[0]->messages;
498 real_file_name = real_filename;
499 logical_file_name = xstrdup (logical_filename);
502 last_comment_line = -1;
503 last_non_comment_line = -1;
505 /* Eat tokens until eof is seen. */
507 /* 0 when no "NLS" has been seen.
511 4 after "NLS at: <string>".
512 5 after "NLS at: <string> plural:". */
514 /* Remember the message containing the msgid, for msgid_plural.
515 Non-NULL in states 4, 5. */
516 message_ty *plural_mp = NULL;
518 /* Start state is 0. */
525 x_smalltalk_lex (&token);
529 case token_type_symbol:
530 state = (strcmp (token.string, "NLS") == 0 ? 1 :
531 strcmp (token.string, "?") == 0 && state == 1 ? 2 :
532 strcmp (token.string, "at:") == 0 && state == 1 ? 3 :
533 strcmp (token.string, "plural:") == 0 && state == 4 ? 5 :
538 case token_type_string_literal:
542 pos.file_name = logical_file_name;
543 pos.line_number = token.line_number;
544 remember_a_message (mlp, NULL, token.string, null_context,
545 &pos, NULL, savable_comment);
552 pos.file_name = logical_file_name;
553 pos.line_number = token.line_number;
554 plural_mp = remember_a_message (mlp, NULL, token.string,
556 NULL, savable_comment);
563 pos.file_name = logical_file_name;
564 pos.line_number = token.line_number;
565 if (plural_mp != NULL)
566 remember_a_message_plural (plural_mp, token.string,
576 case token_type_uniq:
577 case token_type_other:
588 if (token.type == token_type_eof)
595 real_file_name = NULL;
596 logical_file_name = NULL;