-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_parser_m.f90
88 lines (72 loc) · 3.19 KB
/
json_parser_m.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module json_parser_m
use object_parser_m
implicit none
private
type, public, extends(object_parser_t) :: json_parser_t
private
contains
procedure, public, pass(this) :: parse
end type json_parser_t
contains
subroutine parse(this, raw_data)
class(json_parser_t), intent(inout) :: this
character(len=*), intent(in) :: raw_data
integer :: i, current_pos, str_pos
this%error_m = .false.
this%error_string_m = ''
this%raw_data_m = raw_data
this%parsing_attribute_value_pair_m = .false.
this%parsing_attribute_m = .false.
this%parsing_value_m = .false.
this%object_depth_m = 0
do i=1,len_trim(raw_data)
if (raw_data(i:i) == '{') then
this%object_depth_m = this%object_depth_m + 1
else if (raw_data(i:i) == '}') then
this%object_depth_m = this%object_depth_m - 1
if (this%object_depth_m < 0) then
call this%process_error('Unbalanced curly braces')
exit
end if
else if (raw_data(i:i) == '"' .or. raw_data(i:i) == "'") then
if (.not. this%parsing_attribute_m .and. &
.not. this%parsing_value_m .and. &
.not. this%parsing_attribute_value_pair_m) then
call this%add_attribute_value_pair()
this%parsing_attribute_m = .true.
this%parsing_attribute_value_pair_m = .true.
str_pos = 1
else if (this%parsing_attribute_m .and. &
.not. this%parsing_value_m .and. &
this%parsing_attribute_value_pair_m) then
this%parsing_attribute_m = .false.
else if (.not. this%parsing_attribute_m .and. &
.not. this%parsing_value_m .and. &
this%parsing_attribute_value_pair_m) then
this%parsing_value_m = .true.
str_pos = 1
else if (.not. this%parsing_attribute_m .and. &
this%parsing_value_m .and. &
this%parsing_attribute_value_pair_m) then
this%parsing_value_m = .false.
this%parsing_attribute_value_pair_m = .false.
else
call this%process_error('Unablanced quotes')
exit
end if
else
if (this%parsing_attribute_m) then
current_pos = size(this%attribute_value_pairs_m)
this%attribute_value_pairs_m(current_pos)% &
the_attribute(str_pos:str_pos) = raw_data(i:i)
str_pos = str_pos + 1
else if (this%parsing_value_m) then
current_pos = size(this%attribute_value_pairs_m)
this%attribute_value_pairs_m(current_pos)% &
the_value(str_pos:str_pos) = raw_data(i:i)
str_pos = str_pos + 1
end if
end if
end do
end subroutine parse
end module json_parser_m