[REBOL] Re: Embedded macros ..
From: rotenca:telvia:it at: 10-Sep-2002 19:15
Hi,
> I have hundreds of template files and hence the motivation for the embedded
macros.
> I want to process what I have in Rebol without having to recode thousands of
lines of template files into Rebolese.
I think that that format is not the best for rebol, but here it is a recorsive
parse rule, with escape chars.
---- code ----
Rebol []
delimit: ["$(" | "%(" | "*("]
escapes: ["$$" | "%%" | "**"]
notpar: complement charset ")"
alpha: charset [#"a" - #"z" #"A" - #"Z"]
alphadigit: union alpha charset "0123456789"
Embed: function [[catch] String [string!] /trace][
macro escape start end esc comm
][
if trace [print [" +++ Embed" mold string]]
escape: [esc: escapes (esc: remove next esc ) :esc]
macro: [
some [
start:
delimit
[
any [
[
(macro: use [start][bind/copy macro 'start])
macro
]
| escape
| notpar
]
#")"
end:
]
opt (
if "" = comm: copy/part skip start 2 back end [comm: {""}]
change/part start do comm end
if trace [print [" === Eval" comm "=" mold do comm]]
)
:start
]
]
either parse/all String: copy String [
some [macro | escape | skip]
][String][throw make error! "Invalid string"]
]
;--------test
a: "A"
n: "N"
s: "S"
ans: "Answer"
home: "HOME"
dir: "$(DIE)"
die: "DIR"
homedir: "HOMEDIR"
home2: "$(kk)"
kk: "$$(dir)"
f: "%(g)"
g: "%(h)"
h: "333"
m: "23"
aa: "1"
ab: "0"
*(aa)*(ab)
b: "*(aa)*(ab)"
a1: "*(aa)*(ab)"
op: " * "
apname: "MEI"
mei100: "language"
foreach String [
"The answer is: $(ans)"
"The answer is: $$(ans)"
"The homedir is: %(homedir)"
"The answer is: $($(a)$(n)$(s))"
"The homedir is: %($(home)$(dir))"
"$$(home2) = $(home2)"
"$(h)$(h)"
"$($(f)$(g))"
"$($$$(m))"
"$($($(m)))"
"$($($(dir)))"
"$(aa)$(ab)"
"The answer is: *(*(a1)*(op)*(b)) I think"
"The application is: $(APNAME)"
"*($(APNAME): *(*(a1)*(op)*(b)))"
"*($(APNAME)*(*(a1)*(op)*(b)))"
"*(rejoin [{tot: } $(APNAME) * *(*(a1)*(op)*(b) + 345)])"
"*()*(a*()n*()s)"
"*({string})"
][
probe Embed String
]
halt
---
Ciao
Romano