/*
 * Lexan.java
 *
 * Created on May 23, 2006, 12:06 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package cz.pcland.scripting;

import cz.pcland.functions.syntax.*;
/**
 *
 * @author petr
 */
public class Lexan {
    String data;
    int position = 0;
    
    /** Creates a new instance of Lexan */
    public Lexan(String str) {
        this.data = str;
    }
    
    public char lookAtChar() {
        return data.charAt(position);
    }
    
    public boolean isEnd(){
        if(position < data.length()) skipWhiteandComments();
        return position >= data.length();
    }
    
    public boolean skipWhiteandComments() {
       
        boolean skipped = false;
        while((position < data.length()) && (data.charAt(position) == '#' || data.charAt(position) == ' ' || data.charAt(position) == '\n')) {
            if(data.charAt(position) == '#') {
                skipComment();
                skipped = true;
            }
            if(data.charAt(position) == ' ' || data.charAt(position) == '\n'){
                skipWhite();
                skipped = true;
            }
            
            
        }
        return skipped;
    }
    
    public char getNextChar() {
        return data.charAt(position++);
    }
    
    public void putBack() {
        position--;
    }
    
    public void skipWhite() {
       
        while( (position < data.length()) && (data.charAt(position) == ' ' || data.charAt(position) == '\n')) {
            ++position;
        }
    }
    
    public void skipComment() {
        while((position < data.length()) && (data.charAt(position) != '\n')) {
            ++position;
        }
    }
    
    public boolean isDigit(char ch) {
        return (ch >= '0' && ch <= '9');
    }
    
    public boolean isChar(char ch) {
        return (ch != ' ' && ch!= '\n' && ch != '#' && ch!='{' && ch != '}' && ch != '"');
    }
    
    public String  readCommand() {
        char ch;
        String res = "";
        
        skipWhiteandComments();
        char next = getNextChar();

        while(isChar(next)){
            res += next;
            next = getNextChar();
        }
        
        putBack();
        skipWhiteandComments();

        return res;
    }
    
    public String readQuotes(){
        char ch;
        String res = "";
        
        
        skipWhiteandComments();
        char next = getNextChar();

        if(next != '"') {
            return res;
        }
        
        while( isChar(ch = getNextChar()) || ch == ' ' || isDigit(ch)){
            res += ch;
        }
        
        if(lookAtChar() == '"') {
            return res;
        }
        
        skipWhiteandComments();

        return res;
    }
    
    public int readInt() {
        char ch;
        String res = "";
        
        
        skipWhiteandComments();
        char next = getNextChar();

        while(isDigit(next)){
            res += next;
            next = getNextChar();
        }
        
        putBack();
        skipWhiteandComments();

        return Integer.parseInt(res);
    }
    
    public double readDouble() {
        char ch;
        String res = "";
        
        skipWhiteandComments();
        char next = getNextChar();

        
        while(isDigit(next) || next == '.'){
            res += next;
            next = getNextChar();
        }
        
        putBack();
        skipWhiteandComments();

        return Double.parseDouble(res);
    }
    
    public String readTillParenthesis() {
        char ch;
        String res = "";

        
        skipWhiteandComments();
        char next = getNextChar();

        
        res += next;
        
        while(true){
            ch = getNextChar();
            if(ch == '}')break;
            res += ch;

        }
        
        putBack();
        skipWhiteandComments();

        return res;
    }
    
    public String readString() {
        
        char ch;
        String res = "";
        
        skipWhiteandComments();
        char next = getNextChar();
        
        while(isChar(next)){
            res += next;
            next = getNextChar();
        }
        
        putBack();
        skipWhiteandComments();

        return res;
    }
    
    
    
    public boolean  getBoolean() {
        
        skipWhiteandComments();
        char next = getNextChar();
             
        if(next == '1') {

            return true;
            
        } else {

            return false;
        }
        
    }
    
}
