79
|
1 /**
|
|
2 * this implements the JSON capability
|
|
3 */
|
|
4 module dmd.Json;
|
|
5
|
114
|
6 import dmd.common;
|
79
|
7 import dmd.String;
|
|
8 import std.stdio : write, writef;
|
|
9
|
|
10 import dmd.Array;
|
|
11 import dmd.File;
|
|
12 import dmd.FileName;
|
|
13 import dmd.Global;
|
|
14 import dmd.Module;
|
|
15 import dmd.OutBuffer;
|
|
16
|
|
17 immutable Pname = "name";
|
|
18 immutable Pkind = "kind";
|
|
19 immutable Pfile = "file";
|
|
20 immutable Pline = "line";
|
|
21 immutable Ptype = "type";
|
|
22 immutable Pcomment = "comment";
|
|
23 immutable Pmembers = "members";
|
|
24
|
|
25 void json_generate(Array modules)
|
|
26 {
|
|
27 OutBuffer buf;
|
|
28
|
96
|
29 buf.writestring("[\n");
|
79
|
30 for (int i = 0; i < modules.dim; i++)
|
|
31 {
|
|
32 Module m = cast(Module)modules.data[i];
|
|
33 if (global.params.verbose)
|
|
34 writef("json gen %s\n", m.toChars());
|
|
35 m.toJsonBuffer(buf);
|
96
|
36 buf.writestring(",\n");
|
79
|
37 }
|
96
|
38 JsonRemoveComma(buf);
|
|
39 buf.writestring("]\n");
|
79
|
40
|
|
41 // Write buf to file
|
|
42 string arg = global.params.xfilename;
|
|
43 if (arg is null || arg[0] == 0)
|
|
44 { // Generate lib file name from first obj name
|
|
45 String n2 = cast(String) global.params.objfiles.data[0];
|
|
46
|
|
47 string n = FileName.name(n2.toChars());
|
|
48 FileName fn = FileName.forceExt(n, global.json_ext);
|
|
49 arg = fn.toChars();
|
|
50 }
|
|
51 else if (arg[0] == '-' && arg[1] == 0)
|
|
52 {
|
|
53 // Write to stdout
|
|
54 write(buf.data[0..buf.offset]);
|
|
55 return;
|
|
56 }
|
|
57 // if (!FileName.absolute(arg))
|
|
58 // arg = FileName.combine(dir, arg);
|
|
59 FileName jsonfilename = FileName.defaultExt(arg, global.json_ext);
|
|
60 File jsonfile = new File(jsonfilename);
|
|
61 assert(jsonfile);
|
|
62 jsonfile.setbuffer(buf.data, buf.offset);
|
|
63 jsonfile.ref_ = 1;
|
|
64 string pt = FileName.path(jsonfile.toChars());
|
|
65 if (pt[0] != 0)
|
|
66 FileName.ensurePathExists(pt);
|
|
67 // mem.free(pt);
|
|
68 jsonfile.writev();
|
|
69 }
|
|
70
|
|
71
|
|
72 /*********************************
|
|
73 * Encode string into buf, and wrap it in double quotes.
|
|
74 */
|
|
75 void JsonString(OutBuffer buf, const(char)[] s)
|
|
76 {
|
|
77 buf.writeByte('\"');
|
|
78 foreach (c; s)
|
|
79 {
|
|
80 switch (c)
|
|
81 {
|
|
82 case '\n':
|
|
83 buf.writestring(`\n`);
|
|
84 break;
|
|
85
|
|
86 case '\r':
|
|
87 buf.writestring(`\r`);
|
|
88 break;
|
|
89
|
|
90 case '\t':
|
|
91 buf.writestring(`\t`);
|
|
92 break;
|
|
93
|
|
94 case '\"':
|
|
95 buf.writestring(`\"`);
|
|
96 break;
|
|
97
|
|
98 case '\\':
|
|
99 buf.writestring(`\\`);
|
|
100 break;
|
|
101
|
|
102 case '/':
|
|
103 buf.writestring(`\/`);
|
|
104 break;
|
|
105
|
|
106 case '\b':
|
|
107 buf.writestring(`\b`);
|
|
108 break;
|
|
109
|
|
110 case '\f':
|
|
111 buf.writestring(`\f`);
|
|
112 break;
|
|
113
|
|
114 default:
|
|
115 if (c < 0x20)
|
|
116 buf.printf("\\u%04x", c);
|
|
117 else
|
|
118 // Note that UTF-8 chars pass through here just fine
|
|
119 buf.writeByte(c);
|
|
120 break;
|
|
121 }
|
|
122 }
|
|
123 buf.writeByte('\"');
|
|
124 }
|
|
125
|
|
126 void JsonProperty(OutBuffer buf, const(char)[] name, const(char)[] value)
|
|
127 {
|
|
128 JsonString(buf, name);
|
|
129 buf.writestring(" : ");
|
|
130 JsonString(buf, value);
|
|
131 buf.writestring(",\n");
|
|
132 }
|
|
133
|
|
134 void JsonProperty(OutBuffer buf, const(char)[] name, int value)
|
|
135 {
|
|
136 JsonString(buf, name);
|
|
137 buf.writestring(" : ");
|
|
138 buf.printf("%d", value);
|
|
139 buf.writestring(",\n");
|
|
140 }
|
|
141
|
|
142 void JsonRemoveComma(OutBuffer buf)
|
|
143 {
|
|
144 if (buf.offset >= 2 &&
|
|
145 buf.data[buf.offset - 2] == ',' &&
|
|
146 buf.data[buf.offset - 1] == '\n')
|
|
147 buf.offset -= 2;
|
|
148 } |