Skip to content

Commit 662133b

Browse files
Merge pull request #461 from jacobwilliams/456-multiple-comment-chars
added support for multiple comment characters
2 parents 572d9f5 + 5a01735 commit 662133b

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/json_initialize_arguments.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ logical(LK),intent(in),optional :: unescape_strings
3232
!! string is returned from [[json_get_string]]
3333
!! and similar routines. If true [default],
3434
!! then the string is returned unescaped.
35-
character(kind=CK,len=1),intent(in),optional :: comment_char
36-
!! If present, this character is used
35+
character(kind=CK,len=*),intent(in),optional :: comment_char
36+
!! If present, these characters are used
3737
!! to denote comments in the JSON file,
3838
!! which will be ignored if present.
39-
!! Example: `!` or `#`. Setting this
39+
!! Example: `!`, `#`, or `/!#`. Setting this
4040
!! to a blank string disables the
41-
!! ignoring of comments. (Default is `!`).
41+
!! ignoring of comments. (Default is `/!#`).
4242
integer(IK),intent(in),optional :: path_mode
4343
!! How the path strings are interpreted in the
4444
!! `get_by_path` routines:

src/json_value_module.F90

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ module json_value_module
216216
!! then the string is returned unescaped.
217217

218218
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
220220
!! 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_'/!#'`.
224225

225226
integer(IK) :: path_mode = 1_IK !! How the path strings are interpreted in the
226227
!! `get_by_path` routines:
@@ -878,6 +879,7 @@ module json_value_module
878879
procedure :: json_value_print
879880
procedure :: string_to_int
880881
procedure :: string_to_dble
882+
procedure :: prepare_parser => json_prepare_parser
881883
procedure :: parse_end => json_parse_end
882884
procedure :: parse_value
883885
procedure :: parse_number
@@ -1081,7 +1083,7 @@ subroutine json_initialize(me,&
10811083
! [an empty string disables comments]
10821084
if (present(comment_char)) then
10831085
me%allow_comments = comment_char/=CK_''
1084-
me%comment_char = comment_char
1086+
me%comment_char = trim(adjustl(comment_char))
10851087
end if
10861088

10871089
! path separator:
@@ -9690,6 +9692,26 @@ recursive subroutine wrap_json_get_array_by_path(json, me, path, array_callback,
96909692
end subroutine wrap_json_get_array_by_path
96919693
!*****************************************************************************************
96929694

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+
96939715
!*****************************************************************************************
96949716
!>
96959717
! Parse the JSON file and populate the [[json_value]] tree.
@@ -9735,6 +9757,7 @@ subroutine json_parse_file(json, file, p, unit)
97359757

97369758
! clear any exceptions and initialize:
97379759
call json%initialize()
9760+
call json%prepare_parser()
97389761

97399762
if ( present(unit) ) then
97409763

@@ -9845,6 +9868,7 @@ subroutine json_parse_string(json, p, str)
98459868

98469869
! clear any exceptions and initialize:
98479870
call json%initialize()
9871+
call json%prepare_parser()
98489872

98499873
! create the value and associate the pointer
98509874
call json_value_create(p)
@@ -11435,7 +11459,7 @@ subroutine pop_char(json,unit,str,skip_ws,skip_comments,eof,popped)
1143511459

1143611460
end if
1143711461

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
1143911463

1144011464
! skipping the comment
1144111465
parsing_comment = .true.

0 commit comments

Comments
 (0)