You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Expressionator.java 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. /*
  2. Copyright (c) 2016 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.expr;
  14. import java.math.BigDecimal;
  15. import java.text.DateFormat;
  16. import java.text.SimpleDateFormat;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.Date;
  22. import java.util.Deque;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.regex.Pattern;
  31. import java.util.regex.PatternSyntaxException;
  32. import com.healthmarketscience.jackcess.expr.EvalContext;
  33. import com.healthmarketscience.jackcess.expr.EvalException;
  34. import com.healthmarketscience.jackcess.expr.Expression;
  35. import com.healthmarketscience.jackcess.expr.Function;
  36. import com.healthmarketscience.jackcess.expr.FunctionLookup;
  37. import com.healthmarketscience.jackcess.expr.Identifier;
  38. import com.healthmarketscience.jackcess.expr.LocaleContext;
  39. import com.healthmarketscience.jackcess.expr.ParseException;
  40. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  41. import com.healthmarketscience.jackcess.expr.Value;
  42. import com.healthmarketscience.jackcess.impl.expr.ExpressionTokenizer.Token;
  43. import com.healthmarketscience.jackcess.impl.expr.ExpressionTokenizer.TokenType;
  44. /**
  45. *
  46. * @author James Ahlborn
  47. */
  48. public class Expressionator
  49. {
  50. // Useful links:
  51. // - syntax: https://support.office.com/en-us/article/Guide-to-expression-syntax-ebc770bc-8486-4adc-a9ec-7427cce39a90
  52. // - examples: https://support.office.com/en-us/article/Examples-of-expressions-d3901e11-c04e-4649-b40b-8b6ec5aed41f
  53. // - validation rule usage: https://support.office.com/en-us/article/Restrict-data-input-by-using-a-validation-rule-6c0b2ce1-76fa-4be0-8ae9-038b52652320
  54. public enum Type {
  55. DEFAULT_VALUE, EXPRESSION, FIELD_VALIDATOR, RECORD_VALIDATOR;
  56. }
  57. public interface ParseContext extends LocaleContext {
  58. public TemporalConfig getTemporalConfig();
  59. public SimpleDateFormat createDateFormat(String formatStr);
  60. public FunctionLookup getFunctionLookup();
  61. }
  62. private enum WordType {
  63. OP, COMP, LOG_OP, CONST, SPEC_OP_PREFIX, DELIM;
  64. }
  65. private static final String FUNC_START_DELIM = "(";
  66. private static final String FUNC_END_DELIM = ")";
  67. private static final String OPEN_PAREN = "(";
  68. private static final String CLOSE_PAREN = ")";
  69. private static final String FUNC_PARAM_SEP = ",";
  70. private static final Map<String,WordType> WORD_TYPES =
  71. new HashMap<String,WordType>();
  72. static {
  73. setWordType(WordType.OP, "+", "-", "*", "/", "\\", "^", "&", "mod");
  74. setWordType(WordType.COMP, "<", "<=", ">", ">=", "=", "<>");
  75. setWordType(WordType.LOG_OP, "and", "or", "eqv", "xor", "imp");
  76. setWordType(WordType.CONST, "true", "false", "null", "on", "off",
  77. "yes", "no");
  78. setWordType(WordType.SPEC_OP_PREFIX, "is", "like", "between", "in", "not");
  79. // "X is null", "X is not null", "X like P", "X between A and B",
  80. // "X not between A and B", "X in (A, B, C...)", "X not in (A, B, C...)",
  81. // "not X"
  82. setWordType(WordType.DELIM, ".", "!", ",", "(", ")");
  83. }
  84. private static final Collection<String> TRUE_STRS =
  85. Arrays.asList("true", "yes", "on");
  86. private static final Collection<String> FALSE_STRS =
  87. Arrays.asList("false", "no", "off");
  88. private interface OpType {}
  89. private enum UnaryOp implements OpType {
  90. NEG("-", false) {
  91. @Override public Value eval(EvalContext ctx, Value param1) {
  92. return BuiltinOperators.negate(ctx, param1);
  93. }
  94. @Override public UnaryOp getUnaryNumOp() {
  95. return UnaryOp.NEG_NUM;
  96. }
  97. },
  98. POS("+", false) {
  99. @Override public Value eval(EvalContext ctx, Value param1) {
  100. // basically a no-op
  101. return param1;
  102. }
  103. @Override public UnaryOp getUnaryNumOp() {
  104. return UnaryOp.POS_NUM;
  105. }
  106. },
  107. NOT("Not", true) {
  108. @Override public Value eval(EvalContext ctx, Value param1) {
  109. return BuiltinOperators.not(param1);
  110. }
  111. },
  112. // when a '-' immediately precedes a number, it needs "highest" precedence
  113. NEG_NUM("-", false) {
  114. @Override public Value eval(EvalContext ctx, Value param1) {
  115. return BuiltinOperators.negate(ctx, param1);
  116. }
  117. },
  118. // when a '+' immediately precedes a number, it needs "highest" precedence
  119. POS_NUM("+", false) {
  120. @Override public Value eval(EvalContext ctx, Value param1) {
  121. // basically a no-op
  122. return param1;
  123. }
  124. };
  125. private final String _str;
  126. private final boolean _needSpace;
  127. private UnaryOp(String str, boolean needSpace) {
  128. _str = str;
  129. _needSpace = needSpace;
  130. }
  131. public boolean needsSpace() {
  132. return _needSpace;
  133. }
  134. @Override
  135. public String toString() {
  136. return _str;
  137. }
  138. public UnaryOp getUnaryNumOp() {
  139. return null;
  140. }
  141. public abstract Value eval(EvalContext ctx, Value param1);
  142. }
  143. private enum BinaryOp implements OpType {
  144. PLUS("+") {
  145. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  146. return BuiltinOperators.add(ctx, param1, param2);
  147. }
  148. },
  149. MINUS("-") {
  150. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  151. return BuiltinOperators.subtract(ctx, param1, param2);
  152. }
  153. },
  154. MULT("*") {
  155. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  156. return BuiltinOperators.multiply(param1, param2);
  157. }
  158. },
  159. DIV("/") {
  160. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  161. return BuiltinOperators.divide(param1, param2);
  162. }
  163. },
  164. INT_DIV("\\") {
  165. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  166. return BuiltinOperators.intDivide(param1, param2);
  167. }
  168. },
  169. EXP("^") {
  170. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  171. return BuiltinOperators.exp(param1, param2);
  172. }
  173. },
  174. CONCAT("&") {
  175. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  176. return BuiltinOperators.concat(param1, param2);
  177. }
  178. },
  179. MOD("Mod") {
  180. @Override public Value eval(EvalContext ctx, Value param1, Value param2) {
  181. return BuiltinOperators.mod(param1, param2);
  182. }
  183. };
  184. private final String _str;
  185. private BinaryOp(String str) {
  186. _str = str;
  187. }
  188. @Override
  189. public String toString() {
  190. return _str;
  191. }
  192. public abstract Value eval(EvalContext ctx, Value param1, Value param2);
  193. }
  194. private enum CompOp implements OpType {
  195. LT("<") {
  196. @Override public Value eval(Value param1, Value param2) {
  197. return BuiltinOperators.lessThan(param1, param2);
  198. }
  199. },
  200. LTE("<=") {
  201. @Override public Value eval(Value param1, Value param2) {
  202. return BuiltinOperators.lessThanEq(param1, param2);
  203. }
  204. },
  205. GT(">") {
  206. @Override public Value eval(Value param1, Value param2) {
  207. return BuiltinOperators.greaterThan(param1, param2);
  208. }
  209. },
  210. GTE(">=") {
  211. @Override public Value eval(Value param1, Value param2) {
  212. return BuiltinOperators.greaterThanEq(param1, param2);
  213. }
  214. },
  215. EQ("=") {
  216. @Override public Value eval(Value param1, Value param2) {
  217. return BuiltinOperators.equals(param1, param2);
  218. }
  219. },
  220. NE("<>") {
  221. @Override public Value eval(Value param1, Value param2) {
  222. return BuiltinOperators.notEquals(param1, param2);
  223. }
  224. };
  225. private final String _str;
  226. private CompOp(String str) {
  227. _str = str;
  228. }
  229. @Override
  230. public String toString() {
  231. return _str;
  232. }
  233. public abstract Value eval(Value param1, Value param2);
  234. }
  235. private enum LogOp implements OpType {
  236. AND("And") {
  237. @Override public Value eval(Value param1, Value param2) {
  238. return BuiltinOperators.and(param1, param2);
  239. }
  240. },
  241. OR("Or") {
  242. @Override public Value eval(Value param1, Value param2) {
  243. return BuiltinOperators.or(param1, param2);
  244. }
  245. },
  246. EQV("Eqv") {
  247. @Override public Value eval(Value param1, Value param2) {
  248. return BuiltinOperators.eqv(param1, param2);
  249. }
  250. },
  251. XOR("Xor") {
  252. @Override public Value eval(Value param1, Value param2) {
  253. return BuiltinOperators.xor(param1, param2);
  254. }
  255. },
  256. IMP("Imp") {
  257. @Override public Value eval(Value param1, Value param2) {
  258. return BuiltinOperators.imp(param1, param2);
  259. }
  260. };
  261. private final String _str;
  262. private LogOp(String str) {
  263. _str = str;
  264. }
  265. @Override
  266. public String toString() {
  267. return _str;
  268. }
  269. public abstract Value eval(Value param1, Value param2);
  270. }
  271. private enum SpecOp implements OpType {
  272. // note, "NOT" is not actually used as a special operation, always
  273. // replaced with UnaryOp.NOT
  274. NOT("Not") {
  275. @Override public Value eval(Value param1, Object param2, Object param3) {
  276. throw new UnsupportedOperationException();
  277. }
  278. },
  279. IS_NULL("Is Null") {
  280. @Override public Value eval(Value param1, Object param2, Object param3) {
  281. return BuiltinOperators.isNull(param1);
  282. }
  283. },
  284. IS_NOT_NULL("Is Not Null") {
  285. @Override public Value eval(Value param1, Object param2, Object param3) {
  286. return BuiltinOperators.isNotNull(param1);
  287. }
  288. },
  289. LIKE("Like") {
  290. @Override public Value eval(Value param1, Object param2, Object param3) {
  291. return BuiltinOperators.like(param1, (Pattern)param2);
  292. }
  293. },
  294. NOT_LIKE("Not Like") {
  295. @Override public Value eval(Value param1, Object param2, Object param3) {
  296. return BuiltinOperators.notLike(param1, (Pattern)param2);
  297. }
  298. },
  299. BETWEEN("Between") {
  300. @Override public Value eval(Value param1, Object param2, Object param3) {
  301. return BuiltinOperators.between(param1, (Value)param2, (Value)param3);
  302. }
  303. },
  304. NOT_BETWEEN("Not Between") {
  305. @Override public Value eval(Value param1, Object param2, Object param3) {
  306. return BuiltinOperators.notBetween(param1, (Value)param2, (Value)param3);
  307. }
  308. },
  309. IN("In") {
  310. @Override public Value eval(Value param1, Object param2, Object param3) {
  311. return BuiltinOperators.in(param1, (Value[])param2);
  312. }
  313. },
  314. NOT_IN("Not In") {
  315. @Override public Value eval(Value param1, Object param2, Object param3) {
  316. return BuiltinOperators.notIn(param1, (Value[])param2);
  317. }
  318. };
  319. private final String _str;
  320. private SpecOp(String str) {
  321. _str = str;
  322. }
  323. @Override
  324. public String toString() {
  325. return _str;
  326. }
  327. public abstract Value eval(Value param1, Object param2, Object param3);
  328. }
  329. private static final Map<OpType, Integer> PRECENDENCE =
  330. buildPrecedenceMap(
  331. new OpType[]{UnaryOp.NEG_NUM, UnaryOp.POS_NUM},
  332. new OpType[]{BinaryOp.EXP},
  333. new OpType[]{UnaryOp.NEG, UnaryOp.POS},
  334. new OpType[]{BinaryOp.MULT, BinaryOp.DIV},
  335. new OpType[]{BinaryOp.INT_DIV},
  336. new OpType[]{BinaryOp.MOD},
  337. new OpType[]{BinaryOp.PLUS, BinaryOp.MINUS},
  338. new OpType[]{BinaryOp.CONCAT},
  339. new OpType[]{CompOp.LT, CompOp.GT, CompOp.NE, CompOp.LTE, CompOp.GTE,
  340. CompOp.EQ, SpecOp.LIKE, SpecOp.NOT_LIKE,
  341. SpecOp.IS_NULL, SpecOp.IS_NOT_NULL},
  342. new OpType[]{UnaryOp.NOT},
  343. new OpType[]{LogOp.AND},
  344. new OpType[]{LogOp.OR},
  345. new OpType[]{LogOp.XOR},
  346. new OpType[]{LogOp.EQV},
  347. new OpType[]{LogOp.IMP},
  348. new OpType[]{SpecOp.IN, SpecOp.NOT_IN, SpecOp.BETWEEN,
  349. SpecOp.NOT_BETWEEN});
  350. private static final Set<Character> REGEX_SPEC_CHARS = new HashSet<Character>(
  351. Arrays.asList('\\','.','%','=','+', '$','^','|','(',')','{','}','&'));
  352. // this is a regular expression which will never match any string
  353. private static final Pattern UNMATCHABLE_REGEX = Pattern.compile("(?!)");
  354. private static final Expr THIS_COL_VALUE = new EThisValue();
  355. private static final Expr NULL_VALUE = new EConstValue(
  356. ValueSupport.NULL_VAL, "Null");
  357. private static final Expr TRUE_VALUE = new EConstValue(
  358. ValueSupport.TRUE_VAL, "True");
  359. private static final Expr FALSE_VALUE = new EConstValue(
  360. ValueSupport.FALSE_VAL, "False");
  361. private Expressionator() {}
  362. public static Expression parse(Type exprType, String exprStr,
  363. Value.Type resultType,
  364. ParseContext context) {
  365. List<Token> tokens = trimSpaces(
  366. ExpressionTokenizer.tokenize(exprType, exprStr, context));
  367. if(tokens == null) {
  368. throw new ParseException("null/empty expression");
  369. }
  370. TokBuf buf = new TokBuf(exprType, tokens, context);
  371. if(isLiteralDefaultValue(buf, resultType, exprStr)) {
  372. // this is handled as a literal string value, not an expression. no
  373. // need to memo-ize cause it's a simple literal value
  374. return new ExprWrapper(exprStr,
  375. new ELiteralValue(Value.Type.STRING, exprStr, null), resultType);
  376. }
  377. // normal expression handling
  378. Expr expr = parseExpression(buf, false);
  379. if((exprType == Type.FIELD_VALIDATOR) && !expr.isValidationExpr()) {
  380. // a non-validation expression for a FIELD_VALIDATOR treats the result
  381. // as an equality comparison with the field in question. so, transform
  382. // the expression accordingly
  383. expr = new EImplicitCompOp(expr);
  384. }
  385. switch(exprType) {
  386. case DEFAULT_VALUE:
  387. case EXPRESSION:
  388. return (expr.isConstant() ?
  389. // for now, just cache at top-level for speed (could in theory
  390. // cache intermediate values?)
  391. new MemoizedExprWrapper(exprStr, expr, resultType) :
  392. new ExprWrapper(exprStr, expr, resultType));
  393. case FIELD_VALIDATOR:
  394. case RECORD_VALIDATOR:
  395. return (expr.isConstant() ?
  396. // for now, just cache at top-level for speed (could in theory
  397. // cache intermediate values?)
  398. new MemoizedCondExprWrapper(exprStr, expr) :
  399. new CondExprWrapper(exprStr, expr));
  400. default:
  401. throw new ParseException("unexpected expression type " + exprType);
  402. }
  403. }
  404. private static List<Token> trimSpaces(List<Token> tokens) {
  405. if(tokens == null) {
  406. return null;
  407. }
  408. // for the most part, spaces are superfluous except for one situation(?).
  409. // when they appear between a string literal and '(' they help distinguish
  410. // a function call from another expression form
  411. for(int i = 1; i < (tokens.size() - 1); ++i) {
  412. Token t = tokens.get(i);
  413. if(t.getType() == TokenType.SPACE) {
  414. if((tokens.get(i - 1).getType() == TokenType.STRING) &&
  415. isDelim(tokens.get(i + 1), FUNC_START_DELIM)) {
  416. // we want to keep this space
  417. } else {
  418. tokens.remove(i);
  419. --i;
  420. }
  421. }
  422. }
  423. return tokens;
  424. }
  425. private static Expr parseExpression(TokBuf buf, boolean singleExpr)
  426. {
  427. while(buf.hasNext()) {
  428. Token t = buf.next();
  429. switch(t.getType()) {
  430. case OBJ_NAME:
  431. parseObjectRefExpression(t, buf);
  432. break;
  433. case LITERAL:
  434. buf.setPendingExpr(new ELiteralValue(t.getValueType(), t.getValue(),
  435. t.getDateFormat()));
  436. break;
  437. case OP:
  438. WordType wordType = getWordType(t);
  439. if(wordType == null) {
  440. // shouldn't happen
  441. throw new ParseException("Invalid operator " + t);
  442. }
  443. // this can only be an OP or a COMP (those are the only words that the
  444. // tokenizer would define as TokenType.OP)
  445. switch(wordType) {
  446. case OP:
  447. parseOperatorExpression(t, buf);
  448. break;
  449. case COMP:
  450. parseCompOpExpression(t, buf);
  451. break;
  452. default:
  453. throw new ParseException("Unexpected OP word type " + wordType);
  454. }
  455. break;
  456. case DELIM:
  457. parseDelimExpression(t, buf);
  458. break;
  459. case STRING:
  460. // see if it's a special word?
  461. wordType = getWordType(t);
  462. if(wordType == null) {
  463. // is it a function call?
  464. if(!maybeParseFuncCallExpression(t, buf)) {
  465. // is it an object name?
  466. Token next = buf.peekNext();
  467. if((next != null) && isObjNameSep(next)) {
  468. parseObjectRefExpression(t, buf);
  469. } else {
  470. // FIXME maybe bare obj name, maybe string literal?
  471. throw new UnsupportedOperationException("FIXME");
  472. }
  473. }
  474. } else {
  475. // this could be anything but COMP or DELIM (all COMPs would be
  476. // returned as TokenType.OP and all DELIMs would be TokenType.DELIM)
  477. switch(wordType) {
  478. case OP:
  479. parseOperatorExpression(t, buf);
  480. break;
  481. case LOG_OP:
  482. parseLogicalOpExpression(t, buf);
  483. break;
  484. case CONST:
  485. parseConstExpression(t, buf);
  486. break;
  487. case SPEC_OP_PREFIX:
  488. parseSpecOpExpression(t, buf);
  489. break;
  490. default:
  491. throw new ParseException("Unexpected STRING word type "
  492. + wordType);
  493. }
  494. }
  495. break;
  496. case SPACE:
  497. // top-level space is irrelevant (and we strip them anyway)
  498. break;
  499. default:
  500. throw new ParseException("unknown token type " + t);
  501. }
  502. if(singleExpr && buf.hasPendingExpr()) {
  503. break;
  504. }
  505. }
  506. Expr expr = buf.takePendingExpr();
  507. if(expr == null) {
  508. throw new ParseException("No expression found? " + buf);
  509. }
  510. return expr;
  511. }
  512. private static void parseObjectRefExpression(Token firstTok, TokBuf buf) {
  513. // object references may be joined by '.' or '!'. access syntac docs claim
  514. // object identifiers can be formatted like:
  515. // "[Collection name]![Object name].[Property name]"
  516. // However, in practice, they only ever seem to be (at most) two levels
  517. // and only use '.'. Apparently '!' is actually a special late-bind
  518. // operator (not sure it makes a difference for this code?), see:
  519. // http://bytecomb.com/the-bang-exclamation-operator-in-vba/
  520. Deque<String> objNames = new LinkedList<String>();
  521. objNames.add(firstTok.getValueStr());
  522. Token t = null;
  523. boolean atSep = false;
  524. while((t = buf.peekNext()) != null) {
  525. if(!atSep) {
  526. if(isObjNameSep(t)) {
  527. buf.next();
  528. atSep = true;
  529. continue;
  530. }
  531. } else {
  532. if((t.getType() == TokenType.OBJ_NAME) ||
  533. (t.getType() == TokenType.STRING)) {
  534. buf.next();
  535. // always insert at beginning of list so names are in reverse order
  536. objNames.addFirst(t.getValueStr());
  537. atSep = false;
  538. continue;
  539. }
  540. }
  541. break;
  542. }
  543. int numNames = objNames.size();
  544. if(atSep || (numNames > 3)) {
  545. throw new ParseException("Invalid object reference " + buf);
  546. }
  547. // names are in reverse order
  548. String propName = null;
  549. if(numNames == 3) {
  550. propName = objNames.poll();
  551. }
  552. String objName = objNames.poll();
  553. String collectionName = objNames.poll();
  554. buf.setPendingExpr(
  555. new EObjValue(new Identifier(collectionName, objName, propName)));
  556. }
  557. private static void parseDelimExpression(Token firstTok, TokBuf buf) {
  558. // the only "top-level" delim we expect to find is open paren, and
  559. // there shouldn't be any pending expression
  560. if(!isDelim(firstTok, OPEN_PAREN) || buf.hasPendingExpr()) {
  561. throw new ParseException("Unexpected delimiter " +
  562. firstTok.getValue() + " " + buf);
  563. }
  564. Expr subExpr = findParenExprs(buf, false).get(0);
  565. buf.setPendingExpr(new EParen(subExpr));
  566. }
  567. private static boolean maybeParseFuncCallExpression(
  568. Token firstTok, TokBuf buf) {
  569. int startPos = buf.curPos();
  570. boolean foundFunc = false;
  571. try {
  572. Token t = buf.peekNext();
  573. if(!isDelim(t, FUNC_START_DELIM)) {
  574. // not a function call
  575. return false;
  576. }
  577. buf.next();
  578. List<Expr> params = findParenExprs(buf, true);
  579. String funcName = firstTok.getValueStr();
  580. Function func = buf.getFunction(funcName);
  581. if(func == null) {
  582. throw new ParseException("Could not find function '" +
  583. funcName + "' " + buf);
  584. }
  585. buf.setPendingExpr(new EFunc(func, params));
  586. foundFunc = true;
  587. return true;
  588. } finally {
  589. if(!foundFunc) {
  590. buf.reset(startPos);
  591. }
  592. }
  593. }
  594. private static List<Expr> findParenExprs(
  595. TokBuf buf, boolean allowMulti) {
  596. if(allowMulti) {
  597. // simple case, no nested expr
  598. Token t = buf.peekNext();
  599. if(isDelim(t, CLOSE_PAREN)) {
  600. buf.next();
  601. return Collections.emptyList();
  602. }
  603. }
  604. // find closing ")", handle nested parens
  605. List<Expr> exprs = new ArrayList<Expr>(3);
  606. int level = 1;
  607. int startPos = buf.curPos();
  608. while(buf.hasNext()) {
  609. Token t = buf.next();
  610. if(isDelim(t, OPEN_PAREN)) {
  611. ++level;
  612. } else if(isDelim(t, CLOSE_PAREN)) {
  613. --level;
  614. if(level == 0) {
  615. TokBuf subBuf = buf.subBuf(startPos, buf.prevPos());
  616. exprs.add(parseExpression(subBuf, false));
  617. return exprs;
  618. }
  619. } else if(allowMulti && (level == 1) && isDelim(t, FUNC_PARAM_SEP)) {
  620. TokBuf subBuf = buf.subBuf(startPos, buf.prevPos());
  621. exprs.add(parseExpression(subBuf, false));
  622. startPos = buf.curPos();
  623. }
  624. }
  625. throw new ParseException("Missing closing '" + CLOSE_PAREN
  626. + " " + buf);
  627. }
  628. private static void parseOperatorExpression(Token t, TokBuf buf) {
  629. // most ops are two argument except that '-' could be negation, "+" could
  630. // be pos-ation
  631. if(buf.hasPendingExpr()) {
  632. parseBinaryOpExpression(t, buf);
  633. } else if(isEitherOp(t, "-", "+")) {
  634. parseUnaryOpExpression(t, buf);
  635. } else {
  636. throw new ParseException(
  637. "Missing left expression for binary operator " + t.getValue() +
  638. " " + buf);
  639. }
  640. }
  641. private static void parseBinaryOpExpression(Token firstTok, TokBuf buf) {
  642. BinaryOp op = getOpType(firstTok, BinaryOp.class);
  643. Expr leftExpr = buf.takePendingExpr();
  644. Expr rightExpr = parseExpression(buf, true);
  645. buf.setPendingExpr(new EBinaryOp(op, leftExpr, rightExpr));
  646. }
  647. private static void parseUnaryOpExpression(Token firstTok, TokBuf buf) {
  648. UnaryOp op = getOpType(firstTok, UnaryOp.class);
  649. UnaryOp numOp = op.getUnaryNumOp();
  650. if(numOp != null) {
  651. // if this operator is immediately preceding a number, it has a higher
  652. // precedence
  653. Token nextTok = buf.peekNext();
  654. if((nextTok != null) && (nextTok.getType() == TokenType.LITERAL) &&
  655. nextTok.getValueType().isNumeric()) {
  656. op = numOp;
  657. }
  658. }
  659. Expr val = parseExpression(buf, true);
  660. buf.setPendingExpr(new EUnaryOp(op, val));
  661. }
  662. private static void parseCompOpExpression(Token firstTok, TokBuf buf) {
  663. if(!buf.hasPendingExpr()) {
  664. if(buf.getExprType() == Type.FIELD_VALIDATOR) {
  665. // comparison operators for field validators can implicitly use
  666. // the current field value for the left value
  667. buf.setPendingExpr(THIS_COL_VALUE);
  668. } else {
  669. throw new ParseException(
  670. "Missing left expression for comparison operator " +
  671. firstTok.getValue() + " " + buf);
  672. }
  673. }
  674. CompOp op = getOpType(firstTok, CompOp.class);
  675. Expr leftExpr = buf.takePendingExpr();
  676. Expr rightExpr = parseExpression(buf, true);
  677. buf.setPendingExpr(new ECompOp(op, leftExpr, rightExpr));
  678. }
  679. private static void parseLogicalOpExpression(Token firstTok, TokBuf buf) {
  680. if(!buf.hasPendingExpr()) {
  681. throw new ParseException(
  682. "Missing left expression for logical operator " +
  683. firstTok.getValue() + " " + buf);
  684. }
  685. LogOp op = getOpType(firstTok, LogOp.class);
  686. Expr leftExpr = buf.takePendingExpr();
  687. Expr rightExpr = parseExpression(buf, true);
  688. buf.setPendingExpr(new ELogicalOp(op, leftExpr, rightExpr));
  689. }
  690. private static void parseSpecOpExpression(Token firstTok, TokBuf buf) {
  691. SpecOp specOp = getSpecialOperator(firstTok, buf);
  692. if(specOp == SpecOp.NOT) {
  693. // this is the unary prefix operator
  694. parseUnaryOpExpression(firstTok, buf);
  695. return;
  696. }
  697. if(!buf.hasPendingExpr()) {
  698. if(buf.getExprType() == Type.FIELD_VALIDATOR) {
  699. // comparison operators for field validators can implicitly use
  700. // the current field value for the left value
  701. buf.setPendingExpr(THIS_COL_VALUE);
  702. } else {
  703. throw new ParseException(
  704. "Missing left expression for comparison operator " +
  705. specOp + " " + buf);
  706. }
  707. }
  708. Expr expr = buf.takePendingExpr();
  709. Expr specOpExpr = null;
  710. switch(specOp) {
  711. case IS_NULL:
  712. case IS_NOT_NULL:
  713. specOpExpr = new ENullOp(specOp, expr);
  714. break;
  715. case LIKE:
  716. case NOT_LIKE:
  717. Token t = buf.next();
  718. if((t.getType() != TokenType.LITERAL) ||
  719. (t.getValueType() != Value.Type.STRING)) {
  720. throw new ParseException("Missing Like pattern " + buf);
  721. }
  722. String patternStr = t.getValueStr();
  723. specOpExpr = new ELikeOp(specOp, expr, patternStr);
  724. break;
  725. case BETWEEN:
  726. case NOT_BETWEEN:
  727. // the "rest" of a between expression is of the form "X And Y". we are
  728. // going to speculatively parse forward until we find the "And"
  729. // operator.
  730. Expr startRangeExpr = null;
  731. while(true) {
  732. Expr tmpExpr = parseExpression(buf, true);
  733. Token tmpT = buf.peekNext();
  734. if(tmpT == null) {
  735. // ran out of expression?
  736. throw new ParseException(
  737. "Missing 'And' for 'Between' expression " + buf);
  738. }
  739. if(isString(tmpT, "and")) {
  740. buf.next();
  741. startRangeExpr = tmpExpr;
  742. break;
  743. }
  744. // put the pending expression back and try parsing some more
  745. buf.restorePendingExpr(tmpExpr);
  746. }
  747. Expr endRangeExpr = parseExpression(buf, true);
  748. specOpExpr = new EBetweenOp(specOp, expr, startRangeExpr, endRangeExpr);
  749. break;
  750. case IN:
  751. case NOT_IN:
  752. // there might be a space before open paren
  753. t = buf.next();
  754. if(t.getType() == TokenType.SPACE) {
  755. t = buf.next();
  756. }
  757. if(!isDelim(t, OPEN_PAREN)) {
  758. throw new ParseException("Malformed 'In' expression " + buf);
  759. }
  760. List<Expr> exprs = findParenExprs(buf, true);
  761. specOpExpr = new EInOp(specOp, expr, exprs);
  762. break;
  763. default:
  764. throw new ParseException("Unexpected special op " + specOp);
  765. }
  766. buf.setPendingExpr(specOpExpr);
  767. }
  768. private static SpecOp getSpecialOperator(Token firstTok, TokBuf buf) {
  769. String opStr = firstTok.getValueStr().toLowerCase();
  770. if("is".equals(opStr)) {
  771. Token t = buf.peekNext();
  772. if(isString(t, "null")) {
  773. buf.next();
  774. return SpecOp.IS_NULL;
  775. } else if(isString(t, "not")) {
  776. buf.next();
  777. t = buf.peekNext();
  778. if(isString(t, "null")) {
  779. buf.next();
  780. return SpecOp.IS_NOT_NULL;
  781. }
  782. }
  783. } else if("like".equals(opStr)) {
  784. return SpecOp.LIKE;
  785. } else if("between".equals(opStr)) {
  786. return SpecOp.BETWEEN;
  787. } else if("in".equals(opStr)) {
  788. return SpecOp.IN;
  789. } else if("not".equals(opStr)) {
  790. Token t = buf.peekNext();
  791. if(isString(t, "between")) {
  792. buf.next();
  793. return SpecOp.NOT_BETWEEN;
  794. } else if(isString(t, "in")) {
  795. buf.next();
  796. return SpecOp.NOT_IN;
  797. } else if(isString(t, "like")) {
  798. buf.next();
  799. return SpecOp.NOT_LIKE;
  800. }
  801. return SpecOp.NOT;
  802. }
  803. throw new ParseException(
  804. "Malformed special operator " + opStr + " " + buf);
  805. }
  806. private static void parseConstExpression(Token firstTok, TokBuf buf) {
  807. Expr constExpr = null;
  808. String tokStr = firstTok.getValueStr().toLowerCase();
  809. if(TRUE_STRS.contains(tokStr)) {
  810. constExpr = TRUE_VALUE;
  811. } else if(FALSE_STRS.contains(tokStr)) {
  812. constExpr = FALSE_VALUE;
  813. } else if("null".equals(tokStr)) {
  814. constExpr = NULL_VALUE;
  815. } else {
  816. throw new ParseException("Unexpected CONST word "
  817. + firstTok.getValue());
  818. }
  819. buf.setPendingExpr(constExpr);
  820. }
  821. private static boolean isObjNameSep(Token t) {
  822. return (isDelim(t, ".") || isDelim(t, "!"));
  823. }
  824. private static boolean isOp(Token t, String opStr) {
  825. return ((t != null) && (t.getType() == TokenType.OP) &&
  826. opStr.equalsIgnoreCase(t.getValueStr()));
  827. }
  828. private static boolean isEitherOp(Token t, String opStr1, String opStr2) {
  829. return ((t != null) && (t.getType() == TokenType.OP) &&
  830. (opStr1.equalsIgnoreCase(t.getValueStr()) ||
  831. opStr2.equalsIgnoreCase(t.getValueStr())));
  832. }
  833. private static boolean isDelim(Token t, String opStr) {
  834. return ((t != null) && (t.getType() == TokenType.DELIM) &&
  835. opStr.equalsIgnoreCase(t.getValueStr()));
  836. }
  837. private static boolean isString(Token t, String opStr) {
  838. return ((t != null) && (t.getType() == TokenType.STRING) &&
  839. opStr.equalsIgnoreCase(t.getValueStr()));
  840. }
  841. private static WordType getWordType(Token t) {
  842. return WORD_TYPES.get(t.getValueStr().toLowerCase());
  843. }
  844. private static void setWordType(WordType type, String... words) {
  845. for(String w : words) {
  846. WORD_TYPES.put(w, type);
  847. }
  848. }
  849. private static <T extends Enum<T>> T getOpType(Token t, Class<T> opClazz) {
  850. String str = t.getValueStr();
  851. for(T op : opClazz.getEnumConstants()) {
  852. if(str.equalsIgnoreCase(op.toString())) {
  853. return op;
  854. }
  855. }
  856. throw new ParseException("Unexpected op string " + t.getValueStr());
  857. }
  858. private static final class TokBuf
  859. {
  860. private final Type _exprType;
  861. private final List<Token> _tokens;
  862. private final TokBuf _parent;
  863. private final int _parentOff;
  864. private final ParseContext _context;
  865. private int _pos;
  866. private Expr _pendingExpr;
  867. private TokBuf(Type exprType, List<Token> tokens, ParseContext context) {
  868. this(exprType, tokens, null, 0, context);
  869. }
  870. private TokBuf(List<Token> tokens, TokBuf parent, int parentOff) {
  871. this(parent._exprType, tokens, parent, parentOff, parent._context);
  872. }
  873. private TokBuf(Type exprType, List<Token> tokens, TokBuf parent,
  874. int parentOff, ParseContext context) {
  875. _exprType = exprType;
  876. _tokens = tokens;
  877. _parent = parent;
  878. _parentOff = parentOff;
  879. _context = context;
  880. }
  881. public Type getExprType() {
  882. return _exprType;
  883. }
  884. public int curPos() {
  885. return _pos;
  886. }
  887. public int prevPos() {
  888. return _pos - 1;
  889. }
  890. public boolean hasNext() {
  891. return (_pos < _tokens.size());
  892. }
  893. public Token peekNext() {
  894. if(!hasNext()) {
  895. return null;
  896. }
  897. return _tokens.get(_pos);
  898. }
  899. public Token next() {
  900. if(!hasNext()) {
  901. throw new ParseException(
  902. "Unexpected end of expression " + this);
  903. }
  904. return _tokens.get(_pos++);
  905. }
  906. public void reset(int pos) {
  907. _pos = pos;
  908. }
  909. public TokBuf subBuf(int start, int end) {
  910. return new TokBuf(_tokens.subList(start, end), this, start);
  911. }
  912. public void setPendingExpr(Expr expr) {
  913. if(_pendingExpr != null) {
  914. throw new ParseException(
  915. "Found multiple expressions with no operator " + this);
  916. }
  917. _pendingExpr = expr.resolveOrderOfOperations();
  918. }
  919. public void restorePendingExpr(Expr expr) {
  920. // this is an expression which was previously set, so no need to re-resolve
  921. _pendingExpr = expr;
  922. }
  923. public Expr takePendingExpr() {
  924. Expr expr = _pendingExpr;
  925. _pendingExpr = null;
  926. return expr;
  927. }
  928. public boolean hasPendingExpr() {
  929. return (_pendingExpr != null);
  930. }
  931. private Map.Entry<Integer,List<Token>> getTopPos() {
  932. int pos = _pos;
  933. List<Token> toks = _tokens;
  934. TokBuf cur = this;
  935. while(cur._parent != null) {
  936. pos += cur._parentOff;
  937. cur = cur._parent;
  938. toks = cur._tokens;
  939. }
  940. return ExpressionTokenizer.newEntry(pos, toks);
  941. }
  942. public Function getFunction(String funcName) {
  943. return _context.getFunctionLookup().getFunction(funcName);
  944. }
  945. @Override
  946. public String toString() {
  947. Map.Entry<Integer,List<Token>> e = getTopPos();
  948. // TODO actually format expression?
  949. StringBuilder sb = new StringBuilder()
  950. .append("[token ").append(e.getKey()).append("] (");
  951. for(Iterator<Token> iter = e.getValue().iterator(); iter.hasNext(); ) {
  952. Token t = iter.next();
  953. sb.append("'").append(t.getValueStr()).append("'");
  954. if(iter.hasNext()) {
  955. sb.append(",");
  956. }
  957. }
  958. sb.append(")");
  959. if(_pendingExpr != null) {
  960. sb.append(" [pending '").append(_pendingExpr.toDebugString())
  961. .append("']");
  962. }
  963. return sb.toString();
  964. }
  965. }
  966. private static boolean isHigherPrecendence(OpType op1, OpType op2) {
  967. int prec1 = PRECENDENCE.get(op1);
  968. int prec2 = PRECENDENCE.get(op2);
  969. // higher preceendence ops have lower numbers
  970. return (prec1 < prec2);
  971. }
  972. private static final Map<OpType, Integer> buildPrecedenceMap(
  973. OpType[]... opArrs) {
  974. Map<OpType, Integer> prec = new HashMap<OpType, Integer>();
  975. int level = 0;
  976. for(OpType[] ops : opArrs) {
  977. for(OpType op : ops) {
  978. prec.put(op, level);
  979. }
  980. ++level;
  981. }
  982. return prec;
  983. }
  984. private static void exprListToString(
  985. List<Expr> exprs, String sep, StringBuilder sb, boolean isDebug) {
  986. Iterator<Expr> iter = exprs.iterator();
  987. iter.next().toString(sb, isDebug);
  988. while(iter.hasNext()) {
  989. sb.append(sep);
  990. iter.next().toString(sb, isDebug);
  991. }
  992. }
  993. private static Value[] exprListToValues(
  994. List<Expr> exprs, EvalContext ctx) {
  995. Value[] paramVals = new Value[exprs.size()];
  996. for(int i = 0; i < exprs.size(); ++i) {
  997. paramVals[i] = exprs.get(i).eval(ctx);
  998. }
  999. return paramVals;
  1000. }
  1001. private static Value[] exprListToDelayedValues(
  1002. List<Expr> exprs, EvalContext ctx) {
  1003. Value[] paramVals = new Value[exprs.size()];
  1004. for(int i = 0; i < exprs.size(); ++i) {
  1005. paramVals[i] = new DelayedValue(exprs.get(i), ctx);
  1006. }
  1007. return paramVals;
  1008. }
  1009. private static boolean areConstant(List<Expr> exprs) {
  1010. for(Expr expr : exprs) {
  1011. if(!expr.isConstant()) {
  1012. return false;
  1013. }
  1014. }
  1015. return true;
  1016. }
  1017. private static boolean areConstant(Expr... exprs) {
  1018. for(Expr expr : exprs) {
  1019. if(!expr.isConstant()) {
  1020. return false;
  1021. }
  1022. }
  1023. return true;
  1024. }
  1025. private static void literalStrToString(String str, StringBuilder sb) {
  1026. sb.append("\"")
  1027. .append(str.replace("\"", "\"\""))
  1028. .append("\"");
  1029. }
  1030. private static Pattern likePatternToRegex(String pattern) {
  1031. StringBuilder sb = new StringBuilder(pattern.length());
  1032. // Access LIKE pattern supports (note, matching is case-insensitive):
  1033. // - '*' -> 0 or more chars
  1034. // - '?' -> single character
  1035. // - '#' -> single digit
  1036. // - '[...]' -> character class, '[!...]' -> not in char class
  1037. for(int i = 0; i < pattern.length(); ++i) {
  1038. char c = pattern.charAt(i);
  1039. if(c == '*') {
  1040. sb.append(".*");
  1041. } else if(c == '?') {
  1042. sb.append('.');
  1043. } else if(c == '#') {
  1044. sb.append("\\d");
  1045. } else if(c == '[') {
  1046. // find closing brace
  1047. int startPos = i + 1;
  1048. int endPos = -1;
  1049. for(int j = startPos; j < pattern.length(); ++j) {
  1050. if(pattern.charAt(j) == ']') {
  1051. endPos = j;
  1052. break;
  1053. }
  1054. }
  1055. // access treats invalid expression like "unmatchable"
  1056. if(endPos == -1) {
  1057. return UNMATCHABLE_REGEX;
  1058. }
  1059. String charClass = pattern.substring(startPos, endPos);
  1060. if((charClass.length() > 0) && (charClass.charAt(0) == '!')) {
  1061. // this is a negated char class
  1062. charClass = '^' + charClass.substring(1);
  1063. }
  1064. sb.append('[').append(charClass).append(']');
  1065. i += (endPos - startPos) + 1;
  1066. } else if(REGEX_SPEC_CHARS.contains(c)) {
  1067. // this char is special in regexes, so escape it
  1068. sb.append('\\').append(c);
  1069. } else {
  1070. sb.append(c);
  1071. }
  1072. }
  1073. try {
  1074. return Pattern.compile(sb.toString(),
  1075. Pattern.CASE_INSENSITIVE | Pattern.DOTALL |
  1076. Pattern.UNICODE_CASE);
  1077. } catch(PatternSyntaxException ignored) {
  1078. return UNMATCHABLE_REGEX;
  1079. }
  1080. }
  1081. private static Value toLiteralValue(Value.Type valType, Object value,
  1082. DateFormat sdf)
  1083. {
  1084. switch(valType) {
  1085. case STRING:
  1086. return ValueSupport.toValue((String)value);
  1087. case DATE:
  1088. return new DateValue((Date)value, sdf);
  1089. case TIME:
  1090. return new TimeValue((Date)value, sdf);
  1091. case DATE_TIME:
  1092. return new DateTimeValue((Date)value, sdf);
  1093. case LONG:
  1094. return ValueSupport.toValue((Integer)value);
  1095. case DOUBLE:
  1096. return ValueSupport.toValue((Double)value);
  1097. case BIG_DEC:
  1098. return ValueSupport.toValue((BigDecimal)value);
  1099. default:
  1100. throw new ParseException("unexpected literal type " + valType);
  1101. }
  1102. }
  1103. private static boolean isLiteralDefaultValue(
  1104. TokBuf buf, Value.Type resultType, String exprStr) {
  1105. // if a default value expression does not start with an '=' and is used in
  1106. // a string context, then it is taken as a literal value unless it starts
  1107. // with a " char
  1108. if(buf.getExprType() != Type.DEFAULT_VALUE) {
  1109. return false;
  1110. }
  1111. // a leading "=" indicates "full" expression handling for a DEFAULT_VALUE
  1112. // (consume this value once we detect it)
  1113. if(isOp(buf.peekNext(), "=")) {
  1114. buf.next();
  1115. return false;
  1116. }
  1117. return((resultType == Value.Type.STRING) &&
  1118. ((exprStr.length() == 0) ||
  1119. (exprStr.charAt(0) != ExpressionTokenizer.QUOTED_STR_CHAR)));
  1120. }
  1121. private interface LeftAssocExpr {
  1122. public OpType getOp();
  1123. public Expr getLeft();
  1124. public void setLeft(Expr left);
  1125. }
  1126. private interface RightAssocExpr {
  1127. public OpType getOp();
  1128. public Expr getRight();
  1129. public void setRight(Expr right);
  1130. }
  1131. private static final class DelayedValue extends BaseDelayedValue
  1132. {
  1133. private final Expr _expr;
  1134. private final EvalContext _ctx;
  1135. private DelayedValue(Expr expr, EvalContext ctx) {
  1136. _expr = expr;
  1137. _ctx = ctx;
  1138. }
  1139. @Override
  1140. public Value eval() {
  1141. return _expr.eval(_ctx);
  1142. }
  1143. }
  1144. private static abstract class Expr
  1145. {
  1146. @Override
  1147. public String toString() {
  1148. StringBuilder sb = new StringBuilder();
  1149. toString(sb, false);
  1150. return sb.toString();
  1151. }
  1152. public String toDebugString() {
  1153. StringBuilder sb = new StringBuilder();
  1154. toString(sb, true);
  1155. return sb.toString();
  1156. }
  1157. protected boolean isValidationExpr() {
  1158. return false;
  1159. }
  1160. protected void toString(StringBuilder sb, boolean isDebug) {
  1161. if(isDebug) {
  1162. sb.append("<").append(getClass().getSimpleName()).append(">{");
  1163. }
  1164. toExprString(sb, isDebug);
  1165. if(isDebug) {
  1166. sb.append("}");
  1167. }
  1168. }
  1169. protected Expr resolveOrderOfOperations() {
  1170. if(!(this instanceof LeftAssocExpr)) {
  1171. // nothing we can do
  1172. return this;
  1173. }
  1174. // in order to get the precedence right, we need to first associate this
  1175. // expression with the "rightmost" expression preceding it, then adjust
  1176. // this expression "down" (lower precedence) as the precedence of the
  1177. // operations dictates. since we parse from left to right, the initial
  1178. // "left" value isn't the immediate left expression, instead it's based
  1179. // on how the preceding operator precedence worked out. we need to
  1180. // adjust "this" expression to the closest preceding expression before
  1181. // we can correctly resolve precedence.
  1182. Expr outerExpr = this;
  1183. final LeftAssocExpr thisExpr = (LeftAssocExpr)this;
  1184. final Expr thisLeft = thisExpr.getLeft();
  1185. // current: <this>{<left>{A op1 B} op2 <right>{C}}
  1186. if(thisLeft instanceof RightAssocExpr) {
  1187. RightAssocExpr leftOp = (RightAssocExpr)thisLeft;
  1188. // target: <left>{A op1 <this>{B op2 <right>{C}}}
  1189. thisExpr.setLeft(leftOp.getRight());
  1190. // give the new version of this expression an opportunity to further
  1191. // swap (since the swapped expression may itself be a binary
  1192. // expression)
  1193. leftOp.setRight(resolveOrderOfOperations());
  1194. outerExpr = thisLeft;
  1195. // at this point, this expression has been pushed all the way to the
  1196. // rightmost preceding expression (we artifically gave "this" the
  1197. // highest precedence). now, we want to adjust precedence as
  1198. // necessary (shift it back down if the operator precedence is
  1199. // incorrect). note, we only need to check precedence against "this",
  1200. // as all other precedence has been resolved in previous parsing
  1201. // rounds.
  1202. if((leftOp.getRight() == this) &&
  1203. !isHigherPrecendence(thisExpr.getOp(), leftOp.getOp())) {
  1204. // doh, "this" is lower (or the same) precedence, restore the
  1205. // original order of things
  1206. leftOp.setRight(thisExpr.getLeft());
  1207. thisExpr.setLeft(thisLeft);
  1208. outerExpr = this;
  1209. }
  1210. }
  1211. return outerExpr;
  1212. }
  1213. public abstract boolean isConstant();
  1214. public abstract Value eval(EvalContext ctx);
  1215. public abstract void collectIdentifiers(Collection<Identifier> identifiers);
  1216. protected abstract void toExprString(StringBuilder sb, boolean isDebug);
  1217. }
  1218. private static final class EConstValue extends Expr
  1219. {
  1220. private final Value _val;
  1221. private final String _str;
  1222. private EConstValue(Value val, String str) {
  1223. _val = val;
  1224. _str = str;
  1225. }
  1226. @Override
  1227. public boolean isConstant() {
  1228. return true;
  1229. }
  1230. @Override
  1231. public Value eval(EvalContext ctx) {
  1232. return _val;
  1233. }
  1234. @Override
  1235. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1236. // none
  1237. }
  1238. @Override
  1239. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1240. sb.append(_str);
  1241. }
  1242. }
  1243. private static final class EThisValue extends Expr
  1244. {
  1245. @Override
  1246. public boolean isConstant() {
  1247. return false;
  1248. }
  1249. @Override
  1250. public Value eval(EvalContext ctx) {
  1251. return ctx.getThisColumnValue();
  1252. }
  1253. @Override
  1254. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1255. // none
  1256. }
  1257. @Override
  1258. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1259. sb.append("<THIS_COL>");
  1260. }
  1261. }
  1262. private static final class ELiteralValue extends Expr
  1263. {
  1264. private final Value _val;
  1265. private ELiteralValue(Value.Type valType, Object value,
  1266. DateFormat sdf) {
  1267. _val = toLiteralValue(valType, value, sdf);
  1268. }
  1269. @Override
  1270. public boolean isConstant() {
  1271. return true;
  1272. }
  1273. @Override
  1274. public Value eval(EvalContext ctx) {
  1275. return _val;
  1276. }
  1277. @Override
  1278. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1279. // none
  1280. }
  1281. @Override
  1282. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1283. if(_val.getType() == Value.Type.STRING) {
  1284. literalStrToString((String)_val.get(), sb);
  1285. } else if(_val.getType().isTemporal()) {
  1286. sb.append("#").append(_val.getAsString()).append("#");
  1287. } else {
  1288. sb.append(_val.get());
  1289. }
  1290. }
  1291. }
  1292. private static final class EObjValue extends Expr
  1293. {
  1294. private final Identifier _identifier;
  1295. private EObjValue(Identifier identifier) {
  1296. _identifier = identifier;
  1297. }
  1298. @Override
  1299. public boolean isConstant() {
  1300. return false;
  1301. }
  1302. @Override
  1303. public Value eval(EvalContext ctx) {
  1304. return ctx.getIdentifierValue(_identifier);
  1305. }
  1306. @Override
  1307. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1308. identifiers.add(_identifier);
  1309. }
  1310. @Override
  1311. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1312. sb.append(_identifier);
  1313. }
  1314. }
  1315. private static class EParen extends Expr
  1316. {
  1317. private final Expr _expr;
  1318. private EParen(Expr expr) {
  1319. _expr = expr;
  1320. }
  1321. @Override
  1322. public boolean isConstant() {
  1323. return _expr.isConstant();
  1324. }
  1325. @Override
  1326. protected boolean isValidationExpr() {
  1327. return _expr.isValidationExpr();
  1328. }
  1329. @Override
  1330. public Value eval(EvalContext ctx) {
  1331. return _expr.eval(ctx);
  1332. }
  1333. @Override
  1334. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1335. _expr.collectIdentifiers(identifiers);
  1336. }
  1337. @Override
  1338. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1339. sb.append("(");
  1340. _expr.toString(sb, isDebug);
  1341. sb.append(")");
  1342. }
  1343. }
  1344. private static class EFunc extends Expr
  1345. {
  1346. private final Function _func;
  1347. private final List<Expr> _params;
  1348. private EFunc(Function func, List<Expr> params) {
  1349. _func = func;
  1350. _params = params;
  1351. }
  1352. @Override
  1353. public boolean isConstant() {
  1354. return _func.isPure() && areConstant(_params);
  1355. }
  1356. @Override
  1357. public Value eval(EvalContext ctx) {
  1358. return _func.eval(ctx, exprListToValues(_params, ctx));
  1359. }
  1360. @Override
  1361. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1362. for(Expr param : _params) {
  1363. param.collectIdentifiers(identifiers);
  1364. }
  1365. }
  1366. @Override
  1367. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1368. sb.append(_func.getName()).append("(");
  1369. if(!_params.isEmpty()) {
  1370. exprListToString(_params, ",", sb, isDebug);
  1371. }
  1372. sb.append(")");
  1373. }
  1374. }
  1375. private static abstract class EBaseBinaryOp extends Expr
  1376. implements LeftAssocExpr, RightAssocExpr
  1377. {
  1378. protected final OpType _op;
  1379. protected Expr _left;
  1380. protected Expr _right;
  1381. private EBaseBinaryOp(OpType op, Expr left, Expr right) {
  1382. _op = op;
  1383. _left = left;
  1384. _right = right;
  1385. }
  1386. @Override
  1387. public boolean isConstant() {
  1388. return areConstant(_left, _right);
  1389. }
  1390. public OpType getOp() {
  1391. return _op;
  1392. }
  1393. public Expr getLeft() {
  1394. return _left;
  1395. }
  1396. public void setLeft(Expr left) {
  1397. _left = left;
  1398. }
  1399. public Expr getRight() {
  1400. return _right;
  1401. }
  1402. public void setRight(Expr right) {
  1403. _right = right;
  1404. }
  1405. @Override
  1406. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1407. _left.collectIdentifiers(identifiers);
  1408. _right.collectIdentifiers(identifiers);
  1409. }
  1410. @Override
  1411. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1412. _left.toString(sb, isDebug);
  1413. sb.append(" ").append(_op).append(" ");
  1414. _right.toString(sb, isDebug);
  1415. }
  1416. }
  1417. private static class EBinaryOp extends EBaseBinaryOp
  1418. {
  1419. private EBinaryOp(BinaryOp op, Expr left, Expr right) {
  1420. super(op, left, right);
  1421. }
  1422. @Override
  1423. public Value eval(EvalContext ctx) {
  1424. return ((BinaryOp)_op).eval(ctx, _left.eval(ctx), _right.eval(ctx));
  1425. }
  1426. }
  1427. private static class EUnaryOp extends Expr
  1428. implements RightAssocExpr
  1429. {
  1430. private final OpType _op;
  1431. private Expr _expr;
  1432. private EUnaryOp(UnaryOp op, Expr expr) {
  1433. _op = op;
  1434. _expr = expr;
  1435. }
  1436. @Override
  1437. public boolean isConstant() {
  1438. return _expr.isConstant();
  1439. }
  1440. public OpType getOp() {
  1441. return _op;
  1442. }
  1443. public Expr getRight() {
  1444. return _expr;
  1445. }
  1446. public void setRight(Expr right) {
  1447. _expr = right;
  1448. }
  1449. @Override
  1450. public Value eval(EvalContext ctx) {
  1451. return ((UnaryOp)_op).eval(ctx, _expr.eval(ctx));
  1452. }
  1453. @Override
  1454. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1455. _expr.collectIdentifiers(identifiers);
  1456. }
  1457. @Override
  1458. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1459. sb.append(_op);
  1460. if(isDebug || ((UnaryOp)_op).needsSpace()) {
  1461. sb.append(" ");
  1462. }
  1463. _expr.toString(sb, isDebug);
  1464. }
  1465. }
  1466. private static class ECompOp extends EBaseBinaryOp
  1467. {
  1468. private ECompOp(CompOp op, Expr left, Expr right) {
  1469. super(op, left, right);
  1470. }
  1471. @Override
  1472. protected boolean isValidationExpr() {
  1473. return true;
  1474. }
  1475. @Override
  1476. public Value eval(EvalContext ctx) {
  1477. return ((CompOp)_op).eval(_left.eval(ctx), _right.eval(ctx));
  1478. }
  1479. }
  1480. private static class EImplicitCompOp extends ECompOp
  1481. {
  1482. private EImplicitCompOp(Expr right) {
  1483. super(CompOp.EQ, THIS_COL_VALUE, right);
  1484. }
  1485. @Override
  1486. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1487. // only output the full "implicit" comparison in debug mode
  1488. if(isDebug) {
  1489. super.toExprString(sb, isDebug);
  1490. } else {
  1491. // just output the explicit part of the expression
  1492. _right.toString(sb, isDebug);
  1493. }
  1494. }
  1495. }
  1496. private static class ELogicalOp extends EBaseBinaryOp
  1497. {
  1498. private ELogicalOp(LogOp op, Expr left, Expr right) {
  1499. super(op, left, right);
  1500. }
  1501. @Override
  1502. protected boolean isValidationExpr() {
  1503. return true;
  1504. }
  1505. @Override
  1506. public Value eval(final EvalContext ctx) {
  1507. // logical operations do short circuit evaluation, so we need to delay
  1508. // computing results until necessary
  1509. return ((LogOp)_op).eval(new DelayedValue(_left, ctx),
  1510. new DelayedValue(_right, ctx));
  1511. }
  1512. }
  1513. private static abstract class ESpecOp extends Expr
  1514. implements LeftAssocExpr
  1515. {
  1516. protected final SpecOp _op;
  1517. protected Expr _expr;
  1518. private ESpecOp(SpecOp op, Expr expr) {
  1519. _op = op;
  1520. _expr = expr;
  1521. }
  1522. @Override
  1523. public boolean isConstant() {
  1524. return _expr.isConstant();
  1525. }
  1526. public OpType getOp() {
  1527. return _op;
  1528. }
  1529. public Expr getLeft() {
  1530. return _expr;
  1531. }
  1532. public void setLeft(Expr left) {
  1533. _expr = left;
  1534. }
  1535. @Override
  1536. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1537. _expr.collectIdentifiers(identifiers);
  1538. }
  1539. @Override
  1540. protected boolean isValidationExpr() {
  1541. return true;
  1542. }
  1543. }
  1544. private static class ENullOp extends ESpecOp
  1545. {
  1546. private ENullOp(SpecOp op, Expr expr) {
  1547. super(op, expr);
  1548. }
  1549. @Override
  1550. public Value eval(EvalContext ctx) {
  1551. return _op.eval(_expr.eval(ctx), null, null);
  1552. }
  1553. @Override
  1554. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1555. _expr.toString(sb, isDebug);
  1556. sb.append(" ").append(_op);
  1557. }
  1558. }
  1559. private static class ELikeOp extends ESpecOp
  1560. {
  1561. private final String _patternStr;
  1562. private Pattern _pattern;
  1563. private ELikeOp(SpecOp op, Expr expr, String patternStr) {
  1564. super(op, expr);
  1565. _patternStr = patternStr;
  1566. }
  1567. private Pattern getPattern()
  1568. {
  1569. if(_pattern == null) {
  1570. _pattern = likePatternToRegex(_patternStr);
  1571. }
  1572. return _pattern;
  1573. }
  1574. @Override
  1575. public Value eval(EvalContext ctx) {
  1576. return _op.eval(_expr.eval(ctx), getPattern(), null);
  1577. }
  1578. @Override
  1579. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1580. _expr.toString(sb, isDebug);
  1581. sb.append(" ").append(_op).append(" ");
  1582. literalStrToString(_patternStr, sb);
  1583. if(isDebug) {
  1584. sb.append("(").append(getPattern()).append(")");
  1585. }
  1586. }
  1587. }
  1588. private static class EInOp extends ESpecOp
  1589. {
  1590. private final List<Expr> _exprs;
  1591. private EInOp(SpecOp op, Expr expr, List<Expr> exprs) {
  1592. super(op, expr);
  1593. _exprs = exprs;
  1594. }
  1595. @Override
  1596. public boolean isConstant() {
  1597. return super.isConstant() && areConstant(_exprs);
  1598. }
  1599. @Override
  1600. public Value eval(EvalContext ctx) {
  1601. return _op.eval(_expr.eval(ctx),
  1602. exprListToDelayedValues(_exprs, ctx), null);
  1603. }
  1604. @Override
  1605. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1606. for(Expr expr : _exprs) {
  1607. expr.collectIdentifiers(identifiers);
  1608. }
  1609. }
  1610. @Override
  1611. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1612. _expr.toString(sb, isDebug);
  1613. sb.append(" ").append(_op).append(" (");
  1614. exprListToString(_exprs, ",", sb, isDebug);
  1615. sb.append(")");
  1616. }
  1617. }
  1618. private static class EBetweenOp extends ESpecOp
  1619. implements RightAssocExpr
  1620. {
  1621. private final Expr _startRangeExpr;
  1622. private Expr _endRangeExpr;
  1623. private EBetweenOp(SpecOp op, Expr expr, Expr startRangeExpr,
  1624. Expr endRangeExpr) {
  1625. super(op, expr);
  1626. _startRangeExpr = startRangeExpr;
  1627. _endRangeExpr = endRangeExpr;
  1628. }
  1629. @Override
  1630. public boolean isConstant() {
  1631. return _expr.isConstant() && areConstant(_startRangeExpr, _endRangeExpr);
  1632. }
  1633. public Expr getRight() {
  1634. return _endRangeExpr;
  1635. }
  1636. public void setRight(Expr right) {
  1637. _endRangeExpr = right;
  1638. }
  1639. @Override
  1640. public Value eval(EvalContext ctx) {
  1641. return _op.eval(_expr.eval(ctx),
  1642. new DelayedValue(_startRangeExpr, ctx),
  1643. new DelayedValue(_endRangeExpr, ctx));
  1644. }
  1645. @Override
  1646. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1647. super.collectIdentifiers(identifiers);
  1648. _startRangeExpr.collectIdentifiers(identifiers);
  1649. _endRangeExpr.collectIdentifiers(identifiers);
  1650. }
  1651. @Override
  1652. protected void toExprString(StringBuilder sb, boolean isDebug) {
  1653. _expr.toString(sb, isDebug);
  1654. sb.append(" ").append(_op).append(" ");
  1655. _startRangeExpr.toString(sb, isDebug);
  1656. sb.append(" And ");
  1657. _endRangeExpr.toString(sb, isDebug);
  1658. }
  1659. }
  1660. /**
  1661. * Base Expression wrapper for an Expr.
  1662. */
  1663. private static abstract class BaseExprWrapper implements Expression
  1664. {
  1665. private final String _rawExprStr;
  1666. private final Expr _expr;
  1667. private BaseExprWrapper(String rawExprStr, Expr expr) {
  1668. _rawExprStr = rawExprStr;
  1669. _expr = expr;
  1670. }
  1671. public String toDebugString() {
  1672. return _expr.toDebugString();
  1673. }
  1674. public String toRawString() {
  1675. return _rawExprStr;
  1676. }
  1677. public boolean isConstant() {
  1678. return _expr.isConstant();
  1679. }
  1680. public void collectIdentifiers(Collection<Identifier> identifiers) {
  1681. _expr.collectIdentifiers(identifiers);
  1682. }
  1683. @Override
  1684. public String toString() {
  1685. return _expr.toString();
  1686. }
  1687. protected Object evalValue(Value.Type resultType, EvalContext ctx) {
  1688. Value val = _expr.eval(ctx);
  1689. if(val.isNull()) {
  1690. return null;
  1691. }
  1692. if(resultType == null) {
  1693. // return as "native" type
  1694. return val.get();
  1695. }
  1696. // FIXME possibly do some type coercion. are there conversions here which don't work elsewhere? (string -> date, string -> number)?
  1697. switch(resultType) {
  1698. case STRING:
  1699. return val.getAsString();
  1700. case DATE:
  1701. case TIME:
  1702. case DATE_TIME:
  1703. return val.getAsDateTime(ctx);
  1704. case LONG:
  1705. return val.getAsLongInt();
  1706. case DOUBLE:
  1707. return val.getAsDouble();
  1708. case BIG_DEC:
  1709. return val.getAsBigDecimal();
  1710. default:
  1711. throw new IllegalStateException("unexpected result type " + resultType);
  1712. }
  1713. }
  1714. protected Boolean evalCondition(EvalContext ctx) {
  1715. Value val = _expr.eval(ctx);
  1716. if(val.isNull()) {
  1717. // null can't be coerced to a boolean
  1718. throw new EvalException("Condition evaluated to Null");
  1719. }
  1720. return val.getAsBoolean();
  1721. }
  1722. }
  1723. /**
  1724. * Expression wrapper for an Expr which returns a value.
  1725. */
  1726. private static class ExprWrapper extends BaseExprWrapper
  1727. {
  1728. private final Value.Type _resultType;
  1729. private ExprWrapper(String rawExprStr, Expr expr, Value.Type resultType) {
  1730. super(rawExprStr, expr);
  1731. _resultType = resultType;
  1732. }
  1733. public Object eval(EvalContext ctx) {
  1734. return evalValue(_resultType, ctx);
  1735. }
  1736. }
  1737. /**
  1738. * Expression wrapper for an Expr which returns a Boolean from a conditional
  1739. * expression.
  1740. */
  1741. private static class CondExprWrapper extends BaseExprWrapper
  1742. {
  1743. private CondExprWrapper(String rawExprStr, Expr expr) {
  1744. super(rawExprStr, expr);
  1745. }
  1746. public Object eval(EvalContext ctx) {
  1747. return evalCondition(ctx);
  1748. }
  1749. }
  1750. /**
  1751. * Expression wrapper for a <i>pure</i> Expr which caches the result of
  1752. * evaluation.
  1753. */
  1754. private static final class MemoizedExprWrapper extends ExprWrapper
  1755. {
  1756. private Object _val;
  1757. private MemoizedExprWrapper(String rawExprStr, Expr expr,
  1758. Value.Type resultType) {
  1759. super(rawExprStr, expr, resultType);
  1760. }
  1761. @Override
  1762. public Object eval(EvalContext ctx) {
  1763. if(_val == null) {
  1764. _val = super.eval(ctx);
  1765. }
  1766. return _val;
  1767. }
  1768. }
  1769. /**
  1770. * Expression wrapper for a <i>pure</i> conditional Expr which caches the
  1771. * result of evaluation.
  1772. */
  1773. private static final class MemoizedCondExprWrapper extends CondExprWrapper
  1774. {
  1775. private Object _val;
  1776. private MemoizedCondExprWrapper(String rawExprStr, Expr expr) {
  1777. super(rawExprStr, expr);
  1778. }
  1779. @Override
  1780. public Object eval(EvalContext ctx) {
  1781. if(_val == null) {
  1782. _val = super.eval(ctx);
  1783. }
  1784. return _val;
  1785. }
  1786. }
  1787. }