| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Oct-27 17:39 (EDT) | ||
| 6 | # Function: compile job component | ||
| 7 | # | ||
| 8 | # $Id: Compile.pm,v 1.1 2010/11/01 18:42:00 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::Submit::Compile; | ||
| 11 | use AC::MrGamoo::Submit::Compile::Block; | ||
| 12 | use strict; | ||
| 13 | |||
| 14 | my %COMPILE = ( | ||
| 15 | config => { tag => 'config', multi => 1, }, | ||
| 16 | doc => { tag => 'block', multi => 1, }, | ||
| 17 | init => { tag => 'block', multi => 0, }, | ||
| 18 | common => { tag => 'simple', multi => 0, }, | ||
| 19 | map => { tag => 'block', multi => 0, required => 1, }, | ||
| 20 | reduce => { tag => 'block', multi => 1, required => 1, }, | ||
| 21 | final => { tag => 'block', multi => 0, }, | ||
| 22 | readinput => { tag => 'block', multi => 0, }, | ||
| 23 | filefilter => { tag => 'block', multi => 0, }, | ||
| 24 | ); | ||
| 25 | |||
| 26 | my %BLOCK = ( | ||
| 27 | init => 'simple', | ||
| 28 | cleanup => 'simple', | ||
| 29 | attr => 'config', | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $class = shift; | ||
| 34 | |||
| 35 | my $me = bless { | ||
| 36 | @_, | ||
| 37 | # file | text | ||
| 38 | }, $class; | ||
| 39 | |||
| 40 | if( $me->{file} ){ | ||
| 41 | open(my $fd, $me->{file}) || $me->_die("cannot open file: $!"); | ||
| 42 | local $/ = undef; | ||
| 43 | $me->{text} = <$fd>; | ||
| 44 | close $fd; | ||
| 45 | } | ||
| 46 | $me->{lines} = [ split /^/m, $me->{text} ]; | ||
| 47 | |||
| 48 | $me->_compile(); | ||
| 49 | $me->_check(); | ||
| 50 | |||
| 51 | return $me; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub compile { | ||
| 55 | my $me = shift; | ||
| 56 | my $name = shift; | ||
| 57 | my $num = shift; | ||
| 58 | |||
| 59 | my $b = $me->{content}{$name}; | ||
| 60 | return unless $b; | ||
| 61 | $b = $b->[$num] if defined($num) && ref($b); | ||
| 62 | |||
| 63 | return $b->compile( $me->{content}{common} ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub src { | ||
| 67 | my $me = shift; | ||
| 68 | return $me->{_file_content}; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub get_code { | ||
| 72 | my $me = shift; | ||
| 73 | my $name = shift; | ||
| 74 | my $num = shift; | ||
| 75 | |||
| 76 | my $prog = $me->compile( $name, $num ); | ||
| 77 | return unless $prog; | ||
| 78 | |||
| 79 | my $c = eval $prog; | ||
| 80 | die $@ if $@; | ||
| 81 | |||
| 82 | return $c; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _die { | ||
| 86 | my $me = shift; | ||
| 87 | my $err = shift; | ||
| 88 | |||
| 89 | if( $me->{_lineno} ){ | ||
| 90 | die "ERROR: $err\nfile: $me->{file} line: $me->{_lineno}\n$me->{_line}\n"; | ||
| 91 | } | ||
| 92 | die "ERROR: $err\nfile: $me->{file}\n"; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _next { | ||
| 96 | my $me = shift; | ||
| 97 | |||
| 98 | return unless @{ $me->{lines} }; | ||
| 99 | $me->{_line} = shift @{ $me->{lines} }; | ||
| 100 | $me->{_lineno} ++; | ||
| 101 | $me->{_file_content} .= $me->{_line}; | ||
| 102 | return $me->{_line}; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _compile { | ||
| 106 | my $me = shift; | ||
| 107 | |||
| 108 | while(1){ | ||
| 109 | my $line = $me->_next(); | ||
| 110 | last unless defined $line; | ||
| 111 | chomp $line; | ||
| 112 | |||
| 113 | # white, comment, or start | ||
| 114 | $line =~ s/^%#.*//; | ||
| 115 | $line =~ s/#.*//; | ||
| 116 | next if $line =~ /^\s*$/; | ||
| 117 | |||
| 118 | my($tag) = $line =~ m|^<%(.*)>\s*$|; | ||
| 119 | my $d = $COMPILE{$tag}; | ||
| 120 | |||
| 121 | if( $d->{tag} eq 'block'){ | ||
| 122 | $me->_add_block($tag, $me->_compile_block($tag)); | ||
| 123 | } | ||
| 124 | elsif( $d->{tag} eq 'simple' ){ | ||
| 125 | $me->_add_block($tag, $me->_compile_block_simple($tag)); | ||
| 126 | } | ||
| 127 | elsif( $d->{tag} eq 'config' ){ | ||
| 128 | $me->_add_config($tag, $me->_compile_config($tag)); | ||
| 129 | } | ||
| 130 | else{ | ||
| 131 | $me->_die("syntax error"); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | delete $me->{_lineno}; | ||
| 136 | delete $me->{_line}; | ||
| 137 | delete $me->{_fd}; | ||
| 138 | |||
| 139 | 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _lineno_info { | ||
| 143 | my $me = shift; | ||
| 144 | |||
| 145 | # should have the number of the _next_ line | ||
| 146 | return sprintf "#line %d $me->{file}\n", $me->{_lineno} + 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _compile_block { | ||
| 150 | my $me = shift; | ||
| 151 | my $tag = shift; | ||
| 152 | |||
| 153 | my $b = AC::MrGamoo::Submit::Compile::Block->new(); | ||
| 154 | |||
| 155 | $b->{code} = $me->_lineno_info(); | ||
| 156 | |||
| 157 | while(1){ | ||
| 158 | my $line = $me->_next(); | ||
| 159 | $me->_die("end of file reached looking for end of $tag section") unless defined $line; | ||
| 160 | last if $line =~ m|^</%$tag>\s*$|; | ||
| 161 | |||
| 162 | my($tag) = $line =~ m|^<%(.*)>\s*$|; | ||
| 163 | |||
| 164 | if( $BLOCK{$tag} eq 'simple' ){ | ||
| 165 | $b->{$tag} .= $me->_compile_block_simple( $tag ); | ||
| 166 | $b->{code} .= $me->_lineno_info(); | ||
| 167 | }elsif( $BLOCK{$tag} eq 'config' ){ | ||
| 168 | $b->{$tag} = $me->_compile_config( $tag ); | ||
| 169 | }elsif( $tag ){ | ||
| 170 | $me->_die("syntax error"); | ||
| 171 | |||
| 172 | }else{ | ||
| 173 | $b->{code} .= $line; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | return $b; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _compile_block_simple { | ||
| 181 | my $me = shift; | ||
| 182 | my $tag = shift; | ||
| 183 | |||
| 184 | my $b = $me->_lineno_info(); | ||
| 185 | |||
| 186 | while(1){ | ||
| 187 | my $line = $me->_next(); | ||
| 188 | $me->_die("end of file reached looking for end of $tag section") unless defined $line; | ||
| 189 | last if $line =~ m|^</%$tag>\s*$|; | ||
| 190 | $b .= $line; | ||
| 191 | } | ||
| 192 | |||
| 193 | return $b; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub _compile_config { | ||
| 197 | my $me = shift; | ||
| 198 | my $tag = shift; | ||
| 199 | |||
| 200 | my $c = {}; | ||
| 201 | |||
| 202 | while(1){ | ||
| 203 | my $line = $me->_next(); | ||
| 204 | $me->_die("end of file reached looking for end of '$tag' section") unless defined $line; | ||
| 205 | return $c if $line =~ m|^</%$tag>\s*$|; | ||
| 206 | |||
| 207 | $line =~ s/^\s+//; | ||
| 208 | $line =~ s/\s+$//; | ||
| 209 | my($k, $v) = split /\s+=>\s*/, $line, 2; | ||
| 210 | $c->{$k} = $v; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub _add_block { | ||
| 215 | my $me = shift; | ||
| 216 | my $tag = shift; | ||
| 217 | my $blk = shift; | ||
| 218 | |||
| 219 | my $d = $COMPILE{$tag}; | ||
| 220 | |||
| 221 | if( $d->{multi} ){ | ||
| 222 | push @{$me->{content}{$tag}}, $blk; | ||
| 223 | }else{ | ||
| 224 | $me->_die("redefinition of '$tag' section") if $me->{content}{$tag}; | ||
| 225 | $me->{content}{$tag} = $blk; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | sub add_config { | ||
| 230 | my $me = shift; | ||
| 231 | my $cfg = shift; | ||
| 232 | |||
| 233 | $me->_add_config('config', $cfg); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _add_config { | ||
| 237 | my $me = shift; | ||
| 238 | my $tag = shift; | ||
| 239 | my $cfg = shift; | ||
| 240 | |||
| 241 | my $d = $COMPILE{$tag}; | ||
| 242 | |||
| 243 | if( $d->{multi} ){ | ||
| 244 | # merge | ||
| 245 | @{ $me->{content}{$tag} }{ keys %$cfg } = values %$cfg; | ||
| 246 | }else{ | ||
| 247 | $me->_die("redefinition of '$tag' section") if $me->{content}{$tag}; | ||
| 248 | $me->{content}{$tag} = $cfg; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | sub set_initres { | ||
| 253 | my $me = shift; | ||
| 254 | my $ir = shift; | ||
| 255 | |||
| 256 | $me->{initres} = $ir; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub set_config { | ||
| 260 | my $me = shift; | ||
| 261 | my $cfg = shift; | ||
| 262 | |||
| 263 | $me->{content}{config} = $cfg; | ||
| 264 | } | ||
| 265 | |||
| 266 | sub get_config_param { | ||
| 267 | my $me = shift; | ||
| 268 | my $k = shift; | ||
| 269 | |||
| 270 | return $me->{content}{config}{$k}; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub set_config_param { | ||
| 274 | my $me = shift; | ||
| 275 | my $k = shift; | ||
| 276 | my $v = shift; | ||
| 277 | |||
| 278 | return $me->{content}{config}{$k} = $v; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _check { | ||
| 282 | my $me = shift; | ||
| 283 | |||
| 284 | for my $s (keys %COMPILE){ | ||
| 285 | next unless $COMPILE{$s}{required}; | ||
| 286 | next if $me->{content}{$s}; | ||
| 287 | $me->_die("missing required section '$s'"); | ||
| 288 | } | ||
| 289 | 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | 1; |