import { Table } from './SchemaToStruct'; /** * 校验上下文 * @param localVariableList 变量表 * @param errors 异常信息 */ export class CheckContext { localVariable: any[] = [] //局部变量 contextVariables: any[] = [] //上下文变量 sessionVariables: any[] = [] //会话变量 entityStructs: Table[] = [] //数据模型 errors: any[] = [] //异常信息 } /** * 函数参数个数校验 * @description 参数个数校验 * @param tokens tokens * @param errors 异常 * @param funcs 表达式All内置函数 */ export function checkFunctionParams(tokens: any[], errors: any[], funcs: any[]) { var index = [[], [], []]; //二维数组:存储tokens、括号索引、括号索引-不变化版(定位错误行) let Num = 1; //每个峰从1开始 let max = 0; //每次的峰值 let maxs = []; //多个峰值 //构造二位数组和max for (let i = 0; i < tokens.length; i++) { index[0][i] = tokens[i]; index[1][i] = 0; index[2][i] = 0; if (tokens[i] == "(") { index[1][i] = Num; index[2][i] = Num; //第三行与第二行一致 if (max < Num) max = Num; Num++; } if (tokens[i] == ")") { Num--; index[1][i] = Num; index[2][i] = Num; //第三行与第二行一致 //为0时放置最大值 if (Num == 1) { maxs.push(max); max = 0; } } } //遍历所有峰值 maxs.forEach(max => { //遍历每个峰 for (let j = max; j >= 1; j--) { //每次indexs存储Num相同 let start = -1; //此处-1可随意设置,但不要在0~max范围内 let end = -1; for (let i = 0; i < index[0].length; i++) { if (index[1][i] == j) { if (start == -1) { start = i; } else { end = i; break; } } } //索引缩减替换为 0(简单识别) index[1].splice(start, end - start + 1); let funcTitle = index[0][start - 1]; //当前函数 //找到对应函数 let func = funcs.find(func => { return func.name == funcTitle }); //出错位置定位 let errStart = 0; for (let i = 0; i < index[2].length; i++) { if (index[2][i] == j) { errStart = i; break; } } let line = findErrorLine(tokens, errStart); //当前函数所在行数 if (index[0][end - 1] == ",") { //参数倒数第二位为",",参数不合法 errors.push("在第" + line + "行,函数【" + funcTitle + "】的参数不合法,请检查!"); } else if (func != null) { //表达式内置函数 let params = []; //参数列表 let paramLength = 0; //参数长度 for (let i = start; i <= end; i++) { params.push(index[0][i]); } //参数列表中不含有"[","]" if (params.indexOf('[') == -1 && params.indexOf("]") == -1) { //参数数量:params至少包含'('、')' if (params.length == 2 || (params.length == 3 && params[1].trim() == "")) { // 长度为2||长度为3且中间为空串 paramLength = 0; } else { params.forEach(param => { if (param == ",") paramLength++; }) paramLength++; //最终加一(两个","代表三个参数) } if (paramLength != func.params.length && func.key != "Invoke") { //参数个数不匹配 errors.push("在第" + line + "行,函数【" + funcTitle + "】参数数量不匹配,需要" + func.params.length + "个,实际" + paramLength + "个,请检查!"); } } else if (params.indexOf('[') != -1 && params.indexOf("]") != -1 && params.indexOf('[') < params.indexOf("]")) { let left = params.indexOf('['); let right = params.indexOf(']'); //数组内容替换为占位符"$" params.splice(left, right - left + 1, "$"); //参数数量:params至少包含'('、')' if (params.length == 2 || (params.length == 3 && params[1].trim() == "")) { // 长度为2||长度为3且中间为空串 paramLength = 0; } else { params.forEach(param => { if (param == ",") paramLength++; }) paramLength++; //最终加一(两个","代表三个参数) } if (paramLength != func.params.length && func.key != "Invoke") { //参数个数不匹配 errors.push("在第" + line + "行,函数【" + funcTitle + "】的参数数量不匹配,需要" + func.params.length + "个,实际" + paramLength + "个,请检查!"); } } else { //数组类型参数不合法 errors.push("在第" + line + "行,函数【" + funcTitle + "】的数组类型参数不合法,请检查!"); } } //参数个数正确,将此函数缩减替换为 0(简单识别) index[0].splice(start, end - start + 1); //将不变索引置为0(方便定位错误位置) index[2][start] = 0; index[2][end] = 0; } }) } /** * 检查数据模型 * @param tokens * @param checkContext * @param tables */ export function checkEntityStruct(tokens: any[], checkContext: CheckContext, tables: Table[]) { let mainTable = tables[tables.length - 1]; //主表名 if (mainTable != null) { //有数据模型 let mainTableIndexs = []; //tokens中出现的主表名 tokens.forEach((token, index) => { if (token == mainTable.tableName) mainTableIndexs.push(index); }); mainTableIndexs.forEach(tableIndex => { check(tokens, checkContext, mainTable, tables, tableIndex, false); }) } } function check(tokens: any[], checkContext: CheckContext, table: Table, tables: Table[], index: number, isSet: boolean) { let line = findErrorLine(tokens, index); //当前变量所在行 if (isSet) { if (tokens[index + 1] == "[") { //子表一般情况:People.friends[i].name for (let i = index + 1; i < tokens.length; i++) { if (tokens[i] == "]") { index = i; break; } } if (tokens[index + 1] == ".") { let proList = table.properties.filter(property => { return property.code == tokens[index + 2] }); if (proList.length == 1) { //index+2在表中 let pro = proList[0]; if (pro.type == "Set") { let setTable = tables.filter(t => { return t.tableName == pro.code })[0]; check(tokens, checkContext, setTable, tables, index + 2, true); } else if (pro.type == "Type") { let setTable = tables.filter(t => { return t.tableName == pro.code })[0]; check(tokens, checkContext, setTable, tables, index + 2, false); } else { //普通属性 if (tokens[index + 3] == ".") { checkContext.errors.push("在第" + line + "行,普通属性【" + pro.code + "】没有子属性"); } } } else { //index+2不在表中 checkContext.errors.push("在第" + line + "行,子表【" + table.tableName + "】没有属性【" + tokens[index + 2] + "】"); } } } else if (tokens[index + 1] == "." && tokens[index + 2] != "length") { //子表属性特殊情况:People.friends.length checkContext.errors.push("在第" + line + "行,子表【" + table.tableName + "】没有属性【" + tokens[index + 2] + "】"); } } else { //非子表 if (tokens[index + 1] == ".") { let proList = table.properties.filter(property => { return property.code == tokens[index + 2] }); if (proList.length == 1) { //index+2在表中 let pro = proList[0]; if (pro.type == "Set") { let setTable = tables.filter(t => { return t.tableName == pro.code })[0]; check(tokens, checkContext, setTable, tables, index + 2, true); } else if (pro.type == "Type") { let setTable = tables.filter(t => { return t.tableName == pro.code })[0]; check(tokens, checkContext, setTable, tables, index + 2, false); } else { //普通属性 if (tokens[index + 3] == ".") { checkContext.errors.push("在第" + line + "行,普通属性【" + pro.code + "】没有子属性"); } } } else { //index+2不在表中 checkContext.errors.push("在第" + line + "行,字段【" + table.tableName + "】没有属性【" + tokens[index + 2] + "】"); } } } } /** * 检查变量 * @param tokens tokens * @param errors 校验异常信息 * @param functions 函数 * @param names 变量名称list */ export function checkVariable(tokens: any[], errors: any[], functions: any[], names: any[]) { //查找对应两个函数 let funs = []; for (let i = 0; i < functions.length; i++) { if (functions.length != 2) { //找到两个,结束 if (functions[i].key == "GetContextParameter") { funs[0] = functions[i].name; } if (functions[i].key == "GetSessionValue") { funs[1] = functions[i].name; } } else { break; } } //遍历tokens,寻找funs for (let i = 0; i < tokens.length; i++) { //校验是否填写 if (funs.includes(tokens[i]) && tokens[i + 1] == "(" && tokens[i + 3] == ")" && ((tokens[i + 2] as string).startsWith("\"") || (tokens[i + 2] as string).startsWith("'"))) { let tempValue = tokens[i + 2]; if (tempValue != null) { //取真实值 tempValue = tempValue.slice(1, tempValue.length - 1); //不存在则提示 if (!names.includes(tempValue)) { if (tempValue.length == 0) { if (funs.indexOf(tokens[i]) == 0) errors.push("在第" + findErrorLine(tokens, i) + "行,上下文变量 --> 未设置"); else if (funs.indexOf(tokens[i]) == 1) errors.push("在第" + findErrorLine(tokens, i) + "行,会话变量 --> 未设置"); } else { if (funs.indexOf(tokens[i]) == 0) errors.push("在第" + findErrorLine(tokens, i) + "行,上下文变量【" + tempValue + "】 --> 不存在"); else if (funs.indexOf(tokens[i]) == 1) errors.push("在第" + findErrorLine(tokens, i) + "行,会话变量【" + tempValue + "】 --> 不存在"); } } } } } } /** * 找出出错所在行 * @param tokens * @param end * @returns */ function findErrorLine(tokens: any[], end: number): number { //找出\r\n let line = 1; for (let i = 0; i < end; i++) { if (tokens[i] == "\r" && tokens[i + 1] == "\n") line++; } return line; }