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