Mark syntax errors
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLParser.java
index 31bd7fb..8a0fc17 100644 (file)
@@ -1,12 +1,12 @@
 
 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.Reader;
-import java.io.StringReader;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -219,7 +219,8 @@ public class CQLParser {
        throws CQLParseException, IOException {
        debug("in parseTerm()");
 
-       String word;
+       String first;
+        StringBuilder all;
        while (true) {
            if (lexer.what() == '(') {
                debug("parenthesised term");
@@ -232,16 +233,23 @@ public class CQLParser {
            }
 
            debug("non-parenthesised term");
-           word = matchSymbol("index or term");
-            while (lexer.what() == CQLTokenizer.TT_WORD && !isRelation()) {
-              word = word + " " + lexer.value();
-              match(CQLTokenizer.TT_WORD);
+           first = matchSymbol("index or term");
+            all = new StringBuilder(first);
+            //match relation only on second postion
+            while (isWordOrString() && (all.length() > first.length() || !isRelation())) {
+              all.append(" ").append(lexer.value());
+              match(lexer.what());
             }
 
            if (!isRelation())
-               break;
-
-           index = word;
+              break; //we're done if no relation
+           
+            //we have relation, but it only makes sense if preceded by a single term
+            if (all.length() > first.length()) {
+              throw new CQLParseException("unexpected relation '"+lexer.value()+"'"
+                , lexer.pos());
+            }
+            index = first;
            String relstr = (lexer.what() == CQLTokenizer.TT_WORD ?
                             lexer.value() : lexer.render(lexer.what(), false));
            relation = new CQLRelation(relstr);
@@ -251,8 +259,7 @@ public class CQLParser {
            debug("index='" + index + ", " +
                  "relation='" + relation.toCQL() + "'");
        }
-
-       CQLTermNode node = new CQLTermNode(index, relation, word);
+       CQLTermNode node = new CQLTermNode(index, relation, all.toString());
        debug("made term node " + node.toCQL());
        return node;
     }
@@ -276,6 +283,11 @@ public class CQLParser {
 
        return new CQLPrefixNode(name, identifier, node);
     }
+    
+    private boolean isWordOrString() {
+      return CQLTokenizer.TT_WORD == lexer.what() 
+        || CQLTokenizer.TT_STRING == lexer.what();
+    }
 
     private boolean isRelation() {
        debug("isRelation: checking what()=" + lexer.what() +
@@ -324,12 +336,10 @@ public class CQLParser {
 
        debug("in matchSymbol()");
        if (lexer.what() == CQLTokenizer.TT_WORD ||
-           lexer.what() == '"' ||
+           lexer.what() == CQLTokenizer.TT_STRING ||
            // 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.
             (allowKeywordTerms &&
            lexer.what() == CQLTokenizer.TT_AND ||
            lexer.what() == CQLTokenizer.TT_OR ||
@@ -446,47 +456,59 @@ public class CQLParser {
        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());
+            StringBuilder space = new StringBuilder(cql.length());
+            System.out.println(cql);
+            for (int i=0; i<ex.getPosition(); i++) space.append(" ");
+            space.append("^");
+            System.err.println(space.toString());
            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);
@@ -501,8 +523,8 @@ public class CQLParser {
            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);
        }
     }
 }