X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fz3950%2Fzing%2Fcql%2FCQLParser.java;h=c373f092807079ee525fa907b0814e90fa402ccb;hb=277aa21b21ccafbb392f58ba30390040bdb47c94;hp=58e03269c8aa41bc9196ba9649b9f7e191f1fbbc;hpb=d58739419882639439b40b18fcefeb9e51488fb9;p=cql-java-moved-to-github.git diff --git a/src/main/java/org/z3950/zing/cql/CQLParser.java b/src/main/java/org/z3950/zing/cql/CQLParser.java index 58e0326..c373f09 100644 --- a/src/main/java/org/z3950/zing/cql/CQLParser.java +++ b/src/main/java/org/z3950/zing/cql/CQLParser.java @@ -1,27 +1,33 @@ -// $Id: CQLParser.java,v 1.39 2007-08-06 15:54:48 mike Exp $ package org.z3950.zing.cql; import java.io.IOException; -import java.util.Vector; import java.util.Properties; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.Reader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * Compiles CQL strings into parse trees of CQLNode subtypes. * - * @version $Id: CQLParser.java,v 1.39 2007-08-06 15:54:48 mike Exp $ * @see http://zing.z3950.org/cql/index.html */ public class CQLParser { private CQLLexer lexer; - private int compat; // When false, implement CQL 1.2 - public static int V1POINT1 = 12368; - public static int V1POINT2 = 12369; - public static int V1POINT1SORT = 12370; + private final int compat; // When false, implement CQL 1.2 + private final Set customRelations = new HashSet(); + + public static final int V1POINT1 = 12368; + public static final int V1POINT2 = 12369; + public static final int V1POINT1SORT = 12370; + public final boolean allowKeywordTerms; static private boolean DEBUG = false; static private boolean LEXDEBUG = false; @@ -41,19 +47,52 @@ public class CQLParser { */ public CQLParser(int compat) { this.compat = compat; + this.allowKeywordTerms = true; } - + + /** + * Official CQL grammar allows registered keywords like 'and/or/not/sortby/prox' + * to be used unquoted in terms. This constructor allows to create an instance + * of a parser that prohibits this behavior while sacrificing compatibility. + * @param compat CQL version compatibility + * @param allowKeywordTerms when false registered keywords are disallowed in unquoted terms + */ + public CQLParser(int compat, boolean allowKeywordTerms) { + this.compat = compat; + this.allowKeywordTerms = allowKeywordTerms; + } + /** * The new parser implements CQL 1.2 */ public CQLParser() { this.compat = V1POINT2; + this.allowKeywordTerms = true; } private static void debug(String str) { if (DEBUG) System.err.println("PARSEDEBUG: " + str); } + + /** + * Registers custom relation in this parser. Note that when a custom relation + * is registered the parser is no longer strictly compliant with the chosen spec. + * @param relation + * @return true if custom relation has not been registered already + */ + public boolean registerCustomRelation(String relation) { + return customRelations.add(relation); + } + + /** + * Unregisters previously registered custom relation in this instance of the parser. + * @param relation + * @return true is relation has been previously registered + */ + public boolean unregisterCustomRelation(String relation) { + return customRelations.remove(relation); + } /** * Compiles a CQL query. @@ -73,14 +112,15 @@ public class CQLParser { * tree representing the query. */ public CQLNode parse(String cql) throws CQLParseException, IOException { - lexer = new CQLLexer(cql, LEXDEBUG); + lexer = new CQLLexerSimple(cql, LEXDEBUG); - lexer.nextToken(); + lexer.move(); debug("about to parseQuery()"); CQLNode root = parseTopLevelPrefixes("cql.serverChoice", new CQLRelation(compat == V1POINT2 ? "=" : "scr")); - if (lexer.ttype != lexer.TT_EOF) - throw new CQLParseException("junk after end: " + lexer.render()); + if (lexer.what() != CQLLexer.TT_EOF) + throw new CQLParseException("junk after end: " + lexer.render(), + lexer.pos()); return root; } @@ -89,25 +129,25 @@ public class CQLParser { throws CQLParseException, IOException { debug("top-level prefix mapping"); - if (lexer.ttype == '>') { + if (lexer.what() == '>') { return parsePrefix(index, relation, true); } CQLNode node = parseQuery(index, relation); if ((compat == V1POINT2 || compat == V1POINT1SORT) && - lexer.ttype == lexer.TT_SORTBY) { - match(lexer.ttype); + lexer.what() == CQLLexer.TT_SORTBY) { + match(lexer.what()); debug("sortspec"); CQLSortNode sortnode = new CQLSortNode(node); - while (lexer.ttype != lexer.TT_EOF) { + while (lexer.what() != CQLLexer.TT_EOF) { String sortindex = matchSymbol("sort index"); ModifierSet ms = gatherModifiers(sortindex); sortnode.addSortIndex(ms); } if (sortnode.keys.size() == 0) { - throw new CQLParseException("no sort keys"); + throw new CQLParseException("no sort keys", lexer.pos()); } node = sortnode; @@ -121,25 +161,25 @@ public class CQLParser { debug("in parseQuery()"); CQLNode term = parseTerm(index, relation); - while (lexer.ttype != lexer.TT_EOF && - lexer.ttype != ')' && - lexer.ttype != lexer.TT_SORTBY) { - if (lexer.ttype == lexer.TT_AND || - lexer.ttype == lexer.TT_OR || - lexer.ttype == lexer.TT_NOT || - lexer.ttype == lexer.TT_PROX) { - int type = lexer.ttype; - String val = lexer.sval; + while (lexer.what() != CQLLexer.TT_EOF && + lexer.what() != ')' && + lexer.what() != CQLLexer.TT_SORTBY) { + if (lexer.what() == CQLLexer.TT_AND || + lexer.what() == CQLLexer.TT_OR || + lexer.what() == CQLLexer.TT_NOT || + lexer.what() == CQLLexer.TT_PROX) { + int type = lexer.what(); + String val = lexer.value(); match(type); ModifierSet ms = gatherModifiers(val); CQLNode term2 = parseTerm(index, relation); - term = ((type == lexer.TT_AND) ? new CQLAndNode(term, term2, ms) : - (type == lexer.TT_OR) ? new CQLOrNode (term, term2, ms) : - (type == lexer.TT_NOT) ? new CQLNotNode(term, term2, ms) : + term = ((type == CQLLexer.TT_AND) ? new CQLAndNode(term, term2, ms) : + (type == CQLLexer.TT_OR) ? new CQLOrNode (term, term2, ms) : + (type == CQLLexer.TT_NOT) ? new CQLNotNode(term, term2, ms) : new CQLProxNode(term, term2, ms)); } else { throw new CQLParseException("expected boolean, got " + - lexer.render()); + lexer.render(), lexer.pos()); } } @@ -152,20 +192,21 @@ public class CQLParser { debug("in gatherModifiers()"); ModifierSet ms = new ModifierSet(base); - while (lexer.ttype == '/') { + while (lexer.what() == '/') { match('/'); - if (lexer.ttype != lexer.TT_WORD) + if (lexer.what() != CQLLexer.TT_WORD) throw new CQLParseException("expected modifier, " - + "got " + lexer.render()); - String type = lexer.sval.toLowerCase(); - match(lexer.ttype); - if (!isRelation()) { + + "got " + lexer.render(), + lexer.pos()); + String type = lexer.value().toLowerCase(); + match(lexer.what()); + if (!isSymbolicRelation()) { // It's a simple modifier consisting of type only ms.addModifier(type); } else { // It's a complex modifier of the form type=value - String comparision = lexer.render(lexer.ttype, false); - match(lexer.ttype); + String comparision = lexer.render(lexer.what(), false); + match(lexer.what()); String value = matchSymbol("modifier value"); ms.addModifier(type, comparision, value); } @@ -180,28 +221,33 @@ public class CQLParser { String word; while (true) { - if (lexer.ttype == '(') { + if (lexer.what() == '(') { debug("parenthesised term"); match('('); CQLNode expr = parseQuery(index, relation); match(')'); return expr; - } else if (lexer.ttype == '>') { + } else if (lexer.what() == '>') { return parsePrefix(index, relation, false); } debug("non-parenthesised term"); word = matchSymbol("index or term"); - if (!isRelation() && lexer.ttype != lexer.TT_WORD) + while (lexer.what() == CQLLexer.TT_WORD && !isRelation()) { + word = word + " " + lexer.value(); + match(CQLLexer.TT_WORD); + } + + if (!isRelation()) break; index = word; - String relstr = (lexer.ttype == lexer.TT_WORD ? - lexer.sval : lexer.render(lexer.ttype, false)); + String relstr = (lexer.what() == CQLLexer.TT_WORD ? + lexer.value() : lexer.render(lexer.what(), false)); relation = new CQLRelation(relstr); - match(lexer.ttype); + match(lexer.what()); ModifierSet ms = gatherModifiers(relstr); - relation.setModifiers(ms); + relation.ms = ms; debug("index='" + index + ", " + "relation='" + relation.toCQL() + "'"); } @@ -219,7 +265,7 @@ public class CQLParser { match('>'); String name = null; String identifier = matchSymbol("prefix-name"); - if (lexer.ttype == '=') { + if (lexer.what() == '=') { match('='); name = identifier; identifier = matchSymbol("prefix-identifer"); @@ -231,57 +277,72 @@ public class CQLParser { return new CQLPrefixNode(name, identifier, node); } - // Checks for a relation private boolean isRelation() { - debug("isRelation: checking ttype=" + lexer.ttype + + debug("isRelation: checking what()=" + lexer.what() + + " (" + lexer.render() + ")"); + if (lexer.what() == CQLLexer.TT_WORD && + (lexer.value().indexOf('.') >= 0 || + lexer.value().equals("any") || + lexer.value().equals("all") || + lexer.value().equals("within") || + lexer.value().equals("encloses") || + (lexer.value().equals("exact") && compat != V1POINT2) || + (lexer.value().equals("scr") && compat != V1POINT2) || + (lexer.value().equals("adj") && compat == V1POINT2) || + customRelations.contains(lexer.value()))) + return true; + + return isSymbolicRelation(); + } + + private boolean isSymbolicRelation() { + debug("isSymbolicRelation: checking what()=" + lexer.what() + " (" + lexer.render() + ")"); - return (lexer.ttype == '<' || - lexer.ttype == '>' || - lexer.ttype == '=' || - lexer.ttype == lexer.TT_LE || - lexer.ttype == lexer.TT_GE || - lexer.ttype == lexer.TT_NE || - lexer.ttype == lexer.TT_EQEQ); + return (lexer.what() == '<' || + lexer.what() == '>' || + lexer.what() == '=' || + lexer.what() == CQLLexer.TT_LE || + lexer.what() == CQLLexer.TT_GE || + lexer.what() == CQLLexer.TT_NE || + lexer.what() == CQLLexer.TT_EQEQ); } private void match(int token) throws CQLParseException, IOException { debug("in match(" + lexer.render(token, true) + ")"); - if (lexer.ttype != token) + if (lexer.what() != token) throw new CQLParseException("expected " + lexer.render(token, true) + - ", " + "got " + lexer.render()); - int tmp = lexer.nextToken(); - debug("match() got token=" + lexer.ttype + ", " + - "nval=" + lexer.nval + ", sval='" + lexer.sval + "'" + - " (tmp=" + tmp + ")"); + ", " + "got " + lexer.render(), + lexer.pos()); + lexer.move(); + debug("match() got token=" + lexer.what() + ", value()='" + lexer.value() + "'"); } private String matchSymbol(String expected) throws CQLParseException, IOException { debug("in matchSymbol()"); - if (lexer.ttype == lexer.TT_WORD || - lexer.ttype == lexer.TT_NUMBER || - lexer.ttype == '"' || + if (lexer.what() == CQLLexer.TT_WORD || + lexer.what() == '"' || // The following is a complete list of keywords. Because // they're listed here, they can be used unquoted as // indexes, terms, prefix names and prefix identifiers. // ### Instead, we should ask the lexer whether what we // have is a keyword, and let the knowledge reside there. - lexer.ttype == lexer.TT_AND || - lexer.ttype == lexer.TT_OR || - lexer.ttype == lexer.TT_NOT || - lexer.ttype == lexer.TT_PROX || - lexer.ttype == lexer.TT_SORTBY) { - String symbol = (lexer.ttype == lexer.TT_NUMBER) ? - lexer.render() : lexer.sval; - match(lexer.ttype); + (allowKeywordTerms && + lexer.what() == CQLLexer.TT_AND || + lexer.what() == CQLLexer.TT_OR || + lexer.what() == CQLLexer.TT_NOT || + lexer.what() == CQLLexer.TT_PROX || + lexer.what() == CQLLexer.TT_SORTBY)) { + String symbol = lexer.value(); + match(lexer.what()); return symbol; } throw new CQLParseException("expected " + expected + ", " + - "got " + lexer.render()); + "got " + lexer.render(), lexer.pos()); } @@ -348,7 +409,7 @@ public class CQLParser { char mode = 'x'; // x=XCQL, c=CQL, p=PQF String pfile = null; - Vector argv = new Vector(); + List argv = new ArrayList(); for (int i = 0; i < args.length; i++) { argv.add(args[i]); } @@ -421,7 +482,7 @@ public class CQLParser { f.close(); System.out.println(root.toPQF(config)); } else { - System.out.print(root.toXCQL(0)); + System.out.print(root.toXCQL()); } } catch (IOException ex) { System.err.println("Can't render query: " + ex.getMessage());