From c2191f4fc70462176b50a88d4ccf786b085d4294 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 23 Oct 2011 05:10:41 +0200 Subject: [PATCH] Generalized the Lexer a bit more and renamed a function in the parser. --- .../worldedit/expression/lexer/Lexer.java | 76 ++++++++++--------- .../worldedit/expression/parser/Parser.java | 6 +- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/expression/lexer/Lexer.java b/src/main/java/com/sk89q/worldedit/expression/lexer/Lexer.java index e7c2333ed..47ea711bb 100644 --- a/src/main/java/com/sk89q/worldedit/expression/lexer/Lexer.java +++ b/src/main/java/com/sk89q/worldedit/expression/lexer/Lexer.java @@ -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 characterTokens = new HashSet(); + 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()); diff --git a/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java b/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java index ab76d2490..1ad0ceefd 100644 --- a/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java +++ b/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java @@ -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;