LexAn Symbols

This commit is contained in:
Gašper Dobrovoljc
2025-03-05 09:14:18 +01:00
parent 8c6828041d
commit 42fe2bf270
3 changed files with 136 additions and 3 deletions

View File

@@ -123,10 +123,11 @@ public class LexAn implements AutoCloseable {
* {@link peekToken} in {@link takeToken}.
*/
private void nextToken() {
while (buffChar == '\n') {
while (buffChar == ' ' || buffChar == '\n' || buffChar == '\t' || buffChar == '\r') {
nextChar();
}
Report.Location start = currentLocation();
switch (buffChar) {
case -1: // EOF
buffToken = new Token(currentLocation(), Token.Symbol.EOF, null);
@@ -139,6 +140,130 @@ public class LexAn implements AutoCloseable {
case '"':
stringConst();
return;
case '=':
nextChar();
if (buffChar == '=') {
buffToken = new Token(
new Report.Location(start, currentLocation()),
Token.Symbol.EQU,
null
);
nextChar();
return;
}
buffToken = new Token(start, Token.Symbol.ASSIGN, null);
nextChar();
return;
case ',':
buffToken = new Token(currentLocation(), Token.Symbol.COMMA, null);
nextChar();
return;
case '&':
nextChar();
if (buffChar != '&') {
throw new Report.Error(currentLocation(), "Invalid character '" + (char) buffChar + "'");
}
buffToken = new Token(new Report.Location(start, currentLocation()), Token.Symbol.AND, null);
nextChar();
return;
case '|':
nextChar();
if (buffChar != '|') {
throw new Report.Error(currentLocation(), "Invalid character '" + (char) buffChar + "'");
}
buffToken = new Token(new Report.Location(start, currentLocation()), Token.Symbol.OR, null);
nextChar();
return;
case '!':
nextChar();
if (buffChar == '=') {
buffToken = new Token(
new Report.Location(start, currentLocation()),
Token.Symbol.NEQ,
null
);
nextChar();
return;
}
buffToken = new Token(start, Token.Symbol.NOT, null);
return;
case '>':
nextChar();
if (buffChar == '=') {
buffToken = new Token(
new Report.Location(start, currentLocation()),
Token.Symbol.GEQ,
null
);
nextChar();
return;
}
buffToken = new Token(start, Token.Symbol.GTH, null);
return;
case '<':
nextChar();
if (buffChar == '=') {
buffToken = new Token(
new Report.Location(start, currentLocation()),
Token.Symbol.LEQ,
null
);
nextChar();
return;
}
buffToken = new Token(start, Token.Symbol.LTH, null);
return;
case '+':
buffToken = new Token(currentLocation(), Token.Symbol.ADD, null);
nextChar();
return;
case '-':
buffToken = new Token(currentLocation(), Token.Symbol.SUB, null);
nextChar();
return;
case '*':
buffToken = new Token(currentLocation(), Token.Symbol.MUL, null);
nextChar();
return;
case '/':
buffToken = new Token(currentLocation(), Token.Symbol.DIV, null);
nextChar();
return;
case '%':
buffToken = new Token(currentLocation(), Token.Symbol.MOD, null);
nextChar();
return;
case '^':
buffToken = new Token(currentLocation(), Token.Symbol.PTR, null);
nextChar();
return;
case '(':
buffToken = new Token(currentLocation(), Token.Symbol.LPAREN, null);
nextChar();
return;
case ')':
buffToken = new Token(currentLocation(), Token.Symbol.RPAREN, null);
nextChar();
return;
}
if (buffChar >= '0' && buffChar <= '9') {
@@ -238,6 +363,9 @@ public class LexAn implements AutoCloseable {
nextChar();
while (buffChar != '"') {
if (buffChar == -1) {
throw new Report.Error(currentLocation(), "Unterminated string.");
}
if (!isChar()) {
throw new Report.Error(currentLocation(), "Invalid character '" + (char) buffChar + "'.");
}