[REBOL] Re: How to Parse a Nested Block
From: greggirwin:starband at: 17-Sep-2001 17:09
Hi Tim,
<< I want to parse rebol code so that I "extract" the interface block
from a function. >>
If the goal is to get the interface to a function, I think the easiest
way is via reflection:
fn-interface: func [f[word!]] [
first get f
]
; fn-interface 'if
fn-spec: func [f[word!]] [
third get f
]
; fn-spec 'if
; Works only on mezzanine functions
fn-body: func [f[word!]] [
second get f
]
; fn-body 'if
If the goal is to get parse to parse nested blocks, in general, then
Carl's parse-code example shows probably the best way with the use
of a recursive parse rule. Notice that blk-rule is used inside the
blk-rule definition when an left paren or left bracket is found.
REBOL [
Title: "Parse REBOL Source"
Author: "Carl Sassenrath"
File: %parse-code.r
Date: 30-May-2000
Purpose: {An example of how to parse REBOL source code.}
Category: [script util text 2]
]
parse-code: func [
"Parse REBOL source code."
text /local str new
][
parse text blk-rule: [
some [ ; repeat until done
str:
newline |
#";" [thru newline | to end] new: (probe copy/part str new) |
[#"[" | #"("] blk-rule |
[#"]" | #")"] |
skip (set [value new] load/next str probe :value) :new
]
]
]
;example: parse-code read %parse-code.r
Hope this helps,
--Gregg