1 /* xgettext Smalltalk backend.
2 Copyright (C) 2002-2003, 2005-2009, 2011, 2015 Free Software
5 This file was written by Bruno Haible <haible@clisp.cons.org>, 2002.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "x-smalltalk.h"
37 #define _(s) gettext(s)
39 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
42 /* The relevant parts of the Smalltalk syntax are:
44 stringliteral ::= string | stringconst | symconst
45 stringconst ::= "#"string
46 string ::= "'"[char]*"'"
47 symconst ::= "#"symbol
48 symbol ::= id | binsel | keysel[keysel]*
50 id ::= letter[letter|digit]*
51 letter ::= "A".."Z" | "a".."z"
53 binsel ::= selchar[selchar]
54 selchar ::= "+" | "-" | "*" | "/" | "~" | "|" | "," | "<" | ">"
55 | "=" | "&" | "@" | "?" | "%" | "\"
57 Strings can contain any characters; to include the string delimiter itself,
58 it must be duplicated.
60 Character constants are written "$"char
62 Comments are enclosed within double quotes.
64 In well-formed expressions, {} and [] and () are balanced.
68 /* ======================== Reading of characters. ======================== */
71 /* Real filename, used in error messages about the input file. */
72 static const char *real_file_name;
74 /* Logical filename and line number, used to label the extracted messages. */
75 static char *logical_file_name;
76 static int line_number;
78 /* The input file stream. */
82 /* 1. line_number handling. */
92 error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
103 /* Supports only one pushback character. */
105 phase1_ungetc (int c)
117 /* Accumulating comments. */
120 static size_t bufmax;
121 static size_t buflen;
132 if (buflen >= bufmax)
134 bufmax = 2 * bufmax + 10;
135 buffer = xrealloc (buffer, bufmax);
137 buffer[buflen++] = c;
144 && (buffer[buflen - 1] == ' ' || buffer[buflen - 1] == '\t'))
146 if (buflen >= bufmax)
148 bufmax = 2 * bufmax + 10;
149 buffer = xrealloc (buffer, bufmax);
151 buffer[buflen] = '\0';
152 savable_comment_add (buffer);
156 /* These are for tracking whether comments count as immediately before
158 static int last_comment_line;
159 static int last_non_comment_line;
162 /* ========================== Reading of tokens. ========================== */
168 token_type_uniq, /* # */
169 token_type_symbol, /* symbol */
170 token_type_string_literal, /* string, stringconst, symbolconst */
171 token_type_other /* misc. operator */
173 typedef enum token_type_ty token_type_ty;
175 typedef struct token_ty token_ty;
179 char *string; /* for token_type_string_literal, token_type_symbol */
184 /* 2. Combine characters into tokens. Discard comments and whitespace. */
186 static token_ty phase2_pushback[1];
187 static int phase2_pushback_length;
190 phase2_get (token_ty *tp)
197 if (phase2_pushback_length)
199 *tp = phase2_pushback[--phase2_pushback_length];
207 tp->line_number = line_number;
212 tp->type = token_type_eof;
221 lineno = line_number;
225 if (c == '"' || c == EOF)
234 /* We skip all leading white space, but not EOLs. */
235 if (!(buflen == 0 && (c == ' ' || c == '\t')))
240 last_comment_line = lineno;
245 if (last_non_comment_line > last_comment_line)
246 savable_comment_reset ();
251 /* Ignore whitespace. */
255 last_non_comment_line = tp->line_number;
260 /* String literal. */
276 if (bufpos >= bufmax)
278 bufmax = 2 * bufmax + 10;
279 buffer = xrealloc (buffer, bufmax);
281 buffer[bufpos++] = c;
283 if (bufpos >= bufmax)
285 bufmax = 2 * bufmax + 10;
286 buffer = xrealloc (buffer, bufmax);
289 tp->type = token_type_string_literal;
290 tp->string = xstrdup (buffer);
310 int c2 = phase1_getc ();
327 name = XNMALLOC (3, char);
331 tp->type = token_type_symbol;
338 name = XNMALLOC (2, char);
341 tp->type = token_type_symbol;
346 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
347 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
348 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
349 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
351 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
352 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
353 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
354 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
356 /* Recognize id or id":"[id":"]* or id":"[id":"]*id. */
360 if (bufpos >= bufmax)
362 bufmax = 2 * bufmax + 10;
363 buffer = xrealloc (buffer, bufmax);
365 buffer[bufpos++] = c;
369 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
370 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
371 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
372 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
374 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
375 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
376 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
377 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
379 case '0': case '1': case '2': case '3': case '4':
380 case '5': case '6': case '7': case '8': case '9':
383 if (bufpos >= bufmax)
385 bufmax = 2 * bufmax + 10;
386 buffer = xrealloc (buffer, bufmax);
388 buffer[bufpos++] = c;
392 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
393 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
394 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
395 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
397 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
398 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
399 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
400 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
414 if (bufpos >= bufmax)
416 bufmax = 2 * bufmax + 10;
417 buffer = xrealloc (buffer, bufmax);
419 buffer[bufpos] = '\0';
420 tp->string = xstrdup (buffer);
421 tp->type = token_type_symbol;
425 /* Uniquification operator. */
426 tp->type = token_type_uniq;
431 tp->type = token_type_other;
435 tp->type = token_type_other;
441 /* Supports only one pushback token. */
443 phase2_unget (token_ty *tp)
445 if (tp->type != token_type_eof)
447 if (phase2_pushback_length == SIZEOF (phase2_pushback))
449 phase2_pushback[phase2_pushback_length++] = *tp;
454 /* 3. Combine "# string_literal" and "# symbol" to a single token. */
457 x_smalltalk_lex (token_ty *tp)
460 if (tp->type == token_type_uniq)
464 phase2_get (&token2);
465 if (token2.type == token_type_symbol
466 || token2.type == token_type_string_literal)
468 tp->type = token_type_string_literal;
469 tp->string = token2.string;
472 phase2_unget (&token2);
477 /* ========================= Extracting strings. ========================== */
479 /* The file is broken into tokens. Scan the token stream, looking for the
483 NLS at: <string> plural: <string>
484 where <string> is one of
491 extract_smalltalk (FILE *f,
492 const char *real_filename, const char *logical_filename,
493 flag_context_list_table_ty *flag_table,
494 msgdomain_list_ty *mdlp)
496 message_list_ty *mlp = mdlp->item[0]->messages;
499 real_file_name = real_filename;
500 logical_file_name = xstrdup (logical_filename);
503 last_comment_line = -1;
504 last_non_comment_line = -1;
506 /* Eat tokens until eof is seen. */
508 /* 0 when no "NLS" has been seen.
512 4 after "NLS at: <string>".
513 5 after "NLS at: <string> plural:". */
515 /* Remember the message containing the msgid, for msgid_plural.
516 Non-NULL in states 4, 5. */
517 message_ty *plural_mp = NULL;
519 /* Start state is 0. */
526 x_smalltalk_lex (&token);
530 case token_type_symbol:
531 state = (strcmp (token.string, "NLS") == 0 ? 1 :
532 strcmp (token.string, "?") == 0 && state == 1 ? 2 :
533 strcmp (token.string, "at:") == 0 && state == 1 ? 3 :
534 strcmp (token.string, "plural:") == 0 && state == 4 ? 5 :
539 case token_type_string_literal:
543 pos.file_name = logical_file_name;
544 pos.line_number = token.line_number;
545 remember_a_message (mlp, NULL, token.string, null_context,
546 &pos, NULL, savable_comment);
553 pos.file_name = logical_file_name;
554 pos.line_number = token.line_number;
555 plural_mp = remember_a_message (mlp, NULL, token.string,
557 NULL, savable_comment);
564 pos.file_name = logical_file_name;
565 pos.line_number = token.line_number;
566 if (plural_mp != NULL)
567 remember_a_message_plural (plural_mp, token.string,
577 case token_type_uniq:
578 case token_type_other:
589 if (token.type == token_type_eof)
596 real_file_name = NULL;
597 logical_file_name = NULL;