1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CCL lexical analyzer (scanner)
15 #include <yaz/yaz-iconv.h>
19 * token_cmp: Compare token with keyword(s)
20 * kw: Keyword list. Each keyword is separated by space.
22 * return: 1 if token string matches one of the keywords in list;
25 static int token_cmp(CCL_parser cclp, const char **kw, struct ccl_token *token)
28 int case_sensitive = cclp->ccl_case_sensitive;
31 aliases = ccl_qual_search_special(cclp->bibset, "case");
33 case_sensitive = atoi(aliases[0]);
35 for (i = 0; kw[i]; i++)
37 if (token->len == strlen(kw[i]))
41 if (!memcmp(kw[i], token->name, token->len))
46 if (!ccl_memicmp(kw[i], token->name, token->len))
55 * ccl_tokenize: tokenize CCL command string.
56 * return: CCL token list.
58 struct ccl_token *ccl_parser_tokenize(CCL_parser cclp, const char *command)
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;
68 const unsigned char *cp0 = cp;
69 while (*cp && strchr(" \t\r\n", *cp))
73 first = last = (struct ccl_token *)xmalloc(sizeof(*first));
79 last->next = (struct ccl_token *)xmalloc(sizeof(*first));
80 ccl_assert(last->next);
81 last->next->prev = last;
84 last->left_trunc = last->right_trunc = 0;
85 last->ws_prefix_buf = (const char *) cp0;
86 last->ws_prefix_len = cp - cp0;
88 last->name = (const char *) cp;
93 last->kind = CCL_TOK_EOL;
96 last->kind = CCL_TOK_LP;
99 last->kind = CCL_TOK_RP;
102 last->kind = CCL_TOK_COMMA;
106 last->kind = CCL_TOK_PROX;
107 while (yaz_isdigit(*cp))
116 if (*cp == '=' || *cp == '<' || *cp == '>')
119 last->kind = CCL_TOK_REL;
122 else if (cp[-1] == '=')
123 last->kind = CCL_TOK_EQ;
125 last->kind = CCL_TOK_REL;
132 last->left_trunc = 1;
138 last->kind = CCL_TOK_TERM;
139 last->name = (const char *) cp;
140 while (*cp && *cp != '"')
150 last->kind = CCL_TOK_TERM;
151 last->name = (const char *) cp;
152 while (*cp && !strchr("(),%!><=? \t\n\r", *cp))
157 aliases = ccl_qual_search_special(cclp->bibset, "and");
159 aliases = cclp->ccl_token_and;
160 if (token_cmp(cclp, aliases, last))
161 last->kind = CCL_TOK_AND;
163 aliases = ccl_qual_search_special(cclp->bibset, "or");
165 aliases = cclp->ccl_token_or;
166 if (token_cmp(cclp, aliases, last))
167 last->kind = CCL_TOK_OR;
169 aliases = ccl_qual_search_special(cclp->bibset, "not");
171 aliases = cclp->ccl_token_not;
172 if (token_cmp(cclp, aliases, last))
173 last->kind = CCL_TOK_NOT;
175 aliases = ccl_qual_search_special(cclp->bibset, "set");
177 aliases = cclp->ccl_token_set;
179 if (token_cmp(cclp, aliases, last))
180 last->kind = CCL_TOK_SET;
184 last->right_trunc = 1;
192 struct ccl_token *ccl_token_add(struct ccl_token *at)
194 struct ccl_token *n = (struct ccl_token *)xmalloc(sizeof(*n));
202 n->kind = CCL_TOK_TERM;
203 n->left_trunc = n->right_trunc = 0;
206 n->ws_prefix_buf = 0;
207 n->ws_prefix_len = 0;
212 * ccl_token_del: delete CCL tokens
214 void ccl_token_del(struct ccl_token *list)
216 struct ccl_token *list1;
226 static const char **create_ar(const char *v1, const char *v2)
228 const char **a = (const char **) xmalloc(3 * sizeof(*a));
240 static void destroy_ar(const char **a)
245 for (i = 0; a[i]; i++)
246 xfree((char *) a[i]);
251 CCL_parser ccl_parser_create(CCL_bibset bibset)
253 CCL_parser p = (CCL_parser)xmalloc(sizeof(*p));
256 p->look_token = NULL;
261 p->ccl_token_and = create_ar("and", 0);
262 p->ccl_token_or = create_ar("or", 0);
263 p->ccl_token_not = create_ar("not", "andnot");
264 p->ccl_token_set = create_ar("set", 0);
265 p->ccl_case_sensitive = 1;
270 void ccl_parser_destroy(CCL_parser p)
274 destroy_ar(p->ccl_token_and);
275 destroy_ar(p->ccl_token_or);
276 destroy_ar(p->ccl_token_not);
277 destroy_ar(p->ccl_token_set);
281 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag)
284 p->ccl_case_sensitive = case_sensitivity_flag;
287 int ccl_parser_get_error(CCL_parser cclp, int *pos)
289 if (pos && cclp->error_code)
290 *pos = cclp->error_pos - cclp->start_pos;
291 return cclp->error_code;
297 * c-file-style: "Stroustrup"
298 * indent-tabs-mode: nil
300 * vim: shiftwidth=4 tabstop=8 expandtab