1
0
Fork 0
This commit is contained in:
Frank Celler 2012-01-04 09:07:33 +01:00
parent 50aec34531
commit 818c2339c1
10 changed files with 1174 additions and 4737 deletions

777
JsonParserX/InputParser.cpp Normal file
View File

@ -0,0 +1,777 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief input parsers
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2011 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Copyright 2009-2011, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "InputParser.h"
#include <Basics/Exceptions.h>
#include <Variant/VariantArray.h>
#include <Variant/VariantBoolean.h>
#include <Variant/VariantDouble.h>
#include <Variant/VariantInt64.h>
#include <Variant/VariantNull.h>
#include <Variant/VariantObject.h>
#include <Variant/VariantString.h>
#include <Variant/VariantVector.h>
#include <Rest/HttpRequest.h>
#include "JsonParserX/JsonParserXDriver.h"
using namespace std;
using namespace triagens::basics;
using namespace triagens::rest;
// -----------------------------------------------------------------------------
// helper functions
// -----------------------------------------------------------------------------
namespace triagens {
namespace rest {
enum ObjectDescriptionType {
OD_BOOLEAN,
OD_DOUBLE,
OD_INTEGER,
OD_STRING,
OD_STRING_LIST,
OD_VARIANT_ARRAY,
OD_VARIANT_BOOLEAN,
OD_VARIANT_DOUBLE,
OD_VARIANT_INTEGER,
OD_VARIANT_NULL,
OD_VARIANT_STRING,
OD_VARIANT_STRING_LIST,
OD_VARIANT_VECTOR
};
struct AttributeDescription {
AttributeDescription () {
}
AttributeDescription (ObjectDescriptionType type, void* store, bool* hasAttribute)
: type(type), store(store), hasAttribute(hasAttribute) {
}
ObjectDescriptionType type;
void* store;
bool* hasAttribute;
};
}
}
namespace {
void clearObject (ObjectDescriptionType type, void* store) {
switch (type) {
case OD_BOOLEAN:
*reinterpret_cast<bool*>(store) = false;
break;
case OD_DOUBLE:
*reinterpret_cast<double*>(store) = false;
break;
case OD_INTEGER:
*reinterpret_cast<int64_t*>(store) = 0;
break;
case OD_STRING:
*reinterpret_cast<string*>(store) = "";
break;
case OD_VARIANT_ARRAY:
case OD_VARIANT_BOOLEAN:
case OD_VARIANT_DOUBLE:
case OD_VARIANT_INTEGER:
case OD_VARIANT_NULL:
case OD_VARIANT_STRING:
case OD_VARIANT_VECTOR:
*reinterpret_cast<void**>(store) = 0;
break;
case OD_STRING_LIST:
case OD_VARIANT_STRING_LIST:
break;
}
}
bool checkObjectType (string const& name, ObjectDescriptionType type, VariantObject* object, string& errorMessage) {
char const* expecting;
VariantObject::ObjectType otype;
switch (type) {
case OD_VARIANT_ARRAY:
expecting = "ARRAY";
otype = VariantObject::VARIANT_ARRAY;
break;
case OD_BOOLEAN:
case OD_VARIANT_BOOLEAN:
expecting = "BOOLEAN";
otype = VariantObject::VARIANT_BOOLEAN;
break;
case OD_DOUBLE:
case OD_VARIANT_DOUBLE:
expecting = "DOUBLE";
otype = VariantObject::VARIANT_DOUBLE;
break;
case OD_INTEGER:
case OD_VARIANT_INTEGER:
expecting = "INTEGER";
otype = VariantObject::VARIANT_INT64;
break;
case OD_VARIANT_NULL:
expecting = "NULL";
otype = VariantObject::VARIANT_NULL;
break;
case OD_STRING:
case OD_VARIANT_STRING:
expecting = "STRING";
otype = VariantObject::VARIANT_STRING;
break;
case OD_STRING_LIST:
case OD_VARIANT_STRING_LIST:
expecting = "VECTOR OF STRINGS";
otype = VariantObject::VARIANT_VECTOR;
break;
case OD_VARIANT_VECTOR:
expecting = "VECTOR";
otype = VariantObject::VARIANT_VECTOR;
break;
default:
THROW_INTERNAL_ERROR("wrong type");
}
if (otype != object->type()) {
errorMessage = "attribute '"
+ name
+ "' is of wrong type (expecting "
+ expecting
+ ", got "
+ VariantObject::NameObjectType(object->type())
+ ")";
return false;
}
else {
return true;
}
}
template<typename VT>
bool extractObjects (string const& name,
VariantVector* list,
ObjectDescriptionType type,
vector< VT* >* store,
string& errorMessage) {
vector<VariantObject*> const& values = list->getValues();
for (vector<VariantObject*>::const_iterator i = values.begin(); i != values.end(); ++i) {
VariantObject* object = *i;
bool ok = checkObjectType(name, type, object, errorMessage);
if (! ok) {
return false;
}
VT* vt = object->as<VT>();
store->push_back(vt);
}
return true;
}
template<typename VT, typename T>
bool extractObjects (string const& name,
VariantVector* list,
ObjectDescriptionType type,
vector< T >* store,
string& errorMessage) {
vector<VariantObject*> const& values = list->getValues();
for (vector<VariantObject*>::const_iterator i = values.begin(); i != values.end(); ++i) {
VariantObject* object = *i;
bool ok = checkObjectType(name, type, object, errorMessage);
if (! ok) {
return false;
}
VT* vt = object->as<VT>();
store->push_back(vt->getValue());
}
return true;
}
bool extractObject (string const& name,
VariantObject* object,
AttributeDescription const& desc,
string& errorMessage) {
switch (desc.type) {
case OD_VARIANT_ARRAY:
*reinterpret_cast<VariantArray**>(desc.store) = object->as<VariantArray>();
return true;
case OD_BOOLEAN:
*reinterpret_cast<bool*>(desc.store) = object->as<VariantBoolean>()->getValue();
return true;
case OD_VARIANT_BOOLEAN:
*reinterpret_cast<VariantBoolean**>(desc.store) = object->as<VariantBoolean>();
return true;
case OD_DOUBLE:
*reinterpret_cast<double*>(desc.store) = object->as<VariantDouble>()->getValue();
return true;
case OD_VARIANT_DOUBLE:
*reinterpret_cast<VariantDouble**>(desc.store) = object->as<VariantDouble>();
return true;
case OD_INTEGER:
*reinterpret_cast<int64_t*>(desc.store) = object->as<VariantInt64>()->getValue();
return true;
case OD_VARIANT_INTEGER:
*reinterpret_cast<VariantInt64**>(desc.store) = object->as<VariantInt64>();
return true;
case OD_VARIANT_NULL:
*reinterpret_cast<VariantNull**>(desc.store) = object->as<VariantNull>();
return true;
case OD_STRING:
*reinterpret_cast<string*>(desc.store) = object->as<VariantString>()->getValue();
return true;
case OD_VARIANT_STRING:
*reinterpret_cast<VariantString**>(desc.store) = object->as<VariantString>();
return true;
case OD_STRING_LIST:
return extractObjects<VariantString>(name,
object->as<VariantVector>(),
OD_VARIANT_STRING,
reinterpret_cast< vector<string>* >(desc.store),
errorMessage);
case OD_VARIANT_STRING_LIST:
return extractObjects(name,
object->as<VariantVector>(),
OD_VARIANT_STRING,
reinterpret_cast< vector<VariantString*>* >(desc.store),
errorMessage);
case OD_VARIANT_VECTOR:
*reinterpret_cast<VariantVector**>(desc.store) = object->as<VariantVector>();
return true;
default:
THROW_INTERNAL_ERROR("wrong type");
}
}
bool loadObject (VariantArray* array, string const& name, AttributeDescription const& desc, bool optional, string& errorMessage) {
clearObject(desc.type, desc.store);
if (desc.hasAttribute != 0) {
*desc.hasAttribute = false;
}
VariantObject* object = array->lookup(name);
if (object == 0) {
if (optional) {
return true;
}
else {
errorMessage = "attribute '" + name + "' not found";
return false;
}
}
if (object->is<VariantNull>() && optional) {
return true;
}
if (desc.hasAttribute != 0) {
*desc.hasAttribute = true;
}
bool ok = checkObjectType(name, desc.type, object, errorMessage);
if (! ok) {
return false;
}
return extractObject(name, object, desc, errorMessage);
}
bool loadAlternatives (VariantArray* array,
string const& name,
vector<AttributeDescription> const& alternatives,
string& errorMessage) {
for (vector<AttributeDescription>::const_iterator i = alternatives.begin(); i != alternatives.end(); ++i) {
clearObject(i->type, i->store);
}
VariantObject* object = array->lookup(name);
if (object == 0) {
return true;
}
for (vector<AttributeDescription>::const_iterator i = alternatives.begin(); i != alternatives.end(); ++i) {
AttributeDescription const& desc = *i;
if (checkObjectType(name, desc.type, object, errorMessage)) {
return extractObject(name, object, desc, errorMessage);
}
}
errorMessage = "attribute '" + name + "' is of wrong type";
return false;
}
}
namespace triagens {
namespace rest {
namespace InputParser {
// -----------------------------------------------------------------------------
// object description implementation
// -----------------------------------------------------------------------------
struct ObjectDescriptionImpl {
map< string, AttributeDescription > attributes;
map< string, AttributeDescription > optionals;
map< string, vector<AttributeDescription> > alternatives;
string lastError;
};
// -----------------------------------------------------------------------------
// object description
// -----------------------------------------------------------------------------
ObjectDescription::ObjectDescription () {
impl = new ObjectDescriptionImpl();
}
ObjectDescription::~ObjectDescription () {
delete impl;
}
string const& ObjectDescription::lastError () {
return impl->lastError;
}
bool ObjectDescription::parse (VariantObject* object) {
impl->lastError.clear();
// object must be a json array
if (object == 0) {
impl->lastError = "cannot parse object";
return false;
}
if (! object->is<VariantArray>()) {
impl->lastError = "not an object";
return false;
}
VariantArray* array = object->as<VariantArray>();
// now find the attributes, optionals, and alternatives
for (map<string, AttributeDescription>::iterator i = impl->attributes.begin(); i != impl->attributes.end(); ++i) {
string const& name = i->first;
AttributeDescription& desc = i->second;
bool ok = loadObject(array, name, desc, false, impl->lastError);
if (! ok) {
return false;
}
}
for (map<string, AttributeDescription>::iterator i = impl->optionals.begin(); i != impl->optionals.end(); ++i) {
string const& name = i->first;
AttributeDescription& desc = i->second;
bool ok = loadObject(array, name, desc, true, impl->lastError);
if (! ok) {
return false;
}
}
for (map< string, vector<AttributeDescription> >::iterator i = impl->alternatives.begin(); i != impl->alternatives.end(); ++i) {
string const& name = i->first;
bool ok = loadAlternatives(array, name, i->second, impl->lastError);
if (! ok) {
return false;
}
}
transform();
return true;
}
void ObjectDescription::transform () {
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// attribute
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectDescription& ObjectDescription::attribute (string const& name, VariantArray*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_ARRAY, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, VariantBoolean*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_BOOLEAN, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, bool& store) {
impl->attributes[name] = AttributeDescription(OD_BOOLEAN, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, VariantDouble*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_DOUBLE, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, double& store) {
impl->attributes[name] = AttributeDescription(OD_DOUBLE, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, VariantInt64*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_INTEGER, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, int64_t& store) {
impl->attributes[name] = AttributeDescription(OD_INTEGER, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, VariantNull*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_NULL, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, VariantString*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_STRING, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, string& store) {
impl->attributes[name] = AttributeDescription(OD_STRING, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, vector<VariantString*>& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_STRING_LIST, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, vector<string>& store) {
impl->attributes[name] = AttributeDescription(OD_STRING_LIST, reinterpret_cast<void*>(&store), 0);
return *this;
}
ObjectDescription& ObjectDescription::attribute (string const& name, VariantVector*& store) {
impl->attributes[name] = AttributeDescription(OD_VARIANT_VECTOR, reinterpret_cast<void*>(&store), 0);
return *this;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// optional
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectDescription& ObjectDescription::optional (string const& name, VariantArray*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_ARRAY, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, VariantBoolean*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_BOOLEAN, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, bool& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_BOOLEAN, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, VariantDouble*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_DOUBLE, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, double& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_DOUBLE, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, VariantInt64*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_INTEGER, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, int64_t& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_INTEGER, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, VariantNull*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_NULL, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, VariantString*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_STRING, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, string& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_STRING, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, vector<VariantString*>& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_STRING_LIST, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, vector<string>& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_STRING_LIST, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
ObjectDescription& ObjectDescription::optional (string const& name, VariantVector*& store, bool* hasAttribute) {
impl->optionals[name] = AttributeDescription(OD_VARIANT_VECTOR, reinterpret_cast<void*>(&store), hasAttribute);
return *this;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// alternative
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectDescription& ObjectDescription::alternative (string const& name, VariantArray*& store) {
impl->alternatives[name].push_back(AttributeDescription(OD_VARIANT_ARRAY, reinterpret_cast<void*>(&store), 0));
return *this;
}
ObjectDescription& ObjectDescription::alternative (string const& name, VariantBoolean*& store) {
impl->alternatives[name].push_back(AttributeDescription(OD_VARIANT_BOOLEAN, reinterpret_cast<void*>(&store), 0));
return *this;
}
ObjectDescription& ObjectDescription::alternative (string const& name, VariantInt64*& store) {
impl->alternatives[name].push_back(AttributeDescription(OD_VARIANT_INTEGER, reinterpret_cast<void*>(&store), 0));
return *this;
}
ObjectDescription& ObjectDescription::alternative (string const& name, VariantNull*& store) {
impl->alternatives[name].push_back(AttributeDescription(OD_VARIANT_NULL, reinterpret_cast<void*>(&store), 0));
return *this;
}
ObjectDescription& ObjectDescription::alternative (string const& name, VariantString*& store) {
impl->alternatives[name].push_back(AttributeDescription(OD_VARIANT_STRING, reinterpret_cast<void*>(&store), 0));
return *this;
}
ObjectDescription& ObjectDescription::alternative (string const& name, VariantVector*& store) {
impl->alternatives[name].push_back(AttributeDescription(OD_VARIANT_VECTOR, reinterpret_cast<void*>(&store), 0));
return *this;
}
// -----------------------------------------------------------------------------
// public fucntions
// -----------------------------------------------------------------------------
VariantObject* json (string const& input) {
JsonParserXDriver parser;
return parser.parse(input);
}
VariantObject* json (HttpRequest* request) {
JsonParserXDriver parser;
return parser.parse(request->body());
}
VariantArray* jsonArray (string const& input) {
JsonParserXDriver parser;
VariantObject* object = parser.parse(input);
if (object == 0) {
return 0;
}
else if (object->type() == VariantObject::VARIANT_ARRAY) {
return dynamic_cast<VariantArray*>(object);
}
else {
delete object;
return 0;
}
}
VariantArray* jsonArray (HttpRequest* request) {
JsonParserXDriver parser;
VariantObject* object = parser.parse(request->body());
if (object == 0) {
return 0;
}
else if (object->type() == VariantObject::VARIANT_ARRAY) {
return dynamic_cast<VariantArray*>(object);
}
else {
delete object;
return 0;
}
}
}
}
}

363
JsonParserX/InputParser.h Normal file
View File

@ -0,0 +1,363 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief input parsers
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2011 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Copyright 2009-2011, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_FYN_REST_INPUT_PARSER_H
#define TRIAGENS_FYN_REST_INPUT_PARSER_H 1
#include <Basics/Common.h>
namespace triagens {
namespace basics {
class VariantArray;
class VariantBoolean;
class VariantDouble;
class VariantInt64;
class VariantNull;
class VariantObject;
class VariantString;
class VariantVector;
}
namespace rest {
class HttpRequest;
namespace InputParser {
class ObjectDescriptionImpl;
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Utilities
/// @brief object description
////////////////////////////////////////////////////////////////////////////////
class ObjectDescription {
private:
ObjectDescription (ObjectDescription const&);
ObjectDescription& operator= (ObjectDescription const&);
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a new description
////////////////////////////////////////////////////////////////////////////////
ObjectDescription ();
////////////////////////////////////////////////////////////////////////////////
/// @brief destroys a description
////////////////////////////////////////////////////////////////////////////////
virtual ~ObjectDescription ();
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief loads an object
////////////////////////////////////////////////////////////////////////////////
bool parse (basics::VariantObject*);
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the last error message
////////////////////////////////////////////////////////////////////////////////
string const& lastError ();
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief applys transformations after parsing
////////////////////////////////////////////////////////////////////////////////
virtual void transform ();
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an array attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantArray*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a boolean attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantBoolean*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a boolean attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, bool&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a double attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantDouble*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a double attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, double&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an integer attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantInt64*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an integer attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, int64_t&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an null attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantNull*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a string attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantString*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a string attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, string&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a string vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, vector<basics::VariantString*>&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a string vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, vector<string>&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& attribute (string const& name, basics::VariantVector*&);
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional attribute field
////////////////////////////////////////////////////////////////////////////////
template<typename T>
ObjectDescription& optional (string const& name, T& t, bool& hasAttribute) {
return optional(name, t, &hasAttribute);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional attribute field
////////////////////////////////////////////////////////////////////////////////
template<typename T>
ObjectDescription& optional (string const& name, T& t) {
return optional(name, t, 0);
}
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional array attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantArray*&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional boolean attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantBoolean*&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional boolean attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, bool&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional double attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantDouble*&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional double attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, double&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional integer attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantInt64*&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional integer attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, int64_t&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional null attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantNull*&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional string attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantString*&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional string attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, string&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional string vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, vector<basics::VariantString*>&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional string vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, vector<string>&, bool* hasAttribute);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an optional vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& optional (string const& name, basics::VariantVector*&, bool* hasAttribute);
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative array attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantArray*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative boolean attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantBoolean*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative double attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantDouble*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative integer attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantInt64*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative null attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantNull*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative string attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantString*&);
////////////////////////////////////////////////////////////////////////////////
/// @brief adds an alternative vector attribute field
////////////////////////////////////////////////////////////////////////////////
ObjectDescription& alternative (string const& name, basics::VariantVector*&);
private:
ObjectDescriptionImpl* impl;
};
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Utilities
/// @brief a json parser
////////////////////////////////////////////////////////////////////////////////
basics::VariantObject* json (string const& input);
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Utilities
/// @brief a json parser
////////////////////////////////////////////////////////////////////////////////
basics::VariantObject* json (HttpRequest*);
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Utilities
/// @brief a json parser for an array
////////////////////////////////////////////////////////////////////////////////
basics::VariantArray* jsonArray (string const& input);
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Utilities
/// @brief a json parser for an array
////////////////////////////////////////////////////////////////////////////////
basics::VariantArray* jsonArray (HttpRequest*);
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Utilities
/// @brief a json-to-object parser
////////////////////////////////////////////////////////////////////////////////
bool json2object (ObjectDescription&, string const& input);
}
}
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,363 +0,0 @@
/* A Bison parser, made by GNU Bison 2.4.1. */
/* Skeleton interface for Bison LALR(1) parsers in C++
Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* C++ LALR(1) parser skeleton written by Akim Demaille. */
#ifndef PARSER_HEADER_H
# define PARSER_HEADER_H
/* "%code requires" blocks. */
#define NAME_SPACE triagens::json_parser
#define YY_DECL \
NAME_SPACE::JsonParserX::token_type \
yylex (NAME_SPACE::JsonParserX::semantic_type* yylval_param, \
NAME_SPACE::JsonParserX::location_type* yylloc_param, \
void* yyscanner)
#include <string>
#include <iostream>
#include "stack.hh"
namespace triagens { namespace json_parser {
class position;
class location;
} } // triagens::json_parser
#include "location.hh"
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 1
#endif
/* Enabling verbose error messages. */
#ifdef YYERROR_VERBOSE
# undef YYERROR_VERBOSE
# define YYERROR_VERBOSE 1
#else
# define YYERROR_VERBOSE 1
#endif
/* Enabling the token table. */
#ifndef YYTOKEN_TABLE
# define YYTOKEN_TABLE 0
#endif
/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
If N is 0, then set CURRENT to the empty location which ends
the previous symbol: RHS[0] (always defined). */
#ifndef YYLLOC_DEFAULT
# define YYLLOC_DEFAULT(Current, Rhs, N) \
do { \
if (N) \
{ \
(Current).begin = (Rhs)[1].begin; \
(Current).end = (Rhs)[N].end; \
} \
else \
{ \
(Current).begin = (Current).end = (Rhs)[0].end; \
} \
} while (false)
#endif
namespace triagens { namespace json_parser {
/// A Bison parser.
class JsonParserX
{
public:
/// Symbol semantic values.
#ifndef YYSTYPE
union semantic_type
{
std::string* str;
int32_t int32_type;
int64_t int64_type;
uint32_t uint32_type;
uint64_t uint64_type;
double double_type;
triagens::basics::VariantArray* variantArray;
triagens::basics::VariantArray* keyValueList;
triagens::basics::VariantVector* variantVector;
triagens::basics::VariantVector* valueList;
triagens::basics::VariantObject* variantObject;
};
#else
typedef YYSTYPE semantic_type;
#endif
/// Symbol locations.
typedef location location_type;
/// Tokens.
struct token
{
/* Tokens. */
enum yytokentype {
END = 0,
DECIMAL_CONSTANT = 258,
DECIMAL_CONSTANT_STRING = 259,
IDENTIFIER = 260,
SIGNED_INTEGER_CONSTANT = 261,
SIGNED_INTEGER_CONSTANT_STRING = 262,
SIGNED_LONG_INTEGER_CONSTANT = 263,
SIGNED_LONG_INTEGER_CONSTANT_STRING = 264,
STRING_CONSTANT = 265,
UNSIGNED_INTEGER_CONSTANT = 266,
UNSIGNED_INTEGER_CONSTANT_STRING = 267,
UNSIGNED_LONG_INTEGER_CONSTANT = 268,
UNSIGNED_LONG_INTEGER_CONSTANT_STRING = 269,
AND = 270,
ASSIGN = 271,
CLOSE_BRACE = 272,
CLOSE_BRACKET = 273,
CLOSE_PAREN = 274,
COLON = 275,
COMMA = 276,
DOT = 277,
EQ = 278,
FALSE_CONSTANT = 279,
GE = 280,
GT = 281,
LE = 282,
LT = 283,
MINUS = 284,
NE = 285,
NULL_CONSTANT = 286,
OPEN_BRACE = 287,
OPEN_BRACKET = 288,
OPEN_PAREN = 289,
OR = 290,
PLUS = 291,
QUOTIENT = 292,
SEMICOLON = 293,
TIMES = 294,
TRUE_CONSTANT = 295,
STRING_CONSTANT_NULL = 296,
UNQUOTED_STRING = 297,
NEGATION = 298
};
};
/// Token type.
typedef token::yytokentype token_type;
/// Build a parser object.
JsonParserX (triagens::rest::JsonParserXDriver& driver_yyarg);
virtual ~JsonParserX ();
/// Parse.
/// \returns 0 iff parsing succeeded.
virtual int parse ();
#if YYDEBUG
/// The current debugging stream.
std::ostream& debug_stream () const;
/// Set the current debugging stream.
void set_debug_stream (std::ostream &);
/// Type for debugging levels.
typedef int debug_level_type;
/// The current debugging level.
debug_level_type debug_level () const;
/// Set the current debugging level.
void set_debug_level (debug_level_type l);
#endif
private:
/// Report a syntax error.
/// \param loc where the syntax error is found.
/// \param msg a description of the syntax error.
virtual void error (const location_type& loc, const std::string& msg);
/// Generate an error message.
/// \param state the state where the error occurred.
/// \param tok the lookahead token.
virtual std::string yysyntax_error_ (int yystate, int tok);
#if YYDEBUG
/// \brief Report a symbol value on the debug stream.
/// \param yytype The token type.
/// \param yyvaluep Its semantic value.
/// \param yylocationp Its location.
virtual void yy_symbol_value_print_ (int yytype,
const semantic_type* yyvaluep,
const location_type* yylocationp);
/// \brief Report a symbol on the debug stream.
/// \param yytype The token type.
/// \param yyvaluep Its semantic value.
/// \param yylocationp Its location.
virtual void yy_symbol_print_ (int yytype,
const semantic_type* yyvaluep,
const location_type* yylocationp);
#endif
/// State numbers.
typedef int state_type;
/// State stack type.
typedef stack<state_type> state_stack_type;
/// Semantic value stack type.
typedef stack<semantic_type> semantic_stack_type;
/// location stack type.
typedef stack<location_type> location_stack_type;
/// The state stack.
state_stack_type yystate_stack_;
/// The semantic value stack.
semantic_stack_type yysemantic_stack_;
/// The location stack.
location_stack_type yylocation_stack_;
/// Internal symbol numbers.
typedef unsigned char token_number_type;
/* Tables. */
/// For a state, the index in \a yytable_ of its portion.
static const signed char yypact_[];
static const signed char yypact_ninf_;
/// For a state, default rule to reduce.
/// Unless\a yytable_ specifies something else to do.
/// Zero means the default is an error.
static const unsigned char yydefact_[];
static const signed char yypgoto_[];
static const signed char yydefgoto_[];
/// What to do in a state.
/// \a yytable_[yypact_[s]]: what to do in state \a s.
/// - if positive, shift that token.
/// - if negative, reduce the rule which number is the opposite.
/// - if zero, do what YYDEFACT says.
static const unsigned char yytable_[];
static const signed char yytable_ninf_;
static const signed char yycheck_[];
/// For a state, its accessing symbol.
static const unsigned char yystos_[];
/// For a rule, its LHS.
static const unsigned char yyr1_[];
/// For a rule, its RHS length.
static const unsigned char yyr2_[];
#if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
/// For a symbol, its name in clear.
static const char* const yytname_[];
#endif
#if YYERROR_VERBOSE
/// Convert the symbol name \a n to a form suitable for a diagnostic.
virtual std::string yytnamerr_ (const char *n);
#endif
#if YYDEBUG
/// A type to store symbol numbers and -1.
typedef signed char rhs_number_type;
/// A `-1'-separated list of the rules' RHS.
static const rhs_number_type yyrhs_[];
/// For each rule, the index of the first RHS symbol in \a yyrhs_.
static const unsigned char yyprhs_[];
/// For each rule, its source line number.
static const unsigned short int yyrline_[];
/// For each scanner token number, its symbol number.
static const unsigned short int yytoken_number_[];
/// Report on the debug stream that the rule \a r is going to be reduced.
virtual void yy_reduce_print_ (int r);
/// Print the state stack on the debug stream.
virtual void yystack_print_ ();
/* Debugging. */
int yydebug_;
std::ostream* yycdebug_;
#endif
/// Convert a scanner token number \a t to a symbol number.
token_number_type yytranslate_ (int t);
/// \brief Reclaim the memory associated to a symbol.
/// \param yymsg Why this token is reclaimed.
/// \param yytype The symbol type.
/// \param yyvaluep Its semantic value.
/// \param yylocationp Its location.
inline void yydestruct_ (const char* yymsg,
int yytype,
semantic_type* yyvaluep,
location_type* yylocationp);
/// Pop \a n symbols the three stacks.
inline void yypop_ (unsigned int n = 1);
/* Constants. */
static const int yyeof_;
/* LAST_ -- Last index in TABLE_. */
static const int yylast_;
static const int yynnts_;
static const int yyempty_;
static const int yyfinal_;
static const int yyterror_;
static const int yyerrcode_;
static const int yyntokens_;
static const unsigned int yyuser_token_number_max_;
static const token_number_type yyundef_token_;
/* User arguments. */
triagens::rest::JsonParserXDriver& driver;
};
} } // triagens::json_parser
#endif /* ! defined PARSER_HEADER_H */

View File

@ -37,9 +37,9 @@
%define namespace "triagens::json_parser"
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
// preamble
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
%code requires {
#define NAME_SPACE triagens::json_parser
@ -54,18 +54,19 @@
#include <Basics/Common.h>
#include <Basics/StringUtils.h>
#include <Basics/VariantArray.h>
#include <Basics/VariantBoolean.h>
#include <Basics/VariantDouble.h>
#include <Basics/VariantInt32.h>
#include <Basics/VariantInt64.h>
#include <Basics/VariantNull.h>
#include <Basics/VariantObject.h>
#include <Basics/VariantString.h>
#include <Basics/VariantUInt32.h>
#include <Basics/VariantUInt64.h>
#include <Basics/VariantVector.h>
#include <JsonParserX/JsonParserXDriver.h>
#include <Variant/VariantArray.h>
#include <Variant/VariantBoolean.h>
#include <Variant/VariantDouble.h>
#include <Variant/VariantInt32.h>
#include <Variant/VariantInt64.h>
#include <Variant/VariantNull.h>
#include <Variant/VariantObject.h>
#include <Variant/VariantString.h>
#include <Variant/VariantUInt32.h>
#include <Variant/VariantUInt64.h>
#include <Variant/VariantVector.h>
#include "JsonParserX/JsonParserXDriver.h"
using namespace std;
using namespace triagens::basics;
@ -163,26 +164,26 @@ using namespace triagens::basics;
YY_DECL;
%}
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
// grammar
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
%%
%start jsonDefinition;
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
// precedence
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
%left NEGATION;
%left GE LE EQ NE GT LT;
%left PLUS MINUS;
%left TIMES QUOTIENT;
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
// DEFINITION FILE
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
jsonDefinition:
variantArray {
@ -357,9 +358,9 @@ variantObject:
%%
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
// postamble
// /////////////////////////////////////////////////////////////////////////////
// .............................................................................
void triagens::json_parser::JsonParserX::error (const triagens::json_parser::JsonParserX::location_type& l, const string& m) {
triagens::json_parser::position last = l.end - 1;

View File

@ -27,17 +27,17 @@
#include "JsonParserXDriver.h"
#include <Basics/Logger.h>
#include <Basics/VariantArray.h>
#include "Basics/VariantBoolean.h"
#include "Basics/VariantDouble.h"
#include "Basics/VariantNull.h"
#include <Basics/VariantInt32.h>
#include <Basics/VariantInt64.h>
#include <Basics/VariantString.h>
#include <Basics/VariantUInt32.h>
#include <Basics/VariantUInt64.h>
#include <Basics/VariantVector.h>
#include <Logger/Logger.h>
#include <Variant/VariantArray.h>
#include <Variant/VariantBoolean.h>
#include <Variant/VariantDouble.h>
#include <Variant/VariantInt32.h>
#include <Variant/VariantInt64.h>
#include <Variant/VariantNull.h>
#include <Variant/VariantString.h>
#include <Variant/VariantUInt32.h>
#include <Variant/VariantUInt64.h>
#include <Variant/VariantVector.h>
using namespace triagens::basics;

File diff suppressed because it is too large Load Diff

View File

@ -1,161 +0,0 @@
/* A Bison parser, made by GNU Bison 2.4.1. */
/* Locations for Bison parsers in C++
Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/**
** \file location.hh
** Define the triagens::json_parser::location class.
*/
#ifndef BISON_LOCATION_HH
# define BISON_LOCATION_HH
# include <iostream>
# include <string>
# include "position.hh"
namespace triagens { namespace json_parser {
/// Abstract a location.
class location
{
public:
/// Construct a location.
location ()
: begin (), end ()
{
}
/// Initialization.
inline void initialize (std::string* fn)
{
begin.initialize (fn);
end = begin;
}
/** \name Line and Column related manipulators
** \{ */
public:
/// Reset initial location to final location.
inline void step ()
{
begin = end;
}
/// Extend the current location to the COUNT next columns.
inline void columns (unsigned int count = 1)
{
end += count;
}
/// Extend the current location to the COUNT next lines.
inline void lines (unsigned int count = 1)
{
end.lines (count);
}
/** \} */
public:
/// Beginning of the located region.
position begin;
/// End of the located region.
position end;
};
/// Join two location objects to create a location.
inline const location operator+ (const location& begin, const location& end)
{
location res = begin;
res.end = end.end;
return res;
}
/// Add two location objects.
inline const location operator+ (const location& begin, unsigned int width)
{
location res = begin;
res.columns (width);
return res;
}
/// Add and assign a location.
inline location& operator+= (location& res, unsigned int width)
{
res.columns (width);
return res;
}
/// Compare two location objects.
inline bool
operator== (const location& loc1, const location& loc2)
{
return loc1.begin == loc2.begin && loc1.end == loc2.end;
}
/// Compare two location objects.
inline bool
operator!= (const location& loc1, const location& loc2)
{
return !(loc1 == loc2);
}
/** \brief Intercept output stream redirection.
** \param ostr the destination output stream
** \param loc a reference to the location to redirect
**
** Avoid duplicate information.
*/
inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
{
position last = loc.end - 1;
ostr << loc.begin;
if (last.filename
&& (!loc.begin.filename
|| *loc.begin.filename != *last.filename))
ostr << '-' << last;
else if (loc.begin.line != last.line)
ostr << '-' << last.line << '.' << last.column;
else if (loc.begin.column != last.column)
ostr << '-' << last.column;
return ostr;
}
} } // triagens::json_parser
#endif // not BISON_LOCATION_HH

View File

@ -1,157 +0,0 @@
/* A Bison parser, made by GNU Bison 2.4.1. */
/* Positions for Bison parsers in C++
Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/**
** \file position.hh
** Define the triagens::json_parser::position class.
*/
#ifndef BISON_POSITION_HH
# define BISON_POSITION_HH
# include <iostream>
# include <string>
# include <algorithm>
namespace triagens { namespace json_parser {
/// Abstract a position.
class position
{
public:
/// Construct a position.
position ()
: filename (0), line (1), column (1)
{
}
/// Initialization.
inline void initialize (std::string* fn)
{
filename = fn;
line = 1;
column = 1;
}
/** \name Line and Column related manipulators
** \{ */
public:
/// (line related) Advance to the COUNT next lines.
inline void lines (int count = 1)
{
column = 1;
line += count;
}
/// (column related) Advance to the COUNT next columns.
inline void columns (int count = 1)
{
column = std::max (1u, column + count);
}
/** \} */
public:
/// File name to which this position refers.
std::string* filename;
/// Current line number.
unsigned int line;
/// Current column number.
unsigned int column;
};
/// Add and assign a position.
inline const position&
operator+= (position& res, const int width)
{
res.columns (width);
return res;
}
/// Add two position objects.
inline const position
operator+ (const position& begin, const int width)
{
position res = begin;
return res += width;
}
/// Add and assign a position.
inline const position&
operator-= (position& res, const int width)
{
return res += -width;
}
/// Add two position objects.
inline const position
operator- (const position& begin, const int width)
{
return begin + -width;
}
/// Compare two position objects.
inline bool
operator== (const position& pos1, const position& pos2)
{
return
(pos1.filename == pos2.filename
|| pos1.filename && pos2.filename && *pos1.filename == *pos2.filename)
&& pos1.line == pos2.line && pos1.column == pos2.column;
}
/// Compare two position objects.
inline bool
operator!= (const position& pos1, const position& pos2)
{
return !(pos1 == pos2);
}
/** \brief Intercept output stream redirection.
** \param ostr the destination output stream
** \param pos a reference to the position to redirect
*/
inline std::ostream&
operator<< (std::ostream& ostr, const position& pos)
{
if (pos.filename)
ostr << *pos.filename << ':';
return ostr << pos.line << '.' << pos.column;
}
} } // triagens::json_parser
#endif // not BISON_POSITION_HH

View File

@ -1,133 +0,0 @@
/* A Bison parser, made by GNU Bison 2.4.1. */
/* Stack handling for Bison parsers in C++
Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef BISON_STACK_HH
# define BISON_STACK_HH
#include <deque>
namespace triagens { namespace json_parser {
template <class T, class S = std::deque<T> >
class stack
{
public:
// Hide our reversed order.
typedef typename S::reverse_iterator iterator;
typedef typename S::const_reverse_iterator const_iterator;
stack () : seq_ ()
{
}
stack (unsigned int n) : seq_ (n)
{
}
inline
T&
operator [] (unsigned int i)
{
return seq_[i];
}
inline
const T&
operator [] (unsigned int i) const
{
return seq_[i];
}
inline
void
push (const T& t)
{
seq_.push_front (t);
}
inline
void
pop (unsigned int n = 1)
{
for (; n; --n)
seq_.pop_front ();
}
inline
unsigned int
height () const
{
return seq_.size ();
}
inline const_iterator begin () const { return seq_.rbegin (); }
inline const_iterator end () const { return seq_.rend (); }
private:
S seq_;
};
/// Present a slice of the top of a stack.
template <class T, class S = stack<T> >
class slice
{
public:
slice (const S& stack,
unsigned int range) : stack_ (stack),
range_ (range)
{
}
inline
const T&
operator [] (unsigned int i) const
{
return stack_[range_ - i];
}
private:
const S& stack_;
unsigned int range_;
};
} } // triagens::json_parser
#endif // not BISON_STACK_HH[]dnl