Copied from old documentation. Removed all Gedcom_val details.
[gedcom-parse.git] / gedcom / gedcom_date.lex
1 /* Lexer for Gedcom dates
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.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 %{
25 #include "date.h"
26 #include "gedcom_date.tabgen.h"
27   
28 #define YY_NO_UNPUT
29
30 static char buf[MAX_DATE_TOKEN][MAX_PHRASE_LEN+1];
31 static int token_nr = 0; 
32 %}
33
34 %option case-insensitive
35 %s PHRASE
36
37 %%
38
39 %{
40
41 #define SIMPLE_RETURN(TOKEN) \
42   { if (token_nr >= MAX_DATE_TOKEN) { \
43       gedcom_date_error(_("Date token stack overflow")); \
44       return BADTOKEN; \
45     } \
46     memset(buf[token_nr], 0, MAX_PHRASE_LEN+1); \
47     strncpy(buf[token_nr], yytext, yyleng); \
48     gedcom_date_lval.string = buf[token_nr++]; \
49     return TOKEN; \
50   }
51
52 #define ACTION_UNEXPECTED \
53   { gedcom_date_error(_("Unexpected input")); \
54     return BADTOKEN; \
55   }
56
57 #define UNKNOWN_CALENDAR_TYPE \
58   { gedcom_date_error(_("Unknown calendar type")); \
59     return BADTOKEN; \
60   }
61 %}
62
63 [ \t]+          /* ignore whitespace between tokens */
64
65 @#DGREGORIAN@   SIMPLE_RETURN(ESC_DATE_GREG)
66 @#DJULIAN@      SIMPLE_RETURN(ESC_DATE_JULN)
67 @#DHEBREW@      SIMPLE_RETURN(ESC_DATE_HEBR)
68 "@#DFRENCH R@"  SIMPLE_RETURN(ESC_DATE_FREN)
69 @#.+@           UNKNOWN_CALENDAR_TYPE
70      
71 FROM            SIMPLE_RETURN(MOD_FROM)
72 TO              SIMPLE_RETURN(MOD_TO)  
73 BEF             SIMPLE_RETURN(MOD_BEF) 
74 AFT             SIMPLE_RETURN(MOD_AFT) 
75 BET             SIMPLE_RETURN(MOD_BET) 
76 AND             SIMPLE_RETURN(MOD_AND) 
77 ABT             SIMPLE_RETURN(MOD_ABT) 
78 CAL             SIMPLE_RETURN(MOD_CAL) 
79 EST             SIMPLE_RETURN(MOD_EST) 
80 INT             SIMPLE_RETURN(MOD_INT)
81      
82 JAN             SIMPLE_RETURN(MON_JAN) 
83 FEB             SIMPLE_RETURN(MON_FEB) 
84 MAR             SIMPLE_RETURN(MON_MAR) 
85 APR             SIMPLE_RETURN(MON_APR) 
86 MAY             SIMPLE_RETURN(MON_MAY) 
87 JUN             SIMPLE_RETURN(MON_JUN) 
88 JUL             SIMPLE_RETURN(MON_JUL) 
89 AUG             SIMPLE_RETURN(MON_AUG) 
90 SEP             SIMPLE_RETURN(MON_SEP) 
91 OCT             SIMPLE_RETURN(MON_OCT) 
92 NOV             SIMPLE_RETURN(MON_NOV) 
93 DEC             SIMPLE_RETURN(MON_DEC) 
94 TSH             SIMPLE_RETURN(MON_TSH) 
95 CSH             SIMPLE_RETURN(MON_CSH) 
96 KSL             SIMPLE_RETURN(MON_KSL) 
97 TVT             SIMPLE_RETURN(MON_TVT) 
98 SHV             SIMPLE_RETURN(MON_SHV) 
99 ADR             SIMPLE_RETURN(MON_ADR) 
100 ADS             SIMPLE_RETURN(MON_ADS) 
101 NSN             SIMPLE_RETURN(MON_NSN) 
102 IYR             SIMPLE_RETURN(MON_IYR) 
103 SVN             SIMPLE_RETURN(MON_SVN) 
104 TMZ             SIMPLE_RETURN(MON_TMZ) 
105 AAV             SIMPLE_RETURN(MON_AAV) 
106 ELL             SIMPLE_RETURN(MON_ELL) 
107 VEND            SIMPLE_RETURN(MON_VEND)
108 BRUM            SIMPLE_RETURN(MON_BRUM)
109 FRIM            SIMPLE_RETURN(MON_FRIM)
110 NIVO            SIMPLE_RETURN(MON_NIVO)
111 PLUV            SIMPLE_RETURN(MON_PLUV)
112 VENT            SIMPLE_RETURN(MON_VENT)
113 GERM            SIMPLE_RETURN(MON_GERM)
114 FLOR            SIMPLE_RETURN(MON_FLOR)
115 PRAI            SIMPLE_RETURN(MON_PRAI)
116 MESS            SIMPLE_RETURN(MON_MESS)
117 THER            SIMPLE_RETURN(MON_THER)
118 FRUC            SIMPLE_RETURN(MON_FRUC)
119 COMP            SIMPLE_RETURN(MON_COMP)
120
121 "("             { BEGIN(PHRASE);
122                   SIMPLE_RETURN(OPEN); 
123                 }
124 <PHRASE>[^\)]*  SIMPLE_RETURN(TEXT);
125 <PHRASE>")"     { BEGIN(INITIAL); 
126                   SIMPLE_RETURN(CLOSE);
127                 }
128      
129 "/"             SIMPLE_RETURN(SLASH)
130 [0-9]+          SIMPLE_RETURN(NUMBER)
131
132 .               ACTION_UNEXPECTED
133
134 %%
135
136 int get_date_token(const char* str)
137 {
138   int token;
139   YY_BUFFER_STATE buffer;
140
141   token_nr = 0;
142   yy_delete_buffer(YY_CURRENT_BUFFER);
143   buffer = yy_scan_string(str);
144   token = yylex();
145   yy_delete_buffer(buffer);
146   return token;
147 }
148
149 int get_year_tokens(const char* str, char** year1, char** year2)
150 {
151   int token;
152   int num_tokens = 0;
153   YY_BUFFER_STATE buffer;
154
155   token_nr = 0;
156   yy_delete_buffer(YY_CURRENT_BUFFER);
157   buffer = yy_scan_string(str);
158
159   token = yylex();
160   switch (token) {
161     case NUMBER: {
162       *year1 = buf[token_nr - 1];
163       token  = yylex();
164       switch (token) {
165         case SLASH: {
166           token = yylex();
167           switch (token) {
168             case NUMBER:
169               *year2 = buf[token_nr - 1];
170               num_tokens = 2; break;
171             default:
172               num_tokens = 0;
173           }
174           break;
175         }
176         case 0:   num_tokens = 1; break;
177         default:  num_tokens = 0;
178       }
179       break;
180     }
181     case 0:   num_tokens = 0; break;
182     default:  num_tokens = 0;
183   }
184   yy_delete_buffer(buffer);
185   return num_tokens;
186 }
187
188 int yywrap()
189 {
190   return 1;
191 }
192
193 static YY_BUFFER_STATE hndl;
194
195 void init_gedcom_date_lex(const char* string)
196 {
197   token_nr = 0;
198   hndl = yy_scan_string(string);
199 }
200
201 void close_gedcom_date_lex()
202 {
203   yy_delete_buffer(hndl);
204 }