1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CCL lexical analyzer (scanner)
17 * token_cmp: Compare token with keyword(s)
18 * kw: Keyword list. Each keyword is separated by space.
20 * return: 1 if token string matches one of the keywords in list;
23 static int token_cmp(CCL_parser cclp, const char **kw, struct ccl_token *token)
26 int case_sensitive = cclp->ccl_case_sensitive;
29 aliases = ccl_qual_search_special(cclp->bibset, "case");
31 case_sensitive = atoi(aliases[0]);
33 for (i = 0; kw[i]; i++)
35 if (token->len == strlen(kw[i]))
39 if (!memcmp(kw[i], token->name, token->len))
44 if (!ccl_memicmp(kw[i], token->name, token->len))
53 * ccl_tokenize: tokenize CCL command string.
54 * return: CCL token list.
56 struct ccl_token *ccl_parser_tokenize(CCL_parser cclp, const char *command)
59 const unsigned char *cp = (const unsigned char *) command;
60 struct ccl_token *first = NULL;
61 struct ccl_token *last = NULL;
62 cclp->start_pos = command;
66 const unsigned char *cp0 = cp;
67 while (*cp && strchr(" \t\r\n", *cp))
71 first = last = (struct ccl_token *)xmalloc(sizeof(*first));
77 last->next = (struct ccl_token *)xmalloc(sizeof(*first));
78 ccl_assert(last->next);
79 last->next->prev = last;
82 last->left_trunc = last->right_trunc = 0;
83 last->ws_prefix_buf = (const char *) cp0;
84 last->ws_prefix_len = cp - cp0;
86 last->name = (const char *) cp;
91 last->kind = CCL_TOK_EOL;
94 last->kind = CCL_TOK_LP;
97 last->kind = CCL_TOK_RP;
100 last->kind = CCL_TOK_COMMA;
104 last->kind = CCL_TOK_PROX;
114 if (*cp == '=' || *cp == '<' || *cp == '>')
117 last->kind = CCL_TOK_REL;
120 else if (cp[-1] == '=')
121 last->kind = CCL_TOK_EQ;
123 last->kind = CCL_TOK_REL;
130 last->left_trunc = 1;
136 last->kind = CCL_TOK_TERM;
137 last->name = (const char *) cp;
138 while (*cp && *cp != '"')
148 last->kind = CCL_TOK_TERM;
149 last->name = (const char *) cp;
150 while (*cp && !strchr("(),%!><=? \t\n\r", *cp))
155 aliases = ccl_qual_search_special(cclp->bibset, "and");
157 aliases = cclp->ccl_token_and;
158 if (token_cmp(cclp, aliases, last))
159 last->kind = CCL_TOK_AND;
161 aliases = ccl_qual_search_special(cclp->bibset, "or");
163 aliases = cclp->ccl_token_or;
164 if (token_cmp(cclp, aliases, last))
165 last->kind = CCL_TOK_OR;
167 aliases = ccl_qual_search_special(cclp->bibset, "not");
169 aliases = cclp->ccl_token_not;
170 if (token_cmp(cclp, aliases, last))
171 last->kind = CCL_TOK_NOT;
173 aliases = ccl_qual_search_special(cclp->bibset, "set");
175 aliases = cclp->ccl_token_set;
177 if (token_cmp(cclp, aliases, last))
178 last->kind = CCL_TOK_SET;
182 last->right_trunc = 1;
190 struct ccl_token *ccl_token_add(struct ccl_token *at)
192 struct ccl_token *n = (struct ccl_token *)xmalloc(sizeof(*n));
200 n->kind = CCL_TOK_TERM;
201 n->left_trunc = n->right_trunc = 0;
204 n->ws_prefix_buf = 0;
205 n->ws_prefix_len = 0;
210 * ccl_token_del: delete CCL tokens
212 void ccl_token_del(struct ccl_token *list)
214 struct ccl_token *list1;
224 static const char **create_ar(const char *v1, const char *v2)
226 const char **a = (const char **) xmalloc(3 * sizeof(*a));
238 static void destroy_ar(const char **a)
243 for (i = 0; a[i]; i++)
244 xfree((char *) a[i]);
249 CCL_parser ccl_parser_create(CCL_bibset bibset)
251 CCL_parser p = (CCL_parser)xmalloc(sizeof(*p));
254 p->look_token = NULL;
259 p->ccl_token_and = create_ar("and", 0);
260 p->ccl_token_or = create_ar("or", 0);
261 p->ccl_token_not = create_ar("not", "andnot");
262 p->ccl_token_set = create_ar("set", 0);
263 p->ccl_case_sensitive = 1;
268 void ccl_parser_destroy(CCL_parser p)
272 destroy_ar(p->ccl_token_and);
273 destroy_ar(p->ccl_token_or);
274 destroy_ar(p->ccl_token_not);
275 destroy_ar(p->ccl_token_set);
279 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag)
282 p->ccl_case_sensitive = case_sensitivity_flag;
285 int ccl_parser_get_error(CCL_parser cclp, int *pos)
287 if (pos && cclp->error_code)
288 *pos = cclp->error_pos - cclp->start_pos;
289 return cclp->error_code;
295 * c-file-style: "Stroustrup"
296 * indent-tabs-mode: nil
298 * vim: shiftwidth=4 tabstop=8 expandtab