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 = Indent.close, string f = __FILE__, int l = __LINE__)
void push(Indent indent, string f = __FILE__, int l = __LINE__)

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 = Indent.none, string f = __FILE__, int l = __LINE__)
void put(Indent indent)

Places str into the buffer.

put
void put(CodeBuilder build)
rawPush
void rawPush(string str = null, Indent indent = Indent.close, string f = __FILE__, int l = __LINE__)

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

rawPut
void rawPut(string str, Indent indent = Indent.none, string f = __FILE__, int l = __LINE__)

Places str into the buffer.

Static functions

opCall
CodeBuilder opCall(int indentedCount)

Examples

Example usage

1 auto indentCount = 0;
2 auto code = CodeBuilder(indentCount);
3 code.put("void main() {\n", Indent.open);
4 code.push("}\n");
5 code.put("int a = 5;\n");
6 code.put("multiply(a);\n");
7 code.pop();
8 
9 code.put("\nvoid multiply(int v) {\n", Indent.open);
10 code.push("}\n");
11 code.put("try {\n", Indent.open);
12 auto catchblock = CodeBuilder(1);
13 catchblock.put("} catch(Exception e) {\n", Indent.close | Indent.open);
14 catchblock.put("import std.stdio;\n");
15 catchblock.put("writeln(`Exception is bad but I don't care.`);\n");
16 catchblock.put("}\n", Indent.close);
17 code.push(catchblock);
18 
19 code.put("return v * ");
20 code.rawPut(76.to!string ~ ";\n");

Meta