Added body-of-text to BIB-1 ANY and the WAIS profile
[yaz-moved-to-github.git] / ccl / ccltoken.c
1 /* CCL - lexical analysis
2  * Europagate, 1995
3  *
4  * $Log: ccltoken.c,v $
5  * Revision 1.4  1995-11-01 13:54:22  quinn
6  * Minor adjustments
7  *
8  * Revision 1.3  1995/09/29  17:12:00  quinn
9  * Smallish
10  *
11  * Revision 1.2  1995/09/27  15:02:44  quinn
12  * Modified function heads & prototypes.
13  *
14  * Revision 1.1  1995/04/10  10:28:22  quinn
15  * Added copy of CCL.
16  *
17  * Revision 1.5  1995/02/23  08:32:00  adam
18  * Changed header.
19  *
20  * Revision 1.3  1995/02/15  17:42:16  adam
21  * Minor changes of the api of this module. FILE* argument added
22  * to ccl_pr_tree.
23  *
24  * Revision 1.2  1995/02/14  19:55:13  adam
25  * Header files ccl.h/cclp.h are gone! They have been merged an
26  * moved to ../include/ccl.h.
27  * Node kind(s) in ccl_rpn_node have changed names.
28  *
29  * Revision 1.1  1995/02/13  12:35:21  adam
30  * First version of CCL. Qualifiers aren't handled yet.
31  *
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <assert.h>
38
39 #include <ccl.h>
40
41 static int strin (const char *s, const char *cset)
42 {
43     while (*cset)
44     {
45         if (*cset++ == *s)
46             return 1;
47     }
48     return 0;
49 }
50
51 const char *ccl_token_and = "and";
52 const char *ccl_token_or = "or";
53 const char *ccl_token_not = "not";
54 const char *ccl_token_set = "set";
55
56 struct ccl_token *ccl_tokenize (const char *command)
57 {
58     const char *cp = command;
59     struct ccl_token *first = NULL;
60     struct ccl_token *last = NULL;
61
62     while (1)
63     {
64         while (*cp && strin (cp, " \t\r\n"))
65         {
66             cp++;
67             continue;
68         }
69         if (!first)
70         {
71             first = last = xmalloc (sizeof (*first));
72             assert (first);
73             last->prev = NULL;
74         }
75         else
76         {
77             last->next = xmalloc (sizeof(*first));
78             assert (last->next);
79             last->next->prev = last;
80             last = last->next;
81         }
82         last->next = NULL;
83         last->name = cp;
84         last->len = 1;
85         switch (*cp++)
86         {
87         case '\0':
88             last->kind = CCL_TOK_EOL;
89             return first;
90         case '(':
91             last->kind = CCL_TOK_LP;
92             break;
93         case ')':
94             last->kind = CCL_TOK_RP;
95             break;
96         case ',':
97             last->kind = CCL_TOK_COMMA;
98             break;
99         case '%':
100         case '!':
101             last->kind = CCL_TOK_PROX;
102             while (*cp == '%' || *cp == '!')
103             {
104                 ++ last->len;
105                 cp++;
106             }
107             break;
108         case '>':
109         case '<':
110         case '=':
111             if (*cp == '=' || *cp == '<' || *cp == '>')
112             {
113                 cp++;
114                 last->kind = CCL_TOK_REL;
115                 ++ last->len;
116             }
117             else if (cp[-1] == '=')
118                 last->kind = CCL_TOK_EQ;
119             else
120                 last->kind = CCL_TOK_REL;
121             break;
122         case '-':
123             last->kind = CCL_TOK_MINUS;
124             break;
125         case '\"':
126             last->kind = CCL_TOK_TERM;
127             last->name = cp;
128             last->len = 0;
129             while (*cp && *cp != '\"')
130             {
131                 cp++;
132                 ++ last->len;
133             }
134             if (*cp == '\"')
135                 cp++;
136             break;
137         default:
138             while (*cp && !strin (cp, "(),%!><=- \t\n\r"))
139             {
140                 cp++;
141                 ++ last->len;
142             }
143             if (strlen (ccl_token_and)==last->len &&
144                 !memcmp (ccl_token_and, last->name, last->len))
145                 last->kind = CCL_TOK_AND;
146             else if (strlen (ccl_token_or)==last->len &&
147                 !memcmp (ccl_token_or, last->name, last->len))
148                 last->kind = CCL_TOK_OR;
149             else if (strlen (ccl_token_not)==last->len &&
150                 !memcmp (ccl_token_not, last->name, last->len))
151                 last->kind = CCL_TOK_NOT;
152             else if (strlen (ccl_token_set)==last->len &&
153                 !memcmp (ccl_token_set, last->name, last->len))
154                 last->kind = CCL_TOK_SET;
155             else
156                 last->kind = CCL_TOK_TERM;
157         }
158     }
159     return first;
160 }