Page 1 of 1

extend include directive to add rule prefix/suffix

Posted: Thu Feb 03, 2011 5:35 pm
by kipling
Is it possible to extend the parsing of include files to optionally add prefixes or suffixes rule and path names - both definitions and instances?
e.g. so that this:

Code: Select all

// ---- start foo.cfdg
include bar.cfdg prefix "bar_"
rule A{bar_A{s .5}}
rule bar_A .01{}
...
// ---- end foo.cfdg
// ---- start bar.cfdg
rule A{CIRCLE{} A{s .9 x 1 r 5}}
// ---- end bar.cfdg
is functionally equivalent to this:

Code: Select all

// ---- start foo.cfdg
include bar2.cfdg 
rule A{bar_A{s .5}}
rule bar_A .01{}
...
// ---- end foo.cfdg
// ---- start bar2.cfdg
rule bar_A{CIRCLE{} bar_A{s .9 x 1 r 5}}
// ---- end bar2.cfdg
This would just need some on-the-fly translation while parsing include files. If it is done, the only decision (as I can see it) is how nested includes are handled. There are two obvious options, either of which would work.
Obviously I am asking this to make CF a little more modular, while still permitting messing around with the internals of imported rules - not fully encapsulated. It would be fun to do some mashups of gallery designs without having to recode them to remove name clashes.

Re: extend include directive to add rule prefix/suffix

Posted: Fri Feb 04, 2011 1:15 am
by MtnViewJohn
I think I would prefer to implement namespaces, a common feature in many languages.

Code: Select all

// ---- start foo.cfdg
namespace bar {
include bar.cfdg
}

rule A{bar::A{s .5}}
rule bar::A .01{}
...
// ---- end foo.cfdg
// ---- start bar.cfdg
rule A{CIRCLE{} A{s .9 x 1 r 5}}
// ---- end bar.cfdg
In addition to allowing includes without name clashes, this would allow you to directly copy someone's code into your cfdg file without worrying about name clashes.

Code: Select all

namespace bar {
rule A{CIRCLE{} A{s .9 x 1 r 5}}
}

rule A{bar::A{s .5}}
rule bar::A .01{}
...

Re: extend include directive to add rule prefix/suffix

Posted: Fri Feb 04, 2011 3:03 pm
by kipling
yes, that would do the job.