feat: OperatorTokenScanner 重构为状态机

This commit is contained in:
Luke 2025-07-01 17:14:40 +08:00
parent dbc3ea0a33
commit 367ae8653e

View File

@ -45,8 +45,11 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
@Override @Override
protected Token scanToken(LexerContext ctx, int line, int col) { protected Token scanToken(LexerContext ctx, int line, int col) {
char c = ctx.advance(); char c = ctx.advance();
String lexeme; String lexeme = String.valueOf(c);
TokenType type; TokenType type = TokenType.UNKNOWN;
// 当前状态
State currentState = State.OPERATOR;
switch (c) { switch (c) {
case '=': case '=':
@ -54,7 +57,6 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
lexeme = "=="; lexeme = "==";
type = TokenType.DOUBLE_EQUALS; type = TokenType.DOUBLE_EQUALS;
} else { } else {
lexeme = "=";
type = TokenType.EQUALS; type = TokenType.EQUALS;
} }
break; break;
@ -64,7 +66,6 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
lexeme = "!="; lexeme = "!=";
type = TokenType.NOT_EQUALS; type = TokenType.NOT_EQUALS;
} else { } else {
lexeme = "!";
type = TokenType.NOT; type = TokenType.NOT;
} }
break; break;
@ -74,7 +75,6 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
lexeme = ">="; lexeme = ">=";
type = TokenType.GREATER_EQUAL; type = TokenType.GREATER_EQUAL;
} else { } else {
lexeme = ">";
type = TokenType.GREATER_THAN; type = TokenType.GREATER_THAN;
} }
break; break;
@ -84,13 +84,11 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
lexeme = "<="; lexeme = "<=";
type = TokenType.LESS_EQUAL; type = TokenType.LESS_EQUAL;
} else { } else {
lexeme = "<";
type = TokenType.LESS_THAN; type = TokenType.LESS_THAN;
} }
break; break;
case '%': case '%':
lexeme = "%";
type = TokenType.MODULO; type = TokenType.MODULO;
break; break;
@ -98,9 +96,6 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
if (ctx.match('&')) { if (ctx.match('&')) {
lexeme = "&&"; lexeme = "&&";
type = TokenType.AND; type = TokenType.AND;
} else {
lexeme = "&";
type = TokenType.UNKNOWN;
} }
break; break;
@ -108,17 +103,26 @@ public class OperatorTokenScanner extends AbstractTokenScanner {
if (ctx.match('|')) { if (ctx.match('|')) {
lexeme = "||"; lexeme = "||";
type = TokenType.OR; type = TokenType.OR;
} else {
lexeme = "|";
type = TokenType.UNKNOWN;
} }
break; break;
default: default:
lexeme = String.valueOf(c); currentState = State.UNKNOWN;
type = TokenType.UNKNOWN; break;
}
// 执行完扫描后重置状态为初始状态
if (currentState != State.UNKNOWN) {
currentState = State.START;
} }
return new Token(type, lexeme, line, col); return new Token(type, lexeme, line, col);
} }
// 定义状态枚举
private enum State {
START, // 初始状态
OPERATOR, // 当前字符是运算符的一部分
UNKNOWN // 无法识别的状态
}
} }