Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Embedded macros ..

 [1/17] from: reffy::ulrich::net at: 8-Sep-2002 17:58


Could someone give me an idea on how to parse something like embedded macros in Rebol? Assume there are at least two types of macros one might find in a string: $(RebolVariableorExpression) %(EnvironmentVariable) Examples: The answer is: $(ans) The homedir is: %(homedir) Embedded examples: The answer is: $($(a)$(n)$(s)) The homedir is: %($(home)$(dir)) where the variables: a: a" n:"n" s:"s" ans:"Answer" home:"home" dir:"dir" Must be the age ?? Dick

 [2/17] from: al:bri:xtra at: 9-Sep-2002 19:30


Try something like: [ Rebol [] Embed: function [String [string!]] [Rule Start Word End] [ parse String: copy String Rule: [ any [ [ Start: [#"$" | #"%"] #"(" [copy Word [Alpha some Alphadigit] | Rule] #")" End: ( End: change/part Start get to-word Word End ) :End ] | skip ] end ] String ] a: "A" n: "N" s: "S" ans: "Answer" home: "HOME" dir: "DIR" homedir: "HOMEDIR" foreach String [ "The answer is: $(ans)" "The homedir is: %(homedir)" "The answer is: $($(a)$(n)$(s))" "The homedir is: %($(home)$(dir))" ] [ print Embed String ] halt ] Which gives results like: The answer is: Answer The homedir is: HOMEDIR The answer is: $($(a)$(n)$(s)) The homedir is: %(HOMEDIR) I haven't got the recursion part right. Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [3/17] from: reffy:ulrich at: 9-Sep-2002 7:19


Hehehe, no wonder I couldn't figure that out !! I will try to understand what this is doing. I remember doing it in C and it was straight forward (haha in a facetious way) using strchr and strstr functions. I remember being in a while loop looking for $( or %( and recursing to join each subresult into a possibly growing string containing the resolved content. Dick

 [4/17] from: reffy:ulrich at: 9-Sep-2002 7:40


my Rebol version is: 1.2.1.3.1 is that suitable such that what you provided should function? Dick

 [5/17] from: greggirwin:mindspring at: 9-Sep-2002 10:31


Hi Dick, <<
>> print Embed "Hello$(a)"
** Script Error: Alpha has no value ** Where: Embed ** Near: parse String: copy String Rule:
>> print Embed "Hello $(a) "
Wish I knew how to debug this to see what is happening.
>>
I think Andrwe must have Alpha and Alphadigit defined as charset values in his setup. I'm sure he'll jump in to clear it up. --Gregg

 [6/17] from: gscottjones:mchsi at: 9-Sep-2002 10:43


From: Dick Ulrich Regarding message: http://www.escribe.com/internet/rebol/m25365.html
> >> print Embed "Hello$(a)" > ** Script Error: Alpha has no value > ** Where: Embed > ** Near: parse String: copy String Rule: > >> print Embed "Hello $(a) " > > Wish I knew how to debug this to see what is happening.
Hi, Dick, I must say that Andrew's example is fairly advanced, and he warns that the recursive feature is not quite right yet. But the problem that you have run into is that he apparently forgot to supply you the some rule definitions. These may work: digit: charset [#"0" - #"9"] alpha: charset [#"A" - #"Z" #"a" - #"z"] alphadigit: union alpha digit I was going to under take to explain his example more fully, and then I realized that I wasn;t quite sure what he was intending in a few spots. Depending on your ultimate needs, there may be an easier way to accomplish the task. (We all suffer the paradigm shift problem of "I know how to do blah, blah in this or that language, but how do I do it in REBOL?") Andrew is a New Zealander, so he should be awake later in our day (central time zone) and will likely help. --Scott Jones

 [7/17] from: reffy:ulrich at: 9-Sep-2002 10:02


>> print Embed "Hello$(a)"
** Script Error: Alpha has no value ** Where: Embed ** Near: parse String: copy String Rule:
>> print Embed "Hello $(a) "
Wish I knew how to debug this to see what is happening. Dick

 [8/17] from: g:santilli:tiscalinet:it at: 9-Sep-2002 21:10


Hi Dick, On Monday, September 9, 2002, 3:58:48 AM, you wrote: run> Could someone give me an idea on how to parse something like embedded macros in Rebol? I have a couple questions first: run> $(RebolVariableorExpression) What do you exactly mean by "RebolVariableorExpression"? In your example below: run> "The answer is: $($(a)$(n)$(s))" I wouldn't call "$(a)$(n)$(s)" a REBOL expression. run> %(EnvironmentVariable) There's a little problem: how to read the value of an environment variable in REBOL? (That is, in a multiplatform way; if you're just interested in one platform and have the /Pro version there's no problem here.) Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [9/17] from: reffy:ulrich at: 9-Sep-2002 14:00


Thanks Scott, I feel like I am in another whirlwind!! Yet another language learning experience, sigh. But this one seems to have good potential, so I don't want to give up unnecessarily. There are two capabilities I have coded in the past that I would like to code in Rebol: 1. I have a language generator which reads a high-level driver file containing named sets of attributes. The name is a dot name (parent/child) followed by attribute name/value pairs. I read this driver specification and depending on the language (directory of templates), I then apply template files (which contain these macros) based on the "type" attribute of the object in the high level specification. EXAMPLE: CONTROL:language=Java1.3:.....etc. MEI:title=Manufacturing & Engineering Interface:type=JApplication:.....etc. MEI.Splash:type=JPanel:bgc=red:....etc. The types "JApplication" and "JPanel" (etc) would connect up to a corresponding template file which is read and text is emitted to the module file, with macros controlling the action. The JPanel template file might have this line in it: if defined(bgc) setBackground(Color.$(bgc)); .endif 2. I have a configuration library which uses RDBMS tables to hold the configuration information in LN,VN,VV (3 columns - LabelName, VariableName, VariableValue). The VV may contain such embedded macros which may resolve to program variables, environment variables, scoped dataset (VN) variables, or distant references to other parts of the RDBMS configuration table set. EXAMPLE: LN VN VV Task1 homedir $(bindir)/$(apname) I would have no problem changing my current representation to a Rebol one if I knew what it should be. At any rate, I need to be able to resolve/find these macros, so that is the reason for the original question. Dick

 [10/17] from: reffy:ulrich at: 9-Sep-2002 15:35


Hi Gabriele, $(expression) where expression which yields a result (string) to replace the $(expression). result could be null or empty string "" which would elide the macro totally. $(1+1) v: "hello" $(v) The date is: $(now) YIELDS: The date is: 9-Sep-2002/15:33:59-7:00 The date tommorrow is: $(now+1) The date tommorrow is: 10-Sep-2002/15:34:43-7:00 Dick

 [11/17] from: al:bri:xtra at: 10-Sep-2002 9:05


> Wish I knew how to debug this to see what is happening.
Unfortunately, I forgot to say that I normally have this file loaded into Rebol: Rebol [ Name: 'Common-Parse-Values Title: "Common Parse Values" File: %"Common Parse Values.r" Author: "Andrew Martin" eMail: [Al--Bri--xtra--co--nz] Web: http://valley.150m.com Date: 11/July/2002/14:33 Version: 1.1.1 Purpose: {Common Parse Values} Category: [util 1] ] Octet: charset [#"^(00)" - #"^(FF)"] Char: charset [#"^(00)" - #"^(7F)"] Digit: charset "0123456789" Digits: [some Digit] Upper: charset [#"A" - #"Z"] Lower: charset [#"a" - #"z"] Alpha: union Upper Lower AlphaDigit: union Alpha Digit AlphaDigits: [some AlphaDigit] Control: charset [#"^(00)" - #"^(1F)" #"^(7F)"] Hex: union Digit charset [#"A" - #"F" #"a" - #"f"] HT: #"^-" SP: #" " LF: #"^(0A)" LWS: charset reduce [SP HT] LWS*: [some LWS] LWS?: [any LWS] WS: charset reduce [SP HT newline CR LF] WS*: [some WS] WS?: [any WS] Graphic: charset [#"^(21)" - #"^(7E)"] Printable: union Graphic charset " " Andrew Martin Awake, fellow Rebols!
> From: "Gregg Irwin" <[greggirwin--mindspring--com]> > Date: 2002/09/10 Tue AM 04:31:28 GMT+12:00
<<quoted lines omitted: 16>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
Andrew Martin

 [12/17] from: g:santilli:tiscalinet:it at: 10-Sep-2002 0:50


Hi Dick, On Tuesday, September 10, 2002, 1:35:52 AM, you wrote: run> $(1+1) What should the result be if I write: $(1+$(a)) ? Anyway, maybe you'd better go with a different approach. If I'll have some time tomorrow I'll try to give you some hints... (Not that my hints are necessarily useful... :-) Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [13/17] from: reffy:ulrich at: 9-Sep-2002 18:57


a: 10 print "Answer is $(1+$(a))" Answer is 11

 [14/17] from: al:bri:xtra at: 10-Sep-2002 16:54


Dick wrote:
> EXAMPLE: LN VN VV > Task1 homedir $(bindir)/$(apname)
Could the string in the VV column be converted? Perhaps to: bindir/:apname
>> bindir: %/C/Bin/
== %/C/Bin/
>> apname: "Test"
== "Test"
>> bindir/:apname
== %/C/Bin/Test It might be easier to use a dialect, something like: "Task1" homedir: (bindir/:apname) Where the first value is a string!, the second is a set-word! and the last is a paren! value.
> 1. I have a language generator which reads a high-level driver file
containing named sets of attributes. I think that suitable 'parse rules should be able to handle this. A special dialect could be useful as well. You might want to look at my ML, CSS and Fixed dialects to get some ideas. Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [15/17] from: reffy:ulrich at: 10-Sep-2002 10:42


Maybe in another iteration. 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. But, you are giving me food for thought.

 [16/17] 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

 [17/17] from: reffy:ulrich at: 10-Sep-2002 14:01


Wow, Romano, That gives me something to mentally cogitate on for sure. You must have been working with Rebol for a long time to code that solution so quick? I guess working with C for such a long time (not nearly as expressive of course) makes the variety of Rebol approaches hurt my brain :-) Dick

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted