红色解析并用<>代替double%不起作用 [英] red parsing and replacing double % with <> doesn't work

查看:75
本文介绍了红色解析并用<>代替double%不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转型

 test: "bla bla %bla bla% bla bla bla bla %bla% bla"

进入

 test: "bla bla <bla bla> bla bla bla bla <bla> bla"

使用此代码,它不能以红色显示:

with this code, it doesn't work in red:

 rules: [
    (toggle: -1)
    to "%" mark: (
        (toggle: 0 - toggle)
        either (toggle = 1) [change mark {<}][change mark {>}]
    )
    thru "%"
    |
    to end
]

parse test rules

输出为

>> test
== "bla bla <bla bla% bla bla bla bla %bla% bla"

推荐答案

您的规则需要多加注意,尤其是在使用关键字'to'thru时.编写规则的方式将执行一次替换,然后to end会将您推进到输入的末尾-而不是您想要的.

Your rules need a little more care, especially when using the keywords 'to and 'thru. The way your rule is written the replacement will occur once and then to end will advance you to the end of the input -- not what you want.

我会慢慢解决的.

第一个规则to "%",这会将输入推进到第一个%"字符.这是一条成功的规则,因此您的parens表达式将执行并修改输入,即将%"更改为<".一切顺利:第一个替换已完成,输入中的下一个字符现在为<". 您的下一条规则是thru "%".这会使输入 past 输入前进到输入中的下一个%"字符-您要更改为>"的字符.

The first rule, the to "%", this advances the input to the first "%" character. It's a successful rule so your parens expression executes and modifies the input, i.e., changes the "%" to a "<". All good there: the first replacement is complete and the very next char in the input is now "<". BUT your next rule is thru "%". This advances the input past the next "%" character in the input-- a char you want changed to ">".

这里的另一个关键之处(如HostileFork所指出的)是您的替换规则没有重复.您的解析规则运行一次,然后结束.然后,您的替代规则to end将接管并将输入一直跳转到末尾.为了使规则可以重复扫描整个输入,您需要使用someanywhileskip设置规则.

The other key piece here (as HostileFork points out) is that your replacement rule is not repeating. Your parse rule runs once and then it's over. Your alternative rule to end then takes over and jumps the input all way to the end. In order to get a rule to scan repeatedly through the entire input you need to set up your rule with a some, any, while or skip.

这是对规则的快速修改,将大部分代码保留在原处,但达到了我认为想要的结果.

Here's a quick rework of your rule leaving most of your code in place but achieving the result I think you want.

test: "bla bla %bla bla% bla bla bla bla %bla% bla"
toggle: -1
rules: [any [
    to "%" mark: (
        toggle: negate toggle 
        either toggle = 1 [change mark "<"][change mark ">"]
    ) 
    ]
]
parse test rules
test
 == "bla bla <bla bla> bla bla bla bla <bla> bla"

我可能会更改代码中的其他内容,例如切换,您可以看到我摆脱了不必要的括号,但是现在我仍在预测parse的工作方式.

There are other things in the code that I would probably change, such as the toggle, and you can see I got rid of unecessary parentheses, but for now I'm forcusing on the way parse works.

这篇关于红色解析并用&lt;&gt;代替double%不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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