/*
 * LexanInput.java
 *
 * Created on October 29, 2005, 8:54 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */


package cz.pcland.functions.lexan;

import java.util.*;
/**
 *
 * @author petr
 */



public class LexanInput {
    private String expression;
    private int position;
    private int currentSymbol;
    private char currentChar;
    
    /** Creates a new instance of LexanInput */
    public LexanInput(String _expression) {
       //  System.out.println("create LecanInput " + expression);
        expression = _expression;
        position = 0;
    }
    
    public void putCharBack() {
        position = position - 1;
    }
    
    public int read() {
        //System.out.println("position= " + position + expression);
        if(position<expression.length()) {
            setCurrentChar();
            setCurrentSymbol();
            position++;
            
        } else { //last char is allready set
            currentSymbol = InputSymbol.INPUT_END;
            position++;
           
        }
        return currentSymbol;
    }
    
    public int getCurrentInputSymbol() {
        return currentSymbol;
    }
    
    public char getCurrentChar() {
        //System.out.println("input return " + currentChar);
        return currentChar;
    }
    
    private void setCurrentChar() {
        currentChar = expression.charAt(position);
    }
    
    private void setCurrentSymbol() {
        if((currentChar>='A' && currentChar<='Z') || (currentChar>='a' && currentChar<='z')){
            currentSymbol = InputSymbol.INPUT_CHAR;
        } else 
        if (currentChar>='0' && currentChar<='9') {
            currentSymbol = InputSymbol.INPUT_NUMBER;
        } else
        if(currentChar == ' ') {
            currentSymbol = InputSymbol.INPUT_SPACE;
        } else
        if(currentChar == '+') {
            currentSymbol = InputSymbol.INPUT_PLUS;
        } else
        if(currentChar == '-') {
            currentSymbol = InputSymbol.INPUT_MINUS;
        } else
        if(currentChar == '*') {
            currentSymbol = InputSymbol.INPUT_ASTERISK;
        } else
        if(currentChar == '/') {
            currentSymbol = InputSymbol.INPUT_SLASH;
        } else
        if(currentChar == '(') {
            currentSymbol = InputSymbol.INPUT_RIGHT_PARENTHESIS;
        } else
        if(currentChar == ')') {
            currentSymbol = InputSymbol.INPUT_LEFT_PARENTHESIS;
        } else
        if(currentChar == '^') {
            currentSymbol = InputSymbol.INPUT_POWER;
        }else 
        if(currentChar == ',') {
            currentSymbol = InputSymbol.INPUT_COMMA;
        }else 
        if(currentChar == '=') {
            currentSymbol = InputSymbol.INPUT_EQUAL;
        } else 
        if(currentChar == '.') {
            currentSymbol = InputSymbol.INPUT_POINT;
        } else 
        if(currentChar == '!') {
            currentSymbol = InputSymbol.INPUT_SCREAMER;
        }else currentSymbol = InputSymbol.INPUT_UNKNOWN;

    }
    
}
