如何在 Coco/R 中标记缩进(Python、Boo 等缩进)
有一种众所周知的方法,如何在 Coco/R 中实现像 Python/Boo 中那样的标记缩进?
Coco/R 忽略空格,但我需要以某种方式根据下一行缩进生成 beginBlock/endBlock 标记。
现在,我使用预处理器,它插入“{”、“}”和“;”在输入流中,通过比较行之间的缩进。在 Coco/R 语法中,我使用弯括号作为 beginBlock/endBlock 标记。如果输入流没有注释(也可以嵌套),它会很好地工作。一旦出现无序评论,意图比较逻辑就会失败。
对我来说,实现一个跟踪评论的预处理器看起来像是过度设计。
那么问题是,通常可以用 Coco/R 解析基于缩进的语法吗? 或者我应该尝试别的东西?
It there a well known way, how to implement in Coco/R tokenizing indents like in Python/Boo?
Coco/R ignores whitespaces, but I need somehow to generate beginBlock/endBlock tokens, based on next line indent.
Right now, I use preprocessor, which inserts '{', '}', and ';' in input stream, by comparing indents between lines. In Coco/R grammar I use curved braces as beginBlock/endBlock tokens. It works well if input stream has no commens (which could be also nested). As soon as unordered comments coming, intentation comparison logic fails.
Implementing a preprocessor, which traces a comments looks like overenginering to me.
So the question is, is it generally possible to parse indent based grammar with Coco/R?
Or should I try something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了一个理想的方法来做到这一点。
使用以下方法包装 GetNextToken
比较下一个的流位置
令牌与最后一个。
如果position.Y改变了,但是
position.X 增加N个tab,注入N
虚拟 INDENT 标记。
如果position.Y改变了,但是
位置.X减少N个选项卡,注入N个
虚拟 DENDENT 代币。
如果position.Y改变了,但是
position.X不是,注入virtual
SEPARATOR 令牌。
如果position.Y没有改变,则返回
原始下一个标记。
如果之前的标记是软中断(在 python \ 中),
忽略上面的逻辑。
Found a ideal way to do this.
wrap GetNextToken with method that
compares stream positions of the next
token with the last one.
if position.Y is changed, but
position.X increased N tabs, inject N
virtual INDENT tokens.
if position.Y is changed, but
position.X decreased N tabs, inject N
virtual DENDENT tokens.
if position.Y is changed, but
position.X is not, inject virtual
SEPARATOR token.
if position.Y is not changed, return
original next token.
if previous token was a soft break (in python \),
ignore logic above.
首先,Coco/R 默认情况下只忽略空格(空格)。选项卡不会被忽略:
我还没有对此进行测试,但我的猜测是您应该覆盖扫描仪的默认行为:
最简单的方法是首先修改生成的代码,直到它正常工作,然后在框架文件(
Scanner.frame
和Parser.frame
)中进行相同的更改,这样在重新生成后就不会丢失更改代码。First of all, Coco/R only ignores blanks (spaces) by default. Tabs are not ignored:
I haven't tested this, but my guess is that you should overwrite the default behavior of the Scanner:
The easiest way to do this is by first modifying the generated code until it works OK and then making the same changes in the frame files (
Scanner.frame
andParser.frame
) so you won't loose the changes after you regenerate the code.