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

This commit is contained in:
TomyLobo 2011-10-23 05:10:41 +02:00
parent 7dfc2a34e5
commit c2191f4fc7
2 changed files with 44 additions and 38 deletions

View File

@ -21,8 +21,10 @@ package com.sk89q.worldedit.expression.lexer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
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 identifierPattern = Pattern.compile("^([A-Za-z][0-9A-Za-z_]*)");
@ -93,43 +102,40 @@ public class Lexer {
}
final char ch = peek();
switch (ch) {
case ',':
case '(':
case ')':
if (characterTokens.contains(ch)) {
tokens.add(new CharacterToken(position++, ch));
break;
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 + "'");
continue;
}
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());

View File

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