CodeBuilder

An output range that provides extended functionality for constructing a well formatted string of code.

CodeBuilder has three main operations.

  • put - Applies string directly to desired output
  • push - Stacks string for later
  • build - Constructs a sequence of strings

put

The main operation for placing code into the buffer. This will sequentially place code into the buffer. Similar operations are provided for the other operations.

rawPut will place the string without indentation added.

One can place the current indentation level without code by calling put("");

push

This places code onto a stack. pop can be used to put the code into the buffer.

build

Building code in a sequence can be pushed onto the stack, or saved to be put into the buffer later.

Members

Functions

finalize
string finalize()

Returns the buffer, applying an code remaining on the stack.

pop
void pop()

Places the top stack item into the buffer.

push
void push(string str, Indent indent, string f, int l)
void push(Indent indent, string f, int l)

Places str onto a stack that can latter be popped into the current buffer.

push
void push(CodeBuilder build)
put
void put(string str, Indent indent, string f, int l)
void put(Indent indent)

Places str into the buffer.

put
void put(CodeBuilder build)
rawPush
void rawPush(string str, Indent indent, string f, int l)

Places str onto a stack that can latter be popped into the current buffer.

rawPut
void rawPut(string str, Indent indent, string f, int l)

Places str into the buffer.

Static functions

opCall
CodeBuilder opCall(int indentedCount)

Structs

Operation
struct Operation
Undocumented in source.

Variables

indentCount
int indentCount;
Undocumented in source.
modifyLine
bool modifyLine;
Undocumented in source.

Examples

Example usage

auto indentCount = 0;
auto code = CodeBuilder(indentCount);
code.put("void main() {\n", Indent.open);
code.push("}\n");
code.put("int a = 5;\n");
code.put("multiply(a);\n");
code.pop();

code.put("\nvoid multiply(int v) {\n", Indent.open);
code.push("}\n");
code.put("try {\n", Indent.open);
auto catchblock = CodeBuilder(1);
catchblock.put("} catch(Exception e) {\n", Indent.close | Indent.open);
catchblock.put("import std.stdio;\n");
catchblock.put("writeln(`Exception is bad but I don't care.`);\n");
catchblock.put("}\n", Indent.close);
code.push(catchblock);

code.put("return v * ");
code.rawPut(76.to!string ~ ";\n");

Meta