parsing - How to parse a functions argument block? -
i'd reorganise arguments block in given rebol function provide more comprehensible understanding of arguments required function. arguments block in rebol function great example of malleable data structures in rebol:
adjoin: func [ "adjoins" series [series!] "series adjoin" joinee /local other ][...]
but need more predictable make sense of metadata. how more compliant format? example:
[ ; should include value whether string there or not none none "adjoins" argument series [series!] "series adjoin" argument joinee none none option local none none argument other none none ]
any thoughts transform method or best way represent arguments content helpful.
here complete rebol script should out :-)
note variables not have start dot, nor parse rules require surrounded in = signs. quick way separate task of each thing within rules. way easier identify word what, important when start build larger rules.
rebol [ title: "func spec extractor" ] code: {adjoin: func [ "adjoins" series [series!] "series adjoin" joinee /local other ][...] append: func [ {appends value tail of series , returns series head.} series [series! port!] value /only "appends block value block" ][ ... ] } code: load code ;---- ; setting temp variable, prevents .param-str being erased ; if rule doesn't match @ point rule used (it may optional) ;---- =param-str=: [set .tmp string! (.param-str: .tmp)] =param-types=: [set .tmp [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 ) =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 [ opt [ set .doc-str string! ] ( func-def/args: .param-blk: copy [] ) =param= ( func-def/refinements: .param-blk: copy [] ) =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 [ [ =func-rule= ( append funcs func-def) | skip ]] probe funcs
here prints out:
[make object! [ name: adjoin: doc-str: "adjoins" args: [[ series "series adjoin" [series!] ] [ joinee none none ]] refinements: [ /local none [other none none] ] code: [...] ] make object! [ name: append: doc-str: {appends value tail of series , returns series head.} args: [[ series none [series! port!] ] [ value none none ]] refinements: [ /only "appends block value block" ] code: [...] ]]
Comments
Post a Comment