2 Copyright (C) 2001 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"
30 #include "gedcom.tab.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;
39 static char ptr_buf[MAXGEDCPTRLEN * UTF_FACTOR + 1];
40 static char tag_buf[MAXGEDCTAGLEN * UTF_FACTOR + 1];
41 static char str_buf[MAXGEDCLINELEN * UTF_FACTOR + 1];
49 int test_loop(ENCODING enc, char* code)
53 set_encoding_width(enc);
54 res = open_conv_to_internal(code);
56 gedcom_error("Unable to open conversion context: %s",
63 case BADTOKEN: printf("BADTOKEN "); break;
64 case OPEN: printf("OPEN(%d) ", gedcom_lval.number); break;
65 case CLOSE: printf("CLOSE "); break;
66 case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break;
67 case DELIM: printf("DELIM "); break;
68 case ANYCHAR: printf("%s ", gedcom_lval.string); break;
69 case POINTER: printf("POINTER(%s) ", gedcom_lval.string); break;
70 case USERTAG: printf("USERTAG(%s) ", gedcom_lval.string); break;
71 default: printf("TAG(%s) ", gedcom_lval.string); break;
76 close_conv_to_internal();
80 #endif /* of #ifdef LEXER_TEST */
82 #else /* of #ifndef IN_LEX */
84 #define TO_INTERNAL(STR,OUTBUF) \
85 to_internal(STR, yyleng, OUTBUF, sizeof(OUTBUF))
87 #define INIT_LINE_LEN \
90 #define CHECK_LINE_LEN \
91 { if (line_len != (size_t)-1) { \
92 line_len += strlen(yytext); \
93 if (line_len > MAXGEDCLINELEN * encoding_width) { \
94 gedcom_error("Line too long, max %d characters", \
96 line_len = (size_t)-1; \
102 #define MKTAGACTION(THETAG) \
104 gedcom_lval.string = TO_INTERNAL(yytext, tag_buf); \
106 return TAG_##THETAG; \
109 /* The GEDCOM level number is converted into a sequence of opening
110 and closing brackets. Simply put, the following GEDCOM fragment:
123 { SOUR genes (1 higher: no closing brackets)
124 { VERS 1.6 (1 higher: no closing brackets)
125 } { NAME Genes (same level: 1 closing bracket)
126 } } { DATE 07 OCT 2001 (1 lower: 2 closing brackets)
140 But because this means that one token is converted into a series
141 of tokens, there is some initial code following immediately here
142 that returns "pending" tokens. */
144 #define ACTION_BEFORE_REGEXPS \
145 { if (level_diff < 1) { \
149 else if (level_diff == 1) { \
151 gedcom_lval.number = current_level; \
155 /* out of brackets... */ \
160 #define ACTION_INITIAL_WHITESPACE \
162 /* ignore initial whitespace further */ \
166 #define ACTION_0_DIGITS \
167 { gedcom_error ("Level number with leading zero"); \
172 #define ACTION_DIGITS \
173 { int level = atoi(TO_INTERNAL(yytext, str_buf)); \
175 if ((level < 0) || (level > MAXGEDCLEVEL)) { \
176 gedcom_error ("Level number out of range [0..%d]", \
180 level_diff = level - current_level; \
182 current_level = level; \
183 if (level_diff < 1) { \
187 else if (level_diff == 1) { \
189 gedcom_lval.number = current_level; \
193 /* should never happen (error to GEDCOM spec) */ \
194 gedcom_error ("GEDCOM level number is %d higher than " \
202 #define ACTION_ALPHANUM \
203 { if (strlen(yytext) > MAXGEDCTAGLEN * encoding_width) { \
204 gedcom_error("Tag '%s' too long, max %d characters", \
205 yytext, MAXGEDCTAGLEN); \
209 gedcom_lval.string = TO_INTERNAL(yytext, tag_buf); \
215 #define ACTION_DELIM \
217 gedcom_lval.string = TO_INTERNAL(yytext, str_buf); \
224 gedcom_lval.string = TO_INTERNAL(yytext, str_buf); \
225 /* Due to character conversions, it is possible that the current \
226 character will be combined with the next, and so now we don't have a \
228 In principle, this is only applicable to the 1byte case (e.g. ANSEL), \
229 but it doesn't harm the unicode case. \
231 if (strlen(gedcom_lval.string) > 0) \
236 #define ACTION_ESCAPE \
238 gedcom_lval.string = TO_INTERNAL(yytext, str_buf); \
243 #define ACTION_POINTER \
245 if (strlen(yytext) > MAXGEDCPTRLEN * encoding_width) { \
246 gedcom_error("Pointer '%s' too long, max %d characters", \
247 yytext, MAXGEDCPTRLEN); \
250 gedcom_lval.string = TO_INTERNAL(yytext, ptr_buf); \
255 /* Due to the conversion of level numbers into brackets, the
256 terminator is not important, so no token is returned here.
257 Although not strictly according to the GEDCOM spec, we'll ignore
258 whitespace just before the terminator.
261 #define ACTION_TERMINATOR \
269 /* Eventually we have to return 1 closing bracket (for the trailer).
270 We can detect whether we have sent the closing bracket using the
271 level_diff (at eof, first it is 2, then we increment it ourselves)
275 { if (level_diff == 2) { \
280 /* Reset our state */ \
281 current_level = -1; \
282 level_diff = MAXGEDCLEVEL; \
283 /* ... then terminate lex */ \
289 #define ACTION_UNEXPECTED \
290 { gedcom_error("Unexpected character: '%s' (0x%02x)", \
291 yytext, yytext[0]); \