AsciiMathParser.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  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 import org.qedeq.base.trace.Trace;
022 
023 /*
024  * LATER mime 20080131: refactor
025  *
026  */
027 
028 /**
029  * Parse term or formula data into {@link org.qedeq.kernel.bo.parser.Term}s.
030  * This parser uses simple ASCII text operators.
031  *
032  @author  Michael Meyling
033  */
034 public final class AsciiMathParser extends MathParser {
035 
036     /** This class. */
037     private static final Class CLASS = AsciiMathParser.class;
038 
039     /** Separators for tokens. */
040     private static final String SEPARATORS = "()[],{}";
041 
042     /**
043      * Constructor.
044      */
045     public AsciiMathParser() {
046         super();
047     }
048 
049     protected final String readToken() {
050         final String method = "readToken()";
051         int lines = 0;
052         while (getChar() != -&& Character.isWhitespace((chargetChar())) {
053             if ('\n' == (chargetChar()) {
054                 lines++;
055             }
056             readChar();
057         }
058         if (lines > 1) {
059             return "";
060         }
061         if (eof()) {
062             return null;
063         }
064         if (SEPARATORS.indexOf(getChar()) >= 0) {
065             Trace.param(CLASS, this, method, "Read token""" (chargetChar());
066             return "" (charreadChar();
067         }
068         final StringBuffer token = new StringBuffer();
069         String operator = null;
070         markPosition();
071         while (!eof() && !Character.isWhitespace((chargetChar())
072                 && SEPARATORS.indexOf(getChar()) 0) {
073             token.append((charreadChar());
074             if (null != getOperator(token.toString())) {
075                 operator = token.toString();
076                 clearMark();
077                 markPosition();
078             }
079         }
080         if (operator != null) {
081             rewindPosition();
082             token.setLength(0);
083             token.append(operator);
084         else {
085             clearMark();
086         }
087         Trace.param(CLASS, this, method, "Read token", token);
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 }