www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Pegged / Problem using the parse tree

Hi, I'm playing around with PEGGED (which is awsome BTW) but I'm a bit 
stucked how to use it best.

I have this grammer (stripped down to the essentials):

mixin(grammar("
Rebol3:
	set_word		<- word ':' {set_word_handling} & delim
	word 		<- ~[a-z]+ {word_handling}
"
));

I use these two semantic actions:

PT word_handling(PT)(PT p){
	writeln(p.matches[0]);
	return(p);
}

PT set_word_handling(PT)(PT p){
	writeln(p);
	return(p);
}

The grammer output looks like this:

Rebol3 [0, 25]["a", ":", "1", "b", ":", "2", "c", ":", "3", "d", ":", "4"]
 +-Rebol3.start [0, 25]["a", ":", "1", "b", ":", "2", "c", ":", "3", 
"d", ":", "4"]
    +-Rebol3.value [0, 5]["a", ":"]
    |  +-Rebol3.set_word [2, 4]["a", ":"]
    |     +-Rebol3.word [2, 3]["a"]

What I would like to do now, is to access the ["a", ":"] array from 
set_word_handling. But when I print the partse tree p from it I just 
get:

literal!(":") [3, 4][":"]

So, I thought that I can access "a" from the child, but no success.

Why do I want to access the word "a" from the set_word_handling 
function? Because there I know what to do with the word. The workaround 
now, is to keep track of the word "a" by setting a global variable from 
the word_handling rule and later reference it from set_word_handling. 
Not so elegant IMO.

Does anyone has a better idea how to access the child parts from 
set_word_handling?

Thanks.

-- 
Robert M. Münch
Saphirion AG

http://www.saphirion.com
smarter | better | faster
Aug 03 2013