From 5315679add052687560fd68021eb483d0a103939 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 27 Apr 2016 20:58:55 +0200 Subject: [PATCH] Throw exception if expression contains a triple factorial --- .../java/jscl/text/PostfixFunctionParser.java | 54 ++++----------- .../jscl/text/PostfixFunctionsParser.java | 69 +++++++++---------- 2 files changed, 44 insertions(+), 79 deletions(-) diff --git a/jscl/src/main/java/jscl/text/PostfixFunctionParser.java b/jscl/src/main/java/jscl/text/PostfixFunctionParser.java index 1b0c5b30..3850b683 100644 --- a/jscl/src/main/java/jscl/text/PostfixFunctionParser.java +++ b/jscl/src/main/java/jscl/text/PostfixFunctionParser.java @@ -3,59 +3,29 @@ package jscl.text; import jscl.math.Generic; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -/** - * User: serso - * Date: 10/27/11 - * Time: 2:45 PM - */ -public class PostfixFunctionParser implements Parser { +public class PostfixFunctionParser implements Parser { @Nonnull - private final String postfixFunctionName; + private final String name; - protected PostfixFunctionParser(@Nonnull String postfixFunctionName) { - this.postfixFunctionName = postfixFunctionName; + protected PostfixFunctionParser(@Nonnull String name) { + this.name = name; } - @Nonnull - public Result parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException { - int pos0 = p.position.intValue(); - - final boolean postfixFunction; + @Nullable + public String parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException { + final int pos0 = p.position.intValue(); ParserUtils.skipWhitespaces(p); - if (p.position.intValue() < p.expression.length() && p.expression.startsWith(postfixFunctionName, p.position.intValue())) { - p.position.add(postfixFunctionName.length()); - postfixFunction = true; + if (p.position.intValue() < p.expression.length() && p.expression.startsWith(name, p.position.intValue())) { + p.position.add(name.length()); + return name; } else { p.position.setValue(pos0); - postfixFunction = false; - } - - return new Result(postfixFunctionName, postfixFunction); - } - - public static class Result { - - @Nonnull - private final String postfixFunctionName; - - private final boolean postfixFunction; - - public Result(@Nonnull String postfixFunctionName, boolean postfixFunction) { - this.postfixFunctionName = postfixFunctionName; - this.postfixFunction = postfixFunction; - } - - public boolean isPostfixFunction() { - return postfixFunction; - } - - @Nonnull - public String getPostfixFunctionName() { - return postfixFunctionName; + return null; } } } diff --git a/jscl/src/main/java/jscl/text/PostfixFunctionsParser.java b/jscl/src/main/java/jscl/text/PostfixFunctionsParser.java index 5dd1de56..019aa3c5 100644 --- a/jscl/src/main/java/jscl/text/PostfixFunctionsParser.java +++ b/jscl/src/main/java/jscl/text/PostfixFunctionsParser.java @@ -1,24 +1,22 @@ package jscl.text; -import java.util.Collections; -import java.util.List; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - import jscl.math.Generic; import jscl.math.function.PostfixFunctionsRegistry; import jscl.math.operator.Operator; import jscl.math.operator.TripleFactorial; import jscl.text.msg.Messages; -/** - * User: serso - * Date: 10/31/11 - * Time: 11:20 PM - */ +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +import static java.util.Collections.singletonList; + public class PostfixFunctionsParser implements Parser { + private static final PostfixFunctionsRegistry registry = PostfixFunctionsRegistry.getInstance(); + private static final PostfixFunctionParser tripleFactorialParser = new PostfixFunctionParser(TripleFactorial.NAME); + @Nonnull private final Generic content; @@ -27,39 +25,36 @@ public class PostfixFunctionsParser implements Parser { } private static Generic parsePostfix(@Nonnull List names, - Generic content, + final Generic content, @Nullable final Generic previousSumElement, @Nonnull final Parameters p) throws ParseException { - Generic result = content; + checkTripleFactorial(previousSumElement, p); - for (String name : names) { - final PostfixFunctionParser parser = new PostfixFunctionParser(name); - final PostfixFunctionParser.Result postfixResult = parser.parse(p, previousSumElement); - if (postfixResult.isPostfixFunction()) { - final Operator postfixFunction; - - if (previousSumElement == null) { - postfixFunction = PostfixFunctionsRegistry.getInstance().get(postfixResult.getPostfixFunctionName(), new Generic[]{content}); - } else { - postfixFunction = PostfixFunctionsRegistry.getInstance().get(postfixResult.getPostfixFunctionName(), new Generic[]{content, previousSumElement}); - } - - if (postfixFunction == null) { - if (TripleFactorial.NAME.equals(postfixResult.getPostfixFunctionName())) { - throw p.exceptionsPool.obtain(p.position.intValue(), p.expression, Messages.msg_18); - } else { - throw p.exceptionsPool.obtain(p.position.intValue(), p.expression, Messages.msg_4, Collections.singletonList(postfixResult.getPostfixFunctionName())); - } - } - - result = parsePostfix(names, postfixFunction.expressionValue(), previousSumElement, p); + for (int i = 0; i < names.size(); i++) { + final PostfixFunctionParser parser = new PostfixFunctionParser(names.get(i)); + final String functionName = parser.parse(p, previousSumElement); + if (functionName == null) { + continue; } - } + final Generic[] parameters = previousSumElement == null ? new Generic[]{content} : new Generic[]{content, previousSumElement}; + final Operator function = registry.get(functionName, parameters); - return result; + if (function != null) { + return parsePostfix(names, function.expressionValue(), previousSumElement, p); + } + + throw p.exceptionsPool.obtain(p.position.intValue(), p.expression, Messages.msg_4, singletonList(functionName)); + } + return content; + } + + private static void checkTripleFactorial(@Nullable Generic previousSumElement, @Nonnull Parameters p) throws ParseException { + if (tripleFactorialParser.parse(p, previousSumElement) != null) { + throw p.exceptionsPool.obtain(p.position.intValue(), p.expression, Messages.msg_18); + } } public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException { - return parsePostfix(PostfixFunctionsRegistry.getInstance().getNames(), content, previousSumElement, p); + return parsePostfix(registry.getNames(), content, previousSumElement, p); } }