Expression.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.jfireel.expression;
  2. import com.jfireel.exception.UnParsedException;
  3. import com.jfireel.expression.node.CalculateNode;
  4. import com.jfireel.expression.parse.Invoker;
  5. import com.jfireel.expression.parse.impl.*;
  6. import com.jfireel.expression.util.Functional;
  7. import com.jfireel.expression.util.OperatorResultUtil;
  8. import java.util.*;
  9. public class Expression {
  10. private CalculateNode parseNode;
  11. private Deque<CalculateNode> nodes = new LinkedList<CalculateNode>();
  12. private String el;
  13. private final int function;
  14. private final Invoker head;
  15. private static final Invoker DEFAULT_HEAD;
  16. static {
  17. NodeParser[] parsers = new NodeParser[]{ //
  18. new SkipIgnoredToken(), //
  19. new LeftParenParser(), //
  20. new RightParenParser(), //
  21. new LeftBracketParser(), //
  22. new TypeParser(), //
  23. new RightBracketParser(), //
  24. new PropertyParser(), //
  25. new EnumParser(), //
  26. new MethodParser(), //
  27. new CommaParser(), //
  28. new ConstantStringParser(), //
  29. new NumberParser(), //
  30. new IdentifierParser(), //
  31. new OperatorParser(),//
  32. new ChineseParser()
  33. };
  34. Invoker pred = (el, offset, nodes, function) -> offset;
  35. for (int i = parsers.length - 1; i > -1; i--) {
  36. final NodeParser parser = parsers[i];
  37. final Invoker next = pred;
  38. pred = (el1, offset, nodes1, function1) -> parser.parse(el1, offset, nodes1, function1, next);
  39. }
  40. DEFAULT_HEAD = pred;
  41. }
  42. public static Expression parse(String el) {
  43. return new Expression(el, Functional.build().setMethodInvokeByCompile(true).toFunction(), DEFAULT_HEAD);
  44. }
  45. public static Expression parse(String el, int function) {
  46. return new Expression(el, function, DEFAULT_HEAD);
  47. }
  48. public static Expression parse(String el, int function, Invoker head) {
  49. return new Expression(el, function, head);
  50. }
  51. private Expression(String el, int function, Invoker head) {
  52. this.head = head;
  53. this.el = el;
  54. this.function = function;
  55. try {
  56. scan();
  57. } catch (Exception e) {
  58. throw new UnParsedException(el, e);
  59. }
  60. }
  61. private void scan() {
  62. int offset = 0;
  63. int length = el.length();
  64. while (offset < length) {
  65. int result = head.parse(el, offset, nodes, function);
  66. if (result == offset) {
  67. throw new IllegalArgumentException("无法识别的表达式,解析过程预见无法识别的字符:" + el.substring(0, offset));
  68. }
  69. offset = result;
  70. }
  71. List<CalculateNode> list = new ArrayList<CalculateNode>();
  72. CalculateNode tmp;
  73. while ((tmp = nodes.pollFirst()) != null) {
  74. list.add(0, tmp);
  75. }
  76. parseNode = OperatorResultUtil.aggregate(list, function, el, offset);
  77. nodes = null;
  78. el = null;
  79. }
  80. @SuppressWarnings("unchecked")
  81. public <T> T calculate(Map<String, Object> variables) {
  82. return (T) parseNode.calculate(variables);
  83. }
  84. @SuppressWarnings("unchecked")
  85. public <T> T calculate() {
  86. return (T) parseNode.calculate(null);
  87. }
  88. public CalculateNode parseResult() {
  89. return parseNode;
  90. }
  91. /**
  92. * 返回解析的表达式
  93. *
  94. * @return
  95. */
  96. public String getEl() {
  97. return el;
  98. }
  99. }