1 /* This program is free software; you can redistribute it and/or modify *
2 * it under the terms of the GNU General Public License as published by *
3 * the Free Software Foundation; either version 2 of the License, or *
4 * (at your option) any later version. *
6 (C) 2001 by The Genes Development Team
7 Original author: Peter Verthez (Peter.Verthez@advalvas.be)
15 #include "gedcom_internal.h"
19 #include "gedcom.tab.h"
23 static size_t encoding_width;
24 static int current_level = -1;
25 static int level_diff=MAXGEDCLEVEL;
26 static size_t line_len = 0;
28 static char ptr_buf[MAXGEDCPTRLEN * UTF_FACTOR + 1];
29 static char tag_buf[MAXGEDCTAGLEN * UTF_FACTOR + 1];
30 static char str_buf[MAXGEDCLINELEN * UTF_FACTOR + 1];
38 int test_loop(ENCODING enc, char* code)
42 set_encoding_width(enc);
43 res = open_conv_to_internal(code);
45 gedcom_error("Unable to open conversion context: %s",
52 case BADTOKEN: printf("BADTOKEN "); break;
53 case OPEN: printf("OPEN(%d) ", gedcom_lval.number); break;
54 case CLOSE: printf("CLOSE "); break;
55 case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break;
56 case DELIM: printf("DELIM "); break;
57 case ANYCHAR: printf("%s ", gedcom_lval.string); break;
58 case POINTER: printf("POINTER(%s) ", gedcom_lval.string); break;
59 case USERTAG: printf("USERTAG(%s) ", gedcom_lval.string); break;
60 default: printf("TAG(%s) ", gedcom_lval.string); break;
65 close_conv_to_internal();
69 #endif /* of #ifdef LEXER_TEST */
71 #else /* of #ifndef IN_LEX */
73 #define TO_INTERNAL(STR,OUTBUF) \
74 to_internal(STR, yyleng, OUTBUF, sizeof(OUTBUF))
76 #define INIT_LINE_LEN \
79 #define CHECK_LINE_LEN \
80 { if (line_len != (size_t)-1) { \
81 line_len += strlen(yytext); \
82 if (line_len > MAXGEDCLINELEN * encoding_width) { \
83 gedcom_error("Line too long, max %d characters", \
85 line_len = (size_t)-1; \
91 #define MKTAGACTION(THETAG) \
93 gedcom_lval.string = TO_INTERNAL(yytext, tag_buf); \
95 return TAG_##THETAG; \
98 /* The GEDCOM level number is converted into a sequence of opening
99 and closing brackets. Simply put, the following GEDCOM fragment:
112 { SOUR genes (1 higher: no closing brackets)
113 { VERS 1.6 (1 higher: no closing brackets)
114 } { NAME Genes (same level: 1 closing bracket)
115 } } { DATE 07 OCT 2001 (1 lower: 2 closing brackets)
129 But because this means that one token is converted into a series
130 of tokens, there is some initial code following immediately here
131 that returns "pending" tokens. */
133 #define ACTION_BEFORE_REGEXPS \
134 { if (level_diff < 1) { \
138 else if (level_diff == 1) { \
140 gedcom_lval.number = current_level; \
144 /* out of brackets... */ \
149 #define ACTION_INITIAL_WHITESPACE \
151 /* ignore initial whitespace further */ \
155 #define ACTION_0_DIGITS \
156 { gedcom_error ("Level number with leading zero"); \
161 #define ACTION_DIGITS \
162 { int level = atoi(TO_INTERNAL(yytext, str_buf)); \
164 if ((level < 0) || (level > MAXGEDCLEVEL)) { \
165 gedcom_error ("Level number out of range [0..%d]", \
169 level_diff = level - current_level; \
171 current_level = level; \
172 if (level_diff < 1) { \
176 else if (level_diff == 1) { \
178 gedcom_lval.number = current_level; \
182 /* should never happen (error to GEDCOM spec) */ \
183 gedcom_error ("GEDCOM level number is %d higher than " \
191 #define ACTION_ALPHANUM \
192 { if (strlen(yytext) > MAXGEDCTAGLEN * encoding_width) { \
193 gedcom_error("Tag '%s' too long, max %d characters", \
194 yytext, MAXGEDCTAGLEN); \
198 gedcom_lval.string = TO_INTERNAL(yytext, tag_buf); \
204 #define ACTION_DELIM \
206 gedcom_lval.string = TO_INTERNAL(yytext, str_buf); \
213 gedcom_lval.string = TO_INTERNAL(yytext, str_buf); \
214 /* Due to character conversions, it is possible that the current \
215 character will be combined with the next, and so now we don't have a \
217 In principle, this is only applicable to the 1byte case (e.g. ANSEL), \
218 but it doesn't harm the unicode case. \
220 if (strlen(gedcom_lval.string) > 0) \
225 #define ACTION_ESCAPE \
227 gedcom_lval.string = TO_INTERNAL(yytext, str_buf); \
232 #define ACTION_POINTER \
234 if (strlen(yytext) > MAXGEDCPTRLEN * encoding_width) { \
235 gedcom_error("Pointer '%s' too long, max %d characters", \
236 yytext, MAXGEDCPTRLEN); \
239 gedcom_lval.string = TO_INTERNAL(yytext, ptr_buf); \
244 /* Due to the conversion of level numbers into brackets, the
245 terminator is not important, so no token is returned here.
246 Although not strictly according to the GEDCOM spec, we'll ignore
247 whitespace just before the terminator.
250 #define ACTION_TERMINATOR \
258 /* Eventually we have to return 1 closing bracket (for the trailer).
259 We can detect whether we have sent the closing bracket using the
260 level_diff (at eof, first it is 2, then we increment it ourselves)
264 { if (level_diff == 2) { \
269 /* Reset our state */ \
270 current_level = -1; \
271 level_diff = MAXGEDCLEVEL; \
272 /* ... then terminate lex */ \
278 #define ACTION_UNEXPECTED \
279 { gedcom_error("Unexpected character: '%s' (0x%02x)", \
280 yytext, yytext[0]); \