Skip to content

Commit

Permalink
processing more pod elements
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 4, 2012
1 parent 24537d8 commit ee68cab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
31 changes: 26 additions & 5 deletions lib/Pod/Parser.pm
Expand Up @@ -21,6 +21,7 @@ my $pod = '';
my $verbatim = '';
my $text = '';

has $.depth is rw = 0;
has @.data;
has $.title is rw;

Expand Down Expand Up @@ -54,12 +55,15 @@ method parse (Str $string) {
}
# TODO implement the following tags:
if $row ~~ m/^\=over \s+ (\d+) \s* $/ {
self.over($0.Str);
next;
}
if $row ~~ m/^\=item \s+ (.*) $/ {
self.item($0.Str);
next;
}
if $row ~~ m/^\=back\s*/ {
self.back;
next;
}
if $row ~~ m/^\=begin\s+code\s*$/ {
Expand All @@ -76,7 +80,7 @@ method parse (Str $string) {
# TODO: what about '=head' or '=MMMMMM' or '=begin usage' ?

if $row ~~ m/^ \= / {
X::Parser.new(msg => "Unknown tag", text => $row).throw;
X::Pod::Parser.new(msg => "Unknown tag", text => $row).throw;
}

if $row ~~ m/^\s+\S/ {
Expand All @@ -101,17 +105,34 @@ method parse (Str $string) {

# after ending all the rows:
if $in_pod {
die "file ended in the middle of a pod";
X::Pod::Parser.new(msg => 'file ended in the middle of a pod', text => '').throw;
}
self.include_text;

return self.data;
}

method over($text) {
self.include_pod;
self.depth++;
self.data.push({ type => 'over', content => $text });
return;
}

method item($text) {
self.include_pod;
self.data.push({ type => 'item', content => $text });
}

method back() {
self.include_pod;
self.depth--;
}

method set_title($text) {
X::Parser.new(msg => 'TITLE set twice', text => $text).throw if self.title;
X::Parser.new(msg => 'No value given for TITLE', text => $text).throw if $text !~~ /\S/;
#die 'No POD should be before TITLE' if self.data;
X::Pod::Parser.new(msg => 'TITLE set twice', text => $text).throw if self.title;
X::Pod::Parser.new(msg => 'No value given for TITLE', text => $text).throw if $text !~~ /\S/;
#X::Pod::Parser.new(msg => 'No POD should be before TITLE', text => $text).throw if self.data;

$.title = $text;
self.data.push({ type => 'title', content => $text });
Expand Down
2 changes: 1 addition & 1 deletion lib/Pod/Parser/Common.pm6
@@ -1,4 +1,4 @@
class X::Parser is Exception {
class X::Pod::Parser is Exception {
has $.msg;
has $.text;

Expand Down
4 changes: 2 additions & 2 deletions t/01-parse.t
Expand Up @@ -39,7 +39,7 @@ is_deeply @result, @expected, 'parse a.pod';
try {
$pp.parse_file('t/files/two-titles.pod');
CATCH {
when X::Parser {
when X::Pod::Parser {
is $_.msg, 'TITLE set twice', 'exception on duplicate TITLE';
}
}
Expand All @@ -48,7 +48,7 @@ try {
try {
$pp.parse_file('t/files/unknown-tag.pod');
CATCH {
when X::Parser {
when X::Pod::Parser {
is $_.msg, 'Unknown tag', 'exception on unknown =tag';
}
}
Expand Down

0 comments on commit ee68cab

Please sign in to comment.