diff --git a/Test/zpl.refactor b/Test/zpl.refactor index c5172bb..d8737fe 100644 --- a/Test/zpl.refactor +++ b/Test/zpl.refactor @@ -9,9 +9,17 @@ __VERSION 1 // Precedence (highest to lowest): // word, namespace, regex +// Comments +not comments + // Header files -not include zpl_hedley -//not word zpl_hedley +not include zpl_hedley.h +not include allocator.h +not include array.h +not include header/essentials/collections/array.h +not include header/essentials/collections/list.h +not include header/core/file.h +not include header/opts.h // Removes the namespace. namespace zpl_ diff --git a/project/Spec.cpp b/project/Spec.cpp index 3009758..83d9015 100644 --- a/project/Spec.cpp +++ b/project/Spec.cpp @@ -11,6 +11,9 @@ namespace Spec namespace StaticData { + // Custom comment signatures not supported yet (only C/C++ comments for now) + bool Ignore_Comments = false; + Array_Entry Ignore_Includes; Array_Entry Ignore_Words; Array_Entry Ignore_Regexes; @@ -49,7 +52,9 @@ namespace Spec // Allows for '.' while ( zpl_char_is_alphanumeric( current ) || current == '_' - || current == '.' ) + || current == '.' + || current == '/' + || current == '\\' ) { length++; } @@ -161,7 +166,15 @@ namespace Spec find_next_token( type, token, line, length ); } - if ( is_tok( Tok::Word, token, length ) ) + if ( is_tok( Tok::Comment, token, length ) ) + { + // Custom comment signatures not supported yet (only C/C++ comments for now) + Ignore_Comments = true; + + lines++; + continue; + } + else if ( is_tok( Tok::Word, token, length ) ) { type = Tok::Word; } @@ -324,5 +337,15 @@ namespace Spec } } while ( --left ); + + Spec::Entry* ignore = Spec::Ignore_Includes; + sw ignores_left = zpl_array_count( Spec::Ignore_Includes); + + zpl_printf("\nIgnores: "); + for ( ; ignores_left; ignores_left--, ignore++ ) + { + zpl_printf("\n%s", ignore->Sig); + } + zpl_printf("\n"); } } diff --git a/project/Spec.hpp b/project/Spec.hpp index c3e158d..1e855d0 100644 --- a/project/Spec.hpp +++ b/project/Spec.hpp @@ -7,6 +7,7 @@ namespace Spec enum Tok { Not, + Comment, Include, Namespace, Word, @@ -21,6 +22,7 @@ namespace Spec char const* tok_to_str[ Tok::Num_Tok ] = { "not", + "comments", "include", "namespace", "word", @@ -36,6 +38,7 @@ namespace Spec const u8 tok_to_len[ Tok::Num_Tok ] = { 3, + 8, 7, 9, 4, diff --git a/project/refactor.cpp b/project/refactor.cpp index b601e98..b1ab0db 100644 --- a/project/refactor.cpp +++ b/project/refactor.cpp @@ -216,12 +216,41 @@ void refactor() #define pos (IO::Current_Size - left) #define move_forward( Amount_ ) \ - left -= Amount_; \ - col += Amount_; \ - src += Amount_ \ + if ( left - Amount_ <= 0 ) \ + goto End_Search; \ + \ + left -= Amount_; \ + col += Amount_; \ + src += Amount_ \ do { + 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 + { + move_forward( 2 ); + } + while ( (left - 2) > 0 && !( src[0] == '*' && src[1] == '/' ) ); + + move_forward( 2 ); + } + } + // Includes to ignore { Spec::Entry* ignore = Spec::Ignore_Includes; @@ -229,26 +258,34 @@ void refactor() for ( ; ignores_left; ignores_left--, ignore++ ) { - if ( include_sig[0] != src[0] ) + if ( '#' != src[0] ) continue; + move_forward( 1 ); + + // Ignore whitespace + while ( zpl_char_is_space( src[0] ) ) + { + move_forward( 1 ); + } + if ( zpl_strncmp( include_sig, src, sizeof(include_sig) - 1 ) != 0 ) { break; } - src += sizeof(include_sig) - 1; + move_forward( sizeof(include_sig) - 1 ); // Ignore whitespace while ( zpl_char_is_space( src[0] ) || src[0] == '\"' || src[0] == '<' ) { - src++; + move_forward(1); } - u32 sig_length = zpl_string_length( ignore->Sig ); - zpl_string_clear( current ); - current = zpl_string_append_length( current, ignore->Sig, sig_length ); + + u32 sig_length = zpl_string_length( ignore->Sig ); + current = zpl_string_append_length( current, ignore->Sig, sig_length ); if ( zpl_string_are_equal( ignore->Sig, current ) ) { @@ -353,9 +390,17 @@ void refactor() for ( ; includes_left; includes_left--, include++ ) { - if ( include_sig[0] != src[0] ) + if ( '#' != src[0] ) continue; + move_forward( 1 ); + + // Ignore whitespace + while ( zpl_char_is_space( src[0] ) ) + { + move_forward( 1 ); + } + if ( zpl_strncmp( include_sig, src, sizeof(include_sig) - 1 ) != 0 ) { break; @@ -366,13 +411,13 @@ void refactor() // Ignore whitespace while ( zpl_char_is_space( src[0] ) || src[0] == '\"' || src[0] == '<' ) { - src++; + move_forward( 1 ); } - u32 sig_length = zpl_string_length( include->Sig ); - zpl_string_clear( current ); - current = zpl_string_append_length( current, include->Sig, sig_length ); + + u32 sig_length = zpl_string_length( include->Sig ); + current = zpl_string_append_length( current, include->Sig, sig_length ); if ( zpl_string_are_equal( include->Sig, current ) ) { @@ -392,7 +437,8 @@ void refactor() log_fmt("\nFound %-81s line %d, column %d", current, line, col ); - move_forward( sig_length ); + // The + 1 is for the closing " or > of the include + move_forward( sig_length + 1 ); // Force end of line. while ( src[0] != '\n' ) @@ -515,9 +561,10 @@ void refactor() col = 0; } - src++; + move_forward( 1 ); } - while ( --left ); + while ( left ); +End_Search: if (zpl_array_count( tokens ) == 0) { @@ -550,7 +597,7 @@ void refactor() // Append token if ( entry->Sub ) { - refactored = zpl_string_append( refactored, entry->Sub ); + refactored = zpl_string_append( refactored, entry->Sub ); } refactored = zpl_string_append_length( refactored, content, segment_length ); @@ -572,6 +619,9 @@ void refactor() IO::write( refactored ); zpl_free_all( zpl_arena_allocator( & Refactor_Buffer )); + + #undef pos + #undef move_forward } int main( int num, char** arguments ) diff --git a/refactor.10x b/refactor.10x index fc30d17..c3febc7 100644 --- a/refactor.10x +++ b/refactor.10x @@ -2,7 +2,7 @@ *.*, - *.obj,*.lib,*.pch,*.dll,*.pdb,.vs,Debug,Release,x64,obj,*.user,Intermediate, + *.obj,*.lib,*.pch,*.dll,*.pdb,.vs,Debug,Release,x64,obj,*.user,Intermediate,.git,.idea,.vscode, true true true @@ -36,17 +36,17 @@ C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um - .\thirdparty - ZPL_IMPLEMENTATION + Debug:x64 - C:\projects\refactor\thirdparty + ./project + ./thirdparty