【问题标题】:rebol parse rule with compose/deep and append function具有 compose/deep 和 append 功能的 rebol 解析规则
【发布时间】:2009-09-22 04:44:46
【问题描述】:

这很好用(感谢 Sunanda 的建议 How can I parse [a / b] ? syntax error in Rebol?):

attribute: copy []
class: copy []
definition-rule: compose/deep [some [set class word! 'is 'defined 'by 
[some [copy attribute to (to-lit-word "/") thru (to-lit-word "/") ]]
copy attribute to end]]
parse [Customer is defined by First Name / Last Name / Email] definition-rule

但我现在需要添加一些附加指令(附加输出类),它不再起作用了:

attribute: copy []
class: copy []
definition-rule: compose/deep [some [set class word! (append output class) 'is 'defined 'by 
[some [copy attribute to (to-lit-word "/") thru (to-lit-word "/") ]]
copy attribute to end]]
parse [Customer is defined by First Name / Last Name / Email] definition-rule

【问题讨论】:

  • 好吧,让我们看看苏南达是否再次回答。我不太明白为什么教程会问问题,但我准备好出错了。
  • 我很高兴在任何提出 REBOL 问题的论坛上回答有关 REBOL 的问题(如果我知道答案)。到目前为止,REBOL 社区对 Stackoverflow 的使用很少——主要是 RebolTutorial 提出问题,并且有几个人(到目前为止主要是我)做出了回应。如果这给任何人带来了问题,那么 Reboltutorial 可以切换到 REBOL 特定论坛之一以获得他/她的问答支持。

标签: rebol


【解决方案1】:

这里的问题是 compose 正在吃掉括号中的所有表达式。你很高兴它吃掉 (to-lit-word "/") 但你真的不希望它吃掉 (append output class) 因为那是用于parse方言。

可能有一个更聪明的方法,但这应该可行:通过在解析规则之外执行 lit-word 工作来删除 compose...

attribute: copy []
class: copy []
output: copy ""
fs: to-lit-word "/"   ;; define a forward slash lit-word

definition-rule:  [
    some [set class word! (append output class) 'is 'defined 'by [
        some [copy attribute to fs thru fs]
    ]
    copy attribute to end]
    ]

parse [Customer is defined by First Name / Last Name / Email] definition-rule
== true

我不完全确定你想用这段代码做什么,但你想在最后提取一组属性,然后考虑这个改变:

attribute: copy []
attributes: copy []
class: copy []
output: copy ""
fs: to-lit-word "/"   ;; define a forward slash lit-word

definition-rule:  [
    some [set class word! (append output class) 'is 'defined 'by [
        some [copy attribute to fs thru fs (append/only attributes attribute)]
    ]
    copy attribute to end (append/only attributes attribute)]
    ]

parse [Customer is defined by First Name / Last Name / Email] definition-rule

print ["==" class mold attributes]

== Customer [[First Name] [Last Name] [Email]]

【讨论】:

  • 非常感谢,我需要第二个示例来执行此操作: attribute: copy [] class: copy [] fs: to-lit-word "/" output: copy "" definition-rule: [一些[设置类词! (append output join class "|") 'is 'defined 'by [ some [copy attribute to fs thru fs (append output join attribute ";")] ] copy attribute to end (append output attribute)] ] parse [Customer is由名字/姓氏/电子邮件定义]定义规则探测输出
【解决方案2】:

我在评论中重新发布代码,因为它不可读,我想要做的是:

attribute: copy []
class: copy []
fs: to-lit-word "/" 
output: copy ""


definition-rule:  [
    some [set class word! (append output join class "|") 'is 'defined 'by [
        some [copy attribute to fs thru fs (append output join attribute ";")]
    ]
    copy attribute to end (append output attribute)]
]

parse [Customer is defined by First Name / Last Name / Email] definition-rule
probe output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多