2 Copyright (C) 2001, 2002 The Genes Development Team
3 This file is part of the Gedcom parser library.
4 Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
6 The Gedcom parser library is free software; you can redistribute it
7 and/or modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The Gedcom parser library is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the Gedcom parser library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include "gedcom_internal.h"
29 #include "encoding_state.h"
31 #include "gedcom.tabgen.h"
34 static size_t encoding_width;
35 static int current_level = -1;
36 static int level_diff=MAXGEDCLEVEL;
37 static size_t line_len = 0;
38 static int tab_space = 0;
40 static struct conv_buffer* ptr_buffer = NULL;
41 static struct conv_buffer* tag_buffer = NULL;
42 static struct conv_buffer* str_buffer = NULL;
44 #define INITIAL_PTR_BUFFER_LEN MAXGEDCPTRLEN * UTF_FACTOR + 1
45 #define INITIAL_TAG_BUFFER_LEN MAXGEDCTAGLEN * UTF_FACTOR + 1
46 #define INITIAL_STR_BUFFER_LEN MAXGEDCLINELEN * UTF_FACTOR + 1
54 void message_handler(Gedcom_msg_type type, char *msg)
56 fprintf(stderr, "(%d) %s\n", type, msg);
59 int test_loop(ENCODING enc, const char* code)
63 set_encoding_width(enc);
64 gedcom_set_message_handler(message_handler);
65 res = open_conv_to_internal(code);
67 gedcom_error("Unable to open conversion context: %s",
74 case BADTOKEN: printf("BADTOKEN "); break;
75 case OPEN: printf("OPEN(%d) ", gedcom_lval.number); break;
76 case CLOSE: printf("CLOSE "); break;
77 case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break;
78 case DELIM: printf("DELIM "); break;
79 case ANYCHAR: printf("%s ", gedcom_lval.string); break;
80 case POINTER: printf("POINTER(%s) ", gedcom_lval.string); break;
81 case USERTAG: printf("USERTAG(%s) ", gedcom_lval.tag.string); break;
82 default: printf("TAG(%s) ", gedcom_lval.tag.string); break;
87 close_conv_to_internal();
91 #endif /* of #ifdef LEXER_TEST */
93 /* These are defined as functions here, because xgettext has trouble
94 extracting the strings out of long pre-processor defined */
96 static void error_line_too_long(const char *line)
98 gedcom_error(_("Line too long, max %d characters allowed: %s"),
99 MAXGEDCLINELEN, line);
102 static void error_level_leading_zero()
104 gedcom_error (_("Level number with leading zero not allowed"));
107 static void error_level_out_of_range()
109 gedcom_error (_("Level number out of range [0..%d]"), MAXGEDCLEVEL);
112 static void error_level_too_high(int level_diff)
114 gedcom_error (_("GEDCOM level number is %d higher than previous"),
118 static void error_tag_too_long(const char *tag)
120 gedcom_error(_("Tag '%s' too long, max %d characters allowed"),
124 static void error_invalid_character(const char *str, char ch)
126 gedcom_error(_("Invalid character for encoding: '%s' (0x%02x)"), str, ch);
129 static void error_pointer_too_long(const char *ptr)
131 gedcom_error(_("Pointer '%s' too long, max %d characters allowed"),
135 static void error_at_character()
137 gedcom_error(_("'@' character should be written as '@@' in values"));
140 static void error_tab_character()
142 gedcom_error(_("Tab character is not allowed in values"));
145 static void error_unexpected_character(const char* str, char ch)
147 gedcom_error(_("Unexpected character: '%s' (0x%02x)"), str, ch);
150 /* This is to bypass the iconv conversion (if the input is UTF-8 coming
152 static int dummy_conv = 0;
154 #elif LEX_SECTION == 2
156 #define TO_INTERNAL(STR,OUTBUF) \
157 (dummy_conv ? STR : to_internal(STR, yyleng, OUTBUF))
159 #define INIT_LINE_LEN \
162 #define CHECK_LINE_LEN \
163 { if (line_len != (size_t)-1) { \
164 line_len += strlen(yytext); \
165 if (line_len > MAXGEDCLINELEN * encoding_width) { \
166 error_line_too_long(yytext); \
167 line_len = (size_t)-1; \
173 #define GENERATE_TAB_SPACE \
174 { gedcom_lval.string = " "; \
179 #define MKTAGACTION(THETAG) \
181 gedcom_lval.tag.string = TO_INTERNAL(yytext, tag_buffer); \
182 gedcom_lval.tag.value = TAG_##THETAG; \
185 return TAG_##THETAG; \
188 /* The GEDCOM level number is converted into a sequence of opening
189 and closing brackets. Simply put, the following GEDCOM fragment:
202 { SOUR genes (1 higher: no closing brackets)
203 { VERS 1.6 (1 higher: no closing brackets)
204 } { NAME Genes (same level: 1 closing bracket)
205 } } { DATE 07 OCT 2001 (1 lower: 2 closing brackets)
219 But because this means that one token is converted into a series
220 of tokens, there is some initial code following immediately here
221 that returns "pending" tokens.
223 Also, for compatibility tabs are converted into spaces, which is
226 #define ACTION_BEFORE_REGEXPS \
227 { if (compat_mode(C_TAB_CHARACTER) && tab_space-- > 0) { \
228 GENERATE_TAB_SPACE; \
230 else if (level_diff < 1) { \
234 else if (level_diff == 1) { \
236 gedcom_lval.number = current_level; \
240 /* out of brackets... */ \
245 #define ACTION_INITIAL_WHITESPACE \
247 /* ignore initial whitespace further */ \
251 #define ACTION_0_DIGITS \
252 { error_level_leading_zero(); \
257 #define ACTION_DIGITS \
258 { int level = atoi(TO_INTERNAL(yytext, str_buffer)); \
260 if ((level < 0) || (level > MAXGEDCLEVEL)) { \
261 error_level_out_of_range(); \
265 level_diff = level - current_level; \
267 current_level = level; \
268 if (level_diff < 1) { \
272 else if (level_diff == 1) { \
274 gedcom_lval.number = current_level; \
278 /* should never happen (error to GEDCOM spec) */ \
279 error_level_too_high(level_diff); \
286 #define ACTION_ALPHANUM \
287 { if (strlen(yytext) > MAXGEDCTAGLEN * encoding_width) { \
288 error_tag_too_long(yytext); \
293 gedcom_lval.tag.string = TO_INTERNAL(yytext, tag_buffer); \
294 gedcom_lval.tag.value = USERTAG; \
301 #define ACTION_DELIM \
303 gedcom_lval.string = TO_INTERNAL(yytext, str_buffer); \
311 tmp = TO_INTERNAL(yytext, str_buffer); \
313 /* Something went wrong during conversion... */ \
314 error_invalid_character(yytext, yytext[0]); \
318 gedcom_lval.string = tmp; \
319 /* Due to character conversions, it is possible that the current \
320 character will be combined with the next, and so now we don't have a \
322 In principle, this is only applicable to the 1byte case (e.g. ANSEL),\
323 but it doesn't harm the unicode case. \
325 if (strlen(gedcom_lval.string) > 0) \
331 #define ACTION_ESCAPE \
333 gedcom_lval.string = TO_INTERNAL(yytext, str_buffer); \
338 #define ACTION_POINTER \
340 if (strlen(yytext) > MAXGEDCPTRLEN * encoding_width) { \
341 error_pointer_too_long(yytext); \
344 gedcom_lval.string = TO_INTERNAL(yytext, ptr_buffer); \
349 /* Due to the conversion of level numbers into brackets, the
350 terminator is not important, so no token is returned here.
351 Although not strictly according to the GEDCOM spec, we'll ignore
352 whitespace just before the terminator.
355 #define ACTION_TERMINATOR \
359 set_read_encoding_terminator(TO_INTERNAL(yytext, str_buffer)); \
364 /* Eventually we have to return 1 closing bracket (for the trailer).
365 We can detect whether we have sent the closing bracket using the
366 level_diff (at eof, first it is 2, then we increment it ourselves)
370 { if (level_diff == 2) { \
375 char* ptr; int size; \
376 /* ... terminate lex */ \
378 /* Get rid of f*cking compiler warning from lex generated code */ \
379 /* yyterminate does return(), so program will never come here */ \
380 yy_flex_realloc(ptr, size); \
384 #define ACTION_NORMAL_AT \
385 { if (compat_mode(C_NO_DOUBLE_AT)) { \
387 char *yycopy = strdup(yytext); \
389 for (i = 0; i < 2; i++) \
390 for (j = yyleng - 1; j >= 0; --j) \
399 error_at_character(); \
405 { if (compat_mode(C_TAB_CHARACTER)) { \
407 GENERATE_TAB_SPACE; \
410 error_tab_character(); \
415 #define ACTION_UNEXPECTED \
416 { error_unexpected_character(yytext, yytext[0]); \
420 #elif LEX_SECTION == 3
427 static void free_conv_buffers()
429 free_conv_buffer(ptr_buffer);
430 free_conv_buffer(tag_buffer);
431 free_conv_buffer(str_buffer);
434 static void yylex_cleanup()
436 /* fix memory leak in lex */
437 yy_delete_buffer(yy_current_buffer);
438 yy_current_buffer = NULL;
442 static void init_conv_buffers()
445 ptr_buffer = create_conv_buffer(INITIAL_PTR_BUFFER_LEN);
446 tag_buffer = create_conv_buffer(INITIAL_TAG_BUFFER_LEN);
447 str_buffer = create_conv_buffer(INITIAL_STR_BUFFER_LEN);
451 static int exitfuncregistered = 0;
453 void yymyinit(FILE *f)
455 if (! exitfuncregistered && atexit(yylex_cleanup) == 0)
456 exitfuncregistered = 1;
460 /* Reset our state */
462 level_diff = MAXGEDCLEVEL;