如何解析函数参数块? [英] How to parse a functions argument block?

查看:83
本文介绍了如何解析函数参数块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重组给定Rebol函数中的arguments块,以提供对该函数所需参数的更易理解的理解. Rebol函数中的arguments块是Rebol中可延展数据结构的一个很好的例子:

I'd like to reorganise the arguments block in a given Rebol function to provide a more comprehensible understanding of the arguments required by the function. The arguments block in a Rebol function is a great example of the malleable data structures in Rebol:

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

但是我需要更可预测的东西来理解这个元数据.我如何将其转换为更合规的格式?一个例子:

But I need something more predictable to make sense of this metadata. How would I get it to a more compliant format? An example:

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

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

Any thoughts to the transform method or the best way to represent the arguments content would be most helpful.

推荐答案

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

Here is a complete rebol script which should help you out :-)

请注意,变量不必以点开头,解析规则也不必包含在=号内.这是在规则内分离每件事的任务的快速方法.这样,更容易识别哪个词在做什么,这在您开始建立更大的规则时尤其重要.

Note that the variables do not have to start with a dot, nor do the parse rules require to be surrounded in = signs. Its a quick way to separate the task of each thing within the rules. This way its much easier to identify which word does what, which is especially important when you start to build larger rules.

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: [...]
    ]]

这篇关于如何解析函数参数块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆