SimpleMathParser.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.parser;
017 
018 import java.util.ArrayList;
019 import java.util.List;
020 
021 /**
022  * Parse term or formula data into {@link org.qedeq.kernel.bo.parser.Term}s.
023  * This parser uses simple ASCII text operators.
024  *
025  @version $Revision: 1.1 $
026  @author  Michael Meyling
027  */
028 public class SimpleMathParser extends MathParser {
029 
030     /** Characters that are always tokens itself. */
031     private static final String SEPARATORS = "()[],{}";
032 
033     /**
034      * Constructor.
035      */
036     public SimpleMathParser() {
037         super();
038 /*
039         operators.add(new Operator("~",      "NOT",   110, 1, 1, 1));
040         operators.add(new Operator("-",      "NOT",   110, 1, 1, 1));
041         operators.add(new Operator("&",      "AND",   100, 0, 2));
042         operators.add(new Operator("|",      "OR",     90, 0, 2));
043 //        operators.put("v",      new Operator("v",      "OR",     90, 0, 2));
044         operators.add(new Operator("->",     "IMPL",   80, 0, 2, 2));
045         operators.add(new Operator("=>",     "IMPL",   80, 0, 2, 2));
046         operators.add(new Operator("<->",    "EQUI",   80, 0, 2));
047         operators.add(new Operator("<=>",    "EQUI",   80, 0, 2));
048         operators.add(new Operator("all",    "ALL",    40, 1, 2, 3));
049         operators.add(new Operator("exists", "EXISTS", 40, 1, 2, 3));
050         operators.add(new Operator("in",     "IN",    200, 0, 2, 2));
051         operators.add(new Operator("=",      "EQUAL", 200, 0, 2));
052         operators.add(new Operator("{", ",", "}", "SET", 200, 0));
053         operators.add(new Operator("{", ":", "}", "SETPROP", 200, 2, 2));
054 */
055     }
056 
057     protected final String readToken() {
058         int lines = 0;
059         while (getChar() != -&& Character.isWhitespace((chargetChar())) {
060             if ('\n' == (chargetChar()) {
061                 lines++;
062             }
063             readChar();
064         }
065         if (lines > 1) {
066             return "";
067         }
068         if (eof()) {
069             return null;
070         }
071         if (SEPARATORS.indexOf(getChar()) >= 0) {
072             return "" (charreadChar();
073         }
074         final StringBuffer token = new StringBuffer();
075         while (!eof() && !Character.isWhitespace((chargetChar())
076                 && SEPARATORS.indexOf(getChar()) 0) {
077             token.append((charreadChar());
078             if (null != getOperator(token.toString())) {
079                 if (getChar() >= 0) {
080                     final char c = (chargetChar();
081                     if (null != getOperator(token.toString() + c)) {
082                         continue;
083                     }
084                 }
085                 break;
086             }
087         }
088         return token.toString();
089     }
090 
091     protected final Operator getOperator(final String token) {
092         Operator result = null;
093         if (token == null) {
094             return result;
095         }
096         for (int i = 0; i < getOperators().size(); i++) {
097             if (token.equals(((OperatorgetOperators().get(i)).getStartSymbol())) {
098                 result = (OperatorgetOperators().get(i);
099                 break;
100             }
101         }
102         return result;
103     }
104 
105     protected final List getOperators(final String token) {
106         final List result = new ArrayList();
107         if (token == null) {
108             return result;
109         }
110         for (int i = 0; i < getOperators().size(); i++) {
111             if (token.equals(((OperatorgetOperators().get(i)).getStartSymbol())) {
112                 result.add(getOperators().get(i));
113             }
114         }
115         return result;
116     }
117 
118     protected boolean eot(final String token) {
119         return token == null || token.trim().length() == 0;
120     }
121 
122 }