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); Cpp_Get_Token_Result result;
if (!result.in_whitespace && result.token_index < array.count){ if (app->buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){
Cpp_Token *token_ptr = &array.tokens[result.token_index]; if (!result.in_whitespace){
Cpp_Token *token_end = array.tokens + array.count - 1; Cpp_Token stream_space[32];
if (token_ptr->type == CPP_TOKEN_IDENTIFIER){ Stream_Tokens stream = {0};
char old_lexeme_base[128];
String old_lexeme = make_fixed_width_string(old_lexeme_base); if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){
if (token_ptr->size < sizeof(old_lexeme_base)){ int32_t token_index = result.token_index;
Cpp_Token token = stream.tokens[token_index];
Cpp_Token original_token = *token_ptr; if (token.type == CPP_TOKEN_IDENTIFIER){
old_lexeme.size = token_ptr->size;
app->buffer_read_range(app, &buffer, token_ptr->start, char old_lexeme_base[128];
token_ptr->start+token_ptr->size, String old_lexeme = make_fixed_width_string(old_lexeme_base);
old_lexeme.str);
if (token.size < sizeof(old_lexeme_base)){
int32_t proc_body_found = 0; Cpp_Token original_token = token;
for (++token_ptr; token_ptr < token_end; ++token_ptr){ old_lexeme.size = token.size;
switch (token_ptr->type){ app->buffer_read_range(app, &buffer, token.start,
case CPP_TOKEN_BRACE_OPEN: token.start+token.size,
{ old_lexeme.str);
proc_body_found = 1;
goto doublebreak;
}break;
case CPP_TOKEN_BRACE_CLOSE: int32_t proc_body_found = 0;
case CPP_TOKEN_PARENTHESE_OPEN: int32_t still_looping = 0;
{
goto doublebreak;
}break;
}
}
doublebreak:;
if (proc_body_found){
Query_Bar with;
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;
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;
if (edit_max >= 1){
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; ++token_index;
++edit_count; do{
} for (; token_index < stream.end; ++token_index){
Cpp_Token *token_ptr = stream.tokens + token_index;
int32_t nesting_level = 0; switch (token_ptr->type){
int32_t closed_correctly = 0; case CPP_TOKEN_BRACE_OPEN:
token_ptr = token_start_ptr; {
for (; token_ptr < token_end; ++token_ptr){ proc_body_found = 1;
switch (token_ptr->type){ goto doublebreak;
case CPP_TOKEN_IDENTIFIER: }break;
{
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)){ case CPP_TOKEN_BRACE_CLOSE:
Buffer_Edit edit; case CPP_TOKEN_PARENTHESE_OPEN:
edit.str_start = 0; {
edit.len = replace_string.size; goto doublebreak;
edit.start = token_ptr->start; }break;
edit.end = token_ptr->start + token_ptr->size; }
}
still_looping = forward_stream_tokens(&stream);
}while(still_looping);
doublebreak:;
if (proc_body_found){
Query_Bar with;
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;
String replace_string = with.string;
Buffer_Edit *edits = (Buffer_Edit*)partition_current(part);
int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit);
int32_t edit_count = 0;
if (edit_max >= 1){
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;
++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;
if (edit_count < edit_max){ case CPP_TOKEN_BRACE_OPEN:
edits[edit_count] = edit; {
++edit_count; ++nesting_level;
} }break;
else{
goto doublebreak2; 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); Cpp_Get_Token_Result result;
if (!result.in_whitespace && result.token_index < array.count){ if (app->buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){
Cpp_Token *token_ptr = &array.tokens[result.token_index]; if (!result.in_whitespace){
Cpp_Token *token_end = array.tokens + array.count; Cpp_Token stream_space[32];
if (token_ptr->type == CPP_TOKEN_BRACE_OPEN){ Stream_Tokens stream = {0};
++token_ptr; if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){
int32_t token_index = result.token_index;
int32_t closed_correctly = 0; Cpp_Token token = stream.tokens[token_index];
Cpp_Token *token_seeker = token_ptr;
for (; token_seeker < token_end; ++token_seeker){ if (token.type == CPP_TOKEN_BRACE_OPEN){
switch (token_seeker->type){
case CPP_TOKEN_BRACE_CLOSE:
closed_correctly = 1;
goto finished_seek;
case CPP_TOKEN_BRACE_OPEN: ++token_index;
goto finished_seek;
} int32_t closed_correctly = 0;
} int32_t seeker_index = token_index;
finished_seek:; Stream_Tokens seek_stream = begin_temp_stream_token(&stream);
if (closed_correctly){ int32_t still_looping = 0;
int32_t count_estimate = 1 + (int32_t)(token_seeker - token_ptr)/2; do{
for (; seeker_index < seek_stream.end; ++seeker_index){
Buffer_Edit *edits = push_array(part, Buffer_Edit, count_estimate); Cpp_Token *token_seeker = seek_stream.tokens + seeker_index;
int32_t edit_count = 0; switch (token_seeker->type){
case CPP_TOKEN_BRACE_CLOSE:
char *string_base = (char*)partition_current(part); closed_correctly = 1;
String string = make_string(string_base, 0, partition_remaining(part)); 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, " = ");
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;
case CPP_TOKEN_BRACE_CLOSE: finished:;
{ if (closed_correctly){
closed_correctly = 1; app->buffer_batch_edit(app, &buffer, string_base, string.size,
goto finished; edits, edit_count, BatchEdit_Normal);
}break; }
} }
} }
finished:;
if (closed_correctly){
app->buffer_batch_edit(app, &buffer, string_base, string.size,
edits, edit_count, BatchEdit_Normal);
}
} }
} }
} }