3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-26 02:50:06 +01:00

Generalized the Lexer a bit more and renamed a function in the parser.

Dieser Commit ist enthalten in:
TomyLobo 2011-10-23 05:10:41 +02:00
Ursprung 7dfc2a34e5
Commit c2191f4fc7
2 geänderte Dateien mit 44 neuen und 38 gelöschten Zeilen

Datei anzeigen

@ -21,8 +21,10 @@ package com.sk89q.worldedit.expression.lexer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -74,6 +76,13 @@ public class Lexer {
) )
); );
private static final Set<Character> characterTokens = new HashSet<Character>();
static {
characterTokens.add(',');
characterTokens.add('(');
characterTokens.add(')');
}
private static final Pattern numberPattern = Pattern.compile("^([0-9]*(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)"); private static final Pattern numberPattern = Pattern.compile("^([0-9]*(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)");
private static final Pattern identifierPattern = Pattern.compile("^([A-Za-z][0-9A-Za-z_]*)"); private static final Pattern identifierPattern = Pattern.compile("^([A-Za-z][0-9A-Za-z_]*)");
@ -93,43 +102,40 @@ public class Lexer {
} }
final char ch = peek(); final char ch = peek();
switch (ch) {
case ',': if (characterTokens.contains(ch)) {
case '(':
case ')':
tokens.add(new CharacterToken(position++, ch)); tokens.add(new CharacterToken(position++, ch));
break; continue;
default:
final Matcher numberMatcher = numberPattern.matcher(expression.substring(position));
if (numberMatcher.lookingAt()) {
String numberPart = numberMatcher.group(1);
if (!numberPart.isEmpty()) {
try {
tokens.add(new NumberToken(position, Double.parseDouble(numberPart)));
}
catch (NumberFormatException e) {
throw new LexerException(position, "Number parsing failed", e);
}
position += numberPart.length();
continue;
}
}
final Matcher identifierMatcher = identifierPattern.matcher(expression.substring(position));
if (identifierMatcher.lookingAt()) {
String identifierPart = identifierMatcher.group(1);
if (!identifierPart.isEmpty()) {
tokens.add(new IdentifierToken(position, identifierPart));
position += identifierPart.length();
continue;
}
}
throw new LexerException(position, "Unknown character '" + ch + "'");
} }
final Matcher numberMatcher = numberPattern.matcher(expression.substring(position));
if (numberMatcher.lookingAt()) {
String numberPart = numberMatcher.group(1);
if (!numberPart.isEmpty()) {
try {
tokens.add(new NumberToken(position, Double.parseDouble(numberPart)));
}
catch (NumberFormatException e) {
throw new LexerException(position, "Number parsing failed", e);
}
position += numberPart.length();
continue;
}
}
final Matcher identifierMatcher = identifierPattern.matcher(expression.substring(position));
if (identifierMatcher.lookingAt()) {
String identifierPart = identifierMatcher.group(1);
if (!identifierPart.isEmpty()) {
tokens.add(new IdentifierToken(position, identifierPart));
position += identifierPart.length();
continue;
}
}
throw new LexerException(position, "Unknown character '" + ch + "'");
} }
while (position < expression.length()); while (position < expression.length());

Datei anzeigen

@ -95,7 +95,7 @@ public class Parser {
final Token next = peek(); final Token next = peek();
if (next.id() == '(') { if (next.id() == '(') {
halfProcessed.add(parseFunction(identifierToken)); halfProcessed.add(parseFunctionCall(identifierToken));
} }
else { else {
Invokable variable = variables.get(identifierToken.value); Invokable variable = variables.get(identifierToken.value);
@ -301,9 +301,9 @@ public class Parser {
return tokens.get(position); return tokens.get(position);
} }
private Identifiable parseFunction(IdentifierToken identifierToken) throws ParserException { private Identifiable parseFunctionCall(IdentifierToken identifierToken) throws ParserException {
if (peek().id() != '(') { if (peek().id() != '(') {
throw new ParserException(peek().getPosition(), "Unexpected character in parseBracket"); throw new ParserException(peek().getPosition(), "Unexpected character in parseFunctionCall");
} }
++position; ++position;