switched experimental operations over to token streams

This commit is contained in:
Allen Webster 2016-09-16 10:10:45 -04:00
parent bc4905b83c
commit 7a23c72d11
2 changed files with 254 additions and 193 deletions

View File

@ -312,6 +312,7 @@ init_stream_tokens(Stream_Tokens *stream, Application_Links *app, Buffer_Summary
stream->count = count;
stream->start = round_down(pos, count);
stream->end = round_up(pos, count);
stream->token_count = token_count;
app->buffer_read_tokens(app, buffer, stream->start, stream->end, stream->base_tokens);
stream->tokens = stream->base_tokens - stream->start;
@ -321,6 +322,19 @@ init_stream_tokens(Stream_Tokens *stream, Application_Links *app, Buffer_Summary
return(result);
}
static Stream_Tokens
begin_temp_stream_token(Stream_Tokens *stream){
return(*stream);
}
static void
end_temp_stream_token(Stream_Tokens *stream, Stream_Tokens temp){
if (stream->start != temp.start || stream->end != temp.end){
Application_Links *app = stream->app;
app->buffer_read_tokens(app, stream->buffer, stream->start, stream->end, stream->base_tokens);
}
}
static bool32
forward_stream_tokens(Stream_Tokens *stream){
Application_Links *app = stream->app;

View File

@ -319,24 +319,36 @@ CUSTOM_COMMAND_SIG(rename_parameter){
Partition *part = &global_part;
Temp_Memory temp = begin_temp_memory(part);
Cpp_Token_Array array = buffer_get_all_tokens(app, part, &buffer);
Cpp_Get_Token_Result result = cpp_get_token(&array, view.cursor.pos);
if (!result.in_whitespace && result.token_index < array.count){
Cpp_Token *token_ptr = &array.tokens[result.token_index];
Cpp_Token *token_end = array.tokens + array.count - 1;
if (token_ptr->type == CPP_TOKEN_IDENTIFIER){
Cpp_Get_Token_Result result;
if (app->buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){
if (!result.in_whitespace){
Cpp_Token stream_space[32];
Stream_Tokens stream = {0};
if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){
int32_t token_index = result.token_index;
Cpp_Token token = stream.tokens[token_index];
if (token.type == CPP_TOKEN_IDENTIFIER){
char old_lexeme_base[128];
String old_lexeme = make_fixed_width_string(old_lexeme_base);
if (token_ptr->size < sizeof(old_lexeme_base)){
Cpp_Token original_token = *token_ptr;
old_lexeme.size = token_ptr->size;
app->buffer_read_range(app, &buffer, token_ptr->start,
token_ptr->start+token_ptr->size,
if (token.size < sizeof(old_lexeme_base)){
Cpp_Token original_token = token;
old_lexeme.size = token.size;
app->buffer_read_range(app, &buffer, token.start,
token.start+token.size,
old_lexeme.str);
int32_t proc_body_found = 0;
for (++token_ptr; token_ptr < token_end; ++token_ptr){
int32_t still_looping = 0;
++token_index;
do{
for (; token_index < stream.end; ++token_index){
Cpp_Token *token_ptr = stream.tokens + token_index;
switch (token_ptr->type){
case CPP_TOKEN_BRACE_OPEN:
{
@ -351,6 +363,8 @@ CUSTOM_COMMAND_SIG(rename_parameter){
}break;
}
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
doublebreak:;
if (proc_body_found){
@ -363,8 +377,6 @@ CUSTOM_COMMAND_SIG(rename_parameter){
String replace_string = with.string;
Cpp_Token *token_start_ptr = token_ptr+1;
Buffer_Edit *edits = (Buffer_Edit*)partition_current(part);
int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit);
int32_t edit_count = 0;
@ -382,8 +394,11 @@ CUSTOM_COMMAND_SIG(rename_parameter){
int32_t nesting_level = 0;
int32_t closed_correctly = 0;
token_ptr = token_start_ptr;
for (; token_ptr < token_end; ++token_ptr){
++token_index;
still_looping = 0;
do{
for (; token_index < stream.end; ++token_index){
Cpp_Token *token_ptr = stream.tokens + token_index;
switch (token_ptr->type){
case CPP_TOKEN_IDENTIFIER:
{
@ -430,6 +445,8 @@ CUSTOM_COMMAND_SIG(rename_parameter){
}break;
}
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
doublebreak2:;
if (closed_correctly){
@ -440,6 +457,9 @@ CUSTOM_COMMAND_SIG(rename_parameter){
}
}
}
}
}
end_temp_memory(temp);
}
@ -451,18 +471,29 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
Partition *part = &global_part;
Temp_Memory temp = begin_temp_memory(part);
Cpp_Token_Array array = buffer_get_all_tokens(app, part, &buffer);
Cpp_Get_Token_Result result = cpp_get_token(&array, view.cursor.pos);
if (!result.in_whitespace && result.token_index < array.count){
Cpp_Token *token_ptr = &array.tokens[result.token_index];
Cpp_Token *token_end = array.tokens + array.count;
if (token_ptr->type == CPP_TOKEN_BRACE_OPEN){
++token_ptr;
Cpp_Get_Token_Result result;
if (app->buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){
if (!result.in_whitespace){
Cpp_Token stream_space[32];
Stream_Tokens stream = {0};
if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){
int32_t token_index = result.token_index;
Cpp_Token token = stream.tokens[token_index];
if (token.type == CPP_TOKEN_BRACE_OPEN){
++token_index;
int32_t closed_correctly = 0;
Cpp_Token *token_seeker = token_ptr;
for (; token_seeker < token_end; ++token_seeker){
int32_t seeker_index = token_index;
Stream_Tokens seek_stream = begin_temp_stream_token(&stream);
int32_t still_looping = 0;
do{
for (; seeker_index < seek_stream.end; ++seeker_index){
Cpp_Token *token_seeker = seek_stream.tokens + seeker_index;
switch (token_seeker->type){
case CPP_TOKEN_BRACE_CLOSE:
closed_correctly = 1;
@ -472,10 +503,13 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
goto finished_seek;
}
}
still_looping = forward_stream_tokens(&seek_stream);
}while(still_looping);
finished_seek:;
end_temp_stream_token(&stream, seek_stream);
if (closed_correctly){
int32_t count_estimate = 1 + (int32_t)(token_seeker - token_ptr)/2;
int32_t count_estimate = 1 + (seeker_index - token_index)/2;
Buffer_Edit *edits = push_array(part, Buffer_Edit, count_estimate);
int32_t edit_count = 0;
@ -485,7 +519,10 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
int32_t value = 0;
closed_correctly = 0;
for (;token_ptr < token_end; ++token_ptr){
still_looping = 0;
do{
for (;token_index < stream.end; ++token_index){
Cpp_Token *token_ptr = stream.tokens + token_index;
switch (token_ptr->type){
case CPP_TOKEN_IDENTIFIER:
{
@ -493,7 +530,10 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
int32_t edit_stop = edit_start;
int32_t edit_is_good = 0;
for (++token_ptr; token_ptr < token_end; ++token_ptr){
++token_index;
do{
for (; token_index < stream.end; ++token_index){
token_ptr = stream.tokens + token_index;
switch (token_ptr->type){
case CPP_TOKEN_COMMA:
{
@ -511,6 +551,8 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
}break;
}
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
good_edit:;
if (edit_is_good){
@ -548,6 +590,9 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
}
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
finished:;
if (closed_correctly){
app->buffer_batch_edit(app, &buffer, string_base, string.size,
@ -556,6 +601,8 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
}
}
}
}
}
end_temp_memory(temp);
}