Fixed bug #2068: pkg-config trouble.
[yaz-moved-to-github.git] / src / ccltoken.c
1 /*
2  * Copyright (C) 1995-2008, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: ccltoken.c,v 1.15 2008-01-09 21:32:28 adam Exp $
6  */
7 /** 
8  * \file ccltoken.c
9  * \brief Implements CCL lexical analyzer (scanner)
10  */
11
12 #include <string.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15
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 (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         case '\"':
127             last->kind = CCL_TOK_TERM;
128             last->name = (const char *) cp;
129             last->len = 0;
130             while (*cp && *cp != '\"')
131             {
132                 cp++;
133                 ++ last->len;
134             }
135             if (*cp == '\"')
136                 cp++;
137             break;
138         default:
139             if (!strchr("(),%!><= \t\n\r", cp[-1]))
140             {
141                 while (*cp && !strchr("(),%!><= \t\n\r", *cp))
142                 {
143                     cp++;
144                     ++ last->len;
145                 }
146             }
147             last->kind = CCL_TOK_TERM;
148
149             aliases = ccl_qual_search_special(cclp->bibset, "and");
150             if (!aliases)
151                 aliases = cclp->ccl_token_and;
152             if (token_cmp(cclp, aliases, last))
153                 last->kind = CCL_TOK_AND;
154
155             aliases = ccl_qual_search_special(cclp->bibset, "or");
156             if (!aliases)
157                 aliases = cclp->ccl_token_or;
158             if (token_cmp(cclp, aliases, last))
159                 last->kind = CCL_TOK_OR;
160
161             aliases = ccl_qual_search_special(cclp->bibset, "not");
162             if (!aliases)
163                 aliases = cclp->ccl_token_not;
164             if (token_cmp(cclp, aliases, last))
165                 last->kind = CCL_TOK_NOT;
166
167             aliases = ccl_qual_search_special(cclp->bibset, "set");
168             if (!aliases)
169                 aliases = cclp->ccl_token_set;
170
171             if (token_cmp(cclp, aliases, last))
172                 last->kind = CCL_TOK_SET;
173         }
174     }
175     return first;
176 }
177
178 struct ccl_token *ccl_token_add(struct ccl_token *at)
179 {
180     struct ccl_token *n = (struct ccl_token *)xmalloc(sizeof(*n));
181     ccl_assert(n);
182     n->next = at->next;
183     n->prev = at;
184     at->next = n;
185     if (n->next)
186         n->next->prev = n;
187
188     n->kind = CCL_TOK_TERM;
189     n->name = 0;
190     n->len = 0;
191     n->ws_prefix_buf = 0;
192     n->ws_prefix_len = 0;
193     return n;
194 }
195     
196 /*
197  * ccl_token_del: delete CCL tokens
198  */
199 void ccl_token_del(struct ccl_token *list)
200 {
201     struct ccl_token *list1;
202
203     while (list) 
204     {
205         list1 = list->next;
206         xfree(list);
207         list = list1;
208     }
209 }
210
211 static const char **create_ar(const char *v1, const char *v2)
212 {
213     const char **a = (const char **) xmalloc(3 * sizeof(*a));
214     a[0] = xstrdup(v1);
215     if (v2)
216     {
217         a[1] = xstrdup(v2);
218         a[2] = 0;
219     }
220     else
221         a[1] = 0;
222     return a;
223 }
224
225 static void destroy_ar(const char **a)
226 {
227     if (a)
228     {
229         int i;
230         for (i = 0; a[i]; i++)
231             xfree((char *) a[i]);
232         xfree((char **)a);
233     }
234 }
235
236 CCL_parser ccl_parser_create(CCL_bibset bibset)
237 {
238     CCL_parser p = (CCL_parser)xmalloc(sizeof(*p));
239     if (!p)
240         return p;
241     p->look_token = NULL;
242     p->error_code = 0;
243     p->error_pos = NULL;
244     p->bibset = bibset;
245
246     p->ccl_token_and = create_ar("and", 0);
247     p->ccl_token_or = create_ar("or", 0);
248     p->ccl_token_not = create_ar("not", "andnot");
249     p->ccl_token_set = create_ar("set", 0);
250     p->ccl_case_sensitive = 1;
251
252     return p;
253 }
254
255 void ccl_parser_destroy(CCL_parser p)
256 {
257     if (!p)
258         return;
259     destroy_ar(p->ccl_token_and);
260     destroy_ar(p->ccl_token_or);
261     destroy_ar(p->ccl_token_not);
262     destroy_ar(p->ccl_token_set);
263     xfree(p);
264 }
265
266 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag)
267 {
268     if (p)
269         p->ccl_case_sensitive = case_sensitivity_flag;
270 }
271
272 int ccl_parser_get_error(CCL_parser cclp, int *pos)
273 {
274     if (pos && cclp->error_code)
275         *pos = cclp->error_pos - cclp->start_pos;
276     return cclp->error_code;
277 }
278
279 /*
280  * Local variables:
281  * c-basic-offset: 4
282  * indent-tabs-mode: nil
283  * End:
284  * vim: shiftwidth=4 tabstop=8 expandtab
285  */
286