具有compose / deep和append函数的rebol解析规则

时间:2009-09-22 04:44:47

标签: rebol

这很有效(感谢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

2 个答案:

答案 0 :(得分:1)

这里的问题是撰写正在吃掉括号中的所有表达式。你很高兴它吃(点亮字“/”),但你真的不希望它吃(追加输出类)因为那是用于解析方言。

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

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]]

答案 1 :(得分:0)

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

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