From ba96cf25453ec7f3f770a4dcad3bf145354b83e8 Mon Sep 17 00:00:00 2001 From: Jakub Skoczen Date: Thu, 19 Jan 2012 13:01:14 +0100 Subject: [PATCH] Add ability to parse from char streams --- src/main/java/org/z3950/zing/cql/CQLLexer.java | 10 ++++++++-- src/main/java/org/z3950/zing/cql/CQLParser.java | 24 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/z3950/zing/cql/CQLLexer.java b/src/main/java/org/z3950/zing/cql/CQLLexer.java index 7bbf115..69211c8 100644 --- a/src/main/java/org/z3950/zing/cql/CQLLexer.java +++ b/src/main/java/org/z3950/zing/cql/CQLLexer.java @@ -1,6 +1,8 @@ // $Id: CQLLexer.java,v 1.14 2007-07-03 13:30:42 mike Exp $ package org.z3950.zing.cql; +import java.io.InputStream; +import java.io.Reader; import java.io.StreamTokenizer; import java.io.StringReader; @@ -56,8 +58,12 @@ class CQLLexer extends StreamTokenizer { // Controls debugging output private static boolean DEBUG; - CQLLexer(String cql, boolean lexdebug) { - super(new StringReader(cql)); + public CQLLexer(String cql, boolean lexdebug) { + this(new StringReader(cql), DEBUG); + } + + CQLLexer(Reader cql, boolean lexdebug) { + super(cql); wordChars('!', '?'); // ASCII-dependency! wordChars('[', '`'); // ASCII-dependency! quoteChar('"'); diff --git a/src/main/java/org/z3950/zing/cql/CQLParser.java b/src/main/java/org/z3950/zing/cql/CQLParser.java index be365de..9970aa7 100644 --- a/src/main/java/org/z3950/zing/cql/CQLParser.java +++ b/src/main/java/org/z3950/zing/cql/CQLParser.java @@ -6,6 +6,8 @@ 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.List; @@ -55,6 +57,26 @@ public class CQLParser { if (DEBUG) System.err.println("PARSEDEBUG: " + str); } + + /** + * Compiles a CQL query. + *

+ * The resulting parse tree may be further processed by hand (see + * the individual node-types' documentation for details on the + * data structure) or, more often, simply rendered out in the + * desired form using one of the back-ends. toCQL() + * returns a decompiled CQL query equivalent to the one that was + * compiled in the first place; toXCQL() returns an + * XML snippet representing the query; and toPQF() + * returns the query rendered in Index Data's Prefix Query + * Format. + * + * @param cql The query + * @return A CQLNode object which is the root of a parse + * tree representing the query. */ + public CQLNode parse(String cql) throws CQLParseException, IOException { + return parse(new StringReader(cql)); + } /** * Compiles a CQL query. @@ -72,7 +94,7 @@ public class CQLParser { * @param cql The query * @return A CQLNode object which is the root of a parse * tree representing the query. */ - public CQLNode parse(String cql) + public CQLNode parse(Reader cql) throws CQLParseException, IOException { lexer = new CQLLexer(cql, LEXDEBUG); -- 1.7.10.4