6c742265c11fb688b7f3621eecc79ffdbeef86f9
[yaz-moved-to-github.git] / src / ccltoken.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /** 
6  * \file ccltoken.c
7  * \brief Implements CCL lexical analyzer (scanner)
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <string.h>
14 #include <stdlib.h>
15 #include <yaz/yaz-iconv.h>
16 #include "cclp.h"
17
18 /*
19  * token_cmp: Compare token with keyword(s)
20  * kw:     Keyword list. Each keyword is separated by space.
21  * token:  CCL token.
22  * return: 1 if token string matches one of the keywords in list;
23  *         0 otherwise.
24  */
25 static int token_cmp(CCL_parser cclp, const char **kw, struct ccl_token *token)
26 {
27     const char **aliases;
28     int case_sensitive = cclp->ccl_case_sensitive;
29     int i;
30
31     aliases = ccl_qual_search_special(cclp->bibset, "case");
32     if (aliases)
33         case_sensitive = atoi(aliases[0]);
34
35     for (i = 0; kw[i]; i++)
36     {
37         if (token->len == strlen(kw[i]))
38         {
39             if (case_sensitive)
40             {
41                 if (!memcmp(kw[i], token->name, token->len))
42                     return 1;
43             }
44             else
45             {
46                 if (!ccl_memicmp(kw[i], token->name, token->len))
47                     return 1;
48             }
49         }
50     }
51     return 0;
52 }
53
54 /*
55  * ccl_tokenize: tokenize CCL command string.
56  * return: CCL token list.
57  */
58 struct ccl_token *ccl_parser_tokenize(CCL_parser cclp, const char *command)
59 {
60     const char **aliases;
61     const unsigned char *cp = (const unsigned char *) command;
62     struct ccl_token *first = NULL;
63     struct ccl_token *last = NULL;
64     cclp->start_pos = command;
65
66     while (1)
67     {
68         const unsigned char *cp0 = cp;
69         while (*cp && strchr(" \t\r\n", *cp))
70             cp++;
71         if (!first)
72         {
73             first = last = (struct ccl_token *)xmalloc(sizeof(*first));
74             ccl_assert(first);
75             last->prev = NULL;
76         }
77         else
78         {
79             last->next = (struct ccl_token *)xmalloc(sizeof(*first));
80             ccl_assert(last->next);
81             last->next->prev = last;
82             last = last->next;
83         }
84         last->ws_prefix_buf = (const char *) cp0;
85         last->ws_prefix_len = cp - cp0;
86         last->next = NULL;
87         last->name = (const char *) cp;
88         last->len = 1;
89         switch (*cp++)
90         {
91         case '\0':
92             last->kind = CCL_TOK_EOL;
93             return first;
94         case '(':
95             last->kind = CCL_TOK_LP;
96             break;
97         case ')':
98             last->kind = CCL_TOK_RP;
99             break;
100         case ',':
101             last->kind = CCL_TOK_COMMA;
102             break;
103         case '%':
104         case '!':
105             last->kind = CCL_TOK_PROX;
106             while (yaz_isdigit(*cp))
107             {
108                 ++ last->len;
109                 cp++;
110             }
111             break;
112         case '>':
113         case '<':
114         case '=':
115             if (*cp == '=' || *cp == '<' || *cp == '>')
116             {
117                 cp++;
118                 last->kind = CCL_TOK_REL;
119                 ++ last->len;
120             }
121             else if (cp[-1] == '=')
122                 last->kind = CCL_TOK_EQ;
123             else
124                 last->kind = CCL_TOK_REL;
125             break;
126         default:
127             --cp;
128             --last->len;
129             if (*cp == '"')
130             {
131                 cp++;
132                 last->kind = CCL_TOK_TERM;
133                 last->name = (const char *) cp;
134                 while (*cp && *cp != '"')
135                 {
136                     cp++;
137                     ++ last->len;
138                 }
139                 if (*cp)
140                     cp++;
141             }
142             else
143             {
144                 last->kind = CCL_TOK_TERM;
145                 last->name = (const char *) cp;
146                 while (*cp && !strchr("(),%!><= \t\n\r", *cp))
147                 {
148                     ++ last->len;
149                     cp++;
150                 }
151                 aliases = ccl_qual_search_special(cclp->bibset, "and");
152                 if (!aliases)
153                     aliases = cclp->ccl_token_and;
154                 if (token_cmp(cclp, aliases, last))
155                     last->kind = CCL_TOK_AND;
156                 
157                 aliases = ccl_qual_search_special(cclp->bibset, "or");
158                 if (!aliases)
159                     aliases = cclp->ccl_token_or;
160                 if (token_cmp(cclp, aliases, last))
161                     last->kind = CCL_TOK_OR;
162                 
163                 aliases = ccl_qual_search_special(cclp->bibset, "not");
164                 if (!aliases)
165                     aliases = cclp->ccl_token_not;
166                 if (token_cmp(cclp, aliases, last))
167                     last->kind = CCL_TOK_NOT;
168                 
169                 aliases = ccl_qual_search_special(cclp->bibset, "set");
170                 if (!aliases)
171                     aliases = cclp->ccl_token_set;
172                 
173                 if (token_cmp(cclp, aliases, last))
174                     last->kind = CCL_TOK_SET;
175             }
176         }
177     }
178     return first;
179 }
180
181 struct ccl_token *ccl_token_add(struct ccl_token *at)
182 {
183     struct ccl_token *n = (struct ccl_token *)xmalloc(sizeof(*n));
184     ccl_assert(n);
185     n->next = at->next;
186     n->prev = at;
187     at->next = n;
188     if (n->next)
189         n->next->prev = n;
190
191     n->kind = CCL_TOK_TERM;
192     n->name = 0;
193     n->len = 0;
194     n->ws_prefix_buf = 0;
195     n->ws_prefix_len = 0;
196     return n;
197 }
198     
199 /*
200  * ccl_token_del: delete CCL tokens
201  */
202 void ccl_token_del(struct ccl_token *list)
203 {
204     struct ccl_token *list1;
205
206     while (list) 
207     {
208         list1 = list->next;
209         xfree(list);
210         list = list1;
211     }
212 }
213
214 static const char **create_ar(const char *v1, const char *v2)
215 {
216     const char **a = (const char **) xmalloc(3 * sizeof(*a));
217     a[0] = xstrdup(v1);
218     if (v2)
219     {
220         a[1] = xstrdup(v2);
221         a[2] = 0;
222     }
223     else
224         a[1] = 0;
225     return a;
226 }
227
228 static void destroy_ar(const char **a)
229 {
230     if (a)
231     {
232         int i;
233         for (i = 0; a[i]; i++)
234             xfree((char *) a[i]);
235         xfree((char **)a);
236     }
237 }
238
239 CCL_parser ccl_parser_create(CCL_bibset bibset)
240 {
241     CCL_parser p = (CCL_parser)xmalloc(sizeof(*p));
242     if (!p)
243         return p;
244     p->look_token = NULL;
245     p->error_code = 0;
246     p->error_pos = NULL;
247     p->bibset = bibset;
248
249     p->ccl_token_and = create_ar("and", 0);
250     p->ccl_token_or = create_ar("or", 0);
251     p->ccl_token_not = create_ar("not", "andnot");
252     p->ccl_token_set = create_ar("set", 0);
253     p->ccl_case_sensitive = 1;
254
255     return p;
256 }
257
258 void ccl_parser_destroy(CCL_parser p)
259 {
260     if (!p)
261         return;
262     destroy_ar(p->ccl_token_and);
263     destroy_ar(p->ccl_token_or);
264     destroy_ar(p->ccl_token_not);
265     destroy_ar(p->ccl_token_set);
266     xfree(p);
267 }
268
269 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag)
270 {
271     if (p)
272         p->ccl_case_sensitive = case_sensitivity_flag;
273 }
274
275 int ccl_parser_get_error(CCL_parser cclp, int *pos)
276 {
277     if (pos && cclp->error_code)
278         *pos = cclp->error_pos - cclp->start_pos;
279     return cclp->error_code;
280 }
281
282 /*
283  * Local variables:
284  * c-basic-offset: 4
285  * c-file-style: "Stroustrup"
286  * indent-tabs-mode: nil
287  * End:
288  * vim: shiftwidth=4 tabstop=8 expandtab
289  */
290