Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Created September 19, 2021 16:02
Show Gist options
  • Select an option

  • Save UplinkCoder/2b20b2826fc66e397b44211e94e8e972 to your computer and use it in GitHub Desktop.

Select an option

Save UplinkCoder/2b20b2826fc66e397b44211e94e8e972 to your computer and use it in GitHub Desktop.
import std.file;
void main(string[] args)
{
// () @safe { assert(args.length == 2, "exactly one argument must be given") } ();
auto text = cast(string) read(args[1]);
long indent_level;
long beginParenPos;
bool inString = false;
bool inStruct = false;
bool inArray = false;
char[] out_buffer;
foreach(i,c;text)
{
auto p = i;
if (c == '"')
inString = !inString;
if (!inString)
{
if (c == '(' || c == '[')
{
indent_level++;
out_buffer ~= "\n";
foreach(_;0 .. indent_level)
out_buffer ~= " ";
}
if (c == ')' || c == ']')
{
out_buffer ~= "\n";
foreach(_;0 .. indent_level)
out_buffer ~= " ";
indent_level--;
}
}
out_buffer ~= c;
/+
if (c == ',' && !inString)
{
out_buffer ~= "\n";
foreach(_;0 .. indent_level)
out_buffer ~= " ";
}
+/
}
(args[1] ~ ".tree").write(cast(void[])out_buffer);;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment