1 // $Id: CQLParser.java,v 1.11 2002-10-31 22:22:01 mike Exp $
3 package org.z3950.zing.cql;
4 import java.io.IOException;
8 * Compiles a CQL string into a parse tree.
11 * @version $Id: CQLParser.java,v 1.11 2002-10-31 22:22:01 mike Exp $
12 * @see <A href="http://zing.z3950.org/cql/index.html"
13 * >http://zing.z3950.org/cql/index.html</A>
15 public class CQLParser {
16 private CQLLexer lexer;
17 static private boolean DEBUG = false;
18 static private boolean LEXDEBUG = false;
20 private static void debug(String str) {
22 System.err.println("PARSEDEBUG: " + str);
25 public CQLNode parse(String cql)
26 throws CQLParseException, IOException {
27 lexer = new CQLLexer(cql, LEXDEBUG);
30 debug("about to parse_query()");
31 CQLNode root = parse_query("srw.serverChoice", new CQLRelation("="));
32 if (lexer.ttype != lexer.TT_EOF)
33 throw new CQLParseException("junk after end: " + lexer.render());
38 private CQLNode parse_query(String qualifier, CQLRelation relation)
39 throws CQLParseException, IOException {
40 debug("in parse_query()");
42 CQLNode term = parse_term(qualifier, relation);
43 while (lexer.ttype != lexer.TT_EOF &&
45 if (lexer.ttype == lexer.TT_AND) {
47 CQLNode term2 = parse_term(qualifier, relation);
48 term = new CQLAndNode(term, term2);
49 } else if (lexer.ttype == lexer.TT_OR) {
51 CQLNode term2 = parse_term(qualifier, relation);
52 term = new CQLOrNode(term, term2);
53 } else if (lexer.ttype == lexer.TT_NOT) {
55 CQLNode term2 = parse_term(qualifier, relation);
56 term = new CQLNotNode(term, term2);
57 } else if (lexer.ttype == lexer.TT_PROX) {
59 CQLProxNode proxnode = new CQLProxNode(term);
60 gatherProxParameters(proxnode);
61 CQLNode term2 = parse_term(qualifier, relation);
62 proxnode.addSecondSubterm(term2);
63 term = (CQLNode) proxnode;
65 throw new CQLParseException("expected boolean, got " +
74 private CQLNode parse_term(String qualifier, CQLRelation relation)
75 throws CQLParseException, IOException {
76 debug("in parse_term()");
80 if (lexer.ttype == '(') {
81 debug("parenthesised term");
83 CQLNode expr = parse_query(qualifier, relation);
86 } else if (lexer.ttype != lexer.TT_WORD && lexer.ttype != '"') {
87 throw new CQLParseException("expected qualifier or term, " +
88 "got " + lexer.render());
91 debug("non-parenthesised term");
94 if (!isBaseRelation())
98 relation = new CQLRelation(lexer.render(lexer.ttype, false));
101 while (lexer.ttype == '/') {
103 // ### could insist on known modifiers only
104 if (lexer.ttype != lexer.TT_WORD)
105 throw new CQLParseException("expected relation modifier, "
106 + "got " + lexer.render());
107 relation.addModifier(lexer.sval);
108 match(lexer.TT_WORD);
111 debug("qualifier='" + qualifier + ", " +
112 "relation='" + relation.toCQL() + "'");
115 CQLTermNode node = new CQLTermNode(qualifier, relation, word);
116 debug("made term node " + node.toCQL());
120 private void gatherProxParameters(CQLProxNode node)
121 throws CQLParseException, IOException {
122 for (int i = 0; i < 4; i++) {
123 if (lexer.ttype != '/')
124 return; // end of proximity parameters
127 if (lexer.ttype != '/') {
128 // not an omitted default
130 // Assumes order is: relation/distance/unit/ordering
131 case 0: gatherProxRelation(node); break;
132 case 1: gatherProxDistance(node); break;
133 case 2: gatherProxUnit(node); break;
134 case 3: gatherProxOrdering(node); break;
140 private void gatherProxRelation(CQLProxNode node)
141 throws CQLParseException, IOException {
142 if (!isProxRelation())
143 throw new CQLParseException("expected proximity relation, got " +
145 node.addModifier("relation", lexer.render(lexer.ttype, false));
147 debug("gPR matched " + lexer.render(lexer.ttype, false));
150 private void gatherProxDistance(CQLProxNode node)
151 throws CQLParseException, IOException {
152 if (lexer.ttype != lexer.TT_NUMBER)
153 throw new CQLParseException("expected proximity distance, got " +
155 node.addModifier("distance", lexer.render(lexer.ttype, false));
157 debug("gPD matched " + lexer.render(lexer.ttype, false));
160 private void gatherProxUnit(CQLProxNode node)
161 throws CQLParseException, IOException {
162 if (lexer.ttype != lexer.TT_pWORD &&
163 lexer.ttype != lexer.TT_SENTENCE &&
164 lexer.ttype != lexer.TT_PARAGRAPH &&
165 lexer.ttype != lexer.TT_ELEMENT)
166 throw new CQLParseException("expected proximity unit, got " +
168 node.addModifier("unit", lexer.render());
172 private void gatherProxOrdering(CQLProxNode node)
173 throws CQLParseException, IOException {
174 if (lexer.ttype != lexer.TT_ORDERED &&
175 lexer.ttype != lexer.TT_UNORDERED)
176 throw new CQLParseException("expected proximity ordering, got " +
178 node.addModifier("ordering", lexer.render());
182 boolean isBaseRelation() {
183 debug("isBaseRelation: checking ttype=" + lexer.ttype +
184 " (" + lexer.render() + ")");
185 return (isProxRelation() ||
186 lexer.ttype == lexer.TT_ANY ||
187 lexer.ttype == lexer.TT_ALL ||
188 lexer.ttype == lexer.TT_EXACT);
191 boolean isProxRelation() {
192 debug("isProxRelation: checking ttype=" + lexer.ttype +
193 " (" + lexer.render() + ")");
194 return (lexer.ttype == '<' ||
195 lexer.ttype == '>' ||
196 lexer.ttype == '=' ||
197 lexer.ttype == lexer.TT_LE ||
198 lexer.ttype == lexer.TT_GE ||
199 lexer.ttype == lexer.TT_NE);
202 private void match(int token)
203 throws CQLParseException, IOException {
204 debug("in match(" + lexer.render(token, true) + ")");
205 if (lexer.ttype != token)
206 throw new CQLParseException("expected " +
207 lexer.render(token, true) +
208 ", " + "got " + lexer.render());
209 int tmp = lexer.nextToken();
210 debug("match() got token=" + lexer.ttype + ", " +
211 "nval=" + lexer.nval + ", sval='" + lexer.sval + "'" +
212 " (tmp=" + tmp + ")");
218 // e.g. echo '(au=Kerninghan or au=Ritchie) and ti=Unix' |
219 // java org.z3950.zing.cql.CQLParser
222 // <boolean>and</boolean>
224 // <boolean>or</boolean>
227 // <relation>=<relation>
228 // <term>Kerninghan<term>
232 // <relation>=<relation>
233 // <term>Ritchie<term>
238 // <relation>=<relation>
243 public static void main (String[] args) {
244 if (args.length > 1) {
245 System.err.println("Usage: CQLParser [<CQL-query>]");
246 System.err.println("If unspecified, query is read from stdin");
251 if (args.length == 1) {
254 byte[] bytes = new byte[10000];
256 // Read in the whole of standard input in one go
257 int nbytes = System.in.read(bytes);
258 } catch (java.io.IOException ex) {
259 System.err.println("Can't read query: " + ex.getMessage());
262 cql = new String(bytes);
265 CQLParser parser = new CQLParser();
268 root = parser.parse(cql);
269 debug("root='" + root + "'");
270 System.out.println(root.toCQL());
271 } catch (CQLParseException ex) {
272 System.err.println("Syntax error: " + ex.getMessage());
274 } catch (java.io.IOException ex) {
275 System.err.println("Can't compile query: " + ex.getMessage());