From 3eb42ba6232ce2241cb01ea2e86bd0f039c6d58e Mon Sep 17 00:00:00 2001 From: LKedward Date: Sat, 12 Sep 2020 19:23:30 +0100 Subject: Update: file_parse_error with more context output Allows optional line number, line string value and line column output. --- fpm/src/fpm/error.f90 | 59 ++++++++++++++++++++++++++++++++++++++++++------- fpm/src/fpm_sources.f90 | 45 ++++++++++++++++++++++--------------- 2 files changed, 78 insertions(+), 26 deletions(-) diff --git a/fpm/src/fpm/error.f90 b/fpm/src/fpm/error.f90 index ba47034..e69ff1e 100644 --- a/fpm/src/fpm/error.f90 +++ b/fpm/src/fpm/error.f90 @@ -57,7 +57,8 @@ contains !> Error created when file parsing fails - subroutine file_parse_error(error, file_name, line, message) + subroutine file_parse_error(error, file_name, message, line_num, & + line_string, line_col) !> Instance of the error data type(error_t), allocatable, intent(out) :: error @@ -65,19 +66,61 @@ contains !> Name of file character(len=*), intent(in) :: file_name - !> Line number of parse error - integer, intent(in) :: line - !> Parse error message character(len=*), intent(in) :: message - character(50) :: line_no_string + !> Line number of parse error + integer, intent(in), optional :: line_num + + !> Line context string + character(len=*), intent(in), optional :: line_string + + !> Line context column + integer, intent(in), optional :: line_col - write(line_no_string,'(I0)') line + character(50) :: temp_string allocate(error) - error%message = 'Error while parsing file "'//file_name//'" on line '// & - trim(line_no_string)//': '//message + error%message = 'Parse error: '//message//new_line('a') + + error%message = error%message//file_name + + if (present(line_num)) then + + write(temp_string,'(I0)') line_num + + error%message = error%message//':'//trim(temp_string) + + end if + + if (present(line_col)) then + + if (line_col > 0) then + + write(temp_string,'(I0)') line_col + error%message = error%message//':'//trim(temp_string) + + end if + + end if + + if (present(line_string)) then + + error%message = error%message//new_line('a') + error%message = error%message//' | '//line_string + + if (present(line_col)) then + + if (line_col > 0) then + + error%message = error%message//new_line('a') + error%message = error%message//' | '//repeat(' ',line_col-1)//'^' + + end if + + end if + + end if end subroutine file_parse_error diff --git a/fpm/src/fpm_sources.f90 b/fpm/src/fpm_sources.f90 index 3c8a3cf..787efff 100644 --- a/fpm/src/fpm_sources.f90 +++ b/fpm/src/fpm_sources.f90 @@ -224,15 +224,17 @@ function parse_f_source(f_filename,error) result(f_source) temp_string = split_n(file_lines(i)%s,delims=':',n=2,stat=stat) if (stat /= 0) then - call file_parse_error(error,f_filename,i, & - message='unable to find used module name') + call file_parse_error(error,f_filename, & + 'unable to find used module name',i, & + file_lines(i)%s,index(file_lines(i)%s,'::')) return end if mod_name = split_n(temp_string,delims=' ,',n=1,stat=stat) if (stat /= 0) then - call file_parse_error(error,f_filename,i, & - message='unable to find used module name') + call file_parse_error(error,f_filename, & + 'unable to find used module name',i, & + file_lines(i)%s) return end if mod_name = lower(mod_name) @@ -241,8 +243,9 @@ function parse_f_source(f_filename,error) result(f_source) mod_name = split_n(file_lines(i)%s,n=2,delims=' ,',stat=stat) if (stat /= 0) then - call file_parse_error(error,f_filename,i, & - message='unable to find used module name') + call file_parse_error(error,f_filename, & + 'unable to find used module name',i, & + file_lines(i)%s) return end if mod_name = lower(mod_name) @@ -277,8 +280,9 @@ function parse_f_source(f_filename,error) result(f_source) f_source%include_dependencies(n_include)%s = & & split_n(file_lines(i)%s,n=2,delims="'"//'"',stat=stat) if (stat /= 0) then - call file_parse_error(error,f_filename,i, & - message='unable to find include file name') + call file_parse_error(error,f_filename, & + 'unable to find include file name',i, & + file_lines(i)%s) return end if end if @@ -290,8 +294,9 @@ function parse_f_source(f_filename,error) result(f_source) mod_name = lower(split_n(file_lines(i)%s,n=2,delims=' ',stat=stat)) if (stat /= 0) then - call file_parse_error(error,f_filename,i, & - message='unable to find module name') + call file_parse_error(error,f_filename, & + 'unable to find module name',i, & + file_lines(i)%s) return end if @@ -303,8 +308,9 @@ function parse_f_source(f_filename,error) result(f_source) end if if (.not.validate_name(mod_name)) then - call file_parse_error(error,f_filename,i, & - message='empty or invalid name for module') + call file_parse_error(error,f_filename, & + 'empty or invalid name for module',i, & + file_lines(i)%s) return end if @@ -323,8 +329,9 @@ function parse_f_source(f_filename,error) result(f_source) temp_string = split_n(file_lines(i)%s,n=2,delims='()',stat=stat) if (stat /= 0) then - call file_parse_error(error,f_filename,i, & - message='unable to get submodule ancestry') + call file_parse_error(error,f_filename, & + 'unable to get submodule ancestry',i, & + file_lines(i)%s) return end if @@ -343,8 +350,9 @@ function parse_f_source(f_filename,error) result(f_source) f_source%modules_used(n_use)%s = lower(temp_string) if (.not.validate_name(temp_string)) then - call file_parse_error(error,f_filename,i, & - message='empty or invalid name for submodule parent') + call file_parse_error(error,f_filename, & + 'empty or invalid name for submodule parent',i, & + file_lines(i)%s, index(file_lines(i)%s,temp_string)) return end if @@ -461,8 +469,9 @@ function parse_c_source(c_filename,error) result(c_source) c_source%include_dependencies(n_include)%s = & & split_n(file_lines(i)%s,n=2,delims='"',stat=stat) if (stat /= 0) then - call file_parse_error(error,c_filename,i, & - message='unable to get c include file') + call file_parse_error(error,c_filename, & + 'unable to get c include file',i, & + file_lines(i)%s,index(file_lines(i)%s,'"')) return end if -- cgit v1.2.3