| 1 | package Carton::Snapshot::Parser; | ||
| 2 | use Class::Tiny; | ||
| 3 | use warnings NONFATAL => 'all'; | ||
| 4 | use Carton::Dist; | ||
| 5 | use Carton::Error; | ||
| 6 | |||
| 7 | my $machine = { | ||
| 8 | init => [ | ||
| 9 | { | ||
| 10 | re => qr/^\# carton snapshot format: version (1\.0)/, | ||
| 11 | code => sub { | ||
| 12 | my($stash, $snapshot, $ver) = @_; | ||
| 13 | $snapshot->version($ver); | ||
| 14 | }, | ||
| 15 | goto => 'section', | ||
| 16 | }, | ||
| 17 | # TODO support pasing error and version mismatch etc. | ||
| 18 | ], | ||
| 19 | section => [ | ||
| 20 | { | ||
| 21 | re => qr/^DISTRIBUTIONS$/, | ||
| 22 | goto => 'dists', | ||
| 23 | }, | ||
| 24 | { | ||
| 25 | re => qr/^__EOF__$/, | ||
| 26 | done => 1, | ||
| 27 | }, | ||
| 28 | ], | ||
| 29 | dists => [ | ||
| 30 | { | ||
| 31 | re => qr/^ (\S+)$/, | ||
| 32 | code => sub { $_[0]->{dist} = Carton::Dist->new(name => $1) }, | ||
| 33 | goto => 'distmeta', | ||
| 34 | }, | ||
| 35 | { | ||
| 36 | re => qr/^\S/, | ||
| 37 | goto => 'section', | ||
| 38 | redo => 1, | ||
| 39 | }, | ||
| 40 | ], | ||
| 41 | distmeta => [ | ||
| 42 | { | ||
| 43 | re => qr/^ pathname: (.*)$/, | ||
| 44 | code => sub { $_[0]->{dist}->pathname($1) }, | ||
| 45 | }, | ||
| 46 | { | ||
| 47 | re => qr/^\s{4}provides:$/, | ||
| 48 | code => sub { $_[0]->{property} = 'provides' }, | ||
| 49 | goto => 'properties', | ||
| 50 | }, | ||
| 51 | { | ||
| 52 | re => qr/^\s{4}requirements:$/, | ||
| 53 | code => sub { | ||
| 54 | $_[0]->{property} = 'requirements'; | ||
| 55 | }, | ||
| 56 | goto => 'properties', | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | re => qr/^\s{0,2}\S/, | ||
| 60 | code => sub { | ||
| 61 | my($stash, $snapshot) = @_; | ||
| 62 | $snapshot->add_distribution($stash->{dist}); | ||
| 63 | %$stash = (); # clear | ||
| 64 | }, | ||
| 65 | goto => 'dists', | ||
| 66 | redo => 1, | ||
| 67 | }, | ||
| 68 | ], | ||
| 69 | properties => [ | ||
| 70 | { | ||
| 71 | re => qr/^\s{6}([0-9A-Za-z_:]+) ([v0-9\._,=\!<>\s]+|undef)/, | ||
| 72 | code => sub { | ||
| 73 | my($stash, $snapshot, $module, $version) = @_; | ||
| 74 | if ($stash->{property} eq 'provides') { | ||
| 75 | $stash->{dist}->provides->{$module} = { version => $version }; | ||
| 76 | } else { | ||
| 77 | $stash->{dist}->add_string_requirement($module, $version); | ||
| 78 | } | ||
| 79 | }, | ||
| 80 | }, | ||
| 81 | { | ||
| 82 | re => qr/^\s{0,4}\S/, | ||
| 83 | goto => 'distmeta', | ||
| 84 | redo => 1, | ||
| 85 | }, | ||
| 86 | ], | ||
| 87 | }; | ||
| 88 | |||
| 89 | sub parse { | ||
| 90 | my($self, $data, $snapshot) = @_; | ||
| 91 | |||
| 92 | my @lines = split /\r?\n/, $data; | ||
| 93 | |||
| 94 | my $state = $machine->{init}; | ||
| 95 | my $stash = {}; | ||
| 96 | |||
| 97 | LINE: | ||
| 98 | for my $line (@lines, '__EOF__') { | ||
| 99 | last LINE unless @$state; | ||
| 100 | |||
| 101 | STATE: { | ||
| 102 | for my $trans (@{$state}) { | ||
| 103 | if (my @match = $line =~ $trans->{re}) { | ||
| 104 | if (my $code = $trans->{code}) { | ||
| 105 | $code->($stash, $snapshot, @match); | ||
| 106 | } | ||
| 107 | if (my $goto = $trans->{goto}) { | ||
| 108 | $state = $machine->{$goto}; | ||
| 109 | if ($trans->{redo}) { | ||
| 110 | redo STATE; | ||
| 111 | } else { | ||
| 112 | next LINE; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | last STATE; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | Carton::Error::SnapshotParseError->throw(error => "Could not parse snapshot file: $line"); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; |