itec.minicompiler
Class Scanner

java.lang.Object
  extended byitec.minicompiler.Scanner
All Implemented Interfaces:
ConstantsInterface

public class Scanner
extends java.lang.Object
implements ConstantsInterface

The Scanner implements the actions necessary to extract the Token out of the source code. These are reading the source code characterwise and mainly distinguishing between SimpleToken, NumberToken and IdentToken and handling the comments.

Version:
1.0 (29.09.2004)
Author:
Stefan Leitner, Stefan Schauer

Field Summary
private  char actChar
          The character the Scanner just works on
private  int column
          The actual column in sourceCode
private  java.util.HashSet digits
          The set of digits
private  ErrorHandler errorhandler
          The Errorhandler used by the actual Scanner
private  java.util.HashSet keywords
          The set of keywords
private  java.util.HashSet letters
          The set of letters
private  int line
          The actual line in sourceCode
private  int position
          The next position in sourceCode (column number)
private  java.util.HashSet singles
          The set of special characters
private  java.lang.String sourceCode
          The source code the Scanner works on
 
Fields inherited from interface itec.minicompiler.ConstantsInterface
ACTUALPARAMETERS, AND, ARRAY, ARRAYTYPE, ASSIGNMENT, BECOMES, BEGIN, COLON, COMMA, CONST, DECLARATIONS, DIV, DO, ELSE, ELSIF, EMPTY, END, EPSILON, EQUAL, ERR_EXP_ARRAY, ERR_EXP_BECOMES, ERR_EXP_COLON, ERR_EXP_COMMA, ERR_EXP_DO, ERR_EXP_END, ERR_EXP_EQUAL, ERR_EXP_IDENT, ERR_EXP_IF, ERR_EXP_LPAREN, ERR_EXP_MODULE, ERR_EXP_OF, ERR_EXP_PERIOD, ERR_EXP_PROCEDURE, ERR_EXP_RBRAK, ERR_EXP_RECORD, ERR_EXP_REPEAT, ERR_EXP_RPAREN, ERR_EXP_SEMICOLON, ERR_EXP_THEN, ERR_EXP_UNTIL, ERR_EXP_WHILE, ERR_INV_FACTOR, ERR_INV_STATEMENT, ERR_INV_TYPE, ERR_INVALID_CHARACTER, ERR_NOT_EXP_DECLARATIONS, ERR_NOT_EXP_FACTOR, ERR_NOT_EXP_STATEMENT, ERR_NOT_EXP_TYPE, ERR_NOT_TERMINATED, ERR_NUMBER_TOO_HIGH, EXPRESSION, FACTOR, FIELDLIST, FORMALPARAMETERS, FPSECTION, GEQ, GREATER, IDENT, IDENTLIST, IF, IFSTATEMENT, KEYWORD, LBRAK, LEQ, LESS, LPAREN, MINICOMP, MINUS, MOD, MODULE, NOT, NOTEQUAL, NUMBER, OF, OR, PERIOD, PLUS, PROCEDURE, PROCEDUREBODY, PROCEDURECALL, PROCEDUREDECLARATION, PROCEDUREHEADING, RBRAK, RECORD, RECORDTYPE, REPEAT, REPEATSTATEMENT, RPAREN, SELECTOR, SEMICOLON, SIMPLEEXPRESSION, SINGLE, STATEMENT, STATEMENTSEQUENCE, TERM, THEN, TIMES, TYP, TYPE, UNTIL, VAR, WHILE, WHILESTATEMENT
 
Constructor Summary
Scanner(java.lang.String sc, ErrorHandler e)
          Initialize the Scanner with a source code sc and an ErrorHandler e.
 
Method Summary
private  void comment()
          Ignore any comment, i.e. read until a close comment (*)) is found or the end of sourceCode is reached
 int getColumn()
          Return the column of the actual Scanner.
 int getLine()
          Return the line of the actual Scanner.
private  Token ident()
          Get the identifier or keyword starting with actChar
private  void initDigits()
          Initialize the set of digits.
private  void initKeywords()
          Initialize the set of keywords.
private  void initLetters()
          Initialize the set of letters.
private  void initSingles()
          Initialize the set of special characters.
private  void nextChar()
          Get the next character from sourceCode.
 Token nextToken()
          Get the next token from sourceCode.
private  NumberToken number()
          Get the number starting with actChar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keywords

private java.util.HashSet keywords
The set of keywords


letters

private java.util.HashSet letters
The set of letters


digits

private java.util.HashSet digits
The set of digits


singles

private java.util.HashSet singles
The set of special characters


errorhandler

private ErrorHandler errorhandler
The Errorhandler used by the actual Scanner


sourceCode

private java.lang.String sourceCode
The source code the Scanner works on


position

private int position
The next position in sourceCode (column number)


line

private int line
The actual line in sourceCode


column

private int column
The actual column in sourceCode


actChar

private char actChar
The character the Scanner just works on

Constructor Detail

Scanner

public Scanner(java.lang.String sc,
               ErrorHandler e)
Initialize the Scanner with a source code sc and an ErrorHandler e. Furthermore set the position, the line and column to the right value in he source code (i.e. at the position 1:0).

Parameters:
sc - The source code the Scanner should work on
e - The ErrorHandler the Scanner should use
Method Detail

getLine

public int getLine()
Return the line of the actual Scanner.

Returns:
The line the Scanner is currently working in

getColumn

public int getColumn()
Return the column of the actual Scanner.

Returns:
The column the Scanner is currently working in

initKeywords

private void initKeywords()
Initialize the set of keywords.


initLetters

private void initLetters()
Initialize the set of letters.


initDigits

private void initDigits()
Initialize the set of digits.


initSingles

private void initSingles()
Initialize the set of special characters.


nextChar

private void nextChar()
Get the next character from sourceCode.


number

private NumberToken number()
Get the number starting with actChar.

Returns:
A NumberToken containing the value of the whole number

ident

private Token ident()
Get the identifier or keyword starting with actChar

Returns:
A SimpleToken if a keyword is found, an IdentToken otherwise

comment

private void comment()
Ignore any comment, i.e. read until a close comment (*)) is found or the end of sourceCode is reached


nextToken

public Token nextToken()
Get the next token from sourceCode.

Returns:
An IdentToken if an identifier is found, a NumberToken if a number is found or a SimpleToken if a keyword or an operation is found. If the end of sourceCode is reached, a SimpleToken "EMPTY" is returned.