|
@@ -1,13 +1,17 @@
|
|
|
package com.jfireel.expression.parse.impl;
|
|
|
|
|
|
+import com.jfireel.expression.Expression;
|
|
|
import com.jfireel.expression.node.CalculateNode;
|
|
|
import com.jfireel.expression.node.impl.NumberNode;
|
|
|
import com.jfireel.expression.parse.Invoker;
|
|
|
import com.jfireel.expression.token.Operator;
|
|
|
import com.jfireel.expression.token.Symbol;
|
|
|
+import com.jfireel.expression.token.Token;
|
|
|
import com.jfireel.expression.util.CharType;
|
|
|
|
|
|
import java.util.Deque;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
public class NumberParser extends NodeParser {
|
|
|
|
|
@@ -27,15 +31,23 @@ public class NumberParser extends NodeParser {
|
|
|
if (nodes.peek() != null && nodes.peek().type() == Symbol.LEFT_PAREN) {
|
|
|
// 这种情况下,-代表是一个负数
|
|
|
return true;
|
|
|
- } else if (nodes.peek() != null && !(nodes.peek().type() instanceof Operator)) {
|
|
|
+ } else if(nodes.peek() != null&&nodes.stream().anyMatch(e->e.type() == Token.METHOD)&&(nodes.peek().type() == Symbol.COMMA)){
|
|
|
+ return true;
|
|
|
+ } else if(nodes.peek()!=null&&nodes.size()>=2&& (nodes.peek().type() instanceof Operator) && ((LinkedList)nodes).get(1) instanceof NumberNode){
|
|
|
+ return true;
|
|
|
+ }else if (nodes.peek() != null && !(nodes.peek().type() instanceof Operator)) {
|
|
|
// 这种情况下,-是一个操作符
|
|
|
return false;
|
|
|
- } else {
|
|
|
+ }else {
|
|
|
throw new IllegalArgumentException("无法识别的-符号,不是负数也不是操作符,问题区间:" + el.substring(0, offset));
|
|
|
}
|
|
|
} else return CharType.isDigital(getChar(offset, el));
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(Expression.parse("(2+2*3)*(1--1)").calculate().toString());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public int parse(String el, int offset, Deque<CalculateNode> nodes, int function, Invoker next) {
|
|
|
if (!match(el, offset, nodes, function)) {
|
|
@@ -47,7 +59,7 @@ public class NumberParser extends NodeParser {
|
|
|
offset += 1;
|
|
|
}
|
|
|
boolean hasDot = false;
|
|
|
- while (CharType.isDigital(c = getChar(offset, el)) || (hasDot == false && c == '.')) {
|
|
|
+ while (CharType.isDigital(c = getChar(offset, el)) || (!hasDot && c == '.')) {
|
|
|
offset++;
|
|
|
if (c == '.') {
|
|
|
hasDot = true;
|