source: trunk/indexers/mgpp/text/QueryLex.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: 4.3 KB
Line 
1/**************************************************************************
2 *
3 * QueryLex.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 "QueryLex.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
43 int charLen;
44 unsigned short c; // one character lookahead
45 charLen = parse_utf8_char (here, endMinus1, &c);
46
47 // check for positive or negative
48 bool neg = false;
49 if (c == '+') {
50 AddNChar (here, el.text, charLen);
51 charLen = parse_utf8_char (here, endMinus1, &c);
52 } else if (c == '-') {
53 neg = true;
54 AddNChar (here, el.text, charLen);
55 charLen = parse_utf8_char (here, endMinus1, &c);
56 }
57
58 // read in number part
59 el.num = 0;
60 el.lexType = IntegerE;
61 while (c >= '0' && c <= '9') {
62 el.num = el.num*10 + c - '0';
63 AddNChar (here, el.text, charLen);
64 charLen = parse_utf8_char (here, endMinus1, &c);
65 }
66
67 if (neg) el.num *= -1;
68
69 return (!el.text.empty());
70}
71
72static bool ParseTerm (UCArray::const_iterator &here,
73 UCArray::const_iterator end,
74 UCArray &text) {
75 UCArray::const_iterator endMinus1 = end-1;
76 here = ParseIndexWord (here, endMinus1, text);
77 return !text.empty();
78}
79
80
81bool ParseLexEl (UCArray::const_iterator &here,
82 UCArray::const_iterator end,
83 LexEl &el) {
84 el.Clear();
85
86 // strange things can happen if here == end == 0
87 if (here == end) return false;
88
89 // this version of end is used in unitool
90 UCArray::const_iterator endMinus1 = end-1;
91
92 // ignore all white space
93 int charLen;
94 unsigned short c; // one character lookahead
95 charLen = parse_utf8_char (here, endMinus1, &c);
96 while (here != end && is_unicode_space (c)) {
97 here += charLen;
98 charLen = parse_utf8_char (here, endMinus1, &c);
99 }
100 if (here == end) return false;
101
102 if (c == '(') {
103 el.lexType = OpenBracketE;
104 AddNChar (here, el.text, charLen);
105 return true;
106
107 } else if (c == ')') {
108 el.lexType = CloseBracketE;
109 AddNChar (here, el.text, charLen);
110 return true;
111
112 } else if (c == '\"') {
113 el.lexType = QuoteE;
114 AddNChar (here, el.text, charLen);
115 return true;
116
117 } else if (c == '#') {
118 el.lexType = TermWeightE;
119 AddNChar (here, el.text, charLen);
120 return true;
121
122 } else if (c == '$') {
123 el.lexType = StemMethodE;
124 AddNChar (here, el.text, charLen);
125 return true;
126
127 } else if (c == '^') {
128 el.lexType = RangeE;
129 AddNChar (here, el.text, charLen);
130 return true;
131
132 } else if (c == '@') {
133 el.lexType = AtE;
134 AddNChar (here, el.text, charLen);
135 return true;
136
137 } else if (c == ':') {
138 el.lexType = TagE;
139 AddNChar (here, el.text, charLen);
140 return true;
141
142 } else if (c == '+' || c == '-' ||
143 (c >= '0' && c <= '9')) {
144 return ParseInteger (here, end, el);
145 }
146
147 // assume it is a term of some sort
148 if (!ParseTerm (here, end, el.text)) return false;
149
150 UCArray AND; SetCStr (AND, "AND");
151 if (el.text == AND) {
152 el.lexType = AndOpE;
153 return true;
154 }
155 UCArray OR; SetCStr (OR, "OR");
156 if (el.text == OR) {
157 el.lexType = OrOpE;
158 return true;
159 }
160 UCArray NOT; SetCStr (NOT, "NOT");
161 if (el.text == NOT) {
162 el.lexType = NotOpE;
163 return true;
164 }
165
166 el.lexType = TermE;
167 return true;
168}
169
Note: See TracBrowser for help on using the repository browser.