"spirit" like template parser generator in 100 Lines of code (almost)

BCS BCS at pathlink.com
Mon Dec 18 16:12:46 PST 2006


More work and some other thoughts.

BCS wrote:
>[...]
> What I have actually done is make an almost working prototype of a 
> template system that generates functions for a recursive decent parser. 
> It is to be mixed into a class or struct. The parameter for the template 
> is a string containing almost normal BNF reduction definitions.
> 
>[...]
 >
> It isn't implemented yet, but actions will be defined by providing 
> specializations for the "Action" template.
>

This is now done in one form, I'm not sure I quite like it though...

[...]
> Now for why it doesn't work.
> 
> ===Bugs:
> 
> -- Mixins don't work quite right. --
> 
> what seems to be happening is that only the first iteration of the 
> template is actual mixed in. Use of pragma(msg,...) seems to show that 
> template does recurse correctly.
> 
> I think this is a bug

A bit more work and it seems that a mixin can't add new specializations 
to a template in the scope that is mixed into.

template a(){template b(char : 'c'){uint i;}}

struct S
{
	template b(char : 'd'){uint i;}
	mixin a!();
	// only b!('d') avalable
	// b!('c') masked by 'd' version
}

 >[...]
> ===New Feature Needed To Make This Work.
> 
> 
> -- Abstract Templates --
> 
> The idea here is that a template marked as abstract would have no 
> generic implementation, only specializations. Furthermore the compiler 
> would assume that any given specializations it is asked for exists and 
> leave it up to the linker to find the correct implementation. this would 
> allow cyclical calls by specializations of a template.
> 

After a bit of playing around I'm not sure that this is strictly needed 
in this case. The order that templates are evaluated in may make it 
unneeded. I'd have to play around with it more to know for sure. However 
having the concept of an "abstract template" would remove this from the 
realm of "implementation specific details".

source:
http://www.webpages.uidaho.edu/~shro8822/dspirit.d

exmaple:
http://www.cs.uidaho.edu/~temp0092/test_t.d



More information about the Digitalmars-d-announce mailing list