Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created November 11, 2016 00:41
Show Gist options
  • Save Xliff/4a5418933efcf4a2e2c1a710a6c45a6a to your computer and use it in GitHub Desktop.
Save Xliff/4a5418933efcf4a2e2c1a710a6c45a6a to your computer and use it in GitHub Desktop.

So now my code looks like this:

use v6.c;

use Grammar::Tracer;

grammar XSLTFuncDef {
	token TOP { 
		'XSLTPUBFUN' <ws>
		<returnType> <ws>
		'XSLTCALL' <ws>
		<funcName> <wso>
		'(' ( <params>* ) ');' 
	}

	token ws 	{ [ \s ]+ }
	token wso 	{ [ \s ]* }

	token returnType {
		[<typePrefix> <ws>]? (\w+)
	}

	token funcName {
		\w+
	}

	token typePrefix {
		'const' || 'unsigned'
	}

	token params {
		[<typePrefix> <ws>]? (\w+) ' ' [('*'+)? (\w+)] [ ',' <ws> ]? 
	}
}

class grammarActions {
	has @.params;

	method TOP($/) {
		make {
			functionName 	=> $/<funcName>[0],
			returnType 		=> $/<returnType>.made,
			params			=> @.params
		}
	}

	method returnType($/) {
		make {
			ctype 	=> $/[0],
			type 	=> $<typePrefix>.made ~ self!typeConv($/[0])
		}
		
	}

	method typePrefix($/) {
		make $/ == 'unsigned' ?? 'u' !! '';
	}

	method params($/) {
		push @.params, {
			ctype 	=> $/[0],
			type 	=> $<typePrefix>.made ~ self!typeConv($/[0]),
			name 	=> $/[2],
			stars 	=> $/[1]
		}
	}

	# cw: WHEEE! This needs to be reworked since it is a method and not 
	#     an auto-executed action.
	method !typeConv($t) {
		do given $t {
			when /Ptr$/ {
				S/Ptr$//;
			}

			when /Cha?r/ {
				'Str';
			}

			when 'int' {
				'int32';
			}

			when 'long' {
				'int64';
			}

			when 'float' {
				'num32';
			}

			when 'double' {
				'num64';
			}

			default {
				$_;
			}
		}
	}
}

sub MAIN($filename) {
	die "'$filename' does not exist" unless $filename.IO.e;

	my $text = $filename.IO.slurp;

	my @functions;
	for $text ~~ m:g/^^ ('XSLTPUBFUN' .+? ';')/ -> $m {
		say $m[0];
		my $t = XSLTFuncDef.parse($m[0], actions => grammarActions.new);
		say $t;
	}
}

According to the tutorial here, this should work, yet I am getting the following error:

Too many positionals passed; expected 1 argument but got 2
  in regex params at parser.pl line 29
  in block  at /home/cbwood/.rakudobrew/moar-nom/install/share/perl6/site/sources/88A197C57F6C4623C1071F68DFC42E86CA5F4B4D (Grammar::Tracer) line 21
  in regex TOP at parser.pl line 6
  in block  at /home/cbwood/.rakudobrew/moar-nom/install/share/perl6/site/sources/88A197C57F6C4623C1071F68DFC42E86CA5F4B4D (Grammar::Tracer) line 21
  in sub MAIN at parser.pl line 113
  in block <unit> at parser.pl line 105

What am I missing?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment