Created
April 18, 2009 15:29
-
-
Save omega/97654 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This is my attempt at writing a simple grammar in perl6 | |
class EasyCMS::Template { | |
method render (Str $in, %vars) { | |
EasyCMS::Template::Grammar.parse($in) | |
or fail "could not parse: $in"; | |
#$in ~~ /EasyCMS::Template::Grammer::TOP/; | |
my $s; | |
for $/<token>.values -> $token { | |
warn " t: " ~ $token; | |
$s = $s ~ $token; | |
} | |
return $s; | |
} | |
} | |
grammar EasyCMS::Template::Grammar { | |
rule TOP { ^ <token>* $ } | |
rule token { <plain> } | |
token plain { (\w+)\s+? } | |
rule statement { '[%' } | |
} | |
t/01.t | |
#!/usr/bin/perl6 | |
use v6; | |
use Test; | |
use EasyCMS::Template; | |
my @tests = | |
{ | |
'in' => 'ape jens', | |
'vars' => {}, | |
'out' => 'ape jens', | |
}, | |
{ | |
'in' => 'a [% x %] c', | |
'vars' => { | |
'x' => 'b' | |
}, | |
'out' => 'a b c', | |
}, | |
{ | |
'in' => '[% x %]', | |
'vars' => { | |
'x' => 'b' | |
}, | |
'out' => 'b', | |
}, | |
; | |
plan 1 + @tests.elems * 1; | |
my $tmpl = EasyCMS::Template.new; | |
isa_ok($tmpl, "EasyCMS::Template"); | |
for @tests -> $t { | |
# render it | |
my $out = $tmpl.render($t<in>, $t<vars>); | |
is($out, $t<out>, "We could render some text"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment