Main Page | Packages | Class Hierarchy | Class List | File List | Class Members

xmlexpr.Scanner Class Reference

List of all members.

Detailed Description

Scanner.

Read an expression (e.g., 3 + 4 * x) and return a set of tokens that represent the components of the expression.

Author:
Ian Kaplan, iank@bearcave.com, Jul 13, 2004


Public Member Functions

 Scanner (String exp)
Token getToken ()
 Return the next expression token.
void pushToken (Token t)
 "Push" a token back into the token stream.
boolean isEOL ()

Private Member Functions

void putChar (char ch)
 Add a character to the array that contains the token string.
void getIdent (char ch)
 Copy a string of identifier characters into the token character array.
void getInt (char ch)
 Copy a string of numbers into the token character array.
void skipSpaces ()
 Skip white space characters.
TokenType lexer ()
 The lexer method does simple "lexical analysis", which divides the character stream into a set of tokens.

Private Attributes

String mExp = null
int mExpLen = 0
boolean mEOL = false
int mCursor = 0
char[] mCharBuf = new char[ 80 ]
int mBufIx = 0
LinkedList mPushList = new LinkedList()
 A First In, First Out list for tokens which have been "pushed back".


Constructor & Destructor Documentation

xmlexpr.Scanner.Scanner String  exp  ) 
 

00145 { 00146 mExp = exp; 00147 mExpLen = exp.length(); 00148 } // Scanner


Member Function Documentation

void xmlexpr.Scanner.getIdent char  ch  )  [private]
 

Copy a string of identifier characters into the token character array.

00050 { 00051 while (mCursor < mExpLen && Character.isLetterOrDigit( ch )) { 00052 putChar( ch ); 00053 mCursor++; 00054 if (mCursor < mExpLen) { 00055 ch = mExp.charAt( mCursor ); 00056 } 00057 } 00058 mCursor--; 00059 } // getIdent

void xmlexpr.Scanner.getInt char  ch  )  [private]
 

Copy a string of numbers into the token character array.

00066 { 00067 while (mCursor < mExpLen && Character.isDigit( ch )) { 00068 putChar( ch ); 00069 mCursor++; 00070 if (mCursor < mExpLen) { 00071 ch = mExp.charAt( mCursor ); 00072 } 00073 } 00074 mCursor--; 00075 } // getInt

Token xmlexpr.Scanner.getToken  ) 
 

Return the next expression token.

00155 { 00156 Token tok = null; 00157 if (mPushList.size() == 0) { 00158 TokenType type = null; 00159 if (isEOL()) { 00160 type = TokenType.EOL; 00161 } 00162 else { 00163 type = lexer(); 00164 } 00165 tok = new Token( type ); 00166 if (type == TokenType.IDENT || type == TokenType.INT) { 00167 String str = new String( mCharBuf, 0, mBufIx ); 00168 tok.setString( str ); 00169 } 00170 } 00171 else { 00172 tok = (Token)mPushList.removeFirst(); 00173 } 00174 return tok; 00175 } // getToken

boolean xmlexpr.Scanner.isEOL  ) 
 

Returns:
true, if the scanner has reached the end of the line (expression)
00194 { 00195 return mEOL; 00196 } // isEOL

TokenType xmlexpr.Scanner.lexer  )  [private]
 

The lexer method does simple "lexical analysis", which divides the character stream into a set of tokens.

00095 { 00096 boolean start = true; 00097 TokenType type = TokenType.NULL_TYPE; 00098 skipSpaces(); 00099 mBufIx = 0; 00100 if (mCursor < mExpLen) { 00101 char ch = mExp.charAt( mCursor ); 00102 if (Character.isLetter( ch )) { 00103 getIdent( ch ); 00104 type = TokenType.IDENT; 00105 } 00106 else if (Character.isDigit( ch )) { 00107 getInt( ch ); 00108 type = TokenType.INT; 00109 } 00110 else if (ch == '=') { 00111 type = TokenType.EQUAL; 00112 } 00113 else if (ch == '+') { 00114 type = TokenType.PLUS; 00115 } 00116 else if (ch == '-') { 00117 type = TokenType.MINUS; 00118 } 00119 else if (ch == '*') { 00120 type = TokenType.TIMES; 00121 } 00122 else if (ch == '/') { 00123 type = TokenType.DIV; 00124 } 00125 else if (ch == '%') { 00126 type = TokenType.MOD; 00127 } 00128 else if (ch == '(') { 00129 type = TokenType.LPAREN; 00130 } 00131 else if (ch == ')') { 00132 type = TokenType.RPAREN; 00133 } 00134 mCursor++; 00135 } 00136 else { 00137 mEOL = true; 00138 type = TokenType.EOL; 00139 } 00140 return type; 00141 } // lexer

void xmlexpr.Scanner.pushToken Token  t  ) 
 

"Push" a token back into the token stream.

If a token is pushed back and then getToken() is called, the pushed token will the the token returned by getToken().

00185 { 00186 mPushList.add( t ); 00187 } // pushToken

void xmlexpr.Scanner.putChar char  ch  )  [private]
 

Add a character to the array that contains the token string.

00040 { 00041 mCharBuf[ mBufIx ] = ch; 00042 mBufIx++; 00043 }

void xmlexpr.Scanner.skipSpaces  )  [private]
 

Skip white space characters.

00082 { 00083 while (mCursor < mExpLen && 00084 Character.isWhitespace( mExp.charAt( mCursor ) ) ) { 00085 mCursor++; 00086 } 00087 } // skipSpaces


Member Data Documentation

int xmlexpr.Scanner.mBufIx = 0 [private]
 

char [] xmlexpr.Scanner.mCharBuf = new char[ 80 ] [private]
 

int xmlexpr.Scanner.mCursor = 0 [private]
 

boolean xmlexpr.Scanner.mEOL = false [private]
 

String xmlexpr.Scanner.mExp = null [private]
 

int xmlexpr.Scanner.mExpLen = 0 [private]
 

LinkedList xmlexpr.Scanner.mPushList = new LinkedList() [private]
 

A First In, First Out list for tokens which have been "pushed back".


The documentation for this class was generated from the following file:
Generated on Sat Aug 28 13:09:43 2004 for XmlExpr by doxygen 1.3.8