Last active
August 29, 2015 14:04
-
-
Save Hackerpilot/5ff6d86f4d22a14a00f3 to your computer and use it in GitHub Desktop.
DIP65 Upgrade Tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't use this file. Use this instead: https://github.com/Hackerpilot/dfix | |
// Automatically upgrades code to comply with DIP65. | |
// Requires libdparse: https://github.com/Hackerpilot/libdparse | |
module dfix; | |
import std.lexer; | |
import std.d.lexer; | |
import std.array; | |
import std.stdio; | |
void main(string[] args) | |
{ | |
File input = File(args[1]); | |
File output = args.length > 2 ? File(args[2]) : stdout; | |
ubyte[] inputBytes = uninitializedArray!(ubyte[])(input.size); | |
input.rawRead(inputBytes); | |
StringCache cache = StringCache(StringCache.defaultBucketCount); | |
LexerConfig config; | |
config.fileName = args[1]; | |
config.stringBehavior = StringBehavior.source; | |
auto tokens = byToken(inputBytes, config, &cache).array; | |
foreach (i; 0 .. tokens.length) | |
{ | |
switch (tokens[i].type) | |
{ | |
case tok!"catch": | |
size_t j = i + 1; | |
while (j < tokens.length && (tokens[j] == tok!"whitespace" || tokens[j] == tok!"comment")) | |
j++; | |
if (j < tokens.length && tokens[j].type != tok!"(") | |
{ | |
output.write("catch (Throwable)"); | |
break; | |
} | |
else | |
goto default; | |
default: | |
output.write(tokens[i].text is null | |
? str(tokens[i].type) | |
: tokens[i].text); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment