From: James Ahlborn Date: Sat, 15 Oct 2016 01:31:51 +0000 (+0000) Subject: add debug to strings to start enabling some testing X-Git-Tag: jackcess-2.2.0~24^2~69 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=26c20f9517709f1aed44d35f6a2a349c5e5726cf;p=jackcess.git add debug to strings to start enabling some testing git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1048 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/main/java/com/healthmarketscience/jackcess/util/Expressionator.java b/src/main/java/com/healthmarketscience/jackcess/util/Expressionator.java index ce54b98..38d8ae6 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/Expressionator.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/Expressionator.java @@ -133,21 +133,33 @@ public class Expressionator @Override protected Object eval(RowContext ctx) { return ctx.getThisColumnValue(); } + @Override protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append(""); + } }; private static final Expr NULL_VALUE = new Expr() { @Override protected Object eval(RowContext ctx) { return null; } + @Override protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append("Null"); + } }; private static final Expr TRUE_VALUE = new Expr() { @Override protected Object eval(RowContext ctx) { return Boolean.TRUE; } + @Override protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append("True"); + } }; private static final Expr FALSE_VALUE = new Expr() { @Override protected Object eval(RowContext ctx) { return Boolean.FALSE; } + @Override protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append("False"); + } }; private Expressionator() @@ -183,11 +195,7 @@ public class Expressionator return null; } - TokBuf buf = new TokBuf(exprType, tokens); - parseExpression(buf, false); - - // FIXME - return null; + return parseExpression(new TokBuf(exprType, tokens), false); } private static List trimSpaces(List tokens) { @@ -777,7 +785,7 @@ public class Expressionator } public void setPendingExpr(Expr expr) { - if(_pendingExpr == null) { + if(_pendingExpr != null) { throw new IllegalArgumentException( "Found multiple expressions with no operator " + this); } @@ -830,8 +838,8 @@ public class Expressionator } private static boolean isHigherPrecendence(String op1, String op2) { - int prec1 = PRECENDENCE.get(op1); - int prec2 = PRECENDENCE.get(op2); + int prec1 = PRECENDENCE.get(op1.toLowerCase()); + int prec2 = PRECENDENCE.get(op2.toLowerCase()); // higher preceendence ops have lower numbers return (prec1 < prec2); @@ -869,7 +877,32 @@ public class Expressionator return val.equals(ctx.getThisColumnValue()); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + toString(sb, false); + return sb.toString(); + } + + public String toDebugString() { + StringBuilder sb = new StringBuilder(); + toString(sb, true); + return sb.toString(); + } + + protected void toString(StringBuilder sb, boolean isDebug) { + if(isDebug) { + sb.append("<").append(getClass().getSimpleName()).append(">{"); + } + toExprString(sb, isDebug); + if(isDebug) { + sb.append("}"); + } + } + protected abstract Object eval(RowContext ctx); + + protected abstract void toExprString(StringBuilder sb, boolean isDebug); } public interface RowContext @@ -892,6 +925,19 @@ public class Expressionator public Object eval(RowContext ctx) { return _value; } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + // FIXME, stronger typing? + if(_value instanceof String) { + sb.append("\"").append(_value).append("\""); + } else if(_value instanceof Date) { + // FIXME Date,Time,DateTime formatting? + sb.append("#").append(_value).append("#"); + } else { + sb.append(_value); + } + } } private static final class EObjValue extends Expr @@ -911,6 +957,17 @@ public class Expressionator public Object eval(RowContext ctx) { return ctx.getRowValue(_collectionName, _objName, _fieldName); } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + if(_collectionName != null) { + sb.append("[").append(_collectionName).append("]."); + } + if(_objName != null) { + sb.append("[").append(_objName).append("]."); + } + sb.append("[").append(_fieldName).append("]"); + } } private static abstract class EOp @@ -935,6 +992,13 @@ public class Expressionator protected Object eval(RowContext ctx) { return _expr.eval(ctx); } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append("("); + _expr.toString(sb, isDebug); + sb.append(")"); + } } private static class EFunc extends Expr @@ -953,6 +1017,22 @@ public class Expressionator return false; } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append(_name).append("("); + + if(!_params.isEmpty()) { + Iterator iter = _params.iterator(); + iter.next().toString(sb, isDebug); + while(iter.hasNext()) { + sb.append(","); + iter.next().toString(sb, isDebug); + } + } + + sb.append(")"); + } } private static abstract class EBaseBinaryOp extends Expr @@ -1002,6 +1082,8 @@ public class Expressionator // as all other precedence has been resolved in previous parsing // rounds. if((leftOp._right == this) && isHigherPrecendence(leftOp._op, _op)) { + + // FIXME, need to move up if precedecne is the same! // doh, "this" is lower precedence, restore the original order of // things @@ -1013,6 +1095,13 @@ public class Expressionator return outerExpr; } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + _left.toString(sb, isDebug); + sb.append(" ").append(_op).append(" "); + _right.toString(sb, isDebug); + } } private static class EBinaryOp extends EBaseBinaryOp @@ -1033,11 +1122,11 @@ public class Expressionator private static class EUnaryOp extends Expr { private final String _op; - private final Expr _val; + private final Expr _expr; - private EUnaryOp(String op, Expr val) { + private EUnaryOp(String op, Expr expr) { _op = op; - _val = val; + _expr = expr; } @Override @@ -1046,6 +1135,12 @@ public class Expressionator return null; } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + sb.append(" ").append(_op).append(" "); + _expr.toString(sb, isDebug); + } } private static class ECompOp extends EBaseBinaryOp @@ -1085,6 +1180,14 @@ public class Expressionator _op = op; _expr = expr; } + + @Override + protected void toExprString(StringBuilder sb, boolean isDebug) { + // FIXME + throw new UnsupportedOperationException("FIXME"); + // _expr.toString(sb, isDebug); + // sb.append(" ").append(_op); + } } private static class ENullOp extends ESpecOp diff --git a/src/test/java/com/healthmarketscience/jackcess/util/ExpressionatorTest.java b/src/test/java/com/healthmarketscience/jackcess/util/ExpressionatorTest.java new file mode 100644 index 0000000..895adf8 --- /dev/null +++ b/src/test/java/com/healthmarketscience/jackcess/util/ExpressionatorTest.java @@ -0,0 +1,54 @@ +/* +Copyright (c) 2016 James Ahlborn + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.healthmarketscience.jackcess.util; + +import junit.framework.TestCase; + +/** + * + * @author James Ahlborn + */ +public class ExpressionatorTest extends TestCase +{ + + public ExpressionatorTest(String name) { + super(name); + } + + public void testOrderOfOperations() throws Exception + { + Expressionator.Expr expr = Expressionator.parse( + Expressionator.Type.FIELD_VALIDATOR, "\"A\" Eqv \"B\"", null); + assertEquals("{{\"A\"} Eqv {\"B\"}}", + expr.toDebugString()); + + expr = Expressionator.parse( + Expressionator.Type.FIELD_VALIDATOR, "\"A\" Eqv \"B\" Xor \"C\"", null); + assertEquals("{{\"A\"} Eqv {{\"B\"} Xor {\"C\"}}}", + expr.toDebugString()); + + expr = Expressionator.parse( + Expressionator.Type.FIELD_VALIDATOR, "\"A\" Eqv \"B\" Xor \"C\" Or \"D\"", null); + assertEquals("{{\"A\"} Eqv {{\"B\"} Xor {{\"C\"} Or {\"D\"}}}}", + expr.toDebugString()); + + expr = Expressionator.parse( + Expressionator.Type.FIELD_VALIDATOR, "\"A\" Eqv \"B\" Xor \"C\" Or \"D\" And \"E\"", null); + assertEquals("{{\"A\"} Eqv {{\"B\"} Xor {{\"C\"} Or {{\"D\"} And {\"E\"}}}}}", + expr.toDebugString()); + } +}