|
@@ -0,0 +1,136 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+export const formulaStringToArray = (text) => {
|
|
|
+
|
|
|
+ text = "FC.pow(FC.pow(E[assa_faf],3),2)+FC.date(E[assa_aff],我的)";
|
|
|
+
|
|
|
+ // 匹配开始的FC.xxx(
|
|
|
+ const startFCRegExp = /^FC\.([a-zA-Z]+)\(/;
|
|
|
+ const startBracketsReg = /^\(/;//左括号
|
|
|
+ const endBracketsReg = /^\)/;//右括号
|
|
|
+ const elementReg = /^E\[(.[^\]]+_.[^\]]+)\]/;//元素
|
|
|
+ const commaReg = /^,/;//逗号
|
|
|
+ const operatorReg = /^\+|-|\*|÷/;//加减乘除
|
|
|
+ const wordReg = /^[\u4e00-\u9fa5\w'"-]+/;//文本
|
|
|
+
|
|
|
+ let resArr = [];//结果数组
|
|
|
+ let strIndex = 0;//位置索引
|
|
|
+ let nuText = text;//未处理的字符串
|
|
|
+ //let startStack = [];//方法体开始部分和左括号 放进来的栈
|
|
|
+ let contentStack = [];//内容放进来的栈
|
|
|
+ let elementArr = [];//存放元素的数组
|
|
|
+
|
|
|
+ while (strIndex < text.length) {
|
|
|
+ nuText = text.substring(strIndex);
|
|
|
+ if(startFCRegExp.test(nuText)){
|
|
|
+ //console.log('匹配FC开始部分')
|
|
|
+ //匹配FC开始部分FC.xxx(
|
|
|
+ let regRes = nuText.match(startFCRegExp);
|
|
|
+ let startText = regRes[0];//匹配到的文本
|
|
|
+
|
|
|
+ //startStack.push(startText);
|
|
|
+ contentStack.push({
|
|
|
+ type:'Function',
|
|
|
+ children:[],
|
|
|
+ tag:startText
|
|
|
+ });
|
|
|
+
|
|
|
+ strIndex += startText.length;//索引移动
|
|
|
+ }else if(endBracketsReg.test(nuText)){
|
|
|
+ //console.log('匹配右括号')
|
|
|
+ //匹配右括号
|
|
|
+ let endBrackets = nuText.match(endBracketsReg)[0];
|
|
|
+
|
|
|
+ let popObj = contentStack.pop();
|
|
|
+ if(contentStack.length > 0){
|
|
|
+ let content =contentStack[contentStack.length - 1];
|
|
|
+ popObj.parent = content;
|
|
|
+ content.children.push(popObj);
|
|
|
+ }else{
|
|
|
+ //匹配完成最顶层的一整个方法体,或括号
|
|
|
+ resArr.push(popObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ strIndex += endBrackets.length;//索引移动
|
|
|
+ }else if(startBracketsReg.test(nuText)){
|
|
|
+ //console.log('匹配左括号')
|
|
|
+ //匹配左括号
|
|
|
+ let startBrackets = nuText.match(startBracketsReg)[0];
|
|
|
+
|
|
|
+ contentStack.push({
|
|
|
+ type:'Brackets',
|
|
|
+ children:[],
|
|
|
+ tag:startBrackets
|
|
|
+ });
|
|
|
+
|
|
|
+ strIndex += startBrackets.length;//索引移动
|
|
|
+ }else if(elementReg.test(nuText)){
|
|
|
+ //console.log('匹配元素')
|
|
|
+ //匹配元素
|
|
|
+ let eleArr = nuText.match(elementReg);
|
|
|
+ let obj = {
|
|
|
+ type:'Element',
|
|
|
+ tableElementKey:eleArr[1],
|
|
|
+ children:[],
|
|
|
+ tag:eleArr[0]
|
|
|
+ }
|
|
|
+ let content =contentStack[contentStack.length - 1];
|
|
|
+ obj.parent = content;
|
|
|
+ content.children.push(obj);
|
|
|
+
|
|
|
+ elementArr.push(obj)
|
|
|
+
|
|
|
+ strIndex += eleArr[0].length;//索引移动
|
|
|
+ }else if(commaReg.test(nuText)){
|
|
|
+ //console.log('匹配逗号')
|
|
|
+ //匹配逗号
|
|
|
+ let comma = nuText.match(commaReg)[0];
|
|
|
+
|
|
|
+ contentStack[contentStack.length - 1].children.push({
|
|
|
+ type:'Comma',
|
|
|
+ tag:comma
|
|
|
+ });
|
|
|
+
|
|
|
+ strIndex += comma.length;//索引移动
|
|
|
+ }else if(operatorReg.test(nuText)){
|
|
|
+ //console.log('匹配加减乘除')
|
|
|
+ //匹配加减乘除
|
|
|
+ let operator = nuText.match(operatorReg)[0];
|
|
|
+ let obj = {
|
|
|
+ type:'Operator',
|
|
|
+ name:operator,
|
|
|
+ tag:operator,
|
|
|
+ }
|
|
|
+ if(contentStack.length > 0){
|
|
|
+ //不然就在方法体或括号里面
|
|
|
+ contentStack[contentStack.length - 1].children.push(obj);
|
|
|
+ }else{
|
|
|
+ //如果没有,那就是在最上层
|
|
|
+ resArr.push(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ strIndex += operator.length;//索引移动
|
|
|
+ }else if(wordReg.test(nuText)){
|
|
|
+ //匹配文本
|
|
|
+ let word = nuText.match(wordReg)[0];
|
|
|
+ console.log('匹配文本',word)
|
|
|
+
|
|
|
+ contentStack[contentStack.length - 1].children.push({
|
|
|
+ type:'Text',
|
|
|
+ tag:word
|
|
|
+ });
|
|
|
+
|
|
|
+ strIndex += word.length;//索引移动
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ console.log('匹配不到:',nuText)
|
|
|
+ //匹配不到
|
|
|
+ strIndex++
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(contentStack)
|
|
|
+ console.log(resArr);
|
|
|
+ return resArr;
|
|
|
+}
|