diff --git a/dstep/translator/Identifier.d b/dstep/translator/Identifier.d new file mode 100644 index 00000000..baf090e1 --- /dev/null +++ b/dstep/translator/Identifier.d @@ -0,0 +1,173 @@ +/** + * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) + */ +module dstep.translator.Identifier; + +bool isDKeyword (string str) +{ + switch (str) + { + case "abstract": + case "alias": + case "align": + case "asm": + case "assert": + case "auto": + + case "body": + case "bool": + case "break": + case "byte": + + case "case": + case "cast": + case "catch": + case "cdouble": + case "cent": + case "cfloat": + case "char": + case "class": + case "const": + case "continue": + case "creal": + + case "dchar": + case "debug": + case "default": + case "delegate": + case "delete": + case "deprecated": + case "do": + case "double": + + case "else": + case "enum": + case "export": + case "extern": + + case "false": + case "final": + case "finally": + case "float": + case "for": + case "foreach": + case "foreach_reverse": + case "function": + + case "goto": + + case "idouble": + case "if": + case "ifloat": + case "import": + case "in": + case "inout": + case "int": + case "interface": + case "invariant": + case "ireal": + case "is": + + case "lazy": + case "long": + + case "macro": + case "mixin": + case "module": + + case "new": + case "nothrow": + case "null": + + case "out": + case "override": + + case "package": + case "pragma": + case "private": + case "protected": + case "public": + case "pure": + + case "real": + case "ref": + case "return": + + case "scope": + case "shared": + case "short": + case "static": + case "struct": + case "super": + case "switch": + case "synchronized": + + case "template": + case "this": + case "throw": + case "true": + case "try": + case "typedef": + case "typeid": + case "typeof": + + case "ubyte": + case "ucent": + case "uint": + case "ulong": + case "union": + case "unittest": + case "ushort": + + case "version": + case "void": + case "volatile": + + case "wchar": + case "while": + case "with": + + case "__FILE__": + case "__LINE__": + case "__DATE__": + case "__TIME__": + case "__TIMESTAMP__": + case "__VENDOR__": + case "__VERSION__": + return true; + + default: break; + } + + if (true /*D2*/) + { + switch (str) + { + case "immutable": + case "nothrow": + case "pure": + case "shared": + + case "__gshared": + case "__thread": + case "__traits": + + case "__EOF__": + return true; + + default: return str.length && str[0] == '@'; + } + } + + return false; +} + +string renameDKeyword (string str) +{ + return str ~ '_'; +} + +string translateIdentifier (string str) +{ + return isDKeyword(str) ? renameDKeyword(str) : str; +} diff --git a/dstep/translator/Options.d b/dstep/translator/Options.d index d1ec6d68..7d9af330 100644 --- a/dstep/translator/Options.d +++ b/dstep/translator/Options.d @@ -9,6 +9,7 @@ module dstep.translator.Options; import clang.Util; import dstep.translator.ConvertCase; +import dstep.translator.Identifier; enum Language { @@ -103,12 +104,14 @@ string fullModuleName(string packageName, string path, bool normalize = true) if (normalize) { auto segments = moduleName.split!(x => x == '.'); - auto normalized = segments.map!(x => x.toUTF8.toSnakeCase).join('.'); - return only(packageName, normalized).join('.'); + auto normalizedSegments = segments.map!(x => translateIdentifier(x.toUTF8.toSnakeCase)); + return chain(only(packageName), normalizedSegments).join('.'); } else { - return only(packageName, moduleName.toUTF8).join('.'); + auto segments = moduleName.split!(x => x == '.'); + auto translatedSegments = segments.map!(x => translateIdentifier(x.toUTF8)); + return chain(only(packageName), translatedSegments).join('.'); } } @@ -131,7 +134,11 @@ unittest assert(fullModuleName("pkg", "FooBarBaz.ext") == "pkg.foo_bar_baz"); assert(fullModuleName("pkg", "FooBar.BazQux.ext") == "pkg.foo_bar.baz_qux"); + assert(fullModuleName("pkg", "in.h") == "pkg.in_"); + assert(fullModuleName("pkg", "foo-in.h") == "pkg.foo.in_"); assert(fullModuleName("pkg", "FooBarBaz.ext", false) == "pkg.FooBarBaz"); assert(fullModuleName("pkg", "FooBar.BazQux.ext", false) == "pkg.FooBar.BazQux"); + assert(fullModuleName("pkg", "in.h", false) == "pkg.in_"); + assert(fullModuleName("pkg", "foo-in.h", false) == "pkg.foo.in_"); } diff --git a/dstep/translator/Translator.d b/dstep/translator/Translator.d index 2555aa40..234a389c 100644 --- a/dstep/translator/Translator.d +++ b/dstep/translator/Translator.d @@ -33,6 +33,7 @@ import dstep.translator.Record; import dstep.translator.Type; import dstep.translator.TypeInference; +public import dstep.translator.Identifier; public import dstep.translator.Options; class TranslationException : DStepException @@ -498,172 +499,3 @@ void handleInclude (Context context, Type type) context.includeHandler.addInclude(type.declaration.path); } } - -bool isDKeyword (string str) -{ - switch (str) - { - case "abstract": - case "alias": - case "align": - case "asm": - case "assert": - case "auto": - - case "body": - case "bool": - case "break": - case "byte": - - case "case": - case "cast": - case "catch": - case "cdouble": - case "cent": - case "cfloat": - case "char": - case "class": - case "const": - case "continue": - case "creal": - - case "dchar": - case "debug": - case "default": - case "delegate": - case "delete": - case "deprecated": - case "do": - case "double": - - case "else": - case "enum": - case "export": - case "extern": - - case "false": - case "final": - case "finally": - case "float": - case "for": - case "foreach": - case "foreach_reverse": - case "function": - - case "goto": - - case "idouble": - case "if": - case "ifloat": - case "import": - case "in": - case "inout": - case "int": - case "interface": - case "invariant": - case "ireal": - case "is": - - case "lazy": - case "long": - - case "macro": - case "mixin": - case "module": - - case "new": - case "nothrow": - case "null": - - case "out": - case "override": - - case "package": - case "pragma": - case "private": - case "protected": - case "public": - case "pure": - - case "real": - case "ref": - case "return": - - case "scope": - case "shared": - case "short": - case "static": - case "struct": - case "super": - case "switch": - case "synchronized": - - case "template": - case "this": - case "throw": - case "true": - case "try": - case "typedef": - case "typeid": - case "typeof": - - case "ubyte": - case "ucent": - case "uint": - case "ulong": - case "union": - case "unittest": - case "ushort": - - case "version": - case "void": - case "volatile": - - case "wchar": - case "while": - case "with": - - case "__FILE__": - case "__LINE__": - case "__DATE__": - case "__TIME__": - case "__TIMESTAMP__": - case "__VENDOR__": - case "__VERSION__": - return true; - - default: break; - } - - if (true /*D2*/) - { - switch (str) - { - case "immutable": - case "nothrow": - case "pure": - case "shared": - - case "__gshared": - case "__thread": - case "__traits": - - case "__EOF__": - return true; - - default: return str.length && str[0] == '@'; - } - } - - return false; -} - -string renameDKeyword (string str) -{ - return str ~ '_'; -} - -string translateIdentifier (string str) -{ - return isDKeyword(str) ? renameDKeyword(str) : str; -}