Non-include refactors work. Multi-file works.

This commit is contained in:
Edward R. Gonzalez 2023-03-18 03:10:43 -04:00
parent 97967e56d9
commit d0fad572bc
6 changed files with 104 additions and 45 deletions

View File

@ -8,6 +8,7 @@ Refactor c/c++ files (and problably others) with ease.
* `-src` : Source file to refactor
* `-dst` : Destination file after the refactor (omit to use the same as source)
* `-spec` : Specification containing rules to use for the refactor.
* `-debug` : Use only if on debug build and desire to attach to process.
## Syntax :
@ -45,3 +46,4 @@ TODO:
* Test to see how much needs to be ported for other platforms (if at all)
* Setup as api.
* Provide binaries in the release page for github. (debug and release builds)
* Directive to ignore comments (with a way to specify the comment signature). Right now comments that meet the signature of words or namespaces are refactored.

View File

@ -9,7 +9,7 @@ namespace Memory
void setup()
{
zpl_arena_init_from_allocator( & Global_Arena, zpl_heap(), zpl_megabytes(2) );
zpl_arena_init_from_allocator( & Global_Arena, zpl_heap(), Initial_Reserve );
if ( Global_Arena.total_size == 0 )
{

View File

@ -18,7 +18,8 @@ namespace IO
uw Current_Size = 0;
uw Largest_Src_Size = 0;
zpl_arena MemPerist;
zpl_arena MemSpec;
zpl_arena MemSrc;
}
using namespace StaticData;
@ -49,16 +50,17 @@ namespace IO
zpl_file_close( & src );
}
while ( left--, left > 1 );
while ( path++, left--, left > 1 );
uw persist_size = ZPL_ARRAY_GROW_FORMULA( Largest_Src_Size );
uw persist_size = Largest_Src_Size * 2 + 8;
zpl_arena_init_from_allocator( & MemPerist, zpl_heap(), persist_size );
zpl_arena_init_from_allocator( & MemSrc, zpl_heap(), persist_size );
}
void cleanup()
{
zpl_arena_free( & MemPerist );
zpl_arena_free( & MemSpec );
zpl_arena_free( & MemSrc );
}
Array_Line get_specification()
@ -78,22 +80,23 @@ namespace IO
fatal("No content in specificaiton to process");
}
char* content = rcast( char*, zpl_alloc( zpl_arena_allocator( & MemPerist), fsize + 1) );
zpl_arena_init_from_allocator( & MemSpec, zpl_heap(), fsize * 2 + 8 );
char* content = rcast( char*, zpl_alloc( zpl_arena_allocator( & MemSpec), fsize + 1) );
zpl_file_read( & file, content, fsize);
zpl_file_close( & file );
content[fsize] = 0;
Array_Line lines = zpl_str_split_lines( zpl_arena_allocator( & MemPerist ), content, false );
Array_Line lines = zpl_str_split_lines( zpl_arena_allocator( & MemSpec ), content, false );
return lines;
}
char* get_next_source()
{
zpl_memset( MemPerist.physical_start, 0, MemPerist.total_allocated);
MemPerist.total_allocated = 0;
MemPerist.temp_count = 0;
zpl_memset( MemSrc.physical_start, 0, MemSrc.total_allocated);
zpl_free_all( zpl_arena_allocator( & MemSrc) );
Current++;
@ -111,7 +114,7 @@ namespace IO
if ( Current_Size <= 0 )
return nullptr;
Current_Content = rcast( char* , zpl_alloc( zpl_arena_allocator( & MemPerist), Current_Size + 1) );
Current_Content = rcast( char* , zpl_alloc( zpl_arena_allocator( & MemSrc), Current_Size + 1) );
zpl_file_read( & file, Current_Content, Current_Size );
zpl_file_close( & file );
@ -138,7 +141,6 @@ namespace IO
}
zpl_file_write( & file_dest, refacotred, zpl_string_length(refacotred) );
zpl_file_close( & file_dest );
}
}

View File

@ -38,15 +38,30 @@ namespace Spec
// Helper function for process().
forceinline
void find_next_token( zpl_string& token, char*& line, u32& length )
void find_next_token( Tok& type, zpl_string& token, char*& line, u32& length )
{
zpl_string_clear( token );
length = 0;
while ( zpl_char_is_alphanumeric( line[length] ) || line[length] == '_' )
#define current line[length]
if (type == Tok::Include)
{
length++;
// Allows for '.'
while ( zpl_char_is_alphanumeric( current )
|| current == '_'
|| current == '.' )
{
length++;
}
}
else
{
while ( zpl_char_is_alphanumeric( current ) || current == '_' )
{
length++;
}
}
#undef current
if ( length == 0 )
{
@ -119,15 +134,15 @@ namespace Spec
}
}
u32 length = 0;
// Find a valid token
find_next_token( token, line, length );
u32 length = 0;
Tok type = Tok::Num_Tok;
bool ignore = false;
Entry entry {};
// Find a valid token
find_next_token( type, token, line, length );
// Will be reguarded as an ignore.
if ( is_tok( Tok::Not, token, length ))
{
@ -143,7 +158,7 @@ namespace Spec
}
// Find the category token
find_next_token( token, line, length );
find_next_token( type, token, line, length );
}
if ( is_tok( Tok::Word, token, length ) )
@ -175,7 +190,7 @@ namespace Spec
continue;
}
find_next_token( token, line, length );
find_next_token( type, token, line, length );
// First argument is signature.
entry.Sig = zpl_string_make_length( g_allocator, token, length );
@ -285,7 +300,7 @@ namespace Spec
}
}
find_next_token( token, line, length );
find_next_token( type, token, line, length );
// Second argument is substitute.
entry.Sub = zpl_string_make_length( g_allocator, token, length );

View File

@ -90,6 +90,8 @@ ct char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
namespace Memory
{
ct uw Initial_Reserve = zpl_megabytes(2);
extern zpl_arena Global_Arena;
#define g_allocator zpl_arena_allocator( & Memory::Global_Arena)

View File

@ -9,16 +9,28 @@
void parse_options( int num, char** arguments )
{
zpl_opts opts;
zpl_opts_init( & opts, g_allocator, "refactor");
zpl_opts_init( & opts, zpl_heap(), "refactor");
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);
#if Build_Debug
zpl_opts_add( & opts, "debug", "debug", "Allows for wait to attach", ZPL_OPTS_FLAG);
#endif
if (opts_custom_compile( & opts, num, arguments))
{
sw num = 0;
#if Build_Debug
if ( zpl_opts_has_arg( & opts, "debug" ) )
{
zpl_printf("Will wait (pause available for attachment)");
char pause = getchar();
}
#endif
if ( zpl_opts_has_arg( & opts, "num" ) )
{
num = zpl_opts_integer( & opts, "num", -1 );
@ -158,7 +170,7 @@ zpl_arena Refactor_Buffer;
void refactor()
{
ct static char const* include_sig = "#include \"";
ct static char const* include_sig = "include";
struct Token
{
@ -220,12 +232,23 @@ void refactor()
if ( include_sig[0] != src[0] )
continue;
if ( zpl_strncmp( include_sig, src, sizeof(include_sig) - 1 ) != 0 )
{
break;
}
src += sizeof(include_sig) - 1;
// Ignore whitespace
while ( zpl_char_is_space( src[0] ) || src[0] == '\"' || src[0] == '<' )
{
src++;
}
u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_set( current, include_sig );
current = zpl_string_append_length( current, src, sig_length );
current = zpl_string_append_length( current, "\"", 1 );
// Formats current into: #include "<ignore->Sig>"
zpl_string_clear( current );
current = zpl_string_append_length( current, ignore->Sig, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
@ -233,7 +256,8 @@ void refactor()
const sw length = zpl_string_length( current );
move_forward( length );
// The + 1 is for the closing " or > of the include
move_forward( length + 1 );
// Force end of line.
while ( src[0] != '\n' )
@ -332,21 +356,30 @@ void refactor()
if ( include_sig[0] != src[0] )
continue;
if ( zpl_strncmp( include_sig, src, sizeof(include_sig) - 1 ) != 0 )
{
break;
}
src += sizeof(include_sig) - 1;
// Ignore whitespace
while ( zpl_char_is_space( src[0] ) || src[0] == '\"' || src[0] == '<' )
{
src++;
}
u32 sig_length = zpl_string_length( include->Sig );
current = zpl_string_set( current, include_sig );
current = zpl_string_append_length( current, src, sig_length );
current = zpl_string_append_length( current, "\"", 1 );
// Formats current into: #include "<ignore->Sig>"
zpl_string_clear( current );
current = zpl_string_append_length( current, include->Sig, sig_length );
if ( zpl_string_are_equal( include->Sig, current ) )
{
Token entry {};
const sw length = zpl_string_length( current );
entry.Start = pos;
entry.End = pos + length;
entry.End = pos + sig_length;
entry.Sig = include->Sig;
if ( include->Sub != nullptr )
@ -359,10 +392,10 @@ void refactor()
log_fmt("\nFound %-81s line %d, column %d", current, line, col );
move_forward( length );
move_forward( sig_length );
// Force end of line.
while ( src[0] != '\0' )
while ( src[0] != '\n' )
{
move_forward( 1 );
}
@ -486,14 +519,19 @@ void refactor()
}
while ( --left );
if (zpl_array_count( tokens ) == 0)
{
return;
}
// Prep data for building the content
left = zpl_array_count( tokens);
char* content = IO::Current_Content;
zpl_string refactored = zpl_string_make_reserve( zpl_arena_allocator( & Refactor_Buffer ), buffer_size );
// Generate the refactored file content.
static zpl_string
refactored = zpl_string_make_reserve( zpl_arena_allocator( & Refactor_Buffer ), buffer_size );
{
Token* entry = tokens;
sw previous_end = 0;
@ -521,7 +559,7 @@ void refactor()
previous_end = entry->End;
entry++;
}
while ( --left );
while ( --left > 0 );
entry--;
@ -533,7 +571,7 @@ void refactor()
IO::write( refactored );
zpl_string_clear( refactored );
zpl_free_all( zpl_arena_allocator( & Refactor_Buffer ));
}
int main( int num, char** arguments )