#!/usr/bin/env php
.
*/
?>
' . "\n";
}
return sprintf(
'' .
"\n%s" .
" %s\n" .
"\n\n",
$name,
gen_identifier( $id ),
$desc,
$yields,
$prev_value,
gen_any_block( $queue, $or )
);
}
function gen_any_block( $queue, $or )
{
$any = gen_zip_re( $queue ) .
gen_on_class( $or );
return ( $any )
? '' . $any . ''
: '';
}
function gen_zip_re( $data )
{
if ( count( $data ) === 0 )
{
return '';
}
return sprintf(
'',
gen_re_quick( $data )
);
}
function gen_on_class( $data )
{
if ( count( $data ) === 0 )
{
return '';
}
$cur = array_shift( $data );
return sprintf(
'%s',
$cur,
gen_on_class( $data )
);
}
function gen_identifier( $id )
{
return is_numeric( $id )
? $id
: '-' . strtolower( $id );
}
function gen_identifier_value( $id )
{
// for non-numeric identifiers, return ascii value
// of character to represent our value
return is_numeric( $id )
? $id
: ord( $id );
}
$file = $argv[ 1 ];
$fdat = explode( '.', basename( $file ) );
$name = $fdat[ 0 ];
$cur = '';
$queue = array();
$or = array();
$fh = fopen( $file, 'r' );
echo 'name="rates/territories/', $name, '" ', "\n",
'desc="', ucfirst( $name ), ' territory classifications">' . "\n\n";
echo "\n\n";
$ids = array();
$params = array();
$imports = array();
$prev_yields = '';
$prev_yields_all = array();
$classes = '';
$param_type = 'terrType' . ucfirst( $name );
while ( true )
{
// read the line within the loop so that we do not terminate until after we
// treat eof as an empty line
$line = str_replace( array( "\n", "\r" ), '', fgets( $fh ) );
if ( !$cur )
{
if ( substr( $line, 0, 12 ) === '@import-pkg ' )
{
$imports[] = substr( $line, 12 );
continue;
}
// we expect this line to be a territory descriptor
try
{
list ( $id, $desc ) = parse_tdesc( $line );
}
catch ( Exception $e )
{
fwrite( STDERR, 'Invalid territory descriptor: ' . $line );
exit( 1 );
}
$ids[] = $id;
$cur = $id;
}
elseif ( ( $line === '' ) || feof( $fh ) )
{
// generate param for typedef
$params[ $id ] = $desc;
// if there's nothing in the queue, then treat this as an 'ROS' (this
// should appear as the *last* territory, or it will not function as
// expected)
if ( count( $queue ) === 0 )
{
$prev = $prev_yields_all;
}
else
{
$prev = array( $prev_yields );
}
// generate the classification
$classes .= gen_classification( $id, $name, $desc, $prev, $queue, $or );
// this accomplishes two things: (1) avoids regexes if there's a
// previous match and (2) ensures that we cannot possibly match multiple
// territories
$prev_yields = gen_yields( $id, $name );
$prev_yields_all[] = $prev_yields;
$cur = '';
$queue = array();
$or = array();
if ( feof( $fh ) )
{
break;
}
}
elseif ( $line[0] === '=' )
{
// =foo means match on classification @yields "foo"
$or[] = substr( $line, 1 );
}
else
{
$queue[] = $line;
}
}
$param_name = 'territory_' . $name;
?>
$desc ) { ?>