switched experimental operations over to token streams

master
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->count = count;
stream->start = round_down(pos, count); stream->start = round_down(pos, count);
stream->end = round_up(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); app->buffer_read_tokens(app, buffer, stream->start, stream->end, stream->base_tokens);
stream->tokens = stream->base_tokens - stream->start; stream->tokens = stream->base_tokens - stream->start;
@ -321,6 +322,19 @@ init_stream_tokens(Stream_Tokens *stream, Application_Links *app, Buffer_Summary
return(result); 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 static bool32
forward_stream_tokens(Stream_Tokens *stream){ forward_stream_tokens(Stream_Tokens *stream){
Application_Links *app = stream->app; Application_Links *app = stream->app;

View File

@ -319,127 +319,147 @@ CUSTOM_COMMAND_SIG(rename_parameter){
Partition *part = &global_part; Partition *part = &global_part;
Temp_Memory temp = begin_temp_memory(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){
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; Cpp_Get_Token_Result result;
old_lexeme.size = token_ptr->size; if (app->buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){
app->buffer_read_range(app, &buffer, token_ptr->start, if (!result.in_whitespace){
token_ptr->start+token_ptr->size, Cpp_Token stream_space[32];
old_lexeme.str); Stream_Tokens stream = {0};
int32_t proc_body_found = 0; if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){
for (++token_ptr; token_ptr < token_end; ++token_ptr){ int32_t token_index = result.token_index;
switch (token_ptr->type){ Cpp_Token token = stream.tokens[token_index];
case CPP_TOKEN_BRACE_OPEN:
{
proc_body_found = 1;
goto doublebreak;
}break;
case CPP_TOKEN_BRACE_CLOSE: if (token.type == CPP_TOKEN_IDENTIFIER){
case CPP_TOKEN_PARENTHESE_OPEN:
{
goto doublebreak;
}break;
}
}
doublebreak:;
if (proc_body_found){ char old_lexeme_base[128];
String old_lexeme = make_fixed_width_string(old_lexeme_base);
Query_Bar with; if (token.size < sizeof(old_lexeme_base)){
char with_space[1024]; Cpp_Token original_token = token;
with.prompt = make_lit_string("New Name: "); old_lexeme.size = token.size;
with.string = make_fixed_width_string(with_space); app->buffer_read_range(app, &buffer, token.start,
if (!query_user_string(app, &with)) return; token.start+token.size,
old_lexeme.str);
String replace_string = with.string; int32_t proc_body_found = 0;
int32_t still_looping = 0;
Cpp_Token *token_start_ptr = token_ptr+1; ++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:
{
proc_body_found = 1;
goto doublebreak;
}break;
Buffer_Edit *edits = (Buffer_Edit*)partition_current(part); case CPP_TOKEN_BRACE_CLOSE:
int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit); case CPP_TOKEN_PARENTHESE_OPEN:
int32_t edit_count = 0; {
goto doublebreak;
}break;
}
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
doublebreak:;
if (edit_max >= 1){ if (proc_body_found){
Buffer_Edit edit;
edit.str_start = 0;
edit.len = replace_string.size;
edit.start = original_token.start;
edit.end = original_token.start + original_token.size;
edits[edit_count] = edit; Query_Bar with;
++edit_count; char with_space[1024];
} with.prompt = make_lit_string("New Name: ");
with.string = make_fixed_width_string(with_space);
if (!query_user_string(app, &with)) return;
int32_t nesting_level = 0; String replace_string = with.string;
int32_t closed_correctly = 0;
token_ptr = token_start_ptr;
for (; token_ptr < token_end; ++token_ptr){
switch (token_ptr->type){
case CPP_TOKEN_IDENTIFIER:
{
if (token_ptr->size == old_lexeme.size){
char other_lexeme_base[128];
String other_lexeme = make_fixed_width_string(other_lexeme_base);
other_lexeme.size = old_lexeme.size;
app->buffer_read_range(app, &buffer, token_ptr->start,
token_ptr->start+token_ptr->size,
other_lexeme.str);
if (match(old_lexeme, other_lexeme)){ Buffer_Edit *edits = (Buffer_Edit*)partition_current(part);
Buffer_Edit edit; int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit);
edit.str_start = 0; int32_t edit_count = 0;
edit.len = replace_string.size;
edit.start = token_ptr->start;
edit.end = token_ptr->start + token_ptr->size;
if (edit_count < edit_max){ if (edit_max >= 1){
edits[edit_count] = edit; Buffer_Edit edit;
++edit_count; edit.str_start = 0;
} edit.len = replace_string.size;
else{ edit.start = original_token.start;
goto doublebreak2; edit.end = original_token.start + original_token.size;
}
edits[edit_count] = edit;
++edit_count;
}
int32_t nesting_level = 0;
int32_t closed_correctly = 0;
++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:
{
if (token_ptr->size == old_lexeme.size){
char other_lexeme_base[128];
String other_lexeme = make_fixed_width_string(other_lexeme_base);
other_lexeme.size = old_lexeme.size;
app->buffer_read_range(app, &buffer, token_ptr->start,
token_ptr->start+token_ptr->size,
other_lexeme.str);
if (match(old_lexeme, other_lexeme)){
Buffer_Edit edit;
edit.str_start = 0;
edit.len = replace_string.size;
edit.start = token_ptr->start;
edit.end = token_ptr->start + token_ptr->size;
if (edit_count < edit_max){
edits[edit_count] = edit;
++edit_count;
}
else{
goto doublebreak2;
}
}
}
}break;
case CPP_TOKEN_BRACE_OPEN:
{
++nesting_level;
}break;
case CPP_TOKEN_BRACE_CLOSE:
{
if (nesting_level == 0){
closed_correctly = 1;
goto doublebreak2;
}
else{
--nesting_level;
}
}break;
} }
} }
}break; still_looping = forward_stream_tokens(&stream);
}while(still_looping);
doublebreak2:;
case CPP_TOKEN_BRACE_OPEN: if (closed_correctly){
{ app->buffer_batch_edit(app, &buffer, replace_string.str, replace_string.size,
++nesting_level; edits, edit_count, BatchEdit_Normal);
}break; }
case CPP_TOKEN_BRACE_CLOSE:
{
if (nesting_level == 0){
closed_correctly = 1;
goto doublebreak2;
}
else{
--nesting_level;
}
}break;
} }
} }
doublebreak2:;
if (closed_correctly){
app->buffer_batch_edit(app, &buffer, replace_string.str, replace_string.size,
edits, edit_count, BatchEdit_Normal);
}
} }
} }
} }
} }
end_temp_memory(temp); end_temp_memory(temp);
} }
@ -451,108 +471,135 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
Partition *part = &global_part; Partition *part = &global_part;
Temp_Memory temp = begin_temp_memory(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};
int32_t closed_correctly = 0; if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){
Cpp_Token *token_seeker = token_ptr; int32_t token_index = result.token_index;
for (; token_seeker < token_end; ++token_seeker){ Cpp_Token token = stream.tokens[token_index];
switch (token_seeker->type){
case CPP_TOKEN_BRACE_CLOSE:
closed_correctly = 1;
goto finished_seek;
case CPP_TOKEN_BRACE_OPEN: if (token.type == CPP_TOKEN_BRACE_OPEN){
goto finished_seek;
}
}
finished_seek:;
if (closed_correctly){ ++token_index;
int32_t count_estimate = 1 + (int32_t)(token_seeker - token_ptr)/2;
Buffer_Edit *edits = push_array(part, Buffer_Edit, count_estimate); int32_t closed_correctly = 0;
int32_t edit_count = 0; int32_t seeker_index = token_index;
Stream_Tokens seek_stream = begin_temp_stream_token(&stream);
char *string_base = (char*)partition_current(part); int32_t still_looping = 0;
String string = make_string(string_base, 0, partition_remaining(part)); 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;
goto finished_seek;
int32_t value = 0; case CPP_TOKEN_BRACE_OPEN:
closed_correctly = 0; goto finished_seek;
for (;token_ptr < token_end; ++token_ptr){ }
switch (token_ptr->type){ }
case CPP_TOKEN_IDENTIFIER: still_looping = forward_stream_tokens(&seek_stream);
{ }while(still_looping);
int32_t edit_start = token_ptr->start + token_ptr->size; finished_seek:;
int32_t edit_stop = edit_start; end_temp_stream_token(&stream, seek_stream);
int32_t edit_is_good = 0; if (closed_correctly){
for (++token_ptr; token_ptr < token_end; ++token_ptr){ 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;
char *string_base = (char*)partition_current(part);
String string = make_string(string_base, 0, partition_remaining(part));
int32_t value = 0;
closed_correctly = 0;
still_looping = 0;
do{
for (;token_index < stream.end; ++token_index){
Cpp_Token *token_ptr = stream.tokens + token_index;
switch (token_ptr->type){ switch (token_ptr->type){
case CPP_TOKEN_COMMA: case CPP_TOKEN_IDENTIFIER:
{ {
edit_stop = token_ptr->start; int32_t edit_start = token_ptr->start + token_ptr->size;
edit_is_good = 1; int32_t edit_stop = edit_start;
goto good_edit;
int32_t edit_is_good = 0;
++token_index;
do{
for (; token_index < stream.end; ++token_index){
token_ptr = stream.tokens + token_index;
switch (token_ptr->type){
case CPP_TOKEN_COMMA:
{
edit_stop = token_ptr->start;
edit_is_good = 1;
goto good_edit;
}break;
case CPP_TOKEN_BRACE_CLOSE:
{
edit_stop = token_ptr->start;
closed_correctly = 1;
edit_is_good = 1;
goto good_edit;
}break;
}
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
good_edit:;
if (edit_is_good){
int32_t str_pos = string.size;
append(&string, " = ");
append_int_to_str(&string, value);
if (closed_correctly){
append(&string, "\n");
}
++value;
int32_t str_size = string.size - str_pos;
Buffer_Edit edit;
edit.str_start = str_pos;
edit.len = str_size;
edit.start = edit_start;
edit.end = edit_stop;
assert(edit_count < count_estimate);
edits[edit_count] = edit;
++edit_count;
}
if (!edit_is_good || closed_correctly){
goto finished;
}
}break; }break;
case CPP_TOKEN_BRACE_CLOSE: case CPP_TOKEN_BRACE_CLOSE:
{ {
edit_stop = token_ptr->start;
closed_correctly = 1; closed_correctly = 1;
edit_is_good = 1; goto finished;
goto good_edit;
}break; }break;
} }
} }
good_edit:; still_looping = forward_stream_tokens(&stream);
if (edit_is_good){ }while(still_looping);
int32_t str_pos = string.size;
append(&string, " = "); finished:;
append_int_to_str(&string, value); if (closed_correctly){
if (closed_correctly){ app->buffer_batch_edit(app, &buffer, string_base, string.size,
append(&string, "\n"); edits, edit_count, BatchEdit_Normal);
} }
++value;
int32_t str_size = string.size - str_pos;
Buffer_Edit edit;
edit.str_start = str_pos;
edit.len = str_size;
edit.start = edit_start;
edit.end = edit_stop;
assert(edit_count < count_estimate);
edits[edit_count] = edit;
++edit_count;
}
if (!edit_is_good || closed_correctly){
goto finished;
}
}break;
case CPP_TOKEN_BRACE_CLOSE:
{
closed_correctly = 1;
goto finished;
}break;
} }
} }
finished:;
if (closed_correctly){
app->buffer_batch_edit(app, &buffer, string_base, string.size,
edits, edit_count, BatchEdit_Normal);
}
} }
} }
} }