1 // $Id: CQLParser.java,v 1.18 2002-11-08 01:10:55 mike Exp $
3 package org.z3950.zing.cql;
4 import java.io.IOException;
5 import java.util.Vector;
6 import java.util.Properties;
7 import java.io.InputStream;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
13 * Compiles CQL strings into parse trees of CQLNode subtypes.
15 * @version $Id: CQLParser.java,v 1.18 2002-11-08 01:10:55 mike Exp $
16 * @see <A href="http://zing.z3950.org/cql/index.html"
17 * >http://zing.z3950.org/cql/index.html</A>
19 public class CQLParser {
20 private CQLLexer lexer;
21 static private boolean DEBUG = false;
22 static private boolean LEXDEBUG = false;
24 private static void debug(String str) {
26 System.err.println("PARSEDEBUG: " + str);
30 * Compiles a CQL query.
32 * The resulting parse tree may be further processed by hand (see
33 * the individual node-types' documentation for details on the
34 * data structure) or, more often, simply rendered out in the
35 * desired form using one of the back-ends. <TT>toCQL()</TT>
36 * returns a decompiled CQL query equivalent to the one that was
37 * compiled in the first place; and <TT>toXCQL()</TT> returns an
38 * XML snippet representing the query.
40 * @param cql The query
41 * @return A CQLNode object which is the root of a parse
42 * tree representing the query. */
43 public CQLNode parse(String cql)
44 throws CQLParseException, IOException {
45 lexer = new CQLLexer(cql, LEXDEBUG);
48 debug("about to parse_query()");
49 CQLNode root = parse_query("srw.serverChoice", new CQLRelation("scr"));
50 // ### "scr" above should arguably be "="
51 if (lexer.ttype != lexer.TT_EOF)
52 throw new CQLParseException("junk after end: " + lexer.render());
57 private CQLNode parse_query(String qualifier, CQLRelation relation)
58 throws CQLParseException, IOException {
59 debug("in parse_query()");
61 CQLNode term = parse_term(qualifier, relation);
62 while (lexer.ttype != lexer.TT_EOF &&
64 if (lexer.ttype == lexer.TT_AND) {
66 CQLNode term2 = parse_term(qualifier, relation);
67 term = new CQLAndNode(term, term2);
68 } else if (lexer.ttype == lexer.TT_OR) {
70 CQLNode term2 = parse_term(qualifier, relation);
71 term = new CQLOrNode(term, term2);
72 } else if (lexer.ttype == lexer.TT_NOT) {
74 CQLNode term2 = parse_term(qualifier, relation);
75 term = new CQLNotNode(term, term2);
76 } else if (lexer.ttype == lexer.TT_PROX) {
78 CQLProxNode proxnode = new CQLProxNode(term);
79 gatherProxParameters(proxnode);
80 CQLNode term2 = parse_term(qualifier, relation);
81 proxnode.addSecondSubterm(term2);
82 term = (CQLNode) proxnode;
84 throw new CQLParseException("expected boolean, got " +
93 private CQLNode parse_term(String qualifier, CQLRelation relation)
94 throws CQLParseException, IOException {
95 debug("in parse_term()");
99 if (lexer.ttype == '(') {
100 debug("parenthesised term");
102 CQLNode expr = parse_query(qualifier, relation);
105 } else if (lexer.ttype != lexer.TT_WORD &&
106 lexer.ttype != lexer.TT_NUMBER &&
107 lexer.ttype != '"') {
108 throw new CQLParseException("expected qualifier or term, " +
109 "got " + lexer.render());
112 debug("non-parenthesised term");
113 if (lexer.ttype == lexer.TT_NUMBER) {
114 word = lexer.render();
119 if (!isBaseRelation())
123 relation = new CQLRelation(lexer.render(lexer.ttype, false));
126 while (lexer.ttype == '/') {
128 if (lexer.ttype != lexer.TT_RELEVANT &&
129 lexer.ttype != lexer.TT_FUZZY &&
130 lexer.ttype != lexer.TT_STEM)
131 throw new CQLParseException("expected relation modifier, "
132 + "got " + lexer.render());
133 relation.addModifier(lexer.sval);
137 debug("qualifier='" + qualifier + ", " +
138 "relation='" + relation.toCQL() + "'");
141 CQLTermNode node = new CQLTermNode(qualifier, relation, word);
142 debug("made term node " + node.toCQL());
146 private void gatherProxParameters(CQLProxNode node)
147 throws CQLParseException, IOException {
148 for (int i = 0; i < 4; i++) {
149 if (lexer.ttype != '/')
150 return; // end of proximity parameters
153 if (lexer.ttype != '/') {
154 // not an omitted default
156 // Order should be: relation/distance/unit/ordering
157 // For now, use MA's: unit/relation/distance/ordering
158 case 0: gatherProxRelation(node); break;
159 case 1: gatherProxDistance(node); break;
160 case 2: gatherProxUnit(node); break;
161 case 3: gatherProxOrdering(node); break;
167 private void gatherProxRelation(CQLProxNode node)
168 throws CQLParseException, IOException {
169 if (!isProxRelation())
170 throw new CQLParseException("expected proximity relation, got " +
172 node.addModifier("relation", lexer.render(lexer.ttype, false));
174 debug("gPR matched " + lexer.render(lexer.ttype, false));
177 private void gatherProxDistance(CQLProxNode node)
178 throws CQLParseException, IOException {
179 if (lexer.ttype != lexer.TT_NUMBER)
180 throw new CQLParseException("expected proximity distance, got " +
182 node.addModifier("distance", lexer.render(lexer.ttype, false));
184 debug("gPD matched " + lexer.render(lexer.ttype, false));
187 private void gatherProxUnit(CQLProxNode node)
188 throws CQLParseException, IOException {
189 if (lexer.ttype != lexer.TT_pWORD &&
190 lexer.ttype != lexer.TT_SENTENCE &&
191 lexer.ttype != lexer.TT_PARAGRAPH &&
192 lexer.ttype != lexer.TT_ELEMENT)
193 throw new CQLParseException("expected proximity unit, got " +
195 node.addModifier("unit", lexer.render());
199 private void gatherProxOrdering(CQLProxNode node)
200 throws CQLParseException, IOException {
201 if (lexer.ttype != lexer.TT_ORDERED &&
202 lexer.ttype != lexer.TT_UNORDERED)
203 throw new CQLParseException("expected proximity ordering, got " +
205 node.addModifier("ordering", lexer.render());
209 private boolean isBaseRelation() {
210 debug("isBaseRelation: checking ttype=" + lexer.ttype +
211 " (" + lexer.render() + ")");
212 return (isProxRelation() ||
213 lexer.ttype == lexer.TT_ANY ||
214 lexer.ttype == lexer.TT_ALL ||
215 lexer.ttype == lexer.TT_EXACT);
218 private boolean isProxRelation() {
219 debug("isProxRelation: checking ttype=" + lexer.ttype +
220 " (" + lexer.render() + ")");
221 return (lexer.ttype == '<' ||
222 lexer.ttype == '>' ||
223 lexer.ttype == '=' ||
224 lexer.ttype == lexer.TT_LE ||
225 lexer.ttype == lexer.TT_GE ||
226 lexer.ttype == lexer.TT_NE);
229 private void match(int token)
230 throws CQLParseException, IOException {
231 debug("in match(" + lexer.render(token, true) + ")");
232 if (lexer.ttype != token)
233 throw new CQLParseException("expected " +
234 lexer.render(token, true) +
235 ", " + "got " + lexer.render());
236 int tmp = lexer.nextToken();
237 debug("match() got token=" + lexer.ttype + ", " +
238 "nval=" + lexer.nval + ", sval='" + lexer.sval + "'" +
239 " (tmp=" + tmp + ")");
244 * Simple test-harness for the CQLParser class.
246 * Reads a CQL query either from its command-line argument, if
247 * there is one, or standard input otherwise. So these two
248 * invocations are equivalent:
250 * CQLParser 'au=(Kerninghan or Ritchie) and ti=Unix'
251 * echo au=(Kerninghan or Ritchie) and ti=Unix | CQLParser
253 * The test-harness parses the supplied query and renders is as
254 * XCQL, so that both of the invocations above produce the
259 * <value>and</value>
263 * <value>or</value>
265 * <searchClause>
266 * <index>au</index>
268 * <value>=</value>
270 * <term>Kerninghan</term>
271 * </searchClause>
272 * <searchClause>
273 * <index>au</index>
275 * <value>=</value>
277 * <term>Ritchie</term>
278 * </searchClause>
280 * <searchClause>
281 * <index>ti</index>
283 * <value>=</value>
285 * <term>Unix</term>
286 * </searchClause>
291 * Causes the output to be written in CQL rather than XCQL - that
292 * is, a query equivalent to that which was input, is output. In
293 * effect, the test harness acts as a query canonicaliser.
295 * The input query, either as XCQL [default] or CQL [if the
296 * <TT>-c</TT> option is supplied].
298 public static void main (String[] args) {
299 char mode = 'x'; // x=XCQL, c=CQL, p=PQF
302 Vector argv = new Vector();
303 for (int i = 0; i < args.length; i++) {
307 if (argv.size() > 0 && argv.get(0).equals("-c")) {
310 } else if (argv.size() > 1 && argv.get(0).equals("-p")) {
313 pfile = (String) argv.get(0);
317 if (argv.size() > 1) {
319 "Usage: CQLParser [-c] [-p <pqf-properties> [<CQL-query>]");
320 System.err.println("If unspecified, query is read from stdin");
325 if (argv.size() == 1) {
326 cql = (String) argv.get(0);
328 byte[] bytes = new byte[10000];
330 // Read in the whole of standard input in one go
331 int nbytes = System.in.read(bytes);
332 } catch (IOException ex) {
333 System.err.println("Can't read query: " + ex.getMessage());
336 cql = new String(bytes);
339 CQLParser parser = new CQLParser();
342 root = parser.parse(cql);
343 } catch (CQLParseException ex) {
344 System.err.println("Syntax error: " + ex.getMessage());
346 } catch (IOException ex) {
347 System.err.println("Can't compile query: " + ex.getMessage());
353 System.out.println(root.toCQL());
354 } else if (mode == 'p') {
355 InputStream f = new FileInputStream(pfile);
357 throw new FileNotFoundException(pfile);
359 Properties config = new Properties();
362 System.out.println(root.toPQF(config));
364 System.out.print(root.toXCQL(0));
366 } catch (IOException ex) {
367 System.err.println("Can't render query: " + ex.getMessage());
369 } catch (UnknownQualifierException ex) {
370 System.err.println("Unknown qualifier: " + ex.getMessage());
372 } catch (UnknownRelationException ex) {
373 System.err.println("Unknown relation: " + ex.getMessage());
375 } catch (UnknownRelationModifierException ex) {
376 System.err.println("Unknown relation modifier: " +
379 } catch (UnknownPositionException ex) {
380 System.err.println("Unknown position: " + ex.getMessage());
382 } catch (PQFTranslationException ex) {
383 // We catch all of this class's subclasses, so --
384 throw new Error("can't get a PQFTranslationException");