📄 lexemreader.java
字号:
import java.io.*;
/** Internal class: Used by the scanner * */
public class LexemReader {
private final int TABSKIP = 4; // number of characters to be counted for a
// tabular character
// possible to make this a variable and pass the value to the
// Scanner/LexemReader constructor
private Reader reader; // character stream containg the source
private int line; // current line within file
private int pos; // position of the current character within the current
// line
private int lookAhead = -2; // -2 ==> no character has been peeked at
LexemReader(String fileName) throws ScannerException {
try {
reader = new FileReader(fileName);
} catch (FileNotFoundException e) {
throw new ScannerException(line, pos, "File <" + fileName
+ "> not found");
}
line = 1;
pos = 0;
}
int line() {
return line;
}
int pos() {
return pos;
}
// read a character from the stream without interpreting it
int fetchCharacter() throws ScannerException {
int ch;
try {
ch = reader.read();
} catch (IOException e) {
throw new ScannerException(line, pos, "IO Error");
}
return ch;
}
// enable peeking at the very next character
// store it so that it will be used as the next logical character
char followingCharacter() throws ScannerException {
lookAhead = fetchCharacter();
return (char) lookAhead;
}
// read the next character, interpreting it in terms of updating the
// position
// take a possible previous lookahead read into account
// this and the next method return an integer result because EOF is returned
// as -1
int nextCharacter() throws ScannerException {
int ch;
if (lookAhead != -2) {
ch = lookAhead;
lookAhead = -2;
} else
ch = fetchCharacter();
// update position markers
switch (ch) {
case '\t':
pos += TABSKIP;
break;
case '\r':
break;
case '\n':
line++;
pos = 0;
break;
default:
pos++;
}
return ch;
}
// read the next character skipping all whitespace in the process
int nextNonWhiteCharacter() throws ScannerException {
int ch;
do {
ch = nextCharacter();
} while (isWhiteSpace((char) ch));
return ch;
}
// answer whether the argument is a whitespace character
private boolean isWhiteSpace(char c) {
switch (c) {
case ' ':
case '\t':
case '\r':
case '\n':
return true;
default:
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -