rebol parse rule with compose/deep and append func

2019-06-11 11:22发布

问题:

This works great (thanks to Sunanda's suggestion 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

but I now need to add some append instructions (append output class) and it doesn't work any more:

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

回答1:

The problem here is that the compose is eating all the expressions in parentheses. You are happy for it to eat the (to-lit-word "/") but you seriously do not want it to eat the (append output class) because that is intended for the parse dialect.

There is probably a cleverer approach, but this should work: remove the compose by doing the lit-word work outside of the parse rule...

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

I am not entirely sure what you are trying to do with this code, but it you want to extract the set of attributes at the end, then consider this change:

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


回答2:

I repost code in comment as it isn't readable, what I wanted to do exactly was this:

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


标签: rebol