QLEXpress语法分析和计算的入口类详细分析--Expressrunner(三)

本文详细剖析了QLEXpress的入口类Expressrunner,包括执行和解析文本、生成指令集、缓存管理、变量依赖分析以及语法检查等功能。通过缓存优化提升执行效率,并提供了多层次的语法验证来确保表达式的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QLEXpress语法分析和计算的入口类详细分析–Expressrunner(三)

1.执行一段文本(一)

2.执行一段文本(二)

3.执行一段文本(三)

public Object execute(String expressString, IExpressContext<String, Object> context, List<String> errorList,
        boolean isCache, boolean isTrace, Log log) throws Exception {
        InstructionSet parseResult;
        if (isCache) {
            parseResult = expressInstructionSetCache.get(expressString);
            if (parseResult == null) {
                synchronized (expressInstructionSetCache) {
                    parseResult = expressInstructionSetCache.get(expressString);
                    if (parseResult == null) {
                        parseResult = this.parseInstructionSet(expressString);
                        expressInstructionSetCache.put(expressString, parseResult);
                    }
                }
            }
        } else {
            parseResult = this.parseInstructionSet(expressString);
        }
        return executeReentrant(parseResult, context, errorList, isTrace, log);
    }

    private Object executeReentrant(InstructionSet sets, IExpressContext<String, Object> iExpressContext,
        List<String> errorList, boolean isTrace, Log log) throws Exception {
        try {
            int reentrantCount = threadReentrantCount.get() + 1;
            threadReentrantCount.set(reentrantCount);

            return reentrantCount > 1 ?
                // 线程重入
                InstructionSetRunner.execute(this, sets, this.loader, iExpressContext, errorList, isTrace, false, true,
                    log, false) :
                InstructionSetRunner.executeOuter(this, sets, this.loader, iExpressContext, errorList, isTrace, false,
                    log, false);
        } finally {
            threadReentrantCount.set(threadReentrantCount.get() - 1);
        }
    }

4.解析一段文本,生成指令集合

5.输出全局定义信息

6.优先从本地指令集缓存获取指令集,没有的话生成并且缓存在本地

 public InstructionSet getInstructionSetFromLocalCache(String expressString) throws Exception {
        InstructionSet parseResult = expressInstructionSetCache.get(expressString);
        if (parseResult == null) {
            synchronized (expressInstructionSetCache) {
                parseResult = expressInstructionSetCache.get(expressString);
                if (parseResult == null) {
                    parseResult = this.parseInstructionSet(expressString);
                    expressInstructionSetCache.put(expressString, parseResult);
                }
            }
        }
        return parseResult;
    }

    public InstructionSet createInstructionSet(ExpressNode root, String type) throws Exception {
        InstructionSet result = new InstructionSet(type);
        createInstructionSet(root, result);
        return result;
    }

    public void createInstructionSet(ExpressNode root, InstructionSet result) throws Exception {
        Stack<ForRelBreakContinue> forStack = new Stack<>();
        createInstructionSetPrivate(result, forStack, root, true);
        if (!forStack.isEmpty()) {
            throw new QLCompileException("For处理错误");
        }
    }

    public boolean createInstructionSetPrivate(InstructionSet result, Stack<ForRelBreakContinue> forStack,
        ExpressNode node, boolean isRoot) throws Exception {
        InstructionFactory factory = InstructionFactory.getInstructionFactory(node.getInstructionFactory());
        return factory.createInstruction(this, result, forStack, node, isRoot);
    }

7.获取一个表达式需要的外部变量名称列表

8.是否忽略charset类型的数据,而识别为string

9.提供简答的语法检查,保证可以在运行期本地环境编译成指令

10.提供复杂的语法检查,不保证可以在运行期本地环境编译成指令

public boolean checkSyntax(String text, boolean mockRemoteJavaClass, List<String> remoteJavaClassNames) {
        try {
            Map<String, String> selfDefineClass = new HashMap<>();
            for (ExportItem item : this.loader.getExportInfo()) {
                if (item.getType().equals(InstructionSet.TYPE_CLASS)) {
                    selfDefineClass.put(item.getName(), item.getName());
                }
            }
            Word[] words = this.parse.splitWords(text, isTrace, selfDefineClass);
            ExpressNode root = this.parse.parse(this.rootExpressPackage, words, text, isTrace, selfDefineClass,
                mockRemoteJavaClass);
            InstructionSet result = createInstructionSet(root, "main");
            if (this.isTrace && log.isDebugEnabled()) {
                log.debug(result);
            }
            if (mockRemoteJavaClass && remoteJavaClassNames != null) {
                remoteJavaClassNames.addAll(Arrays.asList(result.getVirClasses()));
            }
            return true;
        } catch (Exception e) {
            log.error("checkSyntax has Exception", e);
            return false;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsgodlike

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值