package uloha2;
import java.io.*;

public class Scanner {

	public Scanner(Reader inp)
	{
		this.inp = new LineNumberReader(inp);
		getch();
	}

	public String getStringAttr()
	{
		return stringAttr;
	}
	
	public int getNumberAttr()
	{
		return numberAttr;
	}

	public int nextToken()
	{
		for(;;) {
			skipSpaces();
			lineNo = inp.getLineNumber();
			
			if( isEof )
				return Tokens.EOF;
			
			if( Character.isLetter(ch) ) {
				StringBuffer buf = new StringBuffer();
				do {
					buf.append(ch);
					getch();
				} while( !isEof && Character.isLetterOrDigit(ch) );
				stringAttr = buf.toString();
				if( stringAttr.compareToIgnoreCase("print") == 0 )
					return Tokens.PRINT;
				return Tokens.IDENT;
			}
			
			if( Character.isDigit(ch) ) {
				StringBuffer buf = new StringBuffer();
				do {
					buf.append(ch);
					getch();
				} while( !isEof && Character.isDigit(ch) );
				numberAttr = Integer.parseInt(buf.toString());
				return Tokens.NUMBER;
			}			
			
			if( ch == '/' ) {
				getch();
				if( ch == '/' ) {
					do {
						getch();
					} while( !isEof && ch != '\n' );
					continue;
				}
				return '/';
			}
			
			int t = ch;
			getch();
			return t;
		}
	}

	private void skipSpaces()
	{
		while( !isEof && Character.isWhitespace(ch) ) 
			getch();
	}

	private void getch()
	{
		int c;
		try { 
			c = inp.read();
		} catch( IOException e ) {
			c = -1;
		}
		isEof = c < 0;
		ch = (char)c;
	}

	private LineNumberReader inp;
	private boolean isEof = false;
	private char ch;
	private int lineNo = 1;
	private String stringAttr;
	private int numberAttr;
}

