Mark syntax errors
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLParser.java
1
2 package org.z3950.zing.cql;
3
4 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.util.Properties;
7 import java.io.InputStream;
8 import java.io.FileInputStream;
9 import java.io.InputStreamReader;
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 CQLTokenizer 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 CQLLexer(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() != CQLTokenizer.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() == CQLTokenizer.TT_SORTBY) {
139             match(lexer.what());
140             debug("sortspec");
141
142             CQLSortNode sortnode = new CQLSortNode(node);
143             while (lexer.what() != CQLTokenizer.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() != CQLTokenizer.TT_EOF &&
165                lexer.what() != ')' &&
166                lexer.what() != CQLTokenizer.TT_SORTBY) {
167             if (lexer.what() == CQLTokenizer.TT_AND ||
168                 lexer.what() == CQLTokenizer.TT_OR ||
169                 lexer.what() == CQLTokenizer.TT_NOT ||
170                 lexer.what() == CQLTokenizer.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 == CQLTokenizer.TT_AND) ? new CQLAndNode(term, term2, ms) :
177                         (type == CQLTokenizer.TT_OR)  ? new CQLOrNode (term, term2, ms) :
178                         (type == CQLTokenizer.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() != CQLTokenizer.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 first;
223         StringBuilder all;
224         while (true) {
225             if (lexer.what() == '(') {
226                 debug("parenthesised term");
227                 match('(');
228                 CQLNode expr = parseQuery(index, relation);
229                 match(')');
230                 return expr;
231             } else if (lexer.what() == '>') {
232                 return parsePrefix(index, relation, false);
233             }
234
235             debug("non-parenthesised term");
236             first = matchSymbol("index or term");
237             all = new StringBuilder(first);
238             //match relation only on second postion
239             while (isWordOrString() && (all.length() > first.length() || !isRelation())) {
240               all.append(" ").append(lexer.value());
241               match(lexer.what());
242             }
243
244             if (!isRelation())
245               break; //we're done if no relation
246             
247             //we have relation, but it only makes sense if preceded by a single term
248             if (all.length() > first.length()) {
249               throw new CQLParseException("unexpected relation '"+lexer.value()+"'"
250                 , lexer.pos());
251             }
252             index = first;
253             String relstr = (lexer.what() == CQLTokenizer.TT_WORD ?
254                              lexer.value() : lexer.render(lexer.what(), false));
255             relation = new CQLRelation(relstr);
256             match(lexer.what());
257             ModifierSet ms = gatherModifiers(relstr);
258             relation.ms = ms;
259             debug("index='" + index + ", " +
260                   "relation='" + relation.toCQL() + "'");
261         }
262         CQLTermNode node = new CQLTermNode(index, relation, all.toString());
263         debug("made term node " + node.toCQL());
264         return node;
265     }
266
267     private CQLNode parsePrefix(String index, CQLRelation relation,
268                                 boolean topLevel)
269         throws CQLParseException, IOException {
270         debug("prefix mapping");
271
272         match('>');
273         String name = null;
274         String identifier = matchSymbol("prefix-name");
275         if (lexer.what() == '=') {
276             match('=');
277             name = identifier;
278             identifier = matchSymbol("prefix-identifer");
279         }
280         CQLNode node = topLevel ?
281             parseTopLevelPrefixes(index, relation) :
282             parseQuery(index, relation);
283
284         return new CQLPrefixNode(name, identifier, node);
285     }
286     
287     private boolean isWordOrString() {
288       return CQLTokenizer.TT_WORD == lexer.what() 
289         || CQLTokenizer.TT_STRING == lexer.what();
290     }
291
292     private boolean isRelation() {
293         debug("isRelation: checking what()=" + lexer.what() +
294               " (" + lexer.render() + ")");
295         if (lexer.what() == CQLTokenizer.TT_WORD &&
296             (lexer.value().indexOf('.') >= 0 ||
297              lexer.value().equals("any") ||
298              lexer.value().equals("all") ||
299              lexer.value().equals("within") ||
300              lexer.value().equals("encloses") ||
301              (lexer.value().equals("exact") && compat != V1POINT2) ||
302              (lexer.value().equals("scr") && compat != V1POINT2) ||
303              (lexer.value().equals("adj") && compat == V1POINT2) ||
304              customRelations.contains(lexer.value())))
305           return true;
306
307         return isSymbolicRelation();
308     }
309
310     private boolean isSymbolicRelation() {
311         debug("isSymbolicRelation: checking what()=" + lexer.what() +
312               " (" + lexer.render() + ")");
313         return (lexer.what() == '<' ||
314                 lexer.what() == '>' ||
315                 lexer.what() == '=' ||
316                 lexer.what() == CQLTokenizer.TT_LE ||
317                 lexer.what() == CQLTokenizer.TT_GE ||
318                 lexer.what() == CQLTokenizer.TT_NE ||
319                 lexer.what() == CQLTokenizer.TT_EQEQ);
320     }
321
322     private void match(int token)
323         throws CQLParseException, IOException {
324         debug("in match(" + lexer.render(token, true) + ")");
325         if (lexer.what() != token)
326             throw new CQLParseException("expected " +
327                                         lexer.render(token, true) +
328                                         ", " + "got " + lexer.render(), 
329               lexer.pos());
330         lexer.move();
331         debug("match() got token=" + lexer.what() + ", value()='" + lexer.value() + "'");
332     }
333
334     private String matchSymbol(String expected)
335         throws CQLParseException, IOException {
336
337         debug("in matchSymbol()");
338         if (lexer.what() == CQLTokenizer.TT_WORD ||
339             lexer.what() == CQLTokenizer.TT_STRING ||
340             // The following is a complete list of keywords.  Because
341             // they're listed here, they can be used unquoted as
342             // indexes, terms, prefix names and prefix identifiers.
343             (allowKeywordTerms &&
344             lexer.what() == CQLTokenizer.TT_AND ||
345             lexer.what() == CQLTokenizer.TT_OR ||
346             lexer.what() == CQLTokenizer.TT_NOT ||
347             lexer.what() == CQLTokenizer.TT_PROX ||
348             lexer.what() == CQLTokenizer.TT_SORTBY)) {
349             String symbol = lexer.value();
350             match(lexer.what());
351             return symbol;
352         }
353
354         throw new CQLParseException("expected " + expected + ", " +
355                                     "got " + lexer.render(), lexer.pos());
356     }
357
358
359     /**
360      * Simple test-harness for the CQLParser class.
361      * <P>
362      * Reads a CQL query either from its command-line argument, if
363      * there is one, or standard input otherwise.  So these two
364      * invocations are equivalent:
365      * <PRE>
366      *  CQLParser 'au=(Kerninghan or Ritchie) and ti=Unix'
367      *  echo au=(Kerninghan or Ritchie) and ti=Unix | CQLParser
368      * </PRE>
369      * The test-harness parses the supplied query and renders is as
370      * XCQL, so that both of the invocations above produce the
371      * following output:
372      * <PRE>
373      *  &lt;triple&gt;
374      *    &lt;boolean&gt;
375      *      &lt;value&gt;and&lt;/value&gt;
376      *    &lt;/boolean&gt;
377      *    &lt;triple&gt;
378      *      &lt;boolean&gt;
379      *        &lt;value&gt;or&lt;/value&gt;
380      *      &lt;/boolean&gt;
381      *      &lt;searchClause&gt;
382      *        &lt;index&gt;au&lt;/index&gt;
383      *        &lt;relation&gt;
384      *          &lt;value&gt;=&lt;/value&gt;
385      *        &lt;/relation&gt;
386      *        &lt;term&gt;Kerninghan&lt;/term&gt;
387      *      &lt;/searchClause&gt;
388      *      &lt;searchClause&gt;
389      *        &lt;index&gt;au&lt;/index&gt;
390      *        &lt;relation&gt;
391      *          &lt;value&gt;=&lt;/value&gt;
392      *        &lt;/relation&gt;
393      *        &lt;term&gt;Ritchie&lt;/term&gt;
394      *      &lt;/searchClause&gt;
395      *    &lt;/triple&gt;
396      *    &lt;searchClause&gt;
397      *      &lt;index&gt;ti&lt;/index&gt;
398      *      &lt;relation&gt;
399      *        &lt;value&gt;=&lt;/value&gt;
400      *      &lt;/relation&gt;
401      *      &lt;term&gt;Unix&lt;/term&gt;
402      *    &lt;/searchClause&gt;
403      *  &lt;/triple&gt;
404      * </PRE>
405      * <P>
406      * @param -1
407      *  CQL version 1.1 (default version 1.2)
408      * @param -d
409      *  Debug mode: extra output written to stderr.
410      * @param -c
411      *  Causes the output to be written in CQL rather than XCQL - that
412      *  is, a query equivalent to that which was input, is output.  In
413      *  effect, the test harness acts as a query canonicaliser.
414      * @return
415      *  The input query, either as XCQL [default] or CQL [if the
416      *  <TT>-c</TT> option is supplied].
417      */
418     public static void main (String[] args) {
419         char mode = 'x';        // x=XCQL, c=CQL, p=PQF
420         String pfile = null;
421
422         List<String> argv = new ArrayList<String>();
423         for (int i = 0; i < args.length; i++) {
424             argv.add(args[i]);
425         }
426
427         int compat = V1POINT2;
428         if (argv.size() > 0 && argv.get(0).equals("-1")) {
429             compat = V1POINT1;
430             argv.remove(0);
431         }
432
433         if (argv.size() > 0 && argv.get(0).equals("-d")) {
434             DEBUG = true;
435             argv.remove(0);
436         }
437
438         if (argv.size() > 0 && argv.get(0).equals("-c")) {
439             mode = 'c';
440             argv.remove(0);
441         } else if (argv.size() > 1 && argv.get(0).equals("-p")) {
442             mode = 'p';
443             argv.remove(0);
444             pfile = (String) argv.get(0);
445             argv.remove(0);
446         }
447
448         if (argv.size() > 1) {
449             System.err.println("Usage: CQLParser [-1] [-d] [-c] " +
450                                "[-p <pqf-properties> [<CQL-query>]");
451             System.err.println("If unspecified, query is read from stdin");
452             System.exit(1);
453         }
454
455         String cql;
456         if (argv.size() == 1) {
457             cql = (String) argv.get(0);
458         } else {
459             BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
460             try {
461                 // read a single line of input
462               cql = buff.readLine();
463               if (cql == null) {
464                 System.err.println("Can't read query from stdin");
465                 System.exit(2);
466                 return;
467               }
468             } catch (IOException ex) {
469                 System.err.println("Can't read query: " + ex.getMessage());
470                 System.exit(2);
471                 return;
472             }
473         }
474
475         CQLParser parser = new CQLParser(compat);
476         CQLNode root;
477         try {
478             root = parser.parse(cql);
479         } catch (CQLParseException ex) {
480             System.err.println("Syntax error: " + ex.getMessage());
481             StringBuilder space = new StringBuilder(cql.length());
482             System.out.println(cql);
483             for (int i=0; i<ex.getPosition(); i++) space.append(" ");
484             space.append("^");
485             System.err.println(space.toString());
486             System.exit(3);
487             return; //compiler
488         } catch (IOException ex) {
489             System.err.println("Can't compile query: " + ex.getMessage());
490             System.exit(4);
491             return; //compiler
492         }
493
494         try {
495             if (mode == 'c') {
496                 System.out.println(root.toCQL());
497             } else if (mode == 'p') {
498               try {
499                 InputStream f = new FileInputStream(pfile);
500                 Properties config = new Properties();
501                 config.load(f);
502                 f.close();
503                 System.out.println(root.toPQF(config));
504               } catch (IOException ex) {
505                 System.err.println("Can't load PQF properties:" + 
506                   ex.getMessage());
507                 System.exit(5);
508               }
509             } else {
510                 System.out.print(root.toXCQL());
511             }
512         } catch (UnknownIndexException ex) {
513             System.err.println("Unknown index: " + ex.getMessage());
514             System.exit(6);
515         } catch (UnknownRelationException ex) {
516             System.err.println("Unknown relation: " + ex.getMessage());
517             System.exit(7);
518         } catch (UnknownRelationModifierException ex) {
519             System.err.println("Unknown relation modifier: " +
520                                ex.getMessage());
521             System.exit(8);
522         } catch (UnknownPositionException ex) {
523             System.err.println("Unknown position: " + ex.getMessage());
524             System.exit(9);
525         } catch (PQFTranslationException ex) {
526             System.err.println("Cannot translate to PQF: " + ex.getMessage());
527             System.exit(10);
528         }
529     }
530 }