2013-07-17 43 views
2

我想在給定的Rebol函數中重新組織參數塊,以提供對函數所需參數的更全面理解。這些參數在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 
] 

對transform方法或表示參數內容的最佳方式的任何想法都是最有幫助的。

回答

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