string symbol_table::new_const(token tkconst) { string type = type_of(tkconst); auto ent = consts_.find(type+tkconst.text()); if (ent != consts_.end()) { return ent->second.place(); } const_desc desc(tkconst); consts_[type+tkconst.text()] = desc; return desc.place(); }
void dotfile_visitor::format_node(abstract_node *node, const char *name, const token &tok) { if (!m_connectTo) { if (tok.type() == token_types::STRING_LITERAL) fprintf(m_file, "\tptr%p [label=\"[%s]\\n[%s]\\n\\\"%.*s\\\"\"];\n", node, name, token_types::names[tok.type()], tok.length() - 2, tok.text() + 1); else if (tok.text()) fprintf(m_file, "\tptr%p [label=\"[%s]\\n[%s]\\n%.*s\"];\n", node, name, token_types::names[tok.type()], tok.length(), tok.text()); else format_node(node, name); } }
bool json_tree_builder::insert_to_object(const token &token_) { auto obj = std::static_pointer_cast< json_object >(_current); switch (token_.type()) { case T_STR: if (_accept_name) { if (obj->contains(token_.text())) throw std::logic_error("duplicit key: " + token_.text()); // duplicit key name obj->insert(token_.text()); _accept_colon = true; _accept_comma = false; _accept_name = false; _accept_value = false; _accept_end = false; return true; } else if (_accept_value) { auto str = to_value(token_); obj->insert(str); _accept_colon = false; _accept_comma = true; _accept_name = false; _accept_value = false; _accept_end = true; return true; } else { // unexpected token return false; } break; case T_COLON: if (_accept_colon) { _accept_colon = false; _accept_comma = false; _accept_end = false; _accept_name = false; _accept_value = true; return true; } else { return false; } break; case T_LBRACE: // new object if (_accept_value) { auto new_obj = to_value(token_); _parents.push(_current); _current = new_obj; obj->insert(new_obj); _accept_colon = false; _accept_comma = false; _accept_end = true; _accept_name = true; _accept_value = false; return true; } else { return false; } break; case T_COMMA: if (_accept_comma) { _accept_colon = false; _accept_comma = false; _accept_end = true; // not valid JSON _accept_name = true; _accept_value = false; return true; } else { return false; } break; case T_LBRACKET: // new array if (_accept_value) { auto new_arr = to_value(token_); _parents.push(_current); _current = new_arr; obj->insert(new_arr); _accept_colon = false; _accept_comma = false; _accept_end = true; _accept_name = false; _accept_value = true; return true; } else { return false; } break; case T_RBRACE: if (_accept_end) { if (_parents.empty()) { // parsing successfully finished _finished = true; return true; } _current = _parents.top(); _parents.pop(); _accept_colon = false; _accept_comma = true; _accept_end = true; _accept_name = true; _accept_value = _current->jtype() == json_type::J_ARRAY; return true; } else { return false; } break; case T_TRUE: case T_FALSE: case T_NULL: case T_NUM: if (_accept_value) { obj->insert(to_value(token_)); _accept_colon = false; _accept_comma = true; _accept_end = true; _accept_name = false; _accept_value = false; return true; } else { return false; } break; default: return false; // unexpected token } return false; // should not get here }