source: trunk/indexers/mgpp/text/GSDLQueryLex.cpp@ 3365

Last change on this file since 3365 was 3365, checked in by kjdon, 22 years ago

Initial revision

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/**************************************************************************
2 *
3 * GSDLQueryLex.cpp -- Lexical analyser for a simple query language
4 * Copyright (C) 2000 Rodger McNab
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 **************************************************************************/
21
22#include "GSDLQueryLex.h"
23#include "unitool.h"
24#include "words.h"
25
26inline void AddNChar (UCArray::const_iterator &here,
27 UCArray &text,
28 int len) {
29 while (len > 0) {
30 text.push_back (*here++);
31 len--;
32 }
33}
34
35static bool ParseInteger (UCArray::const_iterator &here,
36 UCArray::const_iterator end,
37 LexEl &el) {
38 el.Clear();
39
40 // this version of end is used in unitool
41 // UCArray::const_iterator endMinus1 = end-1;
42 const unsigned char* endMinus1 = &(*end)-1;
43
44 int charLen;
45 unsigned short c; // one character lookahead
46 charLen = parse_utf8_char (&*here, endMinus1, &c);
47
48 // check for positive or negative
49 bool neg = false;
50 if (c == '+') {
51 AddNChar (here, el.text, charLen);
52 charLen = parse_utf8_char (&*here, endMinus1, &c);
53 } else if (c == '-') {
54 neg = true;
55 AddNChar (here, el.text, charLen);
56 charLen = parse_utf8_char (&*here, endMinus1, &c);
57 }
58
59 // read in number part
60 int numeric=0;
61 el.num = 0;
62 el.lexType = IntegerE;
63 /* stop integers at 4 digits */
64 while (c >= '0' && c <= '9'&& ++numeric<=MAXNUMERIC) {
65 el.num = el.num*10 + c - '0';
66 AddNChar (here, el.text, charLen);
67 charLen = parse_utf8_char (&*here, endMinus1, &c);
68 }
69
70 if (neg) el.num *= -1;
71
72 return (!el.text.empty());
73}
74
75static bool ParsePotentialInteger(UCArray::const_iterator &here,
76 UCArray::const_iterator end,
77 LexEl &el) {
78 el.Clear();
79
80 // this version of end is used in unitool
81 //UCArray::const_iterator endMinus1 = end-1;
82 const unsigned char* endMinus1 = &(*end)-1;
83
84 int charLen=0;
85 int length=0;
86 unsigned short c; // one character lookahead
87 charLen = parse_utf8_char (&*here, endMinus1, &c);
88
89 // read in number part
90 int numeric=0;
91 el.num = 0;
92 el.lexType = IntegerE;
93 /* stop integers at 4 digits */
94 while (c >= '0' && c <= '9'&& ++numeric<=MAXNUMERIC) {
95 el.num = el.num*10 + c - '0';
96 AddNChar (here, el.text, charLen);
97 length += charLen;
98 charLen = parse_utf8_char (&*here, endMinus1, &c);
99 }
100 // check the next character -if it is a letter, then have a term, not an integer
101 if (!is_unicode_letter(c)) {
102 // this was just an integer
103 return (!el.text.empty());
104 }
105 // else its a term
106 el.lexType = TermE;
107 el.num = 0;
108 /* this bit taken from ParseIndexWord in words.h*/
109 while (length+charLen<=MAXSTEMLEN && charLen > 0 &&
110 (is_unicode_letter(c) || (is_unicode_digit(c) &&
111 ++numeric <= MAXNUMERIC))) {
112 AddNChar (here, el.text, charLen);
113 length += charLen;
114 charLen = parse_utf8_char (&*here, endMinus1, &c);
115 }
116
117 return (!el.text.empty());
118}
119static bool ParseTerm (UCArray::const_iterator &here,
120 UCArray::const_iterator end,
121 UCArray &text) {
122 //UCArray::const_iterator endMinus1 = end-1;
123 const unsigned char* endMinus1 = &(*end)-1;
124 const unsigned char* new_here = ParseIndexWord (&*here, endMinus1, text);
125 here += (new_here - &*here); // advance iterator by number of chars advanced
126 return !text.empty();
127}
128
129
130bool ParseLexEl (UCArray::const_iterator &here,
131 UCArray::const_iterator end,
132 LexEl &el) {
133 el.Clear();
134
135 // strange things can happen if here == end == 0
136 if (here == end) return false;
137
138 // this version of end is used in unitool
139 //UCArray::const_iterator endMinus1 = end-1;
140 const unsigned char* endMinus1 = &(*end)-1;
141
142 // ignore all white space
143 int charLen;
144 unsigned short c; // one character lookahead
145 charLen = parse_utf8_char (&*here, endMinus1, &c);
146 while (here != end && is_unicode_space (c)) {
147 here += charLen;
148 charLen = parse_utf8_char (&*here, endMinus1, &c);
149 }
150 if (here == end) return false;
151
152 if (c == '(') {
153 el.lexType = OpenBracketE;
154 AddNChar (here, el.text, charLen);
155 return true;
156
157 } else if (c == ')') {
158 el.lexType = CloseBracketE;
159 AddNChar (here, el.text, charLen);
160 return true;
161
162 } else if (c =='[') {
163 el.lexType = OpenSquareBracketE;
164 AddNChar (here, el.text, charLen);
165 return true;
166
167 } else if (c ==']') {
168 el.lexType = CloseSquareBracketE;
169 AddNChar (here, el.text, charLen);
170 return true;
171
172 } else if (c == '\"') {
173 el.lexType = QuoteE;
174 AddNChar (here, el.text, charLen);
175 return true;
176
177 } else if (c == '/') {
178 el.lexType = TermWeightE;
179 AddNChar (here, el.text, charLen);
180 return true;
181
182 } else if (c == '#') {
183 el.lexType = StemMethodE;
184 AddNChar (here, el.text, charLen);
185 return true;
186
187 } else if (c == '^') {
188 el.lexType = RangeE;
189 AddNChar (here, el.text, charLen);
190 return true;
191
192 } else if (c == '@') {
193 el.lexType = AtE;
194 AddNChar (here, el.text, charLen);
195 return true;
196
197 } else if (c == ':') {
198 el.lexType = TagE;
199 AddNChar (here, el.text, charLen);
200 return true;
201
202 } else if (c=='&') {
203 el.lexType = AndOpE;
204 AddNChar (here, el.text, charLen);
205 return true;
206
207 } else if (c == '|') {
208 el.lexType = OrOpE;
209 AddNChar (here, el.text, charLen);
210 return true;
211
212 } else if (c == '!') {
213 el.lexType = NotOpE;
214 AddNChar (here, el.text, charLen);
215 return true;
216
217 } else if (c == '+' || c == '-' ) {
218 return ParseInteger (here, end, el);
219 }
220
221 else if (c >= '0' && c <= '9') {
222 return ParsePotentialInteger (here, end, el);
223 }
224
225 // assume it is a term of some sort
226 if (!ParseTerm (here, end, el.text)) return false;
227
228 UCArray AND; SetCStr (AND, "AND");
229 if (el.text == AND) {
230 el.lexType = AndOpE;
231 return true;
232 }
233 UCArray OR; SetCStr (OR, "OR");
234 if (el.text == OR) {
235 el.lexType = OrOpE;
236 return true;
237 }
238 UCArray NOT; SetCStr (NOT, "NOT");
239 if (el.text == NOT) {
240 el.lexType = NotOpE;
241 return true;
242 }
243 UCArray NEAR; SetCStr (NEAR, "NEAR");
244 if (PrefixLen(el.text, NEAR)==4) {
245 el.lexType = NearOpE;
246 return true;
247 }
248 el.lexType = TermE;
249 return true;
250}
251
Note: See TracBrowser for help on using the repository browser.