summaryrefslogtreecommitdiff
path: root/cli/context.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-09-19 10:47:42 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-09-19 10:47:42 +0200
commit28b0b90f965f08c6363555b6e672da807f9faf7a (patch)
tree266bb76a474dd87aead1bfba01702b42812c5129 /cli/context.cxx
parentcb65012eb524eb57b00249f1dee0f245e947cda4 (diff)
Open output files and generate boilerplate code
Diffstat (limited to 'cli/context.cxx')
-rw-r--r--cli/context.cxx164
1 files changed, 162 insertions, 2 deletions
diff --git a/cli/context.cxx b/cli/context.cxx
index 3c62e38..d90df99 100644
--- a/cli/context.cxx
+++ b/cli/context.cxx
@@ -3,18 +3,178 @@
// copyright : Copyright (c) 2009 Code Synthesis Tools CC
// license : MIT; see accompanying LICENSE file
+#include <algorithm> // std::binary_search
+
#include "context.hxx"
+using namespace std;
+
context::
context (std::ostream& os_)
: data_ (new (shared) data),
- os (os_)
+ os (os_),
+ reserved_name_map (data_->reserved_name_map_)
{
}
context::
context (context& c)
: data_ (c.data_),
- os (c.os)
+ os (c.os),
+ reserved_name_map (c.reserved_name_map)
+{
+}
+
+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"
+ };
+}
+
+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
+ //
+ size const ks (sizeof (keywords) / sizeof (char*));
+
+ if (std::binary_search (keywords, keywords + ks, r))
+ {
+ 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;
}