如何解析函数参数块?

时间:2013-07-17 18:07:48

标签: parsing rebol

我想在给定的Rebol函数中重新组织arguments块,以便更容易理解函数所需的参数。 Rebol函数中的参数块是Rebol中可塑性数据结构的一个很好的例子:

adjoin: func [
    "Adjoins"
    series [series!] "Series to adjoin"
    joinee
    /local other
][...]

但我需要更具可预测性的东西来理解这些元数据。如何才能使其符合更加合规的格式?一个例子:

[
    ; should include about value whether the about string is there or not
    about none none "Adjoins"
    argument series [series!] "Series to Adjoin"
    argument joinee none none
    option local none none
    argument other none none
]

对转换方法的任何想法或表示参数内容的最佳方式都是最有帮助的。

1 个答案:

答案 0 :(得分:2)

这是一个完整的rebol脚本,可以帮助你: - )

请注意,变量不必以点开头,解析规则也不需要包含在=符号中。它是一种快速分离规则中每个事物的任务的方法。通过这种方式,可以更容易地识别哪个单词做什么,这在您开始构建更大规则时尤为重要。

rebol [
    title: "func spec extractor"
]



code: {adjoin: func [
    "Adjoins"
    series [series!] "Series to adjoin"
    joinee
    /local other
][...]

append: func [
    {Appends a value to the tail of a series and returns the series head.}
    series [series! port!]
    value
    /only "Appends a block value as a block"
][ ... ]
}



code: load code

;----
; setting to a temp variable, prevents .param-str from being erased 
; if the rule doesn't match at the point the rule is used (it may be optional)
;----
=param-str=: [set .tmp string! (.param-str: .tmp)] 
=param-types=: [set .tmp into [some [word!]] (.param-types: .tmp)] 

=param=: [
    (.param-types: .tmp: .param-str: none )
    set .param-name word!
    opt =param-str= 
    opt =param-types= 
    opt =param-str=
    ( 
        append/only .param-blk .tmp: reduce [ .param-name .param-str .param-types ]
    )
]

=refinements=: [
    (.ref-str: none)
    set .refinement refinement! 
    opt [ set .ref-str string! ]
    (
        append .param-blk .refinement
        append .param-blk .ref-str
    )    
    any =param=
]

=func-rule=: [
    ; set/reset variables
    (
        func-def: context [name: none doc-str: none args: [] refinements: [] code: none]
        .tmp: .func-name: .doc-str: .param-str: none
    )

    set .func-name set-word!  
    'func into [
        opt [ set .doc-str string! ]
        ( func-def/args:  .param-blk: copy [] )
        any =param=
        ( func-def/refinements:  .param-blk: copy [] )
        any =refinements=
        here:        
    ]
    set .func-body block!
    (
        func-def/name:    .func-name
        func-def/doc-str: .doc-str
        func-def/code:    .func-body
    )
]      

funcs: []
parse code [ some [ =func-rule=  ( append funcs func-def) | skip ]]

probe funcs

这是打印出来的内容:

[make object! [
        name: adjoin:
        doc-str: "Adjoins"
        args: [[
                series "Series to adjoin" [series!]
            ] [
                joinee none none
            ]]
        refinements: [
            /local none [other none none]
        ]
        code: [...]
    ] make object! [
        name: append:
        doc-str: {Appends a value to the tail of a series and returns the series head.}
        args: [[
                series none [series! port!]
            ] [
                value none none
            ]]
        refinements: [
            /only "Appends a block value as a block"
        ]
        code: [...]
    ]]