правило разбора rebol с функцией compose / deep и append - PullRequest
0 голосов
/ 22 сентября 2009

Это прекрасно работает (благодаря предложению Sunanda Как мне разобрать [a / b]? Синтаксическая ошибка в 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 ]

1 голос
/ 22 сентября 2009

Проблема в том, что compose съедает все выражения в скобках. Вы счастливы, что он съел (на слово "/") , но вы серьезно не хотите, чтобы он съел (добавить выходной класс) , потому что он предназначен для разбирать диалект.

Вероятно, есть более умный подход, но он должен работать: удалите compose , выполнив 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]]
0 голосов
/ 23 сентября 2009

Я отправляю код в комментарии, так как он не читается, я хотел сделать следующее:

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