Rewrite lexer to comply with CQL spec
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLParser.java
1
2 package org.z3950.zing.cql;
3 import java.io.IOException;
4 import java.util.Properties;
5 import java.io.InputStream;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.Reader;
9 import java.io.StringReader;
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15
16 /**
17  * Compiles CQL strings into parse trees of CQLNode subtypes.
18  *
19  * @see         <A href="http://zing.z3950.org/cql/index.html"
20  *                      >http://zing.z3950.org/cql/index.html</A>
21  */
22 public class CQLParser {
23     private CQLLexer lexer;
24     private final int compat;   // When false, implement CQL 1.2
25     private final Set<String> customRelations = new HashSet<String>();
26     
27     public static final int V1POINT1 = 12368;
28     public static final int V1POINT2 = 12369;
29     public static final int V1POINT1SORT = 12370;
30     public final boolean allowKeywordTerms;
31
32     static private boolean DEBUG = false;
33     static private boolean LEXDEBUG = false;
34
35     /**
36      * The new parser implements a dialect of CQL specified by the
37      * <tt>compat</tt> argument:
38      * <ul>
39      *  <li>V1POINT1 - CQL version 1.1
40      *  </li>
41      *  <li>V1POINT2 - CQL version 1.2
42      *  </li>
43      *  <li>V1POINT1SORT - CQL version 1.1 but including
44      *          <tt>sortby</tt> as specified for CQL 1.2.
45      *  </li>
46      * </ul>
47      */
48     public CQLParser(int compat) {
49         this.compat = compat;
50         this.allowKeywordTerms = true;
51     }
52     
53     /**
54      * Official CQL grammar allows registered keywords like 'and/or/not/sortby/prox' 
55      * to be used unquoted in terms. This constructor allows to create an instance 
56      * of a parser that prohibits this behavior while sacrificing compatibility.
57      * @param compat CQL version compatibility
58      * @param allowKeywordTerms when false registered keywords are disallowed in unquoted terms
59      */
60     public CQLParser(int compat, boolean allowKeywordTerms) {
61         this.compat = compat;
62         this.allowKeywordTerms = allowKeywordTerms;
63     }
64     
65     /**
66      * The new parser implements CQL 1.2
67      */
68     public CQLParser() {
69         this.compat = V1POINT2;
70         this.allowKeywordTerms = true;
71     }
72
73     private static void debug(String str) {
74         if (DEBUG)
75             System.err.println("PARSEDEBUG: " + str);
76     }
77     
78     /**
79      * Registers custom relation in this parser. Note that when a custom relation
80      * is registered the parser is no longer strictly compliant with the chosen spec.
81      * @param relation
82      * @return true if custom relation has not been registered already
83      */
84     public boolean registerCustomRelation(String relation) {
85       return customRelations.add(relation);
86     }
87     
88     /**
89      * Unregisters previously registered custom relation in this instance of the parser.
90      * @param relation
91      * @return true is relation has been previously registered
92      */
93     public boolean unregisterCustomRelation(String relation) {
94       return customRelations.remove(relation);
95     }
96
97     /**
98      * Compiles a CQL query.
99      * <P>
100      * The resulting parse tree may be further processed by hand (see
101      * the individual node-types' documentation for details on the
102      * data structure) or, more often, simply rendered out in the
103      * desired form using one of the back-ends.  <TT>toCQL()</TT>
104      * returns a decompiled CQL query equivalent to the one that was
105      * compiled in the first place; <TT>toXCQL()</TT> returns an
106      * XML snippet representing the query; and <TT>toPQF()</TT>
107      * returns the query rendered in Index Data's Prefix Query
108      * Format.
109      *
110      * @param cql       The query
111      * @return          A CQLNode object which is the root of a parse
112      * tree representing the query.  */
113     public CQLNode parse(String cql)
114         throws CQLParseException, IOException {
115         lexer = new CQLLexerSimple(cql, LEXDEBUG);
116
117         lexer.move();
118         debug("about to parseQuery()");
119         CQLNode root = parseTopLevelPrefixes("cql.serverChoice",
120                 new CQLRelation(compat == V1POINT2 ? "=" : "scr"));
121         if (lexer.what() != CQLLexer.TT_EOF)
122             throw new CQLParseException("junk after end: " + lexer.render(), 
123               lexer.pos());
124
125         return root;
126     }
127
128     private CQLNode parseTopLevelPrefixes(String index, CQLRelation relation)
129         throws CQLParseException, IOException {
130         debug("top-level prefix mapping");
131
132         if (lexer.what() == '>') {
133             return parsePrefix(index, relation, true);
134         }
135
136         CQLNode node = parseQuery(index, relation);
137         if ((compat == V1POINT2 || compat == V1POINT1SORT) &&
138             lexer.what() == CQLLexer.TT_SORTBY) {
139             match(lexer.what());
140             debug("sortspec");
141
142             CQLSortNode sortnode = new CQLSortNode(node);
143             while (lexer.what() != CQLLexer.TT_EOF) {
144                 String sortindex = matchSymbol("sort index");
145                 ModifierSet ms = gatherModifiers(sortindex);
146                 sortnode.addSortIndex(ms);
147             }
148
149             if (sortnode.keys.size() == 0) {
150                 throw new CQLParseException("no sort keys", lexer.pos());
151             }
152
153             node = sortnode;
154         }
155
156         return node;
157     }
158
159     private CQLNode parseQuery(String index, CQLRelation relation)
160         throws CQLParseException, IOException {
161         debug("in parseQuery()");
162
163         CQLNode term = parseTerm(index, relation);
164         while (lexer.what() != CQLLexer.TT_EOF &&
165                lexer.what() != ')' &&
166                lexer.what() != CQLLexer.TT_SORTBY) {
167             if (lexer.what() == CQLLexer.TT_AND ||
168                 lexer.what() == CQLLexer.TT_OR ||
169                 lexer.what() == CQLLexer.TT_NOT ||
170                 lexer.what() == CQLLexer.TT_PROX) {
171                 int type = lexer.what();
172                 String val = lexer.value();
173                 match(type);
174                 ModifierSet ms = gatherModifiers(val);
175                 CQLNode term2 = parseTerm(index, relation);
176                 term = ((type == CQLLexer.TT_AND) ? new CQLAndNode(term, term2, ms) :
177                         (type == CQLLexer.TT_OR)  ? new CQLOrNode (term, term2, ms) :
178                         (type == CQLLexer.TT_NOT) ? new CQLNotNode(term, term2, ms) :
179                                                  new CQLProxNode(term, term2, ms));
180             } else {
181                 throw new CQLParseException("expected boolean, got " +
182                                             lexer.render(), lexer.pos());
183             }
184         }
185
186         debug("no more ops");
187         return term;
188     }
189
190     private ModifierSet gatherModifiers(String base)
191         throws CQLParseException, IOException {
192         debug("in gatherModifiers()");
193
194         ModifierSet ms = new ModifierSet(base);
195         while (lexer.what() == '/') {
196             match('/');
197             if (lexer.what() != CQLLexer.TT_WORD)
198                 throw new CQLParseException("expected modifier, "
199                                             + "got " + lexer.render(), 
200                   lexer.pos());
201             String type = lexer.value().toLowerCase();
202             match(lexer.what());
203             if (!isSymbolicRelation()) {
204                 // It's a simple modifier consisting of type only
205                 ms.addModifier(type);
206             } else {
207                 // It's a complex modifier of the form type=value
208                 String comparision = lexer.render(lexer.what(), false);
209                 match(lexer.what());
210                 String value = matchSymbol("modifier value");
211                 ms.addModifier(type, comparision, value);
212             }
213         }
214
215         return ms;
216     }
217
218     private CQLNode parseTerm(String index, CQLRelation relation)
219         throws CQLParseException, IOException {
220         debug("in parseTerm()");
221
222         String word;
223         while (true) {
224             if (lexer.what() == '(') {
225                 debug("parenthesised term");
226                 match('(');
227                 CQLNode expr = parseQuery(index, relation);
228                 match(')');
229                 return expr;
230             } else if (lexer.what() == '>') {
231                 return parsePrefix(index, relation, false);
232             }
233
234             debug("non-parenthesised term");
235             word = matchSymbol("index or term");
236             while (lexer.what() == CQLLexer.TT_WORD && !isRelation()) {
237               word = word + " " + lexer.value();
238               match(CQLLexer.TT_WORD);
239             }
240
241             if (!isRelation())
242                 break;
243
244             index = word;
245             String relstr = (lexer.what() == CQLLexer.TT_WORD ?
246                              lexer.value() : lexer.render(lexer.what(), false));
247             relation = new CQLRelation(relstr);
248             match(lexer.what());
249             ModifierSet ms = gatherModifiers(relstr);
250             relation.ms = ms;
251             debug("index='" + index + ", " +
252                   "relation='" + relation.toCQL() + "'");
253         }
254
255         CQLTermNode node = new CQLTermNode(index, relation, word);
256         debug("made term node " + node.toCQL());
257         return node;
258     }
259
260     private CQLNode parsePrefix(String index, CQLRelation relation,
261                                 boolean topLevel)
262         throws CQLParseException, IOException {
263         debug("prefix mapping");
264
265         match('>');
266         String name = null;
267         String identifier = matchSymbol("prefix-name");
268         if (lexer.what() == '=') {
269             match('=');
270             name = identifier;
271             identifier = matchSymbol("prefix-identifer");
272         }
273         CQLNode node = topLevel ?
274             parseTopLevelPrefixes(index, relation) :
275             parseQuery(index, relation);
276
277         return new CQLPrefixNode(name, identifier, node);
278     }
279
280     private boolean isRelation() {
281         debug("isRelation: checking what()=" + lexer.what() +
282               " (" + lexer.render() + ")");
283         if (lexer.what() == CQLLexer.TT_WORD &&
284             (lexer.value().indexOf('.') >= 0 ||
285              lexer.value().equals("any") ||
286              lexer.value().equals("all") ||
287              lexer.value().equals("within") ||
288              lexer.value().equals("encloses") ||
289              (lexer.value().equals("exact") && compat != V1POINT2) ||
290              (lexer.value().equals("scr") && compat != V1POINT2) ||
291              (lexer.value().equals("adj") && compat == V1POINT2) ||
292              customRelations.contains(lexer.value())))
293           return true;
294
295         return isSymbolicRelation();
296     }
297
298     private boolean isSymbolicRelation() {
299         debug("isSymbolicRelation: checking what()=" + lexer.what() +
300               " (" + lexer.render() + ")");
301         return (lexer.what() == '<' ||
302                 lexer.what() == '>' ||
303                 lexer.what() == '=' ||
304                 lexer.what() == CQLLexer.TT_LE ||
305                 lexer.what() == CQLLexer.TT_GE ||
306                 lexer.what() == CQLLexer.TT_NE ||
307                 lexer.what() == CQLLexer.TT_EQEQ);
308     }
309
310     private void match(int token)
311         throws CQLParseException, IOException {
312         debug("in match(" + lexer.render(token, true) + ")");
313         if (lexer.what() != token)
314             throw new CQLParseException("expected " +
315                                         lexer.render(token, true) +
316                                         ", " + "got " + lexer.render(), 
317               lexer.pos());
318         lexer.move();
319         debug("match() got token=" + lexer.what() + ", value()='" + lexer.value() + "'");
320     }
321
322     private String matchSymbol(String expected)
323         throws CQLParseException, IOException {
324
325         debug("in matchSymbol()");
326         if (lexer.what() == CQLLexer.TT_WORD ||
327             lexer.what() == '"' ||
328             // The following is a complete list of keywords.  Because
329             // they're listed here, they can be used unquoted as
330             // indexes, terms, prefix names and prefix identifiers.
331             // ### Instead, we should ask the lexer whether what we
332             // have is a keyword, and let the knowledge reside there.
333             (allowKeywordTerms &&
334             lexer.what() == CQLLexer.TT_AND ||
335             lexer.what() == CQLLexer.TT_OR ||
336             lexer.what() == CQLLexer.TT_NOT ||
337             lexer.what() == CQLLexer.TT_PROX ||
338             lexer.what() == CQLLexer.TT_SORTBY)) {
339             String symbol = lexer.value();
340             match(lexer.what());
341             return symbol;
342         }
343
344         throw new CQLParseException("expected " + expected + ", " +
345                                     "got " + lexer.render(), lexer.pos());
346     }
347
348
349     /**
350      * Simple test-harness for the CQLParser class.
351      * <P>
352      * Reads a CQL query either from its command-line argument, if
353      * there is one, or standard input otherwise.  So these two
354      * invocations are equivalent:
355      * <PRE>
356      *  CQLParser 'au=(Kerninghan or Ritchie) and ti=Unix'
357      *  echo au=(Kerninghan or Ritchie) and ti=Unix | CQLParser
358      * </PRE>
359      * The test-harness parses the supplied query and renders is as
360      * XCQL, so that both of the invocations above produce the
361      * following output:
362      * <PRE>
363      *  &lt;triple&gt;
364      *    &lt;boolean&gt;
365      *      &lt;value&gt;and&lt;/value&gt;
366      *    &lt;/boolean&gt;
367      *    &lt;triple&gt;
368      *      &lt;boolean&gt;
369      *        &lt;value&gt;or&lt;/value&gt;
370      *      &lt;/boolean&gt;
371      *      &lt;searchClause&gt;
372      *        &lt;index&gt;au&lt;/index&gt;
373      *        &lt;relation&gt;
374      *          &lt;value&gt;=&lt;/value&gt;
375      *        &lt;/relation&gt;
376      *        &lt;term&gt;Kerninghan&lt;/term&gt;
377      *      &lt;/searchClause&gt;
378      *      &lt;searchClause&gt;
379      *        &lt;index&gt;au&lt;/index&gt;
380      *        &lt;relation&gt;
381      *          &lt;value&gt;=&lt;/value&gt;
382      *        &lt;/relation&gt;
383      *        &lt;term&gt;Ritchie&lt;/term&gt;
384      *      &lt;/searchClause&gt;
385      *    &lt;/triple&gt;
386      *    &lt;searchClause&gt;
387      *      &lt;index&gt;ti&lt;/index&gt;
388      *      &lt;relation&gt;
389      *        &lt;value&gt;=&lt;/value&gt;
390      *      &lt;/relation&gt;
391      *      &lt;term&gt;Unix&lt;/term&gt;
392      *    &lt;/searchClause&gt;
393      *  &lt;/triple&gt;
394      * </PRE>
395      * <P>
396      * @param -1
397      *  CQL version 1.1 (default version 1.2)
398      * @param -d
399      *  Debug mode: extra output written to stderr.
400      * @param -c
401      *  Causes the output to be written in CQL rather than XCQL - that
402      *  is, a query equivalent to that which was input, is output.  In
403      *  effect, the test harness acts as a query canonicaliser.
404      * @return
405      *  The input query, either as XCQL [default] or CQL [if the
406      *  <TT>-c</TT> option is supplied].
407      */
408     public static void main (String[] args) {
409         char mode = 'x';        // x=XCQL, c=CQL, p=PQF
410         String pfile = null;
411
412         List<String> argv = new ArrayList<String>();
413         for (int i = 0; i < args.length; i++) {
414             argv.add(args[i]);
415         }
416
417         int compat = V1POINT2;
418         if (argv.size() > 0 && argv.get(0).equals("-1")) {
419             compat = V1POINT1;
420             argv.remove(0);
421         }
422
423         if (argv.size() > 0 && argv.get(0).equals("-d")) {
424             DEBUG = true;
425             argv.remove(0);
426         }
427
428         if (argv.size() > 0 && argv.get(0).equals("-c")) {
429             mode = 'c';
430             argv.remove(0);
431         } else if (argv.size() > 1 && argv.get(0).equals("-p")) {
432             mode = 'p';
433             argv.remove(0);
434             pfile = (String) argv.get(0);
435             argv.remove(0);
436         }
437
438         if (argv.size() > 1) {
439             System.err.println("Usage: CQLParser [-1] [-d] [-c] " +
440                                "[-p <pqf-properties> [<CQL-query>]");
441             System.err.println("If unspecified, query is read from stdin");
442             System.exit(1);
443         }
444
445         String cql;
446         if (argv.size() == 1) {
447             cql = (String) argv.get(0);
448         } else {
449             byte[] bytes = new byte[10000];
450             try {
451                 // Read in the whole of standard input in one go
452                 int nbytes = System.in.read(bytes);
453             } catch (IOException ex) {
454                 System.err.println("Can't read query: " + ex.getMessage());
455                 System.exit(2);
456             }
457             cql = new String(bytes);
458         }
459
460         CQLParser parser = new CQLParser(compat);
461         CQLNode root = null;
462         try {
463             root = parser.parse(cql);
464         } catch (CQLParseException ex) {
465             System.err.println("Syntax error: " + ex.getMessage());
466             System.exit(3);
467         } catch (IOException ex) {
468             System.err.println("Can't compile query: " + ex.getMessage());
469             System.exit(4);
470         }
471
472         try {
473             if (mode == 'c') {
474                 System.out.println(root.toCQL());
475             } else if (mode == 'p') {
476                 InputStream f = new FileInputStream(pfile);
477                 if (f == null)
478                     throw new FileNotFoundException(pfile);
479
480                 Properties config = new Properties();
481                 config.load(f);
482                 f.close();
483                 System.out.println(root.toPQF(config));
484             } else {
485                 System.out.print(root.toXCQL());
486             }
487         } catch (IOException ex) {
488             System.err.println("Can't render query: " + ex.getMessage());
489             System.exit(5);
490         } catch (UnknownIndexException ex) {
491             System.err.println("Unknown index: " + ex.getMessage());
492             System.exit(6);
493         } catch (UnknownRelationException ex) {
494             System.err.println("Unknown relation: " + ex.getMessage());
495             System.exit(7);
496         } catch (UnknownRelationModifierException ex) {
497             System.err.println("Unknown relation modifier: " +
498                                ex.getMessage());
499             System.exit(8);
500         } catch (UnknownPositionException ex) {
501             System.err.println("Unknown position: " + ex.getMessage());
502             System.exit(9);
503         } catch (PQFTranslationException ex) {
504             // We catch all of this class's subclasses, so --
505             throw new Error("can't get a PQFTranslationException");
506         }
507     }
508 }