@@ -216,11 +216,12 @@ module json_value_module
216
216
! ! then the string is returned unescaped.
217
217
218
218
logical (LK) :: allow_comments = .true. ! ! if true, any comments will be ignored when
219
- ! ! parsing a file. The comment token is defined
219
+ ! ! parsing a file. The comment tokens are defined
220
220
! ! by the `comment_char` character variable.
221
- character (kind= CK,len= 1 ) :: comment_char = CK_' !' ! ! comment token when
222
- ! ! `allow_comments` is true.
223
- ! ! Examples: '`!`' or '`#`'.
221
+ character (kind= CK,len= :),allocatable :: comment_char ! ! comment tokens when
222
+ ! ! `allow_comments` is true.
223
+ ! ! Examples: '`!`' or '`#`'.
224
+ ! ! Default is `CK_'/!#'`.
224
225
225
226
integer (IK) :: path_mode = 1_IK ! ! How the path strings are interpreted in the
226
227
! ! `get_by_path` routines:
@@ -878,6 +879,7 @@ module json_value_module
878
879
procedure :: json_value_print
879
880
procedure :: string_to_int
880
881
procedure :: string_to_dble
882
+ procedure :: prepare_parser = > json_prepare_parser
881
883
procedure :: parse_end = > json_parse_end
882
884
procedure :: parse_value
883
885
procedure :: parse_number
@@ -1081,7 +1083,7 @@ subroutine json_initialize(me,&
1081
1083
! [an empty string disables comments]
1082
1084
if (present (comment_char)) then
1083
1085
me% allow_comments = comment_char/= CK_' '
1084
- me% comment_char = comment_char
1086
+ me% comment_char = trim ( adjustl ( comment_char))
1085
1087
end if
1086
1088
1087
1089
! path separator:
@@ -9690,6 +9692,26 @@ recursive subroutine wrap_json_get_array_by_path(json, me, path, array_callback,
9690
9692
end subroutine wrap_json_get_array_by_path
9691
9693
! *****************************************************************************************
9692
9694
9695
+ ! *****************************************************************************************
9696
+ ! >
9697
+ ! Internal routine to be called before parsing JSON.
9698
+ ! Currently, all this does it allocate the `comment_char` if none was specified.
9699
+
9700
+ subroutine json_prepare_parser (json )
9701
+
9702
+ implicit none
9703
+
9704
+ class(json_core),intent (inout ) :: json
9705
+
9706
+ if (json% allow_comments .and. .not. allocated (json% comment_char)) then
9707
+ ! comments are enabled, but user hasn't set the comment char,
9708
+ ! so in this case use the default:
9709
+ json% comment_char = CK_' /!#'
9710
+ end if
9711
+
9712
+ end subroutine json_prepare_parser
9713
+ ! *****************************************************************************************
9714
+
9693
9715
! *****************************************************************************************
9694
9716
! >
9695
9717
! Parse the JSON file and populate the [[json_value]] tree.
@@ -9735,6 +9757,7 @@ subroutine json_parse_file(json, file, p, unit)
9735
9757
9736
9758
! clear any exceptions and initialize:
9737
9759
call json% initialize()
9760
+ call json% prepare_parser()
9738
9761
9739
9762
if ( present (unit) ) then
9740
9763
@@ -9845,6 +9868,7 @@ subroutine json_parse_string(json, p, str)
9845
9868
9846
9869
! clear any exceptions and initialize:
9847
9870
call json% initialize()
9871
+ call json% prepare_parser()
9848
9872
9849
9873
! create the value and associate the pointer
9850
9874
call json_value_create(p)
@@ -11435,7 +11459,7 @@ subroutine pop_char(json,unit,str,skip_ws,skip_comments,eof,popped)
11435
11459
11436
11460
end if
11437
11461
11438
- if (ignore_comments .and. (parsing_comment .or. c == json% comment_char) ) then
11462
+ if (ignore_comments .and. (parsing_comment .or. scan (c, json% comment_char,kind = IK)> 0_IK ) ) then
11439
11463
11440
11464
! skipping the comment
11441
11465
parsing_comment = .true.
0 commit comments