Permalink
Browse files

content in the right places

  • Loading branch information...
masak committed Mar 22, 2014
1 parent a1609e6 commit b3e390b16ea5ef09fb2e0ba0c1eaabdf8f312c81
Showing with 35 additions and 1 deletion.
  1. +21 −1 lib/Text/Indented.pm
  2. +14 −0 t/parse-indent.t
View
@@ -1,24 +1,44 @@
grammar Text::Indented;
+class Suite {
+ has @.items;
+}
+
class TooMuchIndent is Exception {}
constant TABSTOP = 4;
regex TOP {
- :my @*SUITES = "root";
+ :my @*SUITES = Suite.new;
<line>*
+
+ { make root_suite }
}
sub indent { @*SUITES.end }
+sub root_suite { @*SUITES[0] }
+sub current_suite { @*SUITES[indent] }
+sub add_to_current_suite($item) { current_suite.items.push($item) }
+
+sub increase_indent($new_suite) { @*SUITES.push($new_suite) }
regex line {
^^ (<{ "\\x20" x TABSTOP }>*) (\h*) (\N*) $$ \n?
{
my $new_indent = $0.chars div TABSTOP;
+ my $line = ~$2;
die TooMuchIndent.new
if $new_indent > indent() + 1;
+
+ if $new_indent > indent() {
+ my $new_suite = Suite.new;
+ add_to_current_suite($new_suite);
+ increase_indent($new_suite);
+ }
+
+ add_to_current_suite($line);
}
}
View
@@ -50,4 +50,18 @@ sub fails_with($input, $ex_type, $message = $ex_type.^name) {
fails_with($input, Text::Indented::TooMuchIndent);
}
+{
+ my $input = q:to/EOF/;
+ Level 1
+ Level 2
+ EOF
+
+ my $root = parse($input);
+
+ isa_ok $root, Text::Indented::Suite;
+ is $root.items.elems, 2, 'two things were parsed:';
+ isa_ok $root.items[0], Str, 'a string';
+ isa_ok $root.items[1], Text::Indented::Suite, 'and a suite';
+}
+
done;

0 comments on commit b3e390b

Please sign in to comment.