-
Notifications
You must be signed in to change notification settings - Fork 3
/
JSONParser.cmake
298 lines (248 loc) · 9.86 KB
/
JSONParser.cmake
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
cmake_minimum_required(VERSION 3.1)
if (DEFINED JSonParserGuard)
return()
endif()
set(JSonParserGuard yes)
macro(sbeParseJson prefix jsonString)
cmake_policy(PUSH)
set(json_string "${${jsonString}}")
string(LENGTH "${json_string}" json_jsonLen)
set(json_index 0)
set(json_AllVariables ${prefix})
set(json_ArrayNestingLevel 0)
set(json_MaxArrayNestingLevel 0)
_sbeParse(${prefix})
unset(json_index)
unset(json_AllVariables)
unset(json_jsonLen)
unset(json_string)
unset(json_value)
unset(json_inValue)
unset(json_name)
unset(json_inName)
unset(json_newPrefix)
unset(json_reservedWord)
unset(json_arrayIndex)
unset(json_char)
unset(json_end)
unset(json_ArrayNestingLevel)
foreach(json_nestingLevel RANGE ${json_MaxArrayNestingLevel})
unset(json_${json_nestingLevel}_arrayIndex)
endforeach()
unset(json_nestingLevel)
unset(json_MaxArrayNestingLevel)
cmake_policy(POP)
endmacro()
macro(sbeClearJson prefix)
foreach(json_var ${${prefix}})
unset(${json_var})
endforeach()
unset(${prefix})
unset(json_var)
endmacro()
macro(sbePrintJson prefix)
foreach(json_var ${${prefix}})
message("${json_var} = ${${json_var}}")
endforeach()
endmacro()
macro(_sbeParse prefix)
while(${json_index} LESS ${json_jsonLen})
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
if("\"" STREQUAL "${json_char}")
_sbeParseNameValue(${prefix})
elseif("{" STREQUAL "${json_char}")
_sbeMoveToNextNonEmptyCharacter()
_sbeParseObject(${prefix})
elseif("[" STREQUAL "${json_char}")
_sbeMoveToNextNonEmptyCharacter()
_sbeParseArray(${prefix})
endif()
if(${json_index} LESS ${json_jsonLen})
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
else()
break()
endif()
if ("}" STREQUAL "${json_char}" OR "]" STREQUAL "${json_char}")
break()
endif()
_sbeMoveToNextNonEmptyCharacter()
endwhile()
endmacro()
macro(_sbeParseNameValue prefix)
set(json_name "")
set(json_inName no)
while(${json_index} LESS ${json_jsonLen})
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
# check if name ends
if("\"" STREQUAL "${json_char}" AND json_inName)
set(json_inName no)
_sbeMoveToNextNonEmptyCharacter()
if(NOT ${json_index} LESS ${json_jsonLen})
break()
endif()
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
set(json_newPrefix ${prefix}.${json_name})
set(json_name "")
if(":" STREQUAL "${json_char}")
_sbeMoveToNextNonEmptyCharacter()
if(NOT ${json_index} LESS ${json_jsonLen})
break()
endif()
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
if("\"" STREQUAL "${json_char}")
_sbeParseValue(${json_newPrefix})
break()
elseif("{" STREQUAL "${json_char}")
_sbeMoveToNextNonEmptyCharacter()
_sbeParseObject(${json_newPrefix})
break()
elseif("[" STREQUAL "${json_char}")
_sbeMoveToNextNonEmptyCharacter()
_sbeParseArray(${json_newPrefix})
break()
else()
# reserved word starts
_sbeParseReservedWord(${json_newPrefix})
break()
endif()
else()
# name without value
list(APPEND ${json_AllVariables} ${json_newPrefix})
set(${json_newPrefix} "")
break()
endif()
endif()
if(json_inName)
# remove escapes
if("\\" STREQUAL "${json_char}")
math(EXPR json_index "${json_index} + 1")
if(NOT ${json_index} LESS ${json_jsonLen})
break()
endif()
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
endif()
set(json_name "${json_name}${json_char}")
endif()
# check if name starts
if("\"" STREQUAL "${json_char}" AND NOT json_inName)
set(json_inName yes)
endif()
_sbeMoveToNextNonEmptyCharacter()
endwhile()
endmacro()
macro(_sbeParseReservedWord prefix)
set(json_reservedWord "")
set(json_end no)
while(${json_index} LESS ${json_jsonLen} AND NOT json_end)
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
if("," STREQUAL "${json_char}" OR "}" STREQUAL "${json_char}" OR "]" STREQUAL "${json_char}")
set(json_end yes)
else()
set(json_reservedWord "${json_reservedWord}${json_char}")
math(EXPR json_index "${json_index} + 1")
endif()
endwhile()
list(APPEND ${json_AllVariables} ${prefix})
string(STRIP "${json_reservedWord}" json_reservedWord)
set(${prefix} ${json_reservedWord})
endmacro()
macro(_sbeParseValue prefix)
cmake_policy(SET CMP0054 NEW) # turn off implicit expansions in if statement
set(json_value "")
set(json_inValue no)
while(${json_index} LESS ${json_jsonLen})
# fast path for copying strings
if (json_inValue)
# attempt to gobble up to 128 bytes of string
string(SUBSTRING "${json_string}" ${json_index} 128 try_gobble)
# consume a piece of string we can just straight copy before encountering \ or "
string(REGEX MATCH "^[^\"\\\\]+" simple_copy "${try_gobble}")
string(CONCAT json_value "${json_value}" "${simple_copy}")
string(LENGTH "${simple_copy}" copy_length)
math(EXPR json_index "${json_index} + ${copy_length}")
endif()
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
# check if json_value ends, it is ended by "
if("\"" STREQUAL "${json_char}" AND json_inValue)
set(json_inValue no)
set(${prefix} ${json_value})
list(APPEND ${json_AllVariables} ${prefix})
_sbeMoveToNextNonEmptyCharacter()
break()
endif()
if(json_inValue)
# if " is escaped consume
if("\\" STREQUAL "${json_char}")
math(EXPR json_index "${json_index} + 1")
if(NOT ${json_index} LESS ${json_jsonLen})
break()
endif()
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
if(NOT "\"" STREQUAL "${json_char}")
# if it is not " then copy also escape character
set(json_char "\\${json_char}")
endif()
endif()
_sbeAddEscapedCharacter("${json_char}")
endif()
# check if value starts
if("\"" STREQUAL "${json_char}" AND NOT json_inValue)
set(json_inValue yes)
endif()
math(EXPR json_index "${json_index} + 1")
endwhile()
endmacro()
macro(_sbeAddEscapedCharacter char)
string(CONCAT json_value "${json_value}" "${char}")
endmacro()
macro(_sbeParseObject prefix)
_sbeParse(${prefix})
_sbeMoveToNextNonEmptyCharacter()
endmacro()
macro(_sbeParseArray prefix)
math(EXPR json_ArrayNestingLevel "${json_ArrayNestingLevel} + 1")
set(json_${json_ArrayNestingLevel}_arrayIndex 0)
set(${prefix} "")
list(APPEND ${json_AllVariables} ${prefix})
while(${json_index} LESS ${json_jsonLen})
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
if("\"" STREQUAL "${json_char}")
# simple value
list(APPEND ${prefix} ${json_${json_ArrayNestingLevel}_arrayIndex})
_sbeParseValue(${prefix}_${json_${json_ArrayNestingLevel}_arrayIndex})
elseif("{" STREQUAL "${json_char}")
# object
_sbeMoveToNextNonEmptyCharacter()
list(APPEND ${prefix} ${json_${json_ArrayNestingLevel}_arrayIndex})
_sbeParseObject(${prefix}_${json_${json_ArrayNestingLevel}_arrayIndex})
else()
list(APPEND ${prefix} ${json_${json_ArrayNestingLevel}_arrayIndex})
_sbeParseReservedWord(${prefix}_${json_${json_ArrayNestingLevel}_arrayIndex})
endif()
if(NOT ${json_index} LESS ${json_jsonLen})
break()
endif()
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
if("]" STREQUAL "${json_char}")
_sbeMoveToNextNonEmptyCharacter()
break()
elseif("," STREQUAL "${json_char}")
math(EXPR json_${json_ArrayNestingLevel}_arrayIndex "${json_${json_ArrayNestingLevel}_arrayIndex} + 1")
endif()
_sbeMoveToNextNonEmptyCharacter()
endwhile()
if(${json_MaxArrayNestingLevel} LESS ${json_ArrayNestingLevel})
set(json_MaxArrayNestingLevel ${json_ArrayNestingLevel})
endif()
math(EXPR json_ArrayNestingLevel "${json_ArrayNestingLevel} - 1")
endmacro()
macro(_sbeMoveToNextNonEmptyCharacter)
math(EXPR json_index "${json_index} + 1")
if(${json_index} LESS ${json_jsonLen})
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
while(${json_char} MATCHES "[ \t\n\r]" AND ${json_index} LESS ${json_jsonLen})
math(EXPR json_index "${json_index} + 1")
string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
endwhile()
endif()
endmacro()