-
Notifications
You must be signed in to change notification settings - Fork 1
/
md2sexpr.d
85 lines (85 loc) · 2.35 KB
/
md2sexpr.d
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
import parser;
import std.string;
private string escape(string text) {
return `"` ~ text.replace(`\`, `\\`).replace(`"`, `\"`) ~ `"`;
}
struct ConvertToSExprEventHandler {
string start(string text) {
return " (" ~ escape(text);
}
string end() {
return ")";
}
}
struct ConvertToSExprListEventHandler {
int[] nchildren;
string start(string text) {
int current;
if (nchildren.length) {
nchildren[$-1]++;
current = nchildren[$-1];
} else {
current = 0;
}
nchildren ~= 0;
// If we are starting a list with more than one child, add 2 parens
if (current == 1) {
return " ((" ~ escape(text);
} else {
return " (" ~ escape(text);
}
}
string end() {
// If we are ending a list with more than one child, add 2 parens
if (nchildren.length > 0 && nchildren[$-1] > 0) {
nchildren.length--;
return "))";
} else {
nchildren.length--;
return ")";
}
}
}
void main(string[] args) {
import std.stdio, std.file;
string[] inputnames;
string[] inputs;
args = args[1..$]; // skip first arg, which is name of binary
bool list = false;
if (args.length > 0 && args[0] == "-l") {
args = args[1..$];
list = true;
}
if (args.length == 0) {
// stdin
string input;
string line;
while ((line = readln()) !is null)
input ~= line;
inputnames ~= "stdin";
inputs ~= input;
}
void readRestArgs() {
foreach (filename; args) {
inputnames ~= filename;
inputs ~= filename.readText;
}
}
if (list) {
readRestArgs();
for (int i = 0; i < inputs.length; ++i) {
string output;
parse!(ConvertToSExprListEventHandler, s => output ~= s, s => output ~=s)(inputs[i], 4, inputnames[i]);
// Write each on a separate lines
writeln(output);
}
} else {
readRestArgs();
for (int i = 0; i < inputs.length; ++i) {
string output;
parse!(ConvertToSExprEventHandler, s => output ~= s, s => output ~=s)(inputs[i], 4, inputnames[i]);
// Write each on separate lines
writeln(output);
}
}
}