summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/webidl2/test/widlproc/src/process.c
blob: d6a343b8aa018c5a0d13f0e44ddb6fca7ef2e220 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/***********************************************************************
 * $Id$
 * Copyright 2009 Aplix Corporation. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***********************************************************************/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "comment.h"
#include "lex.h"
#include "misc.h"
#include "node.h"
#include "os.h"
#include "parse.h"
#include "process.h"

#if 0
static const char ntnames[] = { NTNAMES };
#endif /*0*/

/***********************************************************************
 * printtext : print text with xml entity escapes
 *
 * Enter:   s = text
 *          len = number of bytes
 *          escamp = whether to escape &
 *
 * This also escapes double quote mark so it can be used for an
 * attribute value. It also turns a tab into spaces.
 */
void
printtext(const char *s, unsigned int len, int escamp)
{
    const char *p = s, *end = s + len;
    unsigned int count = 0;
    while (p != end) {
        int ch = *p;
        char buf[9];
        const char *seq = 0;
        count++;
        switch (ch) {
        case '<':
            seq = "&lt;";
            break;
        case '&':
            seq = escamp ? "&amp;" : "&";
            break;
        case '"':
            seq = "&quot;";
            break;
        case '\n':
            p++;
            count = 0;
            continue;
        case '\t':
            seq = "        " + ((count - 1) & 7);
            count = 0;
            break;
        default:
            if ((unsigned char)ch >= 0x20) {
                p++;
                continue;
            }
            snprintf(buf, 9, "&#%i;", ch);
            seq = buf;
            break;
        }
        if (p - s != fwrite(s, 1, p - s, stdout))
            errorexit("write error");
        fputs(seq, stdout);
        s = ++p;
    }
    if (p - s != fwrite(s, 1, p - s, stdout))
        errorexit("write error");
}

#if 0
/***********************************************************************
 * outputnodeastext : output parse node and descendants as deparsed text
 *
 * Enter:   node = parse node
 *          needspace = true if last output char was an identifier char
 *
 * Return:  updated needspace value
 */
static int
outputnodeastext(struct node *node, int needspace)
{
    if (node->type >= NT_START) {
        struct node *child = node->children;
        while (child) {
            needspace = outputnodeastext(child, needspace);
            child = child->next;
        }
    } else {
        unsigned int len = strlen(node->name);
        if (len) {
            int ch = node->name[0];
            if (ch == '_' || ((unsigned)(ch - '0') < 10
                    || (unsigned)((ch & ~0x20) - 'A') < 26))
            {
                if (needspace) putchar(' ');
            }
            ch = node->name[len - 1];
            if (ch == '_' || ((unsigned)(ch - '0') < 10
                    || (unsigned)((ch & ~0x20) - 'A') < 26))
            {
                needspace = 1;
            }
            printtext(node->name, len, 1);
        }
    }
    return needspace;
}

/***********************************************************************
 * printfqid : print fully-qualified id
 *
 * Enter:   node struct
 *
 * Return:  whether anything printed
 */
static int
printfqid(struct node *node)
{
    int any = 0;
    struct node *identifier;
    if (node->parent) {
        any = printfqid(node->parent);
    }
    switch (node->type) {
    case NT_Module:
    case NT_Interface:
    case NT_Typedef:
    case NT_Operation:
    case NT_Attribute:
    case NT_Const:
        if (any)
            printf(":");
        /* Find identifier child if any. */
        identifier = node->children;
        while (identifier) {
            if (identifier->type == TOK_IDENTIFIER)
                break;
            if (identifier->type == NT_TypedefRest) {
                identifier = identifier->children;
                continue;
            }
            identifier = identifier->next;
        }
        if (identifier) {
            printtext(identifier->name, strlen(identifier->name), 1);
            any = 1;
        }
        break;
    }
    return any;
}

/***********************************************************************
 * output : output subtree of parse tree
 *
 * Enter:   node = root of subtree
 *          extendedattributelist = 0 else extended attribute list node
 *                                  applying to node
 *          indent = indent (nesting) level
 */
static void outputchildren(struct node *node, struct node *identifier, unsigned int indent);

static void
output(struct node *node, struct node *extendedattributelist,
       unsigned int indent)
{
    if (extendedattributelist) {
        node->wsstart = extendedattributelist->wsstart;
        node->start = extendedattributelist->start;
    }
    if (node->type == NT_ExtendedAttribute) {
        printf("%*s<ExtendedAttribute value=\"", indent, "");
        outputnodeastext(node, 0);
        printf("\"/>\n");
    } else if (node->type == NT_BooleanLiteral) {
        printf("%*s<BooleanLiteral value=\"%s\"/>", indent, "",
            node->children->name);
    } else if (node->type == NT_ReadOnly) {
        printf("%*s<ReadOnly/>\n", indent, "");
    } else if (node->type >= NT_START) {
        const char *ntname;
        /* Find identifier child if any. */
        struct node *identifier = node->children;
        while (identifier) {
            if (identifier->type == TOK_IDENTIFIER)
                break;
            identifier = identifier->next;
        }
        /* Find nonterminal name. */
        ntname = ntnames + 2;
        while (node->type - NT_START != ((unsigned char)ntname[-2] | (unsigned char)ntname[-1] << 8))
            ntname += strlen(ntname) + 3;
        /* Output start of element. */
        printf("%*s<%s", indent, "", ntname);
        /* Output identifier if any as attribute. */
        if (identifier) {
            printf(" identifier=\"");
            printtext(identifier->name, strlen(identifier->name), 1);
            printf("\"");
        }
        switch (node->type) {
        case NT_Module:
        case NT_Interface:
        case NT_Typedef:
        case NT_Const:
            /* Output fully qualified id. */
            printf(" fqid=\"");
            printfqid(node);
            printf("\"");
            break;
        }
        if (!identifier && !extendedattributelist && !node->children && !node->comments)
            printf("/>\n");
        else {
            printf(">\n");
            /* Output descriptive elements (doxygen comments) for node. */
            outputdescriptive(node, indent + 2);
            /* Output descriptive elements (doxygen comments) for identifier. */
            if (identifier)
                outputdescriptive(identifier, indent + 2);
            /* Output extended attribute list. */
            if (extendedattributelist)
                output(extendedattributelist, 0, indent + 2);
            /* Output children (excluding identifier child). */
            outputchildren(node, identifier, indent + 2);
            printf("%*s</%s>\n", indent, "", ntname);
        }
    } else switch (node->type) {
    case TOK_DOMString:
    case TOK_any:
    case TOK_boolean:
    case TOK_octet:
    case TOK_float:
    case TOK_double:
    case TOK_Object:
    case TOK_unsigned:
    case TOK_short:
    case TOK_long:
    case TOK_void:
        printf("%*s<%s/>\n", indent, "", node->name);
        break;
    case TOK_INTEGER:
        printf("%*s<integer value=\"", indent, "");
        printtext(node->name, strlen(node->name), 1);
        printf("\"/>\n");
        break;
    case TOK_FLOAT:
        printf("%*s<Float value=\"", indent, "");
        printtext(node->name, strlen(node->name), 1);
        printf("\"/>\n");
        break;
    case TOK_STRING:
        printf("%*s<string value=\"", indent, "");
        printtext(node->name, strlen(node->name), 1);
        printf("\"/>\n");
        break;
    }
}

/***********************************************************************
 * outputchildren : call output for each child of node
 *
 * Enter:   node
 *          identifier = child node to omit from output
 *          indent = indent (nesting) level
 */
static void
outputchildren(struct node *node, struct node *identifier, unsigned int indent)
{
    struct node *extendedattributelist;
    struct node *child;
    child = node->children;
    extendedattributelist = 0;
    while (child) {
        if (child->type == NT_ExtendedAttributeList && node->type != NT_Argument)
            extendedattributelist = child;
        else {
            if (identifier != child)
                output(child, extendedattributelist, indent);
            extendedattributelist = 0;
        }
        child = child->next;
    }
}
#endif /*0*/

/***********************************************************************
 * processfiles : process input files
 *
 * Enter:   name = filename
 */
void
processfiles(const char *const *names, int dtdref)
{
    struct node *root;
    readinput(names);
    root = parse();
    processcomments(root);
    printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    if(dtdref)
        printf("<!DOCTYPE Definitions SYSTEM \"widlprocxml.dtd\">\n");
    outputnode(root, 0);
}