HPCW 3.0
Loading...
Searching...
No Matches
JSONParser.cmake
Go to the documentation of this file.
1# Copyright (c) 2019 - 2024 David Guibert
2# All rights reserved.
3#
4# SPDX-License-Identifier: Apache-2.0
5
6# From
7# https://raw.githubusercontent.com/sbellus/json-cmake/master/JSONParser.cmake
8cmake_minimum_required(VERSION 3.1)
9
10if(DEFINED JSonParserGuard)
11 return()
12endif()
13
14set(JSonParserGuard yes)
15
16macro(sbeParseJson prefix jsonString)
17 cmake_policy(PUSH)
18
19 set(json_string "${${jsonString}}")
20 string(LENGTH "${json_string}" json_jsonLen)
21 set(json_index 0)
22 set(json_AllVariables ${prefix})
23 set(json_ArrayNestingLevel 0)
24 set(json_MaxArrayNestingLevel 0)
25
26 _sbeparse(${prefix})
27
28 unset(json_index)
29 unset(json_AllVariables)
30 unset(json_jsonLen)
31 unset(json_string)
32 unset(json_value)
33 unset(json_inValue)
34 unset(json_name)
35 unset(json_inName)
36 unset(json_newPrefix)
37 unset(json_reservedWord)
38 unset(json_arrayIndex)
39 unset(json_char)
40 unset(json_end)
41 unset(json_ArrayNestingLevel)
42 foreach(json_nestingLevel RANGE ${json_MaxArrayNestingLevel})
43 unset(json_${json_nestingLevel}_arrayIndex)
44 endforeach()
45 unset(json_nestingLevel)
46 unset(json_MaxArrayNestingLevel)
47
48 cmake_policy(POP)
49endmacro()
50
51macro(sbeClearJson prefix)
52 foreach(json_var ${${prefix}})
53 unset(${json_var})
54 endforeach()
55
56 unset(${prefix})
57 unset(json_var)
58endmacro()
59
60macro(sbePrintJson prefix)
61 foreach(json_var ${${prefix}})
62 message("${json_var} = ${${json_var}}")
63 endforeach()
64endmacro()
65
66macro(_sbeParse prefix)
67
68 while(${json_index} LESS ${json_jsonLen})
69 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
70
71 if("\"" STREQUAL "${json_char}")
72 _sbeparsenamevalue(${prefix})
73 elseif("{" STREQUAL "${json_char}")
74 _sbemovetonextnonemptycharacter()
75 _sbeparseobject(${prefix})
76 elseif("[" STREQUAL "${json_char}")
77 _sbemovetonextnonemptycharacter()
78 _sbeparsearray(${prefix})
79 endif()
80
81 if(${json_index} LESS ${json_jsonLen})
82 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
83 else()
84 break()
85 endif()
86
87 if("}" STREQUAL "${json_char}" OR "]" STREQUAL "${json_char}")
88 break()
89 endif()
90
91 _sbemovetonextnonemptycharacter()
92 endwhile()
93endmacro()
94
95macro(_sbeParseNameValue prefix)
96 set(json_name "")
97 set(json_inName no)
98
99 while(${json_index} LESS ${json_jsonLen})
100 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
101
102 # check if name ends
103 if("\"" STREQUAL "${json_char}" AND json_inName)
104 set(json_inName no)
105 _sbemovetonextnonemptycharacter()
106 if(NOT ${json_index} LESS ${json_jsonLen})
107 break()
108 endif()
109 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
110 set(json_newPrefix ${prefix}.${json_name})
111 set(json_name "")
112
113 if(":" STREQUAL "${json_char}")
114 _sbemovetonextnonemptycharacter()
115 if(NOT ${json_index} LESS ${json_jsonLen})
116 break()
117 endif()
118 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
119
120 if("\"" STREQUAL "${json_char}")
121 _sbeparsevalue(${json_newPrefix})
122 break()
123 elseif("{" STREQUAL "${json_char}")
124 _sbemovetonextnonemptycharacter()
125 _sbeparseobject(${json_newPrefix})
126 break()
127 elseif("[" STREQUAL "${json_char}")
128 _sbemovetonextnonemptycharacter()
129 _sbeparsearray(${json_newPrefix})
130 break()
131 else()
132 # reserved word starts
133 _sbeparsereservedword(${json_newPrefix})
134 break()
135 endif()
136 else()
137 # name without value
138 list(APPEND ${json_AllVariables} ${json_newPrefix})
139 set(${json_newPrefix} "")
140 break()
141 endif()
142 endif()
143
144 if(json_inName)
145 # remove escapes
146 if("\\" STREQUAL "${json_char}")
147 math(EXPR json_index "${json_index} + 1")
148 if(NOT ${json_index} LESS ${json_jsonLen})
149 break()
150 endif()
151 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
152 endif()
153
154 set(json_name "${json_name}${json_char}")
155 endif()
156
157 # check if name starts
158 if("\"" STREQUAL "${json_char}" AND NOT json_inName)
159 set(json_inName yes)
160 endif()
161
162 _sbemovetonextnonemptycharacter()
163 endwhile()
164endmacro()
165
166macro(_sbeParseReservedWord prefix)
167 set(json_reservedWord "")
168 set(json_end no)
169 while(${json_index} LESS ${json_jsonLen} AND NOT json_end)
170 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
171
172 if("," STREQUAL "${json_char}"
173 OR "}" STREQUAL "${json_char}"
174 OR "]" STREQUAL "${json_char}")
175 set(json_end yes)
176 else()
177 set(json_reservedWord "${json_reservedWord}${json_char}")
178 math(EXPR json_index "${json_index} + 1")
179 endif()
180 endwhile()
181
182 list(APPEND ${json_AllVariables} ${prefix})
183 string(STRIP "${json_reservedWord}" json_reservedWord)
184 set(${prefix} ${json_reservedWord})
185endmacro()
186
187macro(_sbeParseValue prefix)
188 cmake_policy(SET CMP0054 NEW) # turn off implicit expansions in if statement
189
190 set(json_value "")
191 set(json_inValue no)
192
193 while(${json_index} LESS ${json_jsonLen})
194 # fast path for copying strings
195 if(json_inValue)
196 # attempt to gobble up to 128 bytes of string
197 string(SUBSTRING "${json_string}" ${json_index} 128 try_gobble)
198 # consume a piece of string we can just straight copy before encountering
199 # \ or "
200 string(REGEX MATCH "^[^\"\\\\]+" simple_copy "${try_gobble}")
201 string(CONCAT json_value "${json_value}" "${simple_copy}")
202 string(LENGTH "${simple_copy}" copy_length)
203 math(EXPR json_index "${json_index} + ${copy_length}")
204 endif()
205
206 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
207
208 # check if json_value ends, it is ended by "
209 if("\"" STREQUAL "${json_char}" AND json_inValue)
210 set(json_inValue no)
211
212 set(${prefix} ${json_value})
213 list(APPEND ${json_AllVariables} ${prefix})
214 _sbemovetonextnonemptycharacter()
215 break()
216 endif()
217
218 if(json_inValue)
219 # if " is escaped consume
220 if("\\" STREQUAL "${json_char}")
221 math(EXPR json_index "${json_index} + 1")
222 if(NOT ${json_index} LESS ${json_jsonLen})
223 break()
224 endif()
225 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
226 if(NOT "\"" STREQUAL "${json_char}")
227 # if it is not " then copy also escape character
228 set(json_char "\\${json_char}")
229 endif()
230 endif()
231
232 _sbeaddescapedcharacter("${json_char}")
233 endif()
234
235 # check if value starts
236 if("\"" STREQUAL "${json_char}" AND NOT json_inValue)
237 set(json_inValue yes)
238 endif()
239
240 math(EXPR json_index "${json_index} + 1")
241 endwhile()
242endmacro()
243
244macro(_sbeAddEscapedCharacter char)
245 string(CONCAT json_value "${json_value}" "${char}")
246endmacro()
247
248macro(_sbeParseObject prefix)
249 _sbeparse(${prefix})
250 _sbemovetonextnonemptycharacter()
251endmacro()
252
253macro(_sbeParseArray prefix)
254 math(EXPR json_ArrayNestingLevel "${json_ArrayNestingLevel} + 1")
255 set(json_${json_ArrayNestingLevel}_arrayIndex 0)
256
257 set(${prefix} "")
258 list(APPEND ${json_AllVariables} ${prefix})
259
260 while(${json_index} LESS ${json_jsonLen})
261 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
262
263 if("\"" STREQUAL "${json_char}")
264 # simple value
265 list(APPEND ${prefix} ${json_${json_ArrayNestingLevel}_arrayIndex})
266 _sbeparsevalue(${prefix}_${json_${json_ArrayNestingLevel}_arrayIndex})
267 elseif("{" STREQUAL "${json_char}")
268 # object
269 _sbemovetonextnonemptycharacter()
270 list(APPEND ${prefix} ${json_${json_ArrayNestingLevel}_arrayIndex})
271 _sbeparseobject(${prefix}_${json_${json_ArrayNestingLevel}_arrayIndex})
272 else()
273 list(APPEND ${prefix} ${json_${json_ArrayNestingLevel}_arrayIndex})
274 _sbeparsereservedword(
275 ${prefix}_${json_${json_ArrayNestingLevel}_arrayIndex})
276 endif()
277
278 if(NOT ${json_index} LESS ${json_jsonLen})
279 break()
280 endif()
281
282 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
283
284 if("]" STREQUAL "${json_char}")
285 _sbemovetonextnonemptycharacter()
286 break()
287 elseif("," STREQUAL "${json_char}")
288 math(EXPR json_${json_ArrayNestingLevel}_arrayIndex
289 "${json_${json_ArrayNestingLevel}_arrayIndex} + 1")
290 endif()
291
292 _sbemovetonextnonemptycharacter()
293 endwhile()
294
295 if(${json_MaxArrayNestingLevel} LESS ${json_ArrayNestingLevel})
296 set(json_MaxArrayNestingLevel ${json_ArrayNestingLevel})
297 endif()
298 math(EXPR json_ArrayNestingLevel "${json_ArrayNestingLevel} - 1")
299endmacro()
300
301macro(_sbeMoveToNextNonEmptyCharacter)
302 math(EXPR json_index "${json_index} + 1")
303 if(${json_index} LESS ${json_jsonLen})
304 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
305 while(${json_char} MATCHES "[ \t\n\r]" AND ${json_index} LESS
306 ${json_jsonLen})
307 math(EXPR json_index "${json_index} + 1")
308 string(SUBSTRING "${json_string}" ${json_index} 1 json_char)
309 endwhile()
310 endif()
311endmacro()