summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-05-21 11:22:50 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-05-21 11:22:50 +0200
commitd50b3099ec02374e3c45782b7e1dca84bf53e376 (patch)
tree3ead6da5cb1eb491ac4bb2aa363147709d9f9318
parent1d805d74cc78ecf61e9530f1132faa9eae68287a (diff)
Add escape() function
-rw-r--r--odb/context.cxx164
-rw-r--r--odb/context.hxx11
2 files changed, 173 insertions, 2 deletions
diff --git a/odb/context.cxx b/odb/context.cxx
index 905d511..c90086e 100644
--- a/odb/context.cxx
+++ b/odb/context.cxx
@@ -7,6 +7,88 @@
using namespace std;
+namespace
+{
+ char const* keywords[] =
+ {
+ "NULL",
+ "and",
+ "asm",
+ "auto",
+ "bitand",
+ "bitor",
+ "bool",
+ "break",
+ "case",
+ "catch",
+ "char",
+ "class",
+ "compl",
+ "const",
+ "const_cast",
+ "continue",
+ "default",
+ "delete",
+ "do",
+ "double",
+ "dynamic_cast",
+ "else",
+ "end_eq",
+ "enum",
+ "explicit",
+ "export",
+ "extern",
+ "false",
+ "float",
+ "for",
+ "friend",
+ "goto",
+ "if",
+ "inline",
+ "int",
+ "long",
+ "mutable",
+ "namespace",
+ "new",
+ "not",
+ "not_eq",
+ "operator",
+ "or",
+ "or_eq",
+ "private",
+ "protected",
+ "public",
+ "register",
+ "reinterpret_cast",
+ "return",
+ "short",
+ "signed",
+ "sizeof",
+ "static",
+ "static_cast",
+ "struct",
+ "switch",
+ "template",
+ "this",
+ "throw",
+ "true",
+ "try",
+ "typedef",
+ "typeid",
+ "typename",
+ "union",
+ "unsigned",
+ "using",
+ "virtual",
+ "void",
+ "volatile",
+ "wchar_t",
+ "while",
+ "xor",
+ "xor_eq"
+ };
+}
+
context::
context (ostream& os_,
semantics::unit& unit_,
@@ -14,8 +96,11 @@ context (ostream& os_,
: data_ (new (shared) data),
os (os_),
unit (unit_),
- options (ops)
+ options (ops),
+ keyword_set (data_->keyword_set_)
{
+ for (size_t i (0); i < sizeof (keywords) / sizeof (char*); ++i)
+ data_->keyword_set_.insert (keywords[i]);
}
context::
@@ -23,10 +108,85 @@ context (context& c)
: data_ (c.data_),
os (c.os),
unit (c.unit),
- options (c.options)
+ options (c.options),
+ keyword_set (c.keyword_set)
{
}
+string context::
+escape (string const& name) const
+{
+ typedef string::size_type size;
+
+ string r;
+ size n (name.size ());
+
+ // In most common cases we will have that many characters.
+ //
+ r.reserve (n);
+
+ for (size i (0); i < n; ++i)
+ {
+ char c (name[i]);
+
+ if (i == 0)
+ {
+ if (!((c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') ||
+ c == '_'))
+ r = (c >= '0' && c <= '9') ? "cxx_" : "cxx";
+ }
+
+ if (!((c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') ||
+ c == '_'))
+ r += '_';
+ else
+ r += c;
+ }
+
+ if (r.empty ())
+ r = "cxx";
+
+ // Custom reserved words.
+ //
+ /*
+ reserved_name_map_type::const_iterator i (reserved_name_map.find (r));
+
+ if (i != reserved_name_map.end ())
+ {
+ if (!i->second.empty ())
+ return i->second;
+ else
+ r += L'_';
+ }
+ */
+
+ // Keywords
+ //
+ if (keyword_set.find (r) != keyword_set.end ())
+ {
+ r += '_';
+
+ // Re-run custom words.
+ //
+ /*
+ i = reserved_name_map.find (r);
+
+ if (i != reserved_name_map.end ())
+ {
+ if (!i->second.empty ())
+ return i->second;
+ else
+ r += L'_';
+ }
+ */
+ }
+
+ return r;
+}
+
// namespace
//
diff --git a/odb/context.hxx b/odb/context.hxx
index c91c260..057b821 100644
--- a/odb/context.hxx
+++ b/odb/context.hxx
@@ -6,6 +6,7 @@
#ifndef ODB_CONTEXT_HXX
#define ODB_CONTEXT_HXX
+#include <set>
#include <string>
#include <ostream>
#include <cstddef> // std::size_t
@@ -27,6 +28,12 @@ public:
typedef std::string string;
typedef ::options options_type;
+public:
+ // Escape C++ keywords, reserved names, and illegal characters.
+ //
+ string
+ escape (string const&) const;
+
private:
struct data;
cutl::shared_ptr<data> data_;
@@ -36,9 +43,13 @@ public:
semantics::unit& unit;
options_type const& options;
+ typedef std::set<string> keyword_set_type;
+ keyword_set_type const& keyword_set;
+
private:
struct data
{
+ keyword_set_type keyword_set_;
};
public: