Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Parse the simple case of tables
  • Loading branch information
Tadeusz Sośnierz committed Jul 5, 2011
1 parent 4a1e0ad commit 8e69807
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
31 changes: 31 additions & 0 deletions lib/Pod6/Actions.pm
Expand Up @@ -75,6 +75,10 @@ class Pod6::Actions {
make self.raw_block($/);
}

method pod_block:sym<delimited_table>($/) {
make self.table($/);
}

method pod_block:sym<paragraph>($/) {
make self.any_block($/);
}
Expand All @@ -83,6 +87,10 @@ class Pod6::Actions {
make self.raw_block($/);
}

method pod_block:sym<paragraph_table>($/) {
make self.table($/);
}

method pod_block:sym<abbreviated>($/) {
make self.any_block($/);
}
Expand All @@ -91,6 +99,10 @@ class Pod6::Actions {
make self.raw_block($/);
}

method pod_block:sym<abbreviated_table>($/) {
make self.table($/);
}

method any_block($/) {
my $name := $<type>.Str;
my @content;
Expand All @@ -115,6 +127,25 @@ class Pod6::Actions {
return Pod6::Block::Comment.new(content => @content);
}
}

method table($/) {
return Pod6::Block::Table.new(
content => $<table_content>.ast.list
);
}

method table_content($/) {
make $<table_row>».ast;
}

method table_row($/) {
make $<table_cell>».ast
}

method table_cell($/) {
make $/.Str;
}

method list_item($/, @content) {
return Pod6::Item.new(
level => $<type>.substr(4),
Expand Down
42 changes: 37 additions & 5 deletions lib/Pod6/Grammar.pm
Expand Up @@ -72,8 +72,7 @@ grammar Pod6::Grammar {
}

token pod_block:sym<delimited_raw> {
^^ \h* '=begin' \h+ <!before 'END'>
$<type>=[ 'code' || 'comment' ]
^^ \h* '=begin' \h+ $<type>=[ 'code' || 'comment' ]
<pod_newline>+
[
$<pod_content> = [ .*? ]
Expand All @@ -82,6 +81,15 @@ grammar Pod6::Grammar {
]
}

token pod_block:sym<delimited_table> {
^^ \h* '=begin' \h+ 'table' <pod_newline>+
[
<table_content>
^^ \h* '=end' \h+ 'table' <pod_newline>
|| <.panic: '=begin without matching =end'>
]
}

token pod_block:sym<end> {
^^ \h*
[
Expand Down Expand Up @@ -109,19 +117,23 @@ grammar Pod6::Grammar {
}

token pod_block:sym<paragraph_raw> {
^^ \h* '=for' \h+ <!before 'END'>
$<type>=[ 'code' || 'comment' ]
^^ \h* '=for' \h+ $<type>=[ 'code' || 'comment' ]
<pod_newline>
$<pod_content> = <pod_text_para>
}

token pod_block:sym<paragraph_table> {
^^ \h* '=for' \h+ 'table' <pod_newline>
<table_content>
}

token pod_block:sym<abbreviated> {
^^
$<spaces> = [ \h* ]
{}
:my $*VMARGIN := $<spaces>.to - $<spaces>.from;
:my $*ALLOW_CODE := 0;
'=' <!before begin || end || for || END>
'=' <!before begin\s || end\s || for\s || END\s || table\s>
$<type> = [
<pod_code_parent> { $*ALLOW_CODE := 1 }
|| <identifier>
Expand All @@ -135,6 +147,26 @@ grammar Pod6::Grammar {
$<pod_content> = <pod_text_para> *
}

token pod_block:sym<abbreviated_table> {
^^ \h* '=table' <pod_newline>
<table_content>
}

token table_content {
<table_row>+
}

token table_row {
\h* <table_cell> ** [ \h\h+ ] \n
}

token table_cell {
<!before '=' \w> # no pod directives
[
<!before \h\h+ || \h'|'\h || \h'+'\h> \N
]+
}

token pod_newline {
\h* \n
}
Expand Down
52 changes: 52 additions & 0 deletions t/08-tables.t
@@ -0,0 +1,52 @@
use Test;
use Pod6;
plan *;
my ($x, $r);

#you can create tables compactly, line-by-line

$x = q[
=begin pod
=begin table
The Shoveller Eddie Stevens King Arthur's singing shovel
Blue Raja Geoffrey Smith Master of cutlery
Mr Furious Roy Orson Ticking time bomb of fury
The Bowler Carol Pinnsler Haunted bowling ball
=end table
=end pod
];

$r = Pod6::parse($x);
my $t = $r.content[0];
isa_ok $t, Pod6::Block::Table;
is $t.content[0][0], 'The Shoveller';
is $t.content[0][1], 'Eddie Stevens';
is $t.content[0][2], "King Arthur's singing shovel";
is $t.content[1][0], 'Blue Raja';
is $t.content[1][1], 'Geoffrey Smith';
is $t.content[1][2], 'Master of cutlery';
is $t.content[2][0], 'Mr Furious';
is $t.content[2][1], 'Roy Orson';
is $t.content[2][2], 'Ticking time bomb of fury';
is $t.content[3][0], 'The Bowler';
is $t.content[3][1], 'Carol Pinnsler';
is $t.content[3][2], 'Haunted bowling ball';

$x = q[
=table
Constants 1
Variables 10
Subroutines 33
Everything else 57
];
$r = Pod6::parse($x);
is $r.content[0][0], 'Constants';
is $r.content[1][0], 'Variables';
is $r.content[2][0], 'Subroutines';
is $r.content[3][0], 'Everything else';
is $r.content[0][1], '1';
is $r.content[1][1], '10';
is $r.content[2][1], '33';
is $r.content[3][1], '57';

done;

0 comments on commit 8e69807

Please sign in to comment.