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->ws_prefix_buf = (const char *) cp0;
83 last->ws_prefix_len = cp - cp0;
85 last->name = (const char *) cp;
90 last->kind = CCL_TOK_EOL;
93 last->kind = CCL_TOK_LP;
96 last->kind = CCL_TOK_RP;
99 last->kind = CCL_TOK_COMMA;
103 last->kind = CCL_TOK_PROX;
113 if (*cp == '=' || *cp == '<' || *cp == '>')
116 last->kind = CCL_TOK_REL;
119 else if (cp[-1] == '=')
120 last->kind = CCL_TOK_EQ;
122 last->kind = CCL_TOK_REL;
125 last->kind = CCL_TOK_TERM;
126 last->name = (const char *) cp;
128 while (*cp && *cp != '\"')
137 if (!strchr("(),%!><= \t\n\r", cp[-1]))
139 while (*cp && !strchr("(),%!><= \t\n\r", *cp))
145 last->kind = CCL_TOK_TERM;
147 aliases = ccl_qual_search_special(cclp->bibset, "and");
149 aliases = cclp->ccl_token_and;
150 if (token_cmp(cclp, aliases, last))
151 last->kind = CCL_TOK_AND;
153 aliases = ccl_qual_search_special(cclp->bibset, "or");
155 aliases = cclp->ccl_token_or;
156 if (token_cmp(cclp, aliases, last))
157 last->kind = CCL_TOK_OR;
159 aliases = ccl_qual_search_special(cclp->bibset, "not");
161 aliases = cclp->ccl_token_not;
162 if (token_cmp(cclp, aliases, last))
163 last->kind = CCL_TOK_NOT;
165 aliases = ccl_qual_search_special(cclp->bibset, "set");
167 aliases = cclp->ccl_token_set;
169 if (token_cmp(cclp, aliases, last))
170 last->kind = CCL_TOK_SET;
176 struct ccl_token *ccl_token_add(struct ccl_token *at)
178 struct ccl_token *n = (struct ccl_token *)xmalloc(sizeof(*n));
186 n->kind = CCL_TOK_TERM;
189 n->ws_prefix_buf = 0;
190 n->ws_prefix_len = 0;
195 * ccl_token_del: delete CCL tokens
197 void ccl_token_del(struct ccl_token *list)
199 struct ccl_token *list1;
209 static const char **create_ar(const char *v1, const char *v2)
211 const char **a = (const char **) xmalloc(3 * sizeof(*a));
223 static void destroy_ar(const char **a)
228 for (i = 0; a[i]; i++)
229 xfree((char *) a[i]);
234 CCL_parser ccl_parser_create(CCL_bibset bibset)
236 CCL_parser p = (CCL_parser)xmalloc(sizeof(*p));
239 p->look_token = NULL;
244 p->ccl_token_and = create_ar("and", 0);
245 p->ccl_token_or = create_ar("or", 0);
246 p->ccl_token_not = create_ar("not", "andnot");
247 p->ccl_token_set = create_ar("set", 0);
248 p->ccl_case_sensitive = 1;
253 void ccl_parser_destroy(CCL_parser p)
257 destroy_ar(p->ccl_token_and);
258 destroy_ar(p->ccl_token_or);
259 destroy_ar(p->ccl_token_not);
260 destroy_ar(p->ccl_token_set);
264 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag)
267 p->ccl_case_sensitive = case_sensitivity_flag;
270 int ccl_parser_get_error(CCL_parser cclp, int *pos)
272 if (pos && cclp->error_code)
273 *pos = cclp->error_pos - cclp->start_pos;
274 return cclp->error_code;
280 * c-file-style: "Stroustrup"
281 * indent-tabs-mode: nil
283 * vim: shiftwidth=4 tabstop=8 expandtab