📄 lexer.java
字号:
import java.io.IOException;
import java.io.Reader;
/**
*
* Course - CS601 OO Programming
* Instructor - Terence Parr
* Assignment - 4
*
* Class is responsible for return {@link Token} objects corresponding to
* whitespace, integers, addition and multiplication operators, and end of file
* marker
*
* Note: Ideas represented in this class have been recycled/refitted from Terence Parr's
* http://www.antlr.org/book/byhand.pdf. Thanks Terence
*/
public class Lexer {
// this serves as our grammar
public static final int MULTI = '*';
public static final int ADD = '+';
public static final int NEWLINE = '\n';
public static final int CARRIAGE = '\r';
public static final int TAB = '\t';
public static final int SPACE = ' ';
public static final int R_Parenthesis=')';
public static final int L_Parenthesis='(';
public static final int LESS='<';
public static final int EOF = -1;
int[] _buff;
final Reader _reader;
final int _lookahead;
public Lexer(Reader r, int lookahead) {
_reader = r;
_lookahead = lookahead;
initBuff();
}
/**
* Returns the token matched next. Completly consumes whitespace
* @param i One based token access
* @return
*/
public Token getToken() {
for (;;) {
int c = getChar(1);
if (c == MULTI) {
consume();
return new Token(Token.MULTIPLICATION, "*");
}
if (c == ADD) {
consume();
return new Token(Token.ADDITION, "+");
}
if (c == SPACE || c == NEWLINE || c == CARRIAGE || c == TAB) {
// don't return this
whiteSpace();
continue;
}
if(c==R_Parenthesis){
consume();
return new Token(Token.R_PARENTHESIS,")");
}
if(c==L_Parenthesis){
consume();
return new Token(Token.L_PARENTHESIS,"(");
}
if (Character.isDigit(c)) {
return integer();
}
if(c==LESS){
consume();
return new Token(Token.LESS,"<");
}
if (c == EOF) {
return new Token(Token.EOT, "");
}
return new Token(Token.UNDEFINED, ""+(char)c);
}
}
/**
* This is a 1 based accessor method to the buffer of characters being read
* from the stream of input
* @param i
* @return
*/
private int getChar(int i) {
return _buff[i-1];
}
/**
* Consumes the character at front of character buffer and advances the rest of
* the buffer by one
* @return Returns the consumed character from the buffer
*/
private char consume() {
char toReturn = (char) getChar(1);
for (int i = 1; i < _buff.length; i++) {
_buff[i-1] = _buff[i];
}
try {
_buff[_buff.length-1] = _reader.read();
} catch (IOException e) {
throw new RuntimeException("Error reading data", e);
}
return toReturn;
}
private void initBuff() {
_buff = new int[_lookahead];
for (int i = 0; i < _lookahead; i++) {
try {
int read = _reader.read();
_buff[i] = read;
} catch (IOException e) {
throw new RuntimeException("Error reading data", e);
}
}
}
private Token whiteSpace() {
StringBuffer sb = new StringBuffer();
while (getChar(1) == NEWLINE || getChar(1) == TAB
|| getChar(1) == SPACE || getChar(1) == CARRIAGE)
{
sb.append(consume());
}
return new Token(Token.WHITESPACE, sb.toString());
}
private Token integer() {
StringBuffer s = new StringBuffer();
while (Character.isDigit(getChar(1))) {
s.append(consume());
}
return new Token(Token.INTEGER, s.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -