public void getToken(FileReader fd) { /* index for storing into tokenString */ int tokenStringIndex = 0; char[] tokenString = new char[40]; //Holds current token TokenType currentToken = TokenType.ERROR; /* current state - always begins at START */ StateType state = StateType.START; /* flag to indicate save to tokenString */ Boolean save; while (state != StateType.DONE) { if (!(fd.lineno < fd.lines)) { currentToken = TokenType.T_ENDL; break; } char c = fd.getNextChar(); save = true; switch (state) { case StateType.START: if (char.IsDigit(c)) { state = StateType.INNUM; } else if (char.IsLetter(c)) { state = StateType.INID; } else if (c == ':') { state = StateType.INASSIGN; } else if ((c == ' ') || (c == '\t') || (c == '\n')) { save = false; } else if (c == '"') { StringQuotesCount++; state = StateType.STRING; } else if (c == '/') { state = StateType.COMMENTDIV; } else if (c == '&') { state = StateType.ANDLOGIC; } else if (c == '|') { state = StateType.ORLOGIC; } else { state = StateType.DONE; switch (c) { case '=': currentToken = TokenType.T_EQUALS; break; case '<': currentToken = TokenType.T_LESSTHAN; break; case '>': currentToken = TokenType.T_LARGERTHAN; break; case '+': currentToken = TokenType.T_PLUS; break; case '-': currentToken = TokenType.T_MINUS; break; case '*': currentToken = TokenType.T_TIMES; break; case '(': LeftParanthesisCount++; currentToken = TokenType.T_LEFTPAREN; break; case ')': RightParanthesisCount++; currentToken = TokenType.T_RIGHTPAREN; break; case ',': currentToken = TokenType.T_COMMA; break; case '{': LeftBracesCount++; currentToken = TokenType.T_LBRACES; break; case '}': RightBracesCount++; currentToken = TokenType.T_RBRACES; break; case ';': currentToken = TokenType.T_SEMICOLON; break; default: currentToken = TokenType.IllegalChar; break; } } break; case StateType.INCOMMENT: save = false; if (c == '*') { state = StateType.ENDCOMMENT; } break; case StateType.COMMENTDIV: if (c == '*') { save = false; state = StateType.INCOMMENT; tokenStringIndex--; } else { currentToken = TokenType.T_OVER; save = false; fd.linepos--; state = StateType.DONE; } break; case StateType.ENDCOMMENT: if (c == '/') { save = false; state = StateType.START; } else { state = StateType.INCOMMENT; } break; case StateType.ANDLOGIC: state = StateType.DONE; if (c == '&') { currentToken = TokenType.AND; } else { fd.linepos--; save = false; currentToken = TokenType.ERROR; } break; case StateType.ORLOGIC: state = StateType.DONE; if (c == '|') { currentToken = TokenType.OR; } else { fd.linepos--; save = false; currentToken = TokenType.ERROR; } break; case StateType.STRING: if (c == '"') { StringQuotesCount++; state = StateType.DONE; currentToken = TokenType.T_STRING; } break; case StateType.INASSIGN: state = StateType.DONE; if (c == '=') { currentToken = TokenType.T_ASSIGN; } else { /* backup in the input */ fd.linepos--; save = false; currentToken = TokenType.ERROR; } break; case StateType.INNUM: if (c == '.') { state = StateType.INDECIMAL; } else if (char.IsLetter(c)) { currentToken = TokenType.ERROR; state = StateType.DONE; } else if (!char.IsDigit(c)) { /* backup in the input */ fd.linepos--; save = false; state = StateType.DONE; currentToken = TokenType.NUMBER; } break; case StateType.INDECIMAL: if (!char.IsDigit(c)) { /* backup in the input */ fd.linepos--; save = false; state = StateType.DONE; currentToken = TokenType.NUMBER; } break; case StateType.INID: if (char.IsDigit(c)) { state = StateType.ID; } else if (!char.IsLetter(c)) { /* backup in the input */ fd.linepos--; save = false; state = StateType.DONE; currentToken = ReservedWordsLookup(new string(tokenString).Trim('\0')); } break; case StateType.ID: if (!(char.IsLetter(c) || char.IsDigit(c))) { /* backup in the input */ fd.linepos--; save = false; currentToken = TokenType.ID; state = StateType.DONE; } break; case StateType.DONE: default: /* should never happen */ Console.Write("Scanner Bug: state= %d\n", state); state = StateType.DONE; currentToken = TokenType.ERROR; break; } if (save) { if (!(tokenStringIndex > 39)) { tokenString[tokenStringIndex++] = c; } } if (state == StateType.DONE) { if (tokenStringIndex > 39) { currentToken = TokenType.TokenLengthExceeded; } addScanned(tokenString, currentToken); } } }
//Class Constructor to Asssign the FileReader public Scanner(FileReader fd) { this.fd = fd; }