1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 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->ws_prefix_buf = (const char *) cp0;
85 last->ws_prefix_len = cp - cp0;
87 last->name = (const char *) cp;
92 last->kind = CCL_TOK_EOL;
95 last->kind = CCL_TOK_LP;
98 last->kind = CCL_TOK_RP;
101 last->kind = CCL_TOK_COMMA;
105 last->kind = CCL_TOK_PROX;
106 while (yaz_isdigit(*cp))
115 if (*cp == '=' || *cp == '<' || *cp == '>')
118 last->kind = CCL_TOK_REL;
121 else if (cp[-1] == '=')
122 last->kind = CCL_TOK_EQ;
124 last->kind = CCL_TOK_REL;
130 last->kind = CCL_TOK_TERM;
131 last->name = (const char *) cp;
132 while (*cp && !strchr("(),%!><= \t\n\r", *cp))
134 if (*cp == '\\' && cp[1])
145 if (*cp == '\\' && cp[1])
159 aliases = ccl_qual_search_special(cclp->bibset, "and");
161 aliases = cclp->ccl_token_and;
162 if (token_cmp(cclp, aliases, last))
163 last->kind = CCL_TOK_AND;
165 aliases = ccl_qual_search_special(cclp->bibset, "or");
167 aliases = cclp->ccl_token_or;
168 if (token_cmp(cclp, aliases, last))
169 last->kind = CCL_TOK_OR;
171 aliases = ccl_qual_search_special(cclp->bibset, "not");
173 aliases = cclp->ccl_token_not;
174 if (token_cmp(cclp, aliases, last))
175 last->kind = CCL_TOK_NOT;
177 aliases = ccl_qual_search_special(cclp->bibset, "set");
179 aliases = cclp->ccl_token_set;
181 if (token_cmp(cclp, aliases, last))
182 last->kind = CCL_TOK_SET;
188 struct ccl_token *ccl_token_add(struct ccl_token *at)
190 struct ccl_token *n = (struct ccl_token *)xmalloc(sizeof(*n));
198 n->kind = CCL_TOK_TERM;
201 n->ws_prefix_buf = 0;
202 n->ws_prefix_len = 0;
207 * ccl_token_del: delete CCL tokens
209 void ccl_token_del(struct ccl_token *list)
211 struct ccl_token *list1;
221 static const char **create_ar(const char *v1, const char *v2)
223 const char **a = (const char **) xmalloc(3 * sizeof(*a));
235 static void destroy_ar(const char **a)
240 for (i = 0; a[i]; i++)
241 xfree((char *) a[i]);
246 CCL_parser ccl_parser_create(CCL_bibset bibset)
248 CCL_parser p = (CCL_parser)xmalloc(sizeof(*p));
251 p->look_token = NULL;
256 p->ccl_token_and = create_ar("and", 0);
257 p->ccl_token_or = create_ar("or", 0);
258 p->ccl_token_not = create_ar("not", "andnot");
259 p->ccl_token_set = create_ar("set", 0);
260 p->ccl_case_sensitive = 1;
265 void ccl_parser_destroy(CCL_parser p)
269 destroy_ar(p->ccl_token_and);
270 destroy_ar(p->ccl_token_or);
271 destroy_ar(p->ccl_token_not);
272 destroy_ar(p->ccl_token_set);
276 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag)
279 p->ccl_case_sensitive = case_sensitivity_flag;
282 int ccl_parser_get_error(CCL_parser cclp, int *pos)
284 if (pos && cclp->error_code)
285 *pos = cclp->error_pos - cclp->start_pos;
286 return cclp->error_code;
292 * c-file-style: "Stroustrup"
293 * indent-tabs-mode: nil
295 * vim: shiftwidth=4 tabstop=8 expandtab