aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/SourceCodeWriter.java
blob: 6d26e8b32b046061f8bcc4b19ba057b083d7a8fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.zwobble.couscous.backends;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import org.zwobble.couscous.util.Action;

import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;

public class SourceCodeWriter {
    public interface WriterAction {
        void run(SourceCodeWriter writer);
    }

    private static final int SPACES_PER_INDENT = 4;
    private final StringBuilder builder = new StringBuilder();

    private final WriterAction blockStart;
    private final WriterAction blockEnd;
    private final Set<String> reservedIdentifiers;
    private final Function<String, String> mangleReservedIdentifier;
    private int depth = 0;

    public SourceCodeWriter(WriterAction blockStart, WriterAction blockEnd, Set<String> reservedIdentifiers, Function<String, String> mangleReservedIdentifier) {
        this.blockStart = blockStart;
        this.blockEnd = blockEnd;
        this.reservedIdentifiers = reservedIdentifiers;
        this.mangleReservedIdentifier = mangleReservedIdentifier;
    }
    
    public String asString() {
        return builder.toString();
    }

    public void writeInteger(int value) {
        builder.append(value);
    }

    public void writeChar(char value) {
        builder.append("'");
        if (value == '\'') {
            builder.append("\\'");
        } else if (value == '\n') {
            builder.append("\\n");
        } else if (value == '\r') {
            builder.append("\\r");
        } else if (value == '\t') {
            builder.append("\\t");
        } else {
            builder.append(value);
        }
        builder.append("'");
    }

    public void writeStringLiteral(String value) {
        builder.append("\"");
        builder.append(value
            .replace("\\", "\\\\")
            .replace("\"", "\\\"")
            .replace("\n", "\\n")
            .replace("\r", "\\r")
            .replace("\t", "\\t"));
        builder.append("\"");
    }

    public void writeKeyword(String keyword) {
        builder.append(keyword);
    }

    public void writeSpace() {
        builder.append(" ");
    }

    public void writeIdentifier(String name) {
        String nameToWrite = reservedIdentifiers.contains(name)
            ? mangleReservedIdentifier.apply(name)
            : name;
        builder.append(nameToWrite);
    }

    public void writeSymbol(String symbol) {
        builder.append(symbol);
    }

    public void startBlock() {
        runAction(blockStart);
        indent();
        builder.append("\n");
    }

    public void endBlock() {
        dedent();
        runAction(blockEnd);
    }

    public void indent() {
        depth++;
    }

    public void dedent() {
        depth--;
    }

    public void writeStatement(Action action) {
        startStatement();
        action.run();
        endStatement();
    }

    private void startStatement() {
        writeIndentation();
    }

    public void writeIndentation() {
        builder.append(Strings.repeat(" ", depth * SPACES_PER_INDENT));
    }

    private void endStatement() {
        if (builder.charAt(builder.length() - 1) != '\n') {
            builder.append("\n");   
        }
    }

    private void runAction(WriterAction action) {
        action.run(this);
    }

    public <T> void writeCommaSeparated(Iterable<T> values, Consumer<T> writeValue) {
        writeWithSeparator(values, writeValue, () -> {
            writeSymbol(",");
            writeSpace();
        });
    }

    public <T> void writeWithSeparator(Iterable<T> values, Consumer<T> writeValue, Action separator) {
        Iterator<T> iterator = values.iterator();
        if (iterator.hasNext()) {
            writeValue.accept(iterator.next());
            while(iterator.hasNext()) {
                separator.run();
                writeValue.accept(iterator.next());
            }
        }
    }
}