正则表达式增量解析 [英] Regular expression incremental parsing

查看:31
本文介绍了正则表达式增量解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有支持逐个字符解析正则表达式的语言或工具?

Are there languages or tools that support the parsing of regexes on a character-by-character basis?

我认为这可能相当于流上的正则表达式",这似乎是即将推出的 Perl 版本 6 的功能之一.

I think this may be equivalent to "regexes on streams" which is something that seems to be one of the features of the upcoming Perl version 6.

基本上我想这样做是因为我正在构建一个通过伪终端对终端流进行翻译的工具,并且我想到应该可以实现的最终灵活性是允许规范正则表达式替换表达式.

Basically I want to do this because I'm building a tool that does translation of a terminal stream over a pseudo-terminal, and it occurred to me that the ultimate sort of flexibility that should be attainable is by allowing the specification of regex-replace expressions.

用例是我想让我的鼠标滚动事件传递给一个简单的程序,例如 less 寻呼机,这意味着我的工具(产生 less 在 PTY 上)将执行类似发出代码 \x1b[?1000h 的操作,该代码会打开鼠标报告,然后翻译此后收到的每个鼠标滚轮转义码,例如 \x1b[M!!(最后几个字符编码终端内的鼠标位置,应该被忽略但也被剥离)到 \x1b[A 向上箭头代码.

The use case is that I want to allow my mouse scroll events to be passed to a naive program such as the less pager, which means my tool (which spawns less over a PTY) will be doing something like issuing the code \x1b[?1000h which switches on mouse reporting, and then subsequently translating every mouse wheel escape code received thereafter such as \x1b[M!! (the last several chars encode the mouse position within the terminal and should be ignored but also stripped) into the \x1b[A Up-arrow code.

如您所见,能够指定一个适用于 stdin 终端读取流的正则表达式以生成要发送到从属 pty 的翻译流将是理想的.

As you can see being able to specify a regex that works on the stdin terminal-reading stream to generate the translated stream to send to the slave pty would be ideal.

我是否需要等待 Perl 6 才能实现这一点?为什么正则表达式引擎通常需要整个字符串可用,一定有特殊的原因吗?

Do I need to wait for Perl 6 to be able to achieve this? There must be particular reasons for why regex engines generally require having the whole string available?

很明显,我在这里不需要正则表达式的强大功能.例如,我可以推测支持回溯可能会使流解析正则表达式变得不可能.

It's pretty obvious I don't need the full blown power of regex here. I can speculate for instance that it might be the case that supporting backtracking makes stream-parsing regex impossible.

因此,由于我不需要回溯,因此可能存在某种提供流 API 的轻量级正则表达式引擎.似乎利用某种形式的解析系统(如果存在合适的)比构建任意的东西更聪明.

So since I don't need backtracking maybe there is some sort of light-weight regex engine out there that provides a stream API. It just seems like taking advantage of some form of parsing system (if one exists that is suitable) would be smarter than building something arbitrary.

推荐答案

看起来 s2p 是我可以使用的东西的一个例子.

Looks like s2p is an example of something that I can use.

特别是能够设置 $| 不进行行缓冲的潜力.

In particular, the potential of being able to set $| to not do line-buffering.

实际上我认为这行不通.它似乎是围绕行构建的,并使用 s 运算符来运行正则表达式.

Actually I don't think this will work. It seems to be built around lines and uses the s operator to run regex.

这篇关于正则表达式增量解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆