26
+ − 1 package IMPL::Text::Parser::Chunk;
+ − 2 use strict;
+ − 3 use warnings;
+ − 4
+ − 5 use base qw(IMPL::Object);
+ − 6
+ − 7 use IMPL::Class::Property;
+ − 8 use IMPL::Class::Property::Direct;
+ − 9
+ − 10 use constant {
+ − 11 OP_REGEXP => 1,
+ − 12 OP_STRING => 2,
+ − 13 OP_REFERENCE => 3,
+ − 14 OP_CHUNK => 4,
+ − 15 OP_THROW => 5,
+ − 16 OP_TRYCATCH => 6
+ − 17 };
+ − 18
+ − 19 BEGIN {
+ − 20 public _direct property chunkName => prop_get;
+ − 21 public _direct property opStream => prop_get;
+ − 22 }
+ − 23
+ − 24 sub Regexp {
+ − 25 my ($this,$rx) = @_;
+ − 26
+ − 27 if (ref $rx eq 'Regexp') {
+ − 28
+ − 29 } elsif (not ref $rx) {
+ − 30 $rx = q/$rx/;
+ − 31 } else {
+ − 32 die new IMPL::InvalidArgumentException('A regular expression required');
+ − 33 }
+ − 34
+ − 35 push @{$this->{$opStream}}, [OP_REGEXP, $rx];
+ − 36 }
+ − 37
+ − 38 sub String {
+ − 39 my ($this,$str) = @_;
+ − 40
+ − 41 die new IMPL::InvalidArgumentException("A simple value is required") if ref $str;
+ − 42
+ − 43 push @{$this->{$opStream}}, [OP_STRING, $str];
+ − 44 }
+ − 45
+ − 46 sub Reference {
+ − 47 my ($this,$ref) = @_;
+ − 48
+ − 49 die new IMPL::InvalidArgumentException("A simple value is reqiured") if ref $ref;
+ − 50
+ − 51 push @{$this->{$opStream}}, [OP_REFERENCE, $ref];
+ − 52 }
+ − 53
+ − 54 sub Chunk {
+ − 55 my ($this,$chunk) = @_;
+ − 56
+ − 57 die new IMPL::InvalidArgumentException unless UNIVERSAL::isa($chunk,'IMPL::Text::Parser::Chunk');
+ − 58
+ − 59 push @{$this->{$opStream}}, [OP_CHUNK, $chunk];
+ − 60 }
+ − 61
+ − 62 sub Throw {
+ − 63 my ($this, $msg) = @_;
+ − 64
+ − 65 push @{$this->{$opStream}}, [OP_THROW, $msg];
+ − 66 }
+ − 67
+ − 68 sub TryCatch {
+ − 69 my ($this,$chunkTry,$chunkCatch) = @_;
+ − 70
+ − 71 push @{$this->{$opStream}}, [OP_TRYCATCH, $chunkTry, $chunkCatch];
+ − 72 }
+ − 73
+ − 74 sub compile {
+ − 75 my ($this) = @_;
+ − 76
+ − 77 my $text = '';
+ − 78
+ − 79 if ($this->{$opStream}) {
+ − 80 foreach my $op (@{$this->{$opStream}}) {
+ − 81 my $code = shift @$op;
+ − 82
+ − 83 if ($code == OP_REGEXP) {
+ − 84
+ − 85 } elsif ($code == OP_STRING) {
+ − 86
+ − 87 } elsif ($code == OP_REFERENCE) {
+ − 88
+ − 89 } elsif ($code == OP_CHUNK) {
+ − 90
+ − 91 } elsif ($code == OP_THROW) {
+ − 92
+ − 93 } elsif ($code == OP_TRYCATCH) {
+ − 94
+ − 95 }
+ − 96 }
+ − 97 }
+ − 98 }
+ − 99
+ − 100 1;