📄 jmexprparse.java
字号:
package MultiScriptParse.Parse;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class JMExprParse extends JMObject {
//"+,-,*,/,%,^,<,>,<=,>=,==,!=,!,&&,||,(,),#"
static private final int PLUS = 0;
static private final int MINUS = 2;
static private final int MULTIPLY = 4;
static private final int DIVIDE = 6;
static private final int PER_CENT = 8;
static private final int A = 10;
static private final int LESS = 12;
static private final int MORE = 14;
static private final int LESS_EQUAL = 16;
static private final int MORE_EQUAL = 19;
static private final int EQUAL = 22;
static private final int NOTEQUAL = 25;
static private final int NOT = 28;
static private final int AND = 30;
static private final int OR = 33;
static private final int LEFT_PARENTHESES = 36;
static private final int RIGHT_PARENTHESES = 38;
private JMPropParse PropParse;
private JMFunctionParse FunctionParse;
private Object[] postfixExpr = new Object[50];
private boolean[] isOperator = new boolean[50];
private StringBuffer key = new StringBuffer(25);
public JMExprParse() {
}
public JMExprParse(JMParse Owner) {
super();
this.Owner = Owner;
}
public JMExprParse(JMPropParse PropParse, JMParse Owner) {
super(Owner);
this.PropParse = PropParse;
}
public void setPropParse(JMPropParse PropParse) {
this.PropParse = PropParse;
}
public void setFunctionParse(JMFunctionParse FunctionParse) {
this.FunctionParse = FunctionParse;
}
private void ZeroKeyBuffer() {
key.delete(0, 25);
}
private void ParseSymbol(int symbolType, String symbol, Stack stackCalc, Count Index)
throws JMParseException
{
if (symbolType == 0) return;
if (symbolType < 0) {
//比较操作符和栈顶操作符的优先级
int symbolIndexC = JMParse.Operator.indexOf(symbol + ',');
int symbolIndexS = ((Integer)stackCalc.peek()).intValue();
if (symbolIndexC < 0 || symbolIndexS < 0)
throw new JMParseException(new StringBuffer("不能识别的操作符").
append("\"").append(symbol).append("\"").toString());
while(JMParse.SOptpriority[symbolIndexS] > JMParse.COptpriority[symbolIndexC]){
isOperator[Index.Value] = true;
postfixExpr[Index.Value] = stackCalc.pop();
Index.Inc();
symbolIndexS = ((Integer)stackCalc.peek()).intValue();
}
if (JMParse.SOptpriority[symbolIndexS] == JMParse.COptpriority[symbolIndexC])
stackCalc.pop();
else stackCalc.push(new Integer(symbolIndexC));
} else {
//解释操作数字符串为java对象并储存
isOperator[Index.Value] = false;
if (symbolType == 1) {
postfixExpr[Index.Value] = Integer.valueOf(symbol);
Index.Inc();
}
else if (symbolType == 2) throw new JMParseException("没有结束的字符串");
else if (symbolType == 3) {
postfixExpr[Index.Value] = new String(symbol);
Index.Inc();
}
else if (symbolType == 5) {
if (symbol.indexOf("true") >= 0) postfixExpr[Index.Value] = new Boolean("true");
else if (symbol.indexOf("false") >= 0) postfixExpr[Index.Value] = new Boolean("false");
else if (symbol.indexOf("null") >= 0) postfixExpr[Index.Value] = null;
else postfixExpr[Index.Value] = PropParse.FindObject(symbol);
Index.Inc();
}
}
ZeroKeyBuffer();
}
public Object ParseExpr(char[] toParse) throws JMParseException
{
char ckey = toParse[JMParse.CurrPos];
/**
* 1 表示为数字类型常量 2 表示为字符串常量开始 3 表示字符串常量结束
* 4 逻辑型常量 5 不确定的标志符 6 函数 7 对象方法 -1 操作符号
*/
int symbolType = 0;
int left_parentheses = 0;
Stack stackCalc = new Stack();
Count cc = new Count();
ZeroKeyBuffer();
/*虚拟的表达式结束符 # index = 40
操作符入栈的时候好转化为相应的在操作符字符串中的索引值
便于加速
*/
stackCalc.push(new Integer(40));
while (ckey != 0) {
if (ckey == '#'){
JMParse.SkipRemark(toParse);
ckey = toParse[++JMParse.CurrPos];
continue;
}
//处理是分隔可忽略的字符
if (ckey < 33) {
if (ckey == 32)
if (symbolType == 2) {
key.append(ckey);
ckey = toParse[++JMParse.CurrPos];
continue;
}
if (ckey == '\n')
JMParse.CurrLine ++;
if (symbolType != 0 && symbolType != 2){
ParseSymbol(symbolType, key.toString(), stackCalc, cc);
symbolType = 0;
key.delete(0, 25);
}
ckey = toParse[++JMParse.CurrPos];
continue;
}
if (ckey == ';' || ckey == ','){
ParseSymbol(symbolType, key.toString(), stackCalc, cc);
symbolType = 0;
break;
}
//处理操作符
if (JMParse.Operator.indexOf(ckey) > -1) {
if (ckey != '(' && ckey != ')') {
if (symbolType >= 0) {
if (symbolType < 6) //不是对象方法和函数方法
ParseSymbol(symbolType, key.toString(), stackCalc, cc);
symbolType = -1;
}
key.append(ckey);
ckey = toParse[++JMParse.CurrPos];
continue;
}
}
//处理操作数
//自定义函数
if (ckey == '(') {
if (symbolType == 5) {
symbolType = 6;
//增加自定义函数的处理事件把对象加入数组postfixExpr
postfixExpr[cc.Value] = FunctionParse.ExecFunction(key.toString(), toParse, null);
cc.Inc();
key = key.delete(0, 25);
ckey = toParse[JMParse.CurrPos];
} else if (symbolType <= 0){
ParseSymbol(symbolType, key.toString(), stackCalc, cc);
left_parentheses++;
stackCalc.push(new Integer(JMParse.Operator.indexOf("(")));
symbolType = 0;
ckey = toParse[++JMParse.CurrPos];
} else throw new JMParseException("非法使用的\"(\"");
//JMParse.CurrPos;
continue;
}
if (ckey == ')') {
ParseSymbol(symbolType, key.toString(), stackCalc, cc);
if (left_parentheses == 0) {
symbolType = 0;
break;
}
key.append(ckey);
symbolType = -1;
ParseSymbol(symbolType, key.toString(), stackCalc, cc);
left_parentheses--;
symbolType = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -