forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToLatex.java
More file actions
145 lines (117 loc) · 4.67 KB
/
ToLatex.java
File metadata and controls
145 lines (117 loc) · 4.67 KB
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
// Copyright (c) 2019 K Team. All Rights Reserved.
package org.kframework.unparser;
import org.kframework.attributes.Att;
import org.kframework.kore.InjectedKLabel;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.KAs;
import org.kframework.kore.KRewrite;
import org.kframework.kore.KSequence;
import org.kframework.kore.KToken;
import org.kframework.kore.KVariable;
import org.kframework.utils.errorsystem.KEMException;
import org.kframework.utils.StringUtil;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;
/**
* Writes a KAST term to the LaTeX format.
*/
public class ToLatex {
public static byte[] apply(K k) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
apply(new DataOutputStream(out), k);
return out.toByteArray();
} catch (IOException e) {
throw KEMException.criticalError("Could not write K term to LaTeX", e, k);
}
}
private static String[] asciiReadableEncodingLatexCalc() {
String[] latexEncoder = Arrays.copyOf(StringUtil.asciiReadableEncodingDefault, StringUtil.asciiReadableEncodingDefault.length);
latexEncoder[0x30] = "Zero";
latexEncoder[0x31] = "I";
latexEncoder[0x32] = "II";
latexEncoder[0x33] = "III";
latexEncoder[0x34] = "IV";
latexEncoder[0x35] = "V";
latexEncoder[0x36] = "VI";
latexEncoder[0x37] = "VII";
latexEncoder[0x38] = "VIII";
latexEncoder[0x39] = "IX";
latexEncoder[0x7a] = "ActZ";
return latexEncoder;
}
public static final Pattern identChar = Pattern.compile("[A-Za-y]");
public static String[] asciiReadableEncodingLatex = asciiReadableEncodingLatexCalc();
public static String latexedKLabel(String orig) {
StringBuilder buffer = new StringBuilder();
StringUtil.encodeStringToAlphanumeric(buffer, orig, asciiReadableEncodingLatex, identChar, "z");
return "klabel" + buffer.toString();
}
private static void writeString(DataOutputStream out, String str) throws IOException {
out.write(str.getBytes(StandardCharsets.UTF_8));
}
public static void apply(DataOutputStream out, Att att) throws IOException {
writeString(out, ("\\outerAtt{" + att.toString() + "}"));
}
public static void apply(DataOutputStream out, K k) throws IOException {
if (k instanceof KToken) {
KToken tok = (KToken) k;
writeString(out, ("\\texttt{ " + tok.s() + " }"));
} else if (k instanceof KApply) {
KApply app = (KApply) k;
writeString(out, ("\\" + latexedKLabel(app.klabel().name())));
for (K item : app.klist().asIterable()) {
writeString(out, "{");
apply(out, item);
writeString(out, "}");
}
} else if (k instanceof KSequence) {
KSequence kseq = (KSequence) k;
writeString(out, "\\kseq{");
for (K item : kseq.asIterable()) {
apply(out, item);
writeString(out, "}{\\kseq{");
}
writeString(out, "}{\\dotk{}}");
} else if (k instanceof KVariable) {
KVariable var = (KVariable) k;
Optional<String> origName = var.att().getOptional("originalName");
if (origName.isPresent()) {
writeString(out, origName.get());
} else {
writeString(out, var.name());
}
} else if (k instanceof KRewrite) {
KRewrite rew = (KRewrite) k;
writeString(out, "\\krewrites{");
apply(out, rew.left());
writeString(out, "}{");
apply(out, rew.right());
writeString(out, "}{");
apply(out, rew.att());
writeString(out, "}");
} else if (k instanceof KAs) {
KAs alias = (KAs) k;
writeString(out, "\\kas{");
apply(out, alias.pattern());
writeString(out, "}{");
apply(out, alias.alias());
writeString(out, "}{");
apply(out, alias.att());
writeString(out, "}");
} else if (k instanceof InjectedKLabel) {
InjectedKLabel inj = (InjectedKLabel) k;
writeString(out, "\\injectedklabel{");
writeString(out, inj.klabel().name());
writeString(out, "}");
} else {
throw KEMException.criticalError("Unimplemented for LaTeX serialization: ", k);
}
}
}