Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions dstep/translator/Identifier.d
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 10 additions & 3 deletions dstep/translator/Options.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module dstep.translator.Options;
import clang.Util;

import dstep.translator.ConvertCase;
import dstep.translator.Identifier;

enum Language
{
Expand Down Expand Up @@ -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('.');
}
}

Expand All @@ -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_");
}
170 changes: 1 addition & 169 deletions dstep/translator/Translator.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}