forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalt.bal
195 lines (178 loc) · 5.88 KB
/
balt.bal
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import wso2/nballerina.comm.err;
import wso2/nballerina.nback;
import ballerina/io;
import ballerina/file;
type TestKind "output" | "panic" | "error" | "parser-error";
type BaltTestHeader record {|
TestKind 'Test\-Case;
string...;
|};
type BaltTestCase record {|
int offset;
BaltTestHeader header;
string[] content;
|};
enum State {
BOF,
HEADER,
CONTENT
}
// " " is a good approximation
// http://www.bitdance.com/blog/2011/04/11_01_Email6_Rewriting_Header_Folding/
const CONTINUATION_WS = " ";
function compileBaltFile(string filename, string basename, string outDir, nback:Options nbackOptions, Options options) returns error? {
BaltTestCase[] tests = check parseBalt(filename);
foreach var [i, t] in tests.enumerate() {
if t.header["Fail-Issue"] != () {
continue;
}
string outBasename = check chooseBaltCaseOutputFilename(filename, t, i);
string[] lines = t.content;
LlvmEmitter emitter = new(check file:joinPath(outDir, outBasename), nbackOptions, options);
CompileError? err = compileBalFile({ lines }, basename, emitter);
if t.header.Test\-Case is "panic" && err != () {
continue;
}
if t.header.Test\-Case is "parser-error"|"error" {
if err is () {
panic error("expected error in " + filename + " test: " + (i + 1).toString());
}
}
else if err != () {
io:println("unexpected error in ", filename, " test: ", i + 1);
return err;
}
string? expectOutDir = options.expectOutDir;
string expectFilename = check file:joinPath(expectOutDir ?: outDir, outBasename) + ".txt";
check io:fileWriteLines(expectFilename, expect(t.content));
}
}
function parseBalt(string path) returns BaltTestCase[]|io:Error|file:Error|err:Diagnostic {
BaltTestCase[] tests = [];
string[] lines = check io:fileReadLines(path);
BaltTestHeader? maybeHeader = ();
string? prevFiledBody = ();
string? prevFiledName = ();
string[] content = [];
int offset = 0;
State s = BOF;
foreach var [i, l] in lines.enumerate() {
if l.startsWith("Test-Case:") {
if s != BOF {
BaltTestHeader header = <BaltTestHeader>maybeHeader;
tests.push({ offset, header, content });
}
s = HEADER;
offset = i + 1;
content = [];
var [fName, fBody] = parseField(l);
maybeHeader = {Test\-Case: <TestKind>fBody};
prevFiledBody = fBody;
prevFiledName = fName;
}
else if s == HEADER {
if l == "" {
s = CONTENT;
}
else {
BaltTestHeader header = <BaltTestHeader>maybeHeader;
// possible improvement: support other white spaces
if l.startsWith(" ") {
if prevFiledName == () || prevFiledBody == () {
panic err:impossible("folded field body without field");
}
header[<string>prevFiledName] = <string>prevFiledBody + l;
} else {
var [fName, fBody] = parseField(l);
header[fName] = fBody;
prevFiledName = fName;
prevFiledBody = fBody;
}
}
}
else if s == CONTENT {
content.push(l);
}
else {
BOF _ = s;
// xxx add file path
return error("file should start with 'Test-Case:' header field");
}
}
if s == HEADER {
return error("header without content at EOF ");
}
else if s == BOF {
return error("empty file");
}
else {
BaltTestHeader header = <BaltTestHeader>maybeHeader;
tests.push({offset, header, content});
}
return tests;
}
function parseField(string s) returns [string, string] {
int? i = s.indexOf(":");
if i == () {
panic error("Missing `:` in field : " + s);
} else {
string fieldName = s.substring(0, i);
string fieldValue = s.substring(i + 1);
return [fieldName, fieldValue.trim()];
}
}
const OUTPUT_MARKER = "@output";
const ERROR_MARKER = "@error";
const PANIC_MARKER = "@panic";
function expect(string[] src) returns string[] {
string[] expect = [];
foreach var [i, l] in src.enumerate() {
int? comment = l.indexOf("//");
if comment == () {
continue;
}
int begin = <int> comment + 2;
int? output = l.indexOf(OUTPUT_MARKER, begin);
if output != () {
expect.push(l.substring(output + OUTPUT_MARKER.length() + 1));
}
int? err = l.indexOf(ERROR_MARKER, begin);
if err != () {
expect.push(ERROR_MARKER);
}
int? pnk = l.indexOf(PANIC_MARKER, begin);
if pnk != () {
string msg = l.substring(pnk + PANIC_MARKER.length() + 1);
expect.push("panic: line " + (i + 1).toString() + ": " + msg);
}
}
return expect;
}
function chooseBaltCaseOutputFilename(string filename, BaltTestCase t, int i) returns string|file:Error {
string basename = check file:basename(filename);
basename = basename.substring(0, basename.length() - 5);
return basename + pad4(i.toString()) + "L" + pad4(t.offset.toString()) + "-" + testKindToLetter(t.header.Test\-Case);
}
function testKindToLetter(TestKind k) returns string:Char {
match k {
"error"|"parser-error" => {
return "e";
}
"panic" => {
return "p";
}
"output" => {
return "v";
}
_ => {
panic err:impossible();
}
}
}
function pad4(string s) returns string {
int len = s.length() ;
if len < 4 {
return "0000".substring(len) + s;
}
return s;
}