2023-03-17 15:12:20 -07:00
|
|
|
#include "Bloat.cpp"
|
2023-03-16 23:09:19 -07:00
|
|
|
#include "IO.cpp"
|
|
|
|
#include "Spec.cpp"
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
|
2023-03-30 16:33:35 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
void parse_options( int num, char** arguments )
|
|
|
|
{
|
|
|
|
zpl_opts opts;
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_opts_init( & opts, zpl_heap(), "refactor");
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_opts_add( & opts, "num", "num" , "Number of files to refactor" , ZPL_OPTS_INT );
|
|
|
|
zpl_opts_add( & opts, "src" , "src" , "File/s to refactor" , ZPL_OPTS_STRING);
|
|
|
|
zpl_opts_add( & opts, "dst" , "dst" , "File/s post refactor" , ZPL_OPTS_STRING);
|
|
|
|
zpl_opts_add( & opts, "spec", "spec", "Specification for refactoring", ZPL_OPTS_STRING);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_opts_add( & opts, "debug", "debug", "Allows for wait to attach", ZPL_OPTS_FLAG);
|
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
if (opts_custom_compile( & opts, num, arguments))
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
sw num = 0;
|
2023-03-18 00:10:43 -07:00
|
|
|
|
|
|
|
if ( zpl_opts_has_arg( & opts, "debug" ) )
|
|
|
|
{
|
2023-03-30 16:33:35 -07:00
|
|
|
#if Build_Debug
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_printf("Will wait (pause available for attachment)");
|
|
|
|
char pause = getchar();
|
2023-03-30 16:33:35 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Global::ShouldShowDebug = true;
|
2023-03-18 00:10:43 -07:00
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
if ( zpl_opts_has_arg( & opts, "num" ) )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
num = zpl_opts_integer( & opts, "num", -1 );
|
|
|
|
uw global_reserve = num * sizeof(zpl_string) * IO::Path_Size_Largest * 2 + 8;
|
|
|
|
|
|
|
|
if ( global_reserve > zpl_megabytes(1) )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
Memory::resize( global_reserve + zpl_megabytes(2) );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
zpl_array_init_reserve( IO::Sources, g_allocator, num );
|
|
|
|
zpl_array_init_reserve( IO::Destinations, g_allocator, num );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
else
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
num = 1;
|
2023-03-17 15:12:20 -07:00
|
|
|
|
|
|
|
zpl_array_init_reserve( IO::Sources, g_allocator, 1 );
|
|
|
|
zpl_array_init_reserve( IO::Destinations, g_allocator, 1 );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
2023-03-17 15:12:20 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( zpl_opts_has_arg( & opts, "src" ) )
|
|
|
|
{
|
|
|
|
zpl_string opt = zpl_opts_string( & opts, "src", "INVALID SRC ARGUMENT" );
|
|
|
|
|
|
|
|
if ( num == 1 )
|
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt ));
|
|
|
|
zpl_array_append( IO::Sources, path );
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buffer[ IO::Path_Size_Largest ];
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
uw left = num;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char* path = buffer;
|
|
|
|
sw length = 0;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
do
|
|
|
|
{
|
|
|
|
path[length] = *opt;
|
|
|
|
}
|
2023-03-17 15:12:20 -07:00
|
|
|
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
|
|
|
|
zpl_array_append( IO::Sources, path_string );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
opt++;
|
|
|
|
}
|
|
|
|
while ( --left );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
fatal( "-src not provided\n" );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( zpl_opts_has_arg( & opts, "dst" ) )
|
|
|
|
{
|
|
|
|
zpl_string opt = zpl_opts_string( & opts, "dst", "INVALID DST ARGUMENT" );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( num == 1 )
|
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt) );
|
|
|
|
zpl_array_append( IO::Destinations, path );
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buffer[ IO::Path_Size_Largest ];
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
uw left = num;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char* path = buffer;
|
|
|
|
sw length = 0;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
do
|
|
|
|
{
|
|
|
|
path[length] = *opt;
|
|
|
|
}
|
2023-03-17 15:12:20 -07:00
|
|
|
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
|
|
|
|
zpl_array_append( IO::Destinations, path_string );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
opt++;
|
|
|
|
}
|
|
|
|
while ( --left );
|
2023-03-17 15:12:20 -07:00
|
|
|
|
|
|
|
if ( zpl_array_count(IO::Destinations) != zpl_array_count( IO::Sources ) )
|
|
|
|
{
|
|
|
|
fatal("-dst count must match -src count");
|
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 15:12:20 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
uw left = num;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
zpl_array_append( IO::Destinations, IO::Sources[num - left] );
|
|
|
|
}
|
|
|
|
while ( --left );
|
|
|
|
}
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( zpl_opts_has_arg( & opts, "spec" ) )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_string opt = zpl_opts_string( & opts, "spec", "INVALID PATH" );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
IO::Specification = zpl_string_make( g_allocator, "" );
|
|
|
|
IO::Specification = zpl_string_append( IO::Specification, opt );
|
|
|
|
}
|
2023-03-17 15:12:20 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fatal( "-spec not provided\n" );
|
|
|
|
}
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
else
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
zpl_printf("\nArguments: ");
|
|
|
|
for ( int index = 0; index < num; index++)
|
|
|
|
{
|
|
|
|
zpl_printf("\nArg[%d]: %s", index, arguments[index]);
|
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
fatal( "Failed to parse arguments\n" );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_opts_free( & opts);
|
|
|
|
}
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_arena Refactor_Buffer;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
void refactor()
|
|
|
|
{
|
2023-03-18 00:10:43 -07:00
|
|
|
ct static char const* include_sig = "include";
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
struct Token
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
u32 Start;
|
|
|
|
u32 End;
|
|
|
|
|
|
|
|
zpl_string Sig;
|
|
|
|
zpl_string Sub;
|
2023-03-12 18:34:55 -07:00
|
|
|
};
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
static zpl_array(Token) tokens = nullptr;
|
|
|
|
static zpl_string current = zpl_string_make( g_allocator, "");
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
#if Build_Debug
|
|
|
|
static zpl_string preview = zpl_string_make( g_allocator, "");
|
|
|
|
#endif
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
static bool Done = false;
|
|
|
|
if (! Done)
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_array_init( tokens, g_allocator );
|
|
|
|
Done = true;
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
else
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_array_clear( tokens );
|
|
|
|
}
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Prepare data and trackers.
|
2023-03-17 15:12:20 -07:00
|
|
|
char const* src = IO::get_next_source();
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( src == nullptr )
|
|
|
|
return;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
log_fmt("\n\nRefactoring: %s", IO::Sources[IO::Current]);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
sw buffer_size = IO::Current_Size;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
sw left = buffer_size;
|
|
|
|
uw col = 0;
|
|
|
|
uw line = 0;
|
|
|
|
|
|
|
|
#define pos (IO::Current_Size - left)
|
|
|
|
|
|
|
|
#define move_forward( Amount_ ) \
|
2023-03-25 19:44:36 -07:00
|
|
|
if ( left - Amount_ <= 0 ) \
|
|
|
|
goto End_Search; \
|
|
|
|
\
|
|
|
|
left -= Amount_; \
|
|
|
|
col += Amount_; \
|
|
|
|
src += Amount_ \
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
#define move_back( Amount_ ) \
|
|
|
|
left += Amount_; \
|
|
|
|
col -= Amount_; \
|
|
|
|
src -= Amount_ \
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
do
|
|
|
|
{
|
2023-03-25 19:44:36 -07:00
|
|
|
if ( Spec::Ignore_Comments && src[0] == '/' && left - 2 > 0 )
|
|
|
|
{
|
|
|
|
if ( src[1] == '/' )
|
|
|
|
{
|
|
|
|
move_forward( 2 );
|
|
|
|
|
|
|
|
// Force end of line.
|
|
|
|
while ( src[0] != '\n' )
|
|
|
|
{
|
|
|
|
move_forward( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
goto Skip;
|
|
|
|
}
|
|
|
|
else if ( src[1] == '*' )
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2023-03-27 17:14:04 -07:00
|
|
|
move_forward( 1 );
|
2023-03-25 19:44:36 -07:00
|
|
|
}
|
|
|
|
while ( (left - 2) > 0 && !( src[0] == '*' && src[1] == '/' ) );
|
|
|
|
|
2023-03-27 17:14:04 -07:00
|
|
|
move_forward( 1 );
|
|
|
|
|
|
|
|
goto Skip;
|
2023-03-25 19:44:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Includes to ignore
|
2023-03-30 10:59:49 -07:00
|
|
|
do
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
Spec::Entry* ignore = Spec::Ignore_Includes;
|
|
|
|
sw ignores_left = zpl_array_count( Spec::Ignore_Includes);
|
2023-03-30 10:59:49 -07:00
|
|
|
sw rewind = 0;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
if ( '#' != src[0] )
|
|
|
|
break;
|
|
|
|
|
|
|
|
move_forward( 1 );
|
|
|
|
rewind++;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
// Ignore whitespace
|
|
|
|
while ( zpl_char_is_space( src[0] ) )
|
|
|
|
{
|
2023-03-25 19:44:36 -07:00
|
|
|
move_forward( 1 );
|
2023-03-30 10:59:49 -07:00
|
|
|
rewind++;
|
|
|
|
}
|
2023-03-25 19:44:36 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
if ( zpl_strncmp( include_sig, src, sizeof(include_sig) - 1 ) != 0 )
|
|
|
|
{
|
|
|
|
move_back( rewind );
|
|
|
|
break;
|
|
|
|
}
|
2023-03-25 19:44:36 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
const u32 sig_size = sizeof(include_sig) - 1;
|
2023-03-18 00:10:43 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
move_forward( sig_size );
|
|
|
|
rewind += sig_size;
|
2023-03-18 00:10:43 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
// Ignore whitespace
|
|
|
|
while ( zpl_char_is_space( src[0] ) || src[0] == '\"' || src[0] == '<' )
|
|
|
|
{
|
|
|
|
move_forward(1);
|
|
|
|
rewind++;
|
|
|
|
}
|
2023-03-18 00:10:43 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
for ( ; ignores_left; ignores_left--, ignore++ )
|
|
|
|
{
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_string_clear( current );
|
2023-03-25 19:44:36 -07:00
|
|
|
|
|
|
|
u32 sig_length = zpl_string_length( ignore->Sig );
|
2023-03-30 10:59:49 -07:00
|
|
|
current = zpl_string_append_length( current, src, sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( zpl_string_are_equal( ignore->Sig, current ) )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
log_fmt("\nIgnored %-81s line %d, col %d", current, line, col );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
const sw length = zpl_string_length( current );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-18 00:10:43 -07:00
|
|
|
// The + 1 is for the closing " or > of the include
|
|
|
|
move_forward( length + 1 );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Force end of line.
|
2023-03-17 15:12:20 -07:00
|
|
|
while ( src[0] != '\n' )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
move_forward( 1 );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
goto Skip;
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 10:59:49 -07:00
|
|
|
|
|
|
|
move_back( rewind );
|
|
|
|
}
|
|
|
|
while (false);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
// Word Ignores
|
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
Spec::Entry* ignore = Spec::Ignore_Words;
|
|
|
|
sw ignores_left = zpl_array_count( Spec::Ignore_Words);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
for ( ; ignores_left; ignores_left--, ignore++ )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
if ( ignore->Sig[0] != src[0] )
|
2023-03-12 18:34:55 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
zpl_string_clear( current );
|
|
|
|
|
|
|
|
u32 sig_length = zpl_string_length( ignore->Sig );
|
2023-03-17 15:12:20 -07:00
|
|
|
current = zpl_string_append_length( current, src, sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
if ( zpl_string_are_equal( ignore->Sig, current ) )
|
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
char before = src[-1];
|
|
|
|
char after = src[sig_length];
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
if ( zpl_char_is_alphanumeric( before ) || before == '_'
|
|
|
|
|| zpl_char_is_alphanumeric( after ) || after == '_' )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
log_fmt("\nIgnored %-81s line %d, col %d", current, line, col );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
move_forward( sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
goto Skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace Ignores
|
|
|
|
{
|
2023-03-16 23:09:19 -07:00
|
|
|
Spec::Entry* ignore = Spec::Ignore_Namespaces;
|
|
|
|
sw ignores_left = zpl_array_count( Spec::Ignore_Namespaces);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
for ( ; ignores_left; ignores_left--, ignore++ )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
if ( ignore->Sig[0] != src[0] )
|
|
|
|
{
|
|
|
|
ignore++;
|
2023-03-12 18:34:55 -07:00
|
|
|
continue;
|
2023-03-17 15:12:20 -07:00
|
|
|
}
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
zpl_string_clear( current );
|
|
|
|
|
|
|
|
u32 sig_length = zpl_string_length( ignore->Sig );
|
2023-03-17 15:12:20 -07:00
|
|
|
current = zpl_string_append_length( current, src, sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
if ( zpl_string_are_equal( ignore->Sig, current ) )
|
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
u32 length = sig_length;
|
|
|
|
char const* ns_content = src + sig_length;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
while ( zpl_char_is_alphanumeric( ns_content[0] ) || ns_content[0] == '_' )
|
|
|
|
{
|
|
|
|
length++;
|
|
|
|
ns_content++;
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
#if Build_Debug
|
2023-03-12 18:34:55 -07:00
|
|
|
zpl_string_clear( preview );
|
2023-03-17 15:12:20 -07:00
|
|
|
preview = zpl_string_append_length( preview, src, length );
|
|
|
|
log_fmt("\nIgnored %-40s %-40s line %d, column %d", preview, ignore->Sig, line, col );
|
2023-03-16 23:09:19 -07:00
|
|
|
#endif
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
move_forward( length );
|
2023-03-12 18:34:55 -07:00
|
|
|
goto Skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Includes to match
|
2023-03-30 10:59:49 -07:00
|
|
|
do
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-30 10:59:49 -07:00
|
|
|
Spec::Entry* include = Spec::Includes;
|
|
|
|
sw includes_left = zpl_array_count ( Spec::Includes);
|
|
|
|
sw rewind = 0;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
if ( '#' != src[0] )
|
|
|
|
break;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
move_forward( 1 );
|
|
|
|
rewind++;
|
2023-03-16 23:09:19 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
// Ignore whitespace
|
|
|
|
while ( zpl_char_is_space( src[0] ) )
|
|
|
|
{
|
2023-03-25 19:44:36 -07:00
|
|
|
move_forward( 1 );
|
2023-03-30 10:59:49 -07:00
|
|
|
rewind++;
|
|
|
|
}
|
2023-03-25 19:44:36 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
if ( zpl_strncmp( include_sig, src, sizeof(include_sig) - 1 ) != 0 )
|
|
|
|
{
|
|
|
|
move_back( rewind );
|
|
|
|
break;
|
|
|
|
}
|
2023-03-25 19:44:36 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
const u32 sig_size = sizeof(include_sig) - 1;
|
2023-03-18 00:10:43 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
move_forward( sig_size );
|
|
|
|
rewind += sig_size;
|
2023-03-18 00:10:43 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
// Ignore whitespace
|
|
|
|
while ( zpl_char_is_space( src[0] ) || src[0] == '\"' || src[0] == '<' )
|
|
|
|
{
|
|
|
|
move_forward( 1 );
|
|
|
|
rewind++;
|
|
|
|
}
|
2023-03-18 00:10:43 -07:00
|
|
|
|
2023-03-30 10:59:49 -07:00
|
|
|
for ( ; includes_left; includes_left--, include++ )
|
|
|
|
{
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_string_clear( current );
|
2023-03-25 19:44:36 -07:00
|
|
|
|
|
|
|
u32 sig_length = zpl_string_length( include->Sig );
|
2023-03-30 10:59:49 -07:00
|
|
|
current = zpl_string_append_length( current, src, sig_length );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
if ( zpl_string_are_equal( include->Sig, current ) )
|
|
|
|
{
|
|
|
|
Token entry {};
|
|
|
|
|
|
|
|
entry.Start = pos;
|
2023-03-18 00:10:43 -07:00
|
|
|
entry.End = pos + sig_length;
|
2023-03-16 23:09:19 -07:00
|
|
|
entry.Sig = include->Sig;
|
|
|
|
|
|
|
|
if ( include->Sub != nullptr )
|
|
|
|
{
|
|
|
|
entry.Sub = include->Sub;
|
|
|
|
buffer_size += zpl_string_length( entry.Sub) - sig_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
zpl_array_append( tokens, entry );
|
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
log_fmt("\nFound %-81s line %d, column %d", current, line, col );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
2023-03-25 19:44:36 -07:00
|
|
|
// The + 1 is for the closing " or > of the include
|
|
|
|
move_forward( sig_length + 1 );
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
// Force end of line.
|
2023-03-18 00:10:43 -07:00
|
|
|
while ( src[0] != '\n' )
|
2023-03-16 23:09:19 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
move_forward( 1 );
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
goto Skip;
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
2023-03-30 10:59:49 -07:00
|
|
|
|
|
|
|
move_back( rewind );
|
|
|
|
}
|
|
|
|
while (false);
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
// Words to match
|
|
|
|
{
|
|
|
|
Spec::Entry* word = Spec::Words;
|
|
|
|
sw words_left = zpl_array_count ( Spec::Words);
|
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
for ( ; words_left; words_left--, word++ )
|
2023-03-16 23:09:19 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
if ( word->Sig[0] != src[0] )
|
2023-03-16 23:09:19 -07:00
|
|
|
continue;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
zpl_string_clear( current );
|
|
|
|
|
|
|
|
sw sig_length = zpl_string_length( word->Sig);
|
2023-03-17 15:12:20 -07:00
|
|
|
current = zpl_string_append_length( current, src, sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
if ( zpl_string_are_equal( word->Sig, current ) )
|
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
char before = src[-1];
|
|
|
|
char after = src[sig_length];
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
if ( zpl_char_is_alphanumeric( before ) || before == '_'
|
|
|
|
|| zpl_char_is_alphanumeric( after ) || after == '_' )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token entry {};
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
entry.Start = pos;
|
|
|
|
entry.End = pos + sig_length;
|
2023-03-12 18:34:55 -07:00
|
|
|
entry.Sig = word->Sig;
|
|
|
|
|
|
|
|
if ( word->Sub != nullptr )
|
|
|
|
{
|
|
|
|
entry.Sub = word->Sub;
|
|
|
|
buffer_size += zpl_string_length( entry.Sub) - sig_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
zpl_array_append( tokens, entry );
|
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
log_fmt("\nFound %-81s line %d, column %d", current, line, col );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
move_forward( sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
goto Skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespaces to match
|
|
|
|
{
|
|
|
|
Spec::Entry* nspace = Spec::Namespaces;
|
|
|
|
|
|
|
|
sw nspaces_left = zpl_array_count( Spec::Namespaces);
|
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
for ( ; nspaces_left; nspaces_left--, nspace++ )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
if ( nspace->Sig[0] != src[0] )
|
2023-03-12 18:34:55 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
zpl_string_clear( current );
|
|
|
|
|
|
|
|
u32 sig_length = zpl_string_length( nspace->Sig );
|
2023-03-17 15:12:20 -07:00
|
|
|
current = zpl_string_append_length( current, src, sig_length );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
if ( zpl_string_are_equal( nspace->Sig, current ) )
|
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
u32 length = sig_length;
|
|
|
|
char const* ns_content = src + sig_length;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
while ( zpl_char_is_alphanumeric( ns_content[0] ) || ns_content[0] == '_' )
|
|
|
|
{
|
|
|
|
length++;
|
|
|
|
ns_content++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token entry {};
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
entry.Start = pos;
|
|
|
|
entry.End = pos + length;
|
2023-03-12 18:34:55 -07:00
|
|
|
entry.Sig = nspace->Sig;
|
|
|
|
|
|
|
|
buffer_size += sig_length;
|
|
|
|
|
|
|
|
if ( nspace->Sub != nullptr )
|
|
|
|
{
|
|
|
|
entry.Sub = nspace->Sub;
|
|
|
|
buffer_size += zpl_string_length( entry.Sub ) - length;
|
|
|
|
}
|
|
|
|
|
|
|
|
zpl_array_append( tokens, entry );
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
#if Build_Debug
|
2023-03-12 18:34:55 -07:00
|
|
|
zpl_string_clear( preview );
|
2023-03-17 15:12:20 -07:00
|
|
|
preview = zpl_string_append_length( preview, src, length);
|
|
|
|
log_fmt("\nFound %-40s %-40s line %d, column %d", preview, nspace->Sig, line, col );
|
2023-03-16 23:09:19 -07:00
|
|
|
#endif
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
move_forward( length );
|
2023-03-16 23:09:19 -07:00
|
|
|
goto Skip;
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Skip:
|
2023-03-17 15:12:20 -07:00
|
|
|
if ( src[0] == '\n' )
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
|
2023-03-25 19:44:36 -07:00
|
|
|
move_forward( 1 );
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
2023-03-25 19:44:36 -07:00
|
|
|
while ( left );
|
|
|
|
End_Search:
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-18 00:10:43 -07:00
|
|
|
if (zpl_array_count( tokens ) == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Prep data for building the content
|
2023-03-17 15:12:20 -07:00
|
|
|
left = zpl_array_count( tokens);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
char* content = IO::Current_Content;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_string refactored = zpl_string_make_reserve( zpl_arena_allocator( & Refactor_Buffer ), buffer_size );
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Generate the refactored file content.
|
|
|
|
{
|
|
|
|
Token* entry = tokens;
|
|
|
|
sw previous_end = 0;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
do
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
|
|
|
sw segment_length = entry->Start - previous_end;
|
2023-03-16 23:09:19 -07:00
|
|
|
sw sig_length = zpl_string_length( entry->Sig );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
// Append between tokens
|
2023-03-17 15:12:20 -07:00
|
|
|
refactored = zpl_string_append_length( refactored, content, segment_length );
|
|
|
|
content += segment_length + sig_length;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
segment_length = entry->End - entry->Start - sig_length;
|
|
|
|
|
|
|
|
// Append token
|
|
|
|
if ( entry->Sub )
|
2023-03-17 15:12:20 -07:00
|
|
|
{
|
2023-03-25 19:44:36 -07:00
|
|
|
refactored = zpl_string_append( refactored, entry->Sub );
|
2023-03-17 15:12:20 -07:00
|
|
|
}
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-17 15:12:20 -07:00
|
|
|
refactored = zpl_string_append_length( refactored, content, segment_length );
|
|
|
|
content += segment_length;
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
previous_end = entry->End;
|
|
|
|
entry++;
|
|
|
|
}
|
2023-03-18 00:10:43 -07:00
|
|
|
while ( --left > 0 );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
|
|
|
entry--;
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
if ( entry->End < IO::Current_Size )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
2023-03-17 15:12:20 -07:00
|
|
|
refactored = zpl_string_append_length( refactored, content, IO::Current_Size - 1 - entry->End );
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
IO::write( refactored );
|
2023-03-17 15:12:20 -07:00
|
|
|
|
2023-03-18 00:10:43 -07:00
|
|
|
zpl_free_all( zpl_arena_allocator( & Refactor_Buffer ));
|
2023-03-25 19:44:36 -07:00
|
|
|
|
|
|
|
#undef pos
|
|
|
|
#undef move_forward
|
2023-03-12 18:34:55 -07:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
int main( int num, char** arguments )
|
2023-03-12 18:34:55 -07:00
|
|
|
{
|
|
|
|
Memory::setup();
|
2023-03-16 23:09:19 -07:00
|
|
|
|
|
|
|
parse_options( num, arguments);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
IO::prepare();
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
// Just reserving more than we'll ever problably need.
|
|
|
|
zpl_arena_init_from_allocator( & Refactor_Buffer, zpl_heap(), IO::Largest_Src_Size * 4 + 8);
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
Spec::parse();
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
sw left = zpl_array_count( IO::Sources );
|
|
|
|
do
|
|
|
|
{
|
|
|
|
refactor();
|
2023-03-17 15:12:20 -07:00
|
|
|
|
|
|
|
zpl_printf("\nRefactored: %s", IO::Sources[IO::Current]);
|
2023-03-16 23:09:19 -07:00
|
|
|
}
|
|
|
|
while ( --left );
|
2023-03-12 18:34:55 -07:00
|
|
|
|
2023-03-16 23:09:19 -07:00
|
|
|
zpl_arena_free( & Refactor_Buffer );
|
2023-03-13 17:17:12 -07:00
|
|
|
|
2023-03-12 18:34:55 -07:00
|
|
|
Spec:: cleanup();
|
2023-03-16 23:09:19 -07:00
|
|
|
IO:: cleanup();
|
2023-03-12 18:34:55 -07:00
|
|
|
Memory::cleanup();
|
|
|
|
}
|