4coder/4coder_jump_parsing.cpp

371 lines
12 KiB
C++
Raw Normal View History

2016-07-13 19:59:42 +00:00
#ifndef FCODER_JUMP_PARSING
#define FCODER_JUMP_PARSING
struct Jump_Location{
String file;
2016-08-29 01:03:26 +00:00
int32_t line;
int32_t column;
2016-07-13 19:59:42 +00:00
};
static void
jump_to_location(Application_Links *app, View_Summary *view, Jump_Location *l){
view_open_file(app, view, l->file.str, l->file.size, false);
app->view_set_cursor(app, view, seek_line_char(l->line, l->column), true);
}
2016-08-29 01:03:26 +00:00
static int32_t
ms_style_verify(String line, int32_t paren_pos){
int32_t result = false;
2016-07-13 19:59:42 +00:00
String line_part = substr_tail(line, paren_pos);
if (match_part_sc(line_part, ") : ")){
2016-08-29 01:03:26 +00:00
result = true;
}
else if (match_part_sc(line_part, "): ")){
2016-08-29 01:03:26 +00:00
result = true;
}
return(result);
}
static int32_t
2016-07-13 19:59:42 +00:00
parse_error(String line, Jump_Location *location,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_errors, int32_t *colon_char){
int32_t result = false;
2016-07-17 02:24:13 +00:00
String original_line = line;
line = skip_chop_whitespace(line);
2016-08-29 01:03:26 +00:00
int32_t colon_pos = find_s_char(line, 0, ')');
2016-07-13 19:59:42 +00:00
if (ms_style_verify(line, colon_pos)){
colon_pos = find_s_char(line, colon_pos, ':');
2016-07-13 19:59:42 +00:00
if (colon_pos < line.size){
String location_str = substr(line, 0, colon_pos);
2016-07-17 02:24:13 +00:00
if (!(skip_sub_errors && original_line.str[0] == ' ')){
2016-07-13 19:59:42 +00:00
location_str = skip_chop_whitespace(location_str);
2016-08-29 01:03:26 +00:00
int32_t paren_pos = find_s_char(location_str, 0, '(');
2016-07-13 19:59:42 +00:00
if (paren_pos < location_str.size){
String file = substr(location_str, 0, paren_pos);
file = skip_chop_whitespace(file);
2016-08-29 01:03:26 +00:00
int32_t close_pos = find_s_char(location_str, 0, ')') + 1;
2016-07-13 19:59:42 +00:00
if (close_pos == location_str.size && file.size > 0){
String line_number = substr(location_str,
paren_pos+1,
close_pos-paren_pos-2);
line_number = skip_chop_whitespace(line_number);
if (line_number.size > 0){
location->file = file;
2016-08-29 01:03:26 +00:00
int32_t comma_pos = find_s_char(line_number, 0, ',');
2016-07-13 19:59:42 +00:00
if (comma_pos < line_number.size){
2016-08-29 01:03:26 +00:00
int32_t start = comma_pos+1;
2016-07-13 19:59:42 +00:00
String column_number = substr(line_number, start, line_number.size-start);
line_number = substr(line_number, 0, comma_pos);
location->line = str_to_int_s(line_number);
location->column = str_to_int_s(column_number);
2016-07-13 19:59:42 +00:00
}
else{
location->line = str_to_int_s(line_number);
2016-07-13 19:59:42 +00:00
location->column = 1;
}
*colon_char = colon_pos;
result = true;
}
}
}
}
}
}
else{
2016-08-29 01:03:26 +00:00
int32_t colon_pos1 = find_s_char(line, 0, ':');
2016-07-17 02:24:13 +00:00
if (line.size > colon_pos1+1){
if (char_is_slash(line.str[colon_pos1+1])){
colon_pos1 = find_s_char(line, colon_pos1+1, ':');
2016-07-13 19:59:42 +00:00
}
}
2016-08-29 01:03:26 +00:00
int32_t colon_pos2 = find_s_char(line, colon_pos1+1, ':');
int32_t colon_pos3 = find_s_char(line, colon_pos2+1, ':');
2016-07-13 19:59:42 +00:00
2016-08-30 19:30:41 +00:00
if (colon_pos3+1 < line.size && line.str[colon_pos3+1] == ' '){
2016-07-13 19:59:42 +00:00
String filename = substr(line, 0, colon_pos1);
String line_number = substr(line, colon_pos1+1, colon_pos2 - colon_pos1 - 1);
String column_number = substr(line, colon_pos2+1, colon_pos3 - colon_pos2 - 1);
if (filename.size > 0 &&
line_number.size > 0 &&
column_number.size > 0){
location->file = filename;
location->line = str_to_int_s(line_number);
location->column = str_to_int_s(column_number);
2016-07-13 19:59:42 +00:00
*colon_char = colon_pos3;
result = true;
}
}
else{
colon_pos1 = find_s_char(line, 0, ':');
2016-07-17 02:24:13 +00:00
if (line.size > colon_pos1+1){
if (char_is_slash(line.str[colon_pos1+1])){
colon_pos1 = find_s_char(line, colon_pos1+1, ':');
2016-07-17 02:24:13 +00:00
}
}
colon_pos2 = find_s_char(line, colon_pos1+1, ':');
2016-07-13 19:59:42 +00:00
2016-08-30 19:30:41 +00:00
if (colon_pos2+1 < line.size && line.str[colon_pos2+1] == ' '){
String filename = substr(line, 0, colon_pos1);
String line_number = substr(line, colon_pos1+1, colon_pos2 - colon_pos1 - 1);
if (str_is_int_s(line_number)){
if (filename.size > 0 && line_number.size > 0){
location->file = filename;
location->line = str_to_int_s(line_number);
location->column = 0;
*colon_char = colon_pos2;
result = true;
}
}
2016-07-13 19:59:42 +00:00
}
}
}
return(result);
}
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-13 19:59:42 +00:00
goto_error(Application_Links *app,
Partition *part,
2016-08-29 01:03:26 +00:00
View_Summary *view, int32_t line,
2016-07-13 19:59:42 +00:00
Jump_Location *location,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_errors){
2016-07-13 19:59:42 +00:00
2016-08-29 01:03:26 +00:00
int32_t result = false;
2016-07-13 19:59:42 +00:00
String line_str = {0};
Buffer_Summary buffer = app->get_buffer(app, view->buffer_id, AccessAll);
if (read_line(app, part, &buffer, line, &line_str)){
2016-08-29 01:03:26 +00:00
int32_t colon_char = 0;
2016-07-13 19:59:42 +00:00
if (parse_error(line_str, location, skip_sub_errors, &colon_char)){
result = true;
}
}
return(result);
}
CUSTOM_COMMAND_SIG(goto_jump_at_cursor){
Temp_Memory temp = begin_temp_memory(&global_part);
View_Summary view = app->get_active_view(app, AccessProtected);
Jump_Location location = {0};
if (goto_error(app, &global_part,
&view, view.cursor.line,
&location, false)){
exec_command(app, change_active_panel);
view = app->get_active_view(app, AccessAll);
jump_to_location(app, &view, &location);
}
end_temp_memory(temp);
}
//
// Error Jumping
//
struct Prev_Jump{
2016-08-29 01:03:26 +00:00
int32_t buffer_id;
int32_t line;
2016-07-13 19:59:42 +00:00
};
static Prev_Jump null_location = {0};
static Prev_Jump prev_location = {0};
// TODO(allen): GIVE THESE THINGS NAMES I CAN FUCKING UNDERSTAND
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-13 19:59:42 +00:00
next_error(Application_Links *app,
Partition *part,
2016-08-29 01:03:26 +00:00
View_Summary *comp_out, int32_t *start_line,
2016-07-13 19:59:42 +00:00
Jump_Location *location,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_errors,
int32_t direction,
int32_t *colon_char){
2016-07-13 19:59:42 +00:00
2016-08-29 01:03:26 +00:00
int32_t result = false;
int32_t line = *start_line + direction;
2016-07-13 19:59:42 +00:00
String line_str = {0};
Buffer_Summary buffer = app->get_buffer(app, comp_out->buffer_id, AccessAll);
for (;;){
if (read_line(app, part, &buffer, line, &line_str)){
if (parse_error(line_str, location, skip_sub_errors, colon_char)){
result = true;
break;
}
line += direction;
}
else{
break;
}
}
if (line < 0){
line = 0;
}
*start_line = line;
return(result);
}
2016-07-18 19:02:22 +00:00
static Prev_Jump
jump_location_store(Application_Links *app, Jump_Location loc){
Prev_Jump result = {0};
Buffer_Summary buffer =
app->get_buffer_by_name(app, loc.file.str, loc.file.size, AccessAll);
if (buffer.exists){
result.buffer_id = buffer.buffer_id;
result.line = loc.line;
}
return(result);
}
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-18 19:02:22 +00:00
seek_error_internal(Application_Links *app, Partition *part,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_errors, int32_t dir, Jump_Location *loc){
int32_t result = false;
2016-07-13 19:59:42 +00:00
Jump_Location location = {0};
Buffer_Summary buffer = app->get_buffer_by_name(app, literal("*compilation*"), AccessAll);
if (buffer.exists){
View_Summary view = get_first_view_with_buffer(app, buffer.buffer_id);
2016-08-29 01:03:26 +00:00
int32_t line = view.cursor.line;
2016-07-13 19:59:42 +00:00
2016-08-29 01:03:26 +00:00
int32_t colon_char = 0;
2016-07-13 19:59:42 +00:00
if (next_error(app, part, &view, &line, &location,
2016-07-18 19:02:22 +00:00
skip_sub_errors, dir, &colon_char)){
2016-07-13 19:59:42 +00:00
View_Summary active_view = app->get_active_view(app, AccessAll);
if (active_view.view_id == view.view_id){
exec_command(app, change_active_panel_regular);
active_view = app->get_active_view(app, AccessAll);
}
jump_to_location(app, &active_view, &location);
app->view_set_cursor(app, &view, seek_line_char(line, colon_char+1), true);
result = true;
if (loc){
*loc = location;
}
}
}
return(result);
}
2016-07-18 19:02:22 +00:00
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-13 19:59:42 +00:00
skip_this_jump(Prev_Jump prev, Prev_Jump jump){
2016-08-29 01:03:26 +00:00
int32_t result = false;
2016-07-13 19:59:42 +00:00
if (prev.buffer_id != 0 && prev.buffer_id == jump.buffer_id &&
prev.line == jump.line){
result = true;
}
return(result);
}
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-13 19:59:42 +00:00
seek_error_skip_repeats(Application_Links *app, Partition *part,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_error, int32_t dir){
int32_t result = true;
2016-07-13 19:59:42 +00:00
Jump_Location location = {0};
Prev_Jump jump = {0};
do{
Temp_Memory temp = begin_temp_memory(part);
2016-07-18 19:02:22 +00:00
if (seek_error_internal(app, part, skip_sub_error, dir, &location)){
2016-07-13 19:59:42 +00:00
jump = jump_location_store(app, location);
result = true;
}
else{
jump.buffer_id = 0;
result = false;
}
end_temp_memory(temp);
}while(skip_this_jump(prev_location, jump));
prev_location = jump;
return(result);
}
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-18 20:53:04 +00:00
seek_error_no_skip(Application_Links *app, Partition *part,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_error, int32_t dir){
int32_t result = true;
2016-07-18 19:02:22 +00:00
Jump_Location location = {0};
Prev_Jump jump = {0};
Temp_Memory temp = begin_temp_memory(part);
if (seek_error_internal(app, part, skip_sub_error, dir, &location)){
jump = jump_location_store(app, location);
result = true;
}
else{
result = false;
}
end_temp_memory(temp);
prev_location = jump;
return(result);
}
2016-08-29 01:03:26 +00:00
static int32_t
2016-07-18 20:53:04 +00:00
seek_error(Application_Links *app, Partition *part,
2016-08-29 01:03:26 +00:00
int32_t skip_sub_error, int32_t skip_same_line, int32_t dir){
2016-07-18 20:53:04 +00:00
if (skip_same_line){
seek_error_skip_repeats(app, part, skip_sub_error, dir);
}
else{
seek_error_no_skip(app, part, skip_sub_error, dir);
}
}
2016-07-13 19:59:42 +00:00
CUSTOM_COMMAND_SIG(goto_next_error){
seek_error_skip_repeats(app, &global_part, true, 1);
}
CUSTOM_COMMAND_SIG(goto_prev_error){
seek_error_skip_repeats(app, &global_part, true, -1);
}
2016-07-18 19:02:22 +00:00
CUSTOM_COMMAND_SIG(goto_next_error_no_skips){
2016-07-18 20:53:04 +00:00
seek_error_no_skip(app, &global_part, true, 1);
2016-07-18 19:02:22 +00:00
}
CUSTOM_COMMAND_SIG(goto_prev_error_no_skips){
2016-07-18 20:53:04 +00:00
seek_error_no_skip(app, &global_part, true, -1);
2016-07-18 19:02:22 +00:00
}
2016-07-13 19:59:42 +00:00
CUSTOM_COMMAND_SIG(goto_first_error){
Temp_Memory temp = begin_temp_memory(&global_part);
View_Summary active_view = app->get_active_view(app, AccessAll);
app->view_set_cursor(app, &active_view, seek_pos(0), true);
Jump_Location location = {0};
2016-07-18 19:02:22 +00:00
prev_location = null_location;
seek_error_internal(app, &global_part, true, 1, &location);
2016-07-13 19:59:42 +00:00
prev_location = jump_location_store(app, location);
end_temp_memory(temp);
}
#endif