Skip to content

Commit 0793c96

Browse files
committed
updated the error message when an error is encountered reading a json file.
1 parent f2f65cc commit 0793c96

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

src/json_module.f90

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ end subroutine array_callback_func
294294
logical,parameter :: print_tracebacks = .false. !used when debugging
295295

296296
! POP/PUSH CHARACTER [private variables]
297+
integer :: char_count = 0
298+
integer :: line_count = 1
297299
integer :: pushed_index = 0
298300
character (len = 10) :: pushed_char !JW : what is this magic number 10??
299301

@@ -318,6 +320,7 @@ subroutine destroy_json_data_non_polymorphic(me)
318320
! Jacob Williams
319321
!
320322
!********************************************************************************
323+
321324
implicit none
322325

323326
class(json_data_non_polymorphic),intent(inout) :: me
@@ -833,6 +836,8 @@ subroutine json_initialize()
833836
!Just in case, clear these global variables also:
834837
pushed_index = 0
835838
pushed_char = ''
839+
char_count = 0
840+
line_count = 1
836841

837842
!********************************************************************************
838843
end subroutine json_initialize
@@ -992,7 +997,7 @@ subroutine json_value_create(p)
992997
!
993998
! NOTES
994999
! This routine does not check for exceptions.
995-
! The pointer should not already be allocated.
1000+
! The pointer should not already be allocated.
9961001
!
9971002
!********************************************************************************
9981003

@@ -2972,7 +2977,8 @@ subroutine json_parse(file, p)
29722977
type(json_value),pointer :: p
29732978

29742979
integer :: iunit, istat
2975-
character(len=:),allocatable :: line
2980+
character(len=:),allocatable :: line, arrow_str
2981+
character(len=10) :: line_str, char_str
29762982

29772983
!clean any exceptions and initialize:
29782984
call json_initialize()
@@ -2998,21 +3004,33 @@ subroutine json_parse(file, p)
29983004
call parse_value(unit = iunit, value = p)
29993005

30003006
!
3001-
! If there was an error reading the file, then see if we
3007+
! If there was an error reading the file, then
30023008
! can print the line where the error occurred:
30033009
!
30043010
if (exception_thrown) then
3011+
30053012
call get_current_line_from_file(iunit,line)
3006-
if (istat==0) err_message = err_message//newline//&
3007-
'Offending line: '//trim(line)
3013+
3014+
!the counters for the current line and the last character read:
3015+
call integer_to_string(line_count, line_str)
3016+
call integer_to_string(char_count, char_str)
3017+
3018+
!draw the arrow string that points to the current character:
3019+
arrow_str = repeat('-',max( 0, char_count - 1) )//'^'
3020+
3021+
!create the error message:
3022+
err_message = err_message//newline//&
3023+
'line: '//trim(adjustl(line_str))//', '//&
3024+
'character: '//trim(adjustl(char_str))//newline//&
3025+
trim(line)//newline//arrow_str
3026+
30083027
if (allocated(line)) deallocate(line)
3028+
30093029
end if
30103030

30113031
! close the file
30123032
close (iunit, iostat=istat)
30133033

3014-
if (istat/=0) call throw_exception('Error in json_parse: Error closing file: '//trim(file))
3015-
30163034
else
30173035

30183036
call throw_exception('Error in json_parse: Error opening file: '//trim(file))
@@ -3836,13 +3854,17 @@ recursive function pop_char(unit, eof, skip_ws) result(popped)
38363854
else
38373855

38383856
read (unit = unit, fmt = '(A)', advance = 'NO', iostat = ios) c
3857+
char_count = char_count + 1 !character count in the current line
38393858

38403859
if (IS_IOSTAT_EOR(ios)) then !JW : use intrinsic
38413860

3861+
char_count = 0
3862+
line_count = line_count + 1
38423863
cycle
38433864

38443865
else if (IS_IOSTAT_END(ios)) then !JW : use intrinsic
38453866

3867+
char_count = 0
38463868
eof = .true.
38473869
exit
38483870

@@ -3870,8 +3892,6 @@ recursive function pop_char(unit, eof, skip_ws) result(popped)
38703892

38713893
end if
38723894

3873-
!write(*,'(A)') 'pop_char: '//popped
3874-
38753895
!********************************************************************************
38763896
end function pop_char
38773897
!********************************************************************************
@@ -3934,16 +3954,12 @@ subroutine integer_to_string(ival,str)
39343954

39353955
integer :: istat
39363956

3937-
if (.not. exception_thrown) then
3938-
3939-
write(str,fmt=int_fmt,iostat=istat) ival
3940-
3941-
if (istat==0) then
3942-
str = adjustl(str)
3943-
else
3944-
call throw_exception('Error in integer_to_string: invalid value.')
3945-
end if
3957+
write(str,fmt=int_fmt,iostat=istat) ival
39463958

3959+
if (istat==0) then
3960+
str = adjustl(str)
3961+
else
3962+
str = repeat('*',len(str))
39473963
end if
39483964

39493965
!********************************************************************************
@@ -3972,16 +3988,12 @@ subroutine real_to_string(rval,str)
39723988

39733989
integer :: istat
39743990

3975-
if (.not. exception_thrown) then
3976-
3977-
write(str,fmt=real_fmt,iostat=istat) rval
3978-
3979-
if (istat==0) then
3980-
str = adjustl(str)
3981-
else
3982-
call throw_exception('Error in real_to_string: invalid value.')
3983-
end if
3991+
write(str,fmt=real_fmt,iostat=istat) rval
39843992

3993+
if (istat==0) then
3994+
str = adjustl(str)
3995+
else
3996+
str = repeat('*',len(str))
39853997
end if
39863998

39873999
!********************************************************************************

0 commit comments

Comments
 (0)