1 // $Id: CQLParser.java,v 1.16 2002-11-06 20:13:45 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.16 2002-11-06 20:13:45 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("="));
50 if (lexer.ttype != lexer.TT_EOF)
51 throw new CQLParseException("junk after end: " + lexer.render());
56 private CQLNode parse_query(String qualifier, CQLRelation relation)
57 throws CQLParseException, IOException {
58 debug("in parse_query()");
60 CQLNode term = parse_term(qualifier, relation);
61 while (lexer.ttype != lexer.TT_EOF &&
63 if (lexer.ttype == lexer.TT_AND) {
65 CQLNode term2 = parse_term(qualifier, relation);
66 term = new CQLAndNode(term, term2);
67 } else if (lexer.ttype == lexer.TT_OR) {
69 CQLNode term2 = parse_term(qualifier, relation);
70 term = new CQLOrNode(term, term2);
71 } else if (lexer.ttype == lexer.TT_NOT) {
73 CQLNode term2 = parse_term(qualifier, relation);
74 term = new CQLNotNode(term, term2);
75 } else if (lexer.ttype == lexer.TT_PROX) {
77 CQLProxNode proxnode = new CQLProxNode(term);
78 gatherProxParameters(proxnode);
79 CQLNode term2 = parse_term(qualifier, relation);
80 proxnode.addSecondSubterm(term2);
81 term = (CQLNode) proxnode;
83 throw new CQLParseException("expected boolean, got " +
92 private CQLNode parse_term(String qualifier, CQLRelation relation)
93 throws CQLParseException, IOException {
94 debug("in parse_term()");
98 if (lexer.ttype == '(') {
99 debug("parenthesised term");
101 CQLNode expr = parse_query(qualifier, relation);
104 } else if (lexer.ttype != lexer.TT_WORD &&
105 lexer.ttype != lexer.TT_NUMBER &&
106 lexer.ttype != '"') {
107 throw new CQLParseException("expected qualifier or term, " +
108 "got " + lexer.render());
111 debug("non-parenthesised term");
112 if (lexer.ttype == lexer.TT_NUMBER) {
113 word = lexer.render();
118 if (!isBaseRelation())
122 relation = new CQLRelation(lexer.render(lexer.ttype, false));
125 while (lexer.ttype == '/') {
127 if (lexer.ttype != lexer.TT_RELEVANT &&
128 lexer.ttype != lexer.TT_FUZZY &&
129 lexer.ttype != lexer.TT_STEM)
130 throw new CQLParseException("expected relation modifier, "
131 + "got " + lexer.render());
132 relation.addModifier(lexer.sval);
136 debug("qualifier='" + qualifier + ", " +
137 "relation='" + relation.toCQL() + "'");
140 CQLTermNode node = new CQLTermNode(qualifier, relation, word);
141 debug("made term node " + node.toCQL());
145 private void gatherProxParameters(CQLProxNode node)
146 throws CQLParseException, IOException {
147 for (int i = 0; i < 4; i++) {
148 if (lexer.ttype != '/')
149 return; // end of proximity parameters
152 if (lexer.ttype != '/') {
153 // not an omitted default
155 // Order should be: relation/distance/unit/ordering
156 // For now, use MA's: unit/relation/distance/ordering
157 case 0: gatherProxRelation(node); break;
158 case 1: gatherProxDistance(node); break;
159 case 2: gatherProxUnit(node); break;
160 case 3: gatherProxOrdering(node); break;
166 private void gatherProxRelation(CQLProxNode node)
167 throws CQLParseException, IOException {
168 if (!isProxRelation())
169 throw new CQLParseException("expected proximity relation, got " +
171 node.addModifier("relation", lexer.render(lexer.ttype, false));
173 debug("gPR matched " + lexer.render(lexer.ttype, false));
176 private void gatherProxDistance(CQLProxNode node)
177 throws CQLParseException, IOException {
178 if (lexer.ttype != lexer.TT_NUMBER)
179 throw new CQLParseException("expected proximity distance, got " +
181 node.addModifier("distance", lexer.render(lexer.ttype, false));
183 debug("gPD matched " + lexer.render(lexer.ttype, false));
186 private void gatherProxUnit(CQLProxNode node)
187 throws CQLParseException, IOException {
188 if (lexer.ttype != lexer.TT_pWORD &&
189 lexer.ttype != lexer.TT_SENTENCE &&
190 lexer.ttype != lexer.TT_PARAGRAPH &&
191 lexer.ttype != lexer.TT_ELEMENT)
192 throw new CQLParseException("expected proximity unit, got " +
194 node.addModifier("unit", lexer.render());
198 private void gatherProxOrdering(CQLProxNode node)
199 throws CQLParseException, IOException {
200 if (lexer.ttype != lexer.TT_ORDERED &&
201 lexer.ttype != lexer.TT_UNORDERED)
202 throw new CQLParseException("expected proximity ordering, got " +
204 node.addModifier("ordering", lexer.render());
208 private boolean isBaseRelation() {
209 debug("isBaseRelation: checking ttype=" + lexer.ttype +
210 " (" + lexer.render() + ")");
211 return (isProxRelation() ||
212 lexer.ttype == lexer.TT_ANY ||
213 lexer.ttype == lexer.TT_ALL ||
214 lexer.ttype == lexer.TT_EXACT);
217 private boolean isProxRelation() {
218 debug("isProxRelation: checking ttype=" + lexer.ttype +
219 " (" + lexer.render() + ")");
220 return (lexer.ttype == '<' ||
221 lexer.ttype == '>' ||
222 lexer.ttype == '=' ||
223 lexer.ttype == lexer.TT_LE ||
224 lexer.ttype == lexer.TT_GE ||
225 lexer.ttype == lexer.TT_NE);
228 private void match(int token)
229 throws CQLParseException, IOException {
230 debug("in match(" + lexer.render(token, true) + ")");
231 if (lexer.ttype != token)
232 throw new CQLParseException("expected " +
233 lexer.render(token, true) +
234 ", " + "got " + lexer.render());
235 int tmp = lexer.nextToken();
236 debug("match() got token=" + lexer.ttype + ", " +
237 "nval=" + lexer.nval + ", sval='" + lexer.sval + "'" +
238 " (tmp=" + tmp + ")");
243 * Simple test-harness for the CQLParser class.
245 * Reads a CQL query either from its command-line argument, if
246 * there is one, or standard input otherwise. So these two
247 * invocations are equivalent:
249 * CQLParser 'au=(Kerninghan or Ritchie) and ti=Unix'
250 * echo au=(Kerninghan or Ritchie) and ti=Unix | CQLParser
252 * The test-harness parses the supplied query and renders is as
253 * XCQL, so that both of the invocations above produce the
258 * <value>and</value>
262 * <value>or</value>
264 * <searchClause>
265 * <index>au</index>
267 * <value>=</value>
269 * <term>Kerninghan</term>
270 * </searchClause>
271 * <searchClause>
272 * <index>au</index>
274 * <value>=</value>
276 * <term>Ritchie</term>
277 * </searchClause>
279 * <searchClause>
280 * <index>ti</index>
282 * <value>=</value>
284 * <term>Unix</term>
285 * </searchClause>
290 * Causes the output to be written in CQL rather than XCQL - that
291 * is, a query equivalent to that which was input, is output. In
292 * effect, the test harness acts as a query canonicaliser.
294 * The input query, either as XCQL [default] or CQL [if the
295 * <TT>-c</TT> option is supplied].
297 public static void main (String[] args) {
298 char mode = 'x'; // x=XCQL, c=CQL, p=PQF
301 Vector argv = new Vector();
302 for (int i = 0; i < args.length; i++) {
306 if (argv.size() > 0 && argv.get(0).equals("-c")) {
309 } else if (argv.size() > 1 && argv.get(0).equals("-p")) {
312 pfile = (String) argv.get(0);
316 if (argv.size() > 1) {
318 "Usage: CQLParser [-c] [-p <pqf-properties> [<CQL-query>]");
319 System.err.println("If unspecified, query is read from stdin");
324 if (argv.size() == 1) {
325 cql = (String) argv.get(0);
327 byte[] bytes = new byte[10000];
329 // Read in the whole of standard input in one go
330 int nbytes = System.in.read(bytes);
331 } catch (IOException ex) {
332 System.err.println("Can't read query: " + ex.getMessage());
335 cql = new String(bytes);
338 CQLParser parser = new CQLParser();
341 root = parser.parse(cql);
342 } catch (CQLParseException ex) {
343 System.err.println("Syntax error: " + ex.getMessage());
345 } catch (IOException ex) {
346 System.err.println("Can't compile query: " + ex.getMessage());
352 System.out.println(root.toCQL());
353 } else if (mode == 'p') {
354 InputStream f = new FileInputStream(pfile);
356 throw new FileNotFoundException(pfile);
358 Properties config = new Properties();
361 System.out.println(root.toPQF(config));
363 System.out.print(root.toXCQL(0));
365 } catch (IOException ex) {
366 System.err.println("Can't render query: " + ex.getMessage());
368 } catch (UnknownQualifierException ex) {
369 System.err.println("Unknown qualifier: " + ex.getMessage());
371 } catch (UnknownRelationException ex) {
372 System.err.println("Unknown relation: " + ex.getMessage());
374 } catch (UnknownRelationModifierException ex) {
375 System.err.println("Unknown relation modifier: " +
378 } catch (UnknownPositionException ex) {
379 System.err.println("Unknown position: " + ex.getMessage());
381 } catch (PQFTranslationException ex) {
382 // We catch all of this class's subclasses, so --
383 throw new Error("can't get a PQFTranslationException");