package org.z3950.zing.cql;
+import java.io.BufferedReader;
import java.io.IOException;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
* >http://zing.z3950.org/cql/index.html</A>
*/
public class CQLParser {
- private CQLLexer lexer;
+ private CQLTokenizer lexer;
private final int compat; // When false, implement CQL 1.2
private final Set<String> customRelations = new HashSet<String>();
* tree representing the query. */
public CQLNode parse(String cql)
throws CQLParseException, IOException {
- lexer = new CQLLexerSimple(cql, LEXDEBUG);
+ lexer = new CQLLexer(cql, LEXDEBUG);
lexer.move();
debug("about to parseQuery()");
CQLNode root = parseTopLevelPrefixes("cql.serverChoice",
new CQLRelation(compat == V1POINT2 ? "=" : "scr"));
- if (lexer.what() != CQLLexer.TT_EOF)
+ if (lexer.what() != CQLTokenizer.TT_EOF)
throw new CQLParseException("junk after end: " + lexer.render(),
lexer.pos());
CQLNode node = parseQuery(index, relation);
if ((compat == V1POINT2 || compat == V1POINT1SORT) &&
- lexer.what() == CQLLexer.TT_SORTBY) {
+ lexer.what() == CQLTokenizer.TT_SORTBY) {
match(lexer.what());
debug("sortspec");
CQLSortNode sortnode = new CQLSortNode(node);
- while (lexer.what() != CQLLexer.TT_EOF) {
+ while (lexer.what() != CQLTokenizer.TT_EOF) {
String sortindex = matchSymbol("sort index");
ModifierSet ms = gatherModifiers(sortindex);
sortnode.addSortIndex(ms);
debug("in parseQuery()");
CQLNode term = parseTerm(index, relation);
- while (lexer.what() != CQLLexer.TT_EOF &&
+ while (lexer.what() != CQLTokenizer.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) {
+ lexer.what() != CQLTokenizer.TT_SORTBY) {
+ if (lexer.what() == CQLTokenizer.TT_AND ||
+ lexer.what() == CQLTokenizer.TT_OR ||
+ lexer.what() == CQLTokenizer.TT_NOT ||
+ lexer.what() == CQLTokenizer.TT_PROX) {
int type = lexer.what();
String val = lexer.value();
match(type);
ModifierSet ms = gatherModifiers(val);
CQLNode term2 = parseTerm(index, relation);
- 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) :
+ term = ((type == CQLTokenizer.TT_AND) ? new CQLAndNode(term, term2, ms) :
+ (type == CQLTokenizer.TT_OR) ? new CQLOrNode (term, term2, ms) :
+ (type == CQLTokenizer.TT_NOT) ? new CQLNotNode(term, term2, ms) :
new CQLProxNode(term, term2, ms));
} else {
throw new CQLParseException("expected boolean, got " +
ModifierSet ms = new ModifierSet(base);
while (lexer.what() == '/') {
match('/');
- if (lexer.what() != CQLLexer.TT_WORD)
+ if (lexer.what() != CQLTokenizer.TT_WORD)
throw new CQLParseException("expected modifier, "
+ "got " + lexer.render(),
lexer.pos());
debug("non-parenthesised term");
word = matchSymbol("index or term");
- while (lexer.what() == CQLLexer.TT_WORD && !isRelation()) {
+ while (lexer.what() == CQLTokenizer.TT_WORD && !isRelation()) {
word = word + " " + lexer.value();
- match(CQLLexer.TT_WORD);
+ match(CQLTokenizer.TT_WORD);
}
if (!isRelation())
break;
index = word;
- String relstr = (lexer.what() == CQLLexer.TT_WORD ?
+ String relstr = (lexer.what() == CQLTokenizer.TT_WORD ?
lexer.value() : lexer.render(lexer.what(), false));
relation = new CQLRelation(relstr);
match(lexer.what());
private boolean isRelation() {
debug("isRelation: checking what()=" + lexer.what() +
" (" + lexer.render() + ")");
- if (lexer.what() == CQLLexer.TT_WORD &&
+ if (lexer.what() == CQLTokenizer.TT_WORD &&
(lexer.value().indexOf('.') >= 0 ||
lexer.value().equals("any") ||
lexer.value().equals("all") ||
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);
+ lexer.what() == CQLTokenizer.TT_LE ||
+ lexer.what() == CQLTokenizer.TT_GE ||
+ lexer.what() == CQLTokenizer.TT_NE ||
+ lexer.what() == CQLTokenizer.TT_EQEQ);
}
private void match(int token)
throws CQLParseException, IOException {
debug("in matchSymbol()");
- if (lexer.what() == CQLLexer.TT_WORD ||
+ if (lexer.what() == CQLTokenizer.TT_WORD ||
lexer.what() == '"' ||
// The following is a complete list of keywords. Because
// they're listed here, they can be used unquoted as
// ### Instead, we should ask the lexer whether what we
// have is a keyword, and let the knowledge reside there.
(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)) {
+ lexer.what() == CQLTokenizer.TT_AND ||
+ lexer.what() == CQLTokenizer.TT_OR ||
+ lexer.what() == CQLTokenizer.TT_NOT ||
+ lexer.what() == CQLTokenizer.TT_PROX ||
+ lexer.what() == CQLTokenizer.TT_SORTBY)) {
String symbol = lexer.value();
match(lexer.what());
return symbol;
if (argv.size() == 1) {
cql = (String) argv.get(0);
} else {
- byte[] bytes = new byte[10000];
+ BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
try {
- // Read in the whole of standard input in one go
- int nbytes = System.in.read(bytes);
+ // read a single line of input
+ cql = buff.readLine();
+ if (cql == null) {
+ System.err.println("Can't read query from stdin");
+ System.exit(2);
+ return;
+ }
} catch (IOException ex) {
System.err.println("Can't read query: " + ex.getMessage());
System.exit(2);
+ return;
}
- cql = new String(bytes);
}
CQLParser parser = new CQLParser(compat);
- CQLNode root = null;
+ CQLNode root;
try {
root = parser.parse(cql);
} catch (CQLParseException ex) {
System.err.println("Syntax error: " + ex.getMessage());
System.exit(3);
+ return; //compiler
} catch (IOException ex) {
System.err.println("Can't compile query: " + ex.getMessage());
System.exit(4);
+ return; //compiler
}
try {
if (mode == 'c') {
System.out.println(root.toCQL());
} else if (mode == 'p') {
+ try {
InputStream f = new FileInputStream(pfile);
- if (f == null)
- throw new FileNotFoundException(pfile);
-
Properties config = new Properties();
config.load(f);
f.close();
System.out.println(root.toPQF(config));
+ } catch (IOException ex) {
+ System.err.println("Can't load PQF properties:" +
+ ex.getMessage());
+ System.exit(5);
+ }
} else {
System.out.print(root.toXCQL());
}
- } catch (IOException ex) {
- System.err.println("Can't render query: " + ex.getMessage());
- System.exit(5);
} catch (UnknownIndexException ex) {
System.err.println("Unknown index: " + ex.getMessage());
System.exit(6);
System.err.println("Unknown position: " + ex.getMessage());
System.exit(9);
} catch (PQFTranslationException ex) {
- // We catch all of this class's subclasses, so --
- throw new Error("can't get a PQFTranslationException");
+ System.err.println("Cannot translate to PQF: " + ex.getMessage());
+ System.exit(10);
}
}
}