CBMC
json_symbol_table.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: JSON symbol table deserialization
4 
5 Author: Chris Smowton, chris.smowton@diffblue.com
6 
7 \*******************************************************************/
8 
9 #include "json_symbol_table.h"
10 #include "json_symbol.h"
11 
12 #include <util/exception_utils.h>
13 #include <util/json.h>
14 #include <util/symbol_table.h>
15 
16 void symbol_table_from_json(const jsont &in, symbol_tablet &symbol_table)
17 {
18  if(!in.is_object())
19  {
21  "symbol_table_from_json: JSON input must be an object");
22  }
23 
24  const json_objectt &json_object = to_json_object(in);
25  const auto it = json_object.find("symbolTable");
26 
27  if(it == json_object.end())
28  {
30  "symbol_table_from_json: JSON object must have key `symbolTable`");
31  }
32 
33  if(!it->second.is_object())
34  {
36  "symbol_table_from_json: JSON symbol table must be an object");
37  }
38 
39  const json_objectt &json_symbol_table = to_json_object(it->second);
40 
41  for(const auto &pair : json_symbol_table)
42  {
43  const jsont &json_symbol = pair.second;
44 
45  symbolt symbol = symbol_from_json(json_symbol);
46 
47  if(symbol_table.add(symbol))
49  "symbol_table_from_json: duplicate symbol name `" +
50  id2string(symbol.name) + "`");
51  }
52 
54 }
exception_utils.h
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
deserialization_exceptiont
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Definition: exception_utils.h:79
json_objectt::find
iterator find(const std::string &key)
Definition: json.h:356
symbol_tablet::validate
void validate(const validation_modet vm=validation_modet::INVARIANT) const
Check that the symbol table is well-formed.
Definition: symbol_table.cpp:130
jsont
Definition: json.h:26
json_objectt
Definition: json.h:299
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
json_objectt::end
iterator end()
Definition: json.h:386
validation_modet::EXCEPTION
@ EXCEPTION
to_json_object
json_objectt & to_json_object(jsont &json)
Definition: json.h:444
symbolt
Symbol table entry.
Definition: symbol.h:27
json.h
jsont::is_object
bool is_object() const
Definition: json.h:56
symbol_table.h
Author: Diffblue Ltd.
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
symbol_from_json
symbolt symbol_from_json(const jsont &in)
Deserialise a json object to a symbolt.
Definition: json_symbol.cpp:45
json_symbol_table.h
symbol_table_from_json
void symbol_table_from_json(const jsont &in, symbol_tablet &symbol_table)
Definition: json_symbol_table.cpp:16
json_symbol.h