summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/webidl2/lib/writer.js
blob: ba8363738235c396225cf3696abb60a44cde05d1 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

(function () {

    var write = function (ast, opt) {
        var curPea = ""
        ,   curTPea = ""
        ,   opt = opt || {}
        ,   noop = function (str) { return str; }
        ,   optNames = "type".split(" ")
        ,   context = []
        ;
        for (var i = 0, n = optNames.length; i < n; i++) {
            var o = optNames[i];
            if (!opt[o]) opt[o] = noop;
        }
        
        var literal = function (it) {
            return it.value;
        };
        var wsPea = function (it) {
            curPea += it.value;
            return "";
        };
        var wsTPea = function (it) {
            curTPea += it.value;
            return "";
        };
        var lineComment = function (it) {
            return "//" + it.value + "\n";
        };
        var multilineComment = function (it) {
            return "/*" + it.value + "*/";
        };
        var type = function (it) {
            if (typeof it === "string") return opt.type(it); // XXX should maintain some context
            if (it.union) return "(" + it.idlType.map(type).join(" or ") + ")";
            var ret = "";
            if (it.sequence) ret += "sequence<";
            ret += type(it.idlType);
            if (it.array) {
                for (var i = 0, n = it.nullableArray.length; i < n; i++) {
                    var val = it.nullableArray[i];
                    if (val) ret += "?";
                    ret += "[]";
                }
            }
            if (it.sequence) ret += ">";
            if (it.nullable) ret += "?";

            return ret;
        };
        var const_value = function (it) {
            var tp = it. type;
            if (tp === "boolean") return it.value ? "true" : "false";
            else if (tp === "null") return "null";
            else if (tp === "Infinity") return (it.negative ? "-" : "") + "Infinity";
            else if (tp === "NaN") return "NaN";
            else if (tp === "number") return it.value;
            else return '"' + it.value + '"';
        };
        var argument = function (arg, pea) {
            var ret = extended_attributes(arg.extAttrs, pea);
            if (arg.optional) ret += "optional ";
            ret += type(arg.idlType);
            if (arg.variadic) ret += "...";
            ret += " " + arg.name;
            if (arg["default"]) ret += " = " + const_value(arg["default"]);
            return ret;
        };
        var args = function (its) {
            var res = ""
            ,   pea = ""
            ;
            for (var i = 0, n = its.length; i < n; i++) {
                var arg = its[i];
                if (arg.type === "ws") res += arg.value;
                else if (arg.type === "ws-pea") pea += arg.value;
                else {
                    res += argument(arg, pea);
                    if (i < n - 1) res += ",";
                    pea = "";
                }
            }
            return res;
        };
        var make_ext_at = function (it) {
            if (it["arguments"] === null) return it.name;
            context.unshift(it);
            var ret = it.name + "(" + (it["arguments"].length ? args(it["arguments"]) : "") + ")";
            context.shift(); // XXX need to add more contexts, but not more than needed for ReSpec
            return ret;
        };
        var extended_attributes = function (eats, pea) {
            if (!eats || !eats.length) return "";
            return "[" + eats.map(make_ext_at).join(", ") + "]" + pea;
        };
        
        var modifiers = "getter setter creator deleter legacycaller stringifier static".split(" ");
        var operation = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            if (it.stringifier && !it.idlType) return "stringifier;";
            for (var i = 0, n = modifiers.length; i < n; i++) {
                var mod = modifiers[i];
                if (it[mod]) ret += mod + " ";
            }
            ret += type(it.idlType) + " ";
            if (it.name) ret += it.name;
            ret += "(" + args(it["arguments"]) + ");";
            return ret;
        };

        var attribute = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            if (it["static"]) ret += "static ";
            if (it.stringifier) ret += "stringifier ";
            if (it.readonly) ret += "readonly ";
            if (it.inherit) ret += "inherit ";
            ret += "attribute " + type(it.idlType) + " " + it.name + ";";
            return ret;
        };
        
        var interface_ = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            if (it.partial) ret += "partial ";
            ret += "interface " + it.name + " ";
            if (it.inheritance) ret += ": " + it.inheritance + " ";
            ret += "{" + iterate(it.members) + "};";
            return ret;
        };
        
        var dictionary = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            if (it.partial) ret += "partial ";
            ret += "dictionary " + it.name + " ";
            ret += "{" + iterate(it.members) + "};";
            return ret;
        };
        var field = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            ret += type(it.idlType) + " " + it.name;
            if (it["default"]) ret += " = " + const_value(it["default"]);
            ret += ";";
            return ret;
        };
        var exception = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            ret += "exception " + it.name + " ";
            if (it.inheritance) ret += ": " + it.inheritance + " ";
            ret += "{" + iterate(it.members) + "};";
            return ret;
        };
        var const_ = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            return ret + "const " + type(it.idlType) + " " + it.name + " = " + const_value(it.value) + ";";
        };
        var typedef = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            ret += "typedef " + extended_attributes(it.typeExtAttrs, curTPea);
            curTPea = "";
            return ret + type(it.idlType) + " " + it.name + ";";
        };
        var implements_ = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            return ret + it.target + " implements " + it["implements"] + ";";
        };
        var callback = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            return ret + "callback " + it.name + " = " + type(it.idlType) +
                   "(" + args(it["arguments"]) + ");";
        };
        var enum_ = function (it) {
            var ret = extended_attributes(it.extAttrs, curPea);
            curPea = "";
            ret += "enum " + it.name + " {";
            for (var i = 0, n = it.values.length; i < n; i++) {
                var v = it.values[i];
                if (typeof v === "string") ret += '"' + v + '"';
                else if (v.type === "ws") ret += v.value;
                else if (v.type === ",") ret += ",";
            }
            return ret + "};";
        };
        
        var table = {
            ws:                     literal
        ,   "ws-pea":               wsPea
        ,   "ws-tpea":              wsTPea
        ,   "line-comment":         lineComment
        ,   "multiline-comment":    multilineComment
        ,   "interface":            interface_
        ,   operation:              operation
        ,   attribute:              attribute
        ,   dictionary:             dictionary
        ,   field:                  field
        ,   exception:              exception
        ,   "const":                const_
        ,   typedef:                typedef
        ,   "implements":           implements_
        ,   callback:               callback
        ,   "enum":                 enum_
        };
        var dispatch = function (it) {
            return table[it.type](it);
        };
        var iterate = function (things) {
            if (!things) return;
            var ret = "";
            for (var i = 0, n = things.length; i < n; i++) ret += dispatch(things[i]);
            return ret;
        };
        return iterate(ast);
    };


    var inNode = typeof module !== "undefined" && module.exports
    ,   obj = {
            write:  function (ast, opt) {
                if (!opt) opt = {};
                return write(ast, opt);
            }
    };

    if (inNode) module.exports = obj;
    else        window.WebIDL2Writer = obj;
    
}());