-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy patheval-parse-xml.cpp
371 lines (335 loc) · 11.9 KB
/
eval-parse-xml.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "avmplus.h"
#ifdef VMCFG_EVAL
#include "eval.h"
namespace avmplus
{
namespace RTC
{
// xmlInitializer is the only entry point.
class XmlContext {
public:
XmlContext(Compiler* compiler)
: allocator(compiler->allocator)
, exprs(allocator)
, text(compiler)
, text_start(0)
{
}
void addExpr(Expr* e);
void addText(Str* s);
void addText(const char* s);
void addText(wchar c);
Seq<Expr*>* get();
private:
void flush();
Allocator * const allocator;
SeqBuilder<Expr*> exprs;
StringBuilder text;
uint32_t text_start;
};
void XmlContext::addExpr(Expr* e)
{
flush();
exprs.addAtEnd(e);
}
void XmlContext::addText(Str* s)
{
text.append(s);
}
void XmlContext::addText(const char* s)
{
while (*s)
text.append(*s++);
}
void XmlContext::addText(wchar c)
{
text.append(c);
}
Seq<Expr*>* XmlContext::get()
{
flush();
return exprs.get();
}
void XmlContext::flush() {
if (text.length() > 0) {
exprs.addAtEnd(ALLOC(LiteralString, (text.str(), text_start)));
/*
char buf[500];
getn(buf, text.str(), 500);
puts(buf);
*/
text.clear();
}
}
Expr* Parser::xmlInitializer()
{
AvmAssert( T0 == T_BreakLeftAngle && T1 == T_LAST );
XmlContext ctx(compiler);
bool is_list = false;
uint32_t pos = position();
xmlPushback('<');
xmlAtom();
switch (T0) {
case T_XmlComment:
case T_XmlCDATA:
case T_XmlProcessingInstruction:
xmlAssert(&ctx, T0);
break;
case T_XmlLeftAngle:
xmlAssert(&ctx, T0);
xmlAtomSkipSpace();
if (T0 == T_XmlRightAngle) {
is_list = true;
xmlListInitializer(&ctx);
}
else
xmlElement(&ctx);
break;
default:
compiler->internalError(position(), "error state in xml handling");
/*NOTREACHED*/
break;
}
next(); // Re-synchronize the lexer for normal lexing
return ALLOC(XmlInitializer, (ctx.get(), is_list, pos));
}
// IN: T0 is ">"
// OUT: T0 is ">"
void Parser::xmlListInitializer(XmlContext* ctx)
{
xmlAssert(ctx, T_XmlRightAngle);
xmlElementContent(ctx);
xmlAssert(ctx, T_XmlLeftAngleSlash);
xmlAtomSkipSpace();
xmlAssert(ctx, T_XmlRightAngle);
}
// IN: T0 is first non-space token of tag name
// OUT: T0 is ">" or "/>"
void Parser::xmlElement(XmlContext* ctx)
{
xmlTagName(ctx);
if (T0 != T_XmlRightAngle && T0 != T_XmlSlashRightAngle) {
ctx->addText(" ");
xmlAttributes(ctx);
}
if (T0 == T_XmlRightAngle) {
xmlAssert(ctx, T0);
xmlElementContent(ctx);
xmlAssert(ctx, T_XmlLeftAngleSlash);
xmlAtomSkipSpace();
xmlTagName(ctx);
xmlAssert(ctx, T_XmlRightAngle);
}
else
xmlAssert(ctx, T_XmlSlashRightAngle);
}
// IN: T0 is first non-space token of name
// OUT: T0 is first non-space token following name
void Parser::xmlTagName(XmlContext* ctx)
{
if (T0 == T_XmlLeftBrace)
xmlExpression(ctx, ESC_none);
else
xmlAssert(ctx, T_XmlName);
xmlAtomSkipSpace();
}
// IN: T0 is first non-space token of first attribute (or /> or >)
// OUT: T0 is /> or >
void Parser::xmlAttributes(XmlContext* ctx)
{
bool first = true;
while (T0 != T_XmlRightAngle && T0 != T_XmlSlashRightAngle) {
if (!first) {
ctx->addText(" ");
first = false;
}
if (T0 == T_XmlLeftBrace) {
xmlExpression(ctx, ESC_attributeValue);
xmlAtomSkipSpace();
// {E} = V is an extension required by the test suite, not in Ecma-357
if (T0 == T_XmlEquals)
goto attrvalue;
}
else {
xmlAssert(ctx, T_XmlName);
xmlAtomSkipSpace();
attrvalue:
xmlAssert(ctx, T_XmlEquals);
xmlAtomSkipSpace();
if (T0 == T_XmlLeftBrace) {
ctx->addText("\"");
xmlExpression(ctx, ESC_attributeValue);
ctx->addText("\"");
}
else
xmlAssert(ctx, T_XmlString, ESC_attributeValue);
xmlAtomSkipSpace();
}
}
}
// IN: T0 is ">"
// OUT: T0 is "</"
void Parser::xmlElementContent(XmlContext* ctx)
{
for (;;) {
xmlAtom();
switch (T0) {
case T_XmlProcessingInstruction:
case T_XmlComment:
case T_XmlCDATA:
case T_XmlName:
case T_XmlWhitespace:
case T_XmlText:
case T_XmlString:
case T_XmlRightBrace:
case T_XmlRightAngle:
case T_XmlSlashRightAngle:
case T_XmlEquals:
xmlAssert(ctx, T0);
continue;
case T_XmlLeftBrace:
xmlExpression(ctx, ESC_elementValue);
continue;
case T_XmlLeftAngle:
xmlAssert(ctx, T0);
xmlAtomSkipSpace();
xmlElement(ctx);
continue;
case T_XmlLeftAngleSlash:
return;
default:
compiler->internalError(position(), "Unexpected state in XML parsing");
}
}
}
// IN: T0 is "{"
// OUT: T0 is "}"
void Parser::xmlExpression(XmlContext* ctx, Escapement esc)
{
AvmAssert( T0 == T_XmlLeftBrace );
next(); // re-enter normal lexing
Expr* expr = commaExpression(0);
if (esc != ESC_none)
expr = ALLOC(EscapeExpr, (expr, esc));
ctx->addExpr(expr);
AvmAssert( T0 == T_RightBrace && T1 == T_LAST );
xmlPushback('}');
xmlAtom();
xmlAssert(ctx, T_XmlRightBrace);
}
void Parser::xmlEscape(XmlContext* ctx, const wchar* cs, const wchar* limit, bool is_attr)
{
while ( cs < limit ) {
wchar c;
switch (c = *cs++) {
case '<':
ctx->addText("<");
continue;
case '&':
ctx->addText("&");
continue;
case '"':
if (!is_attr) break;
ctx->addText(""");
continue;
case '>':
if (is_attr) break;
ctx->addText(">");
continue;
case '\\':
// This is weird but apparently according to spec
if (!is_attr) break;
if (cs + 5 <= limit &&
cs[0] == 'u' &&
cs[1] == '0' &&
cs[2] == '0' &&
cs[3] == '0') {
switch (cs[4]) {
case 'A': case 'a':
cs += 4;
ctx->addText("
");
continue;
case 'D': case 'd':
cs += 4;
ctx->addText("
");
continue;
case '9':
cs += 4;
ctx->addText("	");
continue;
default:
goto add_char;
}
}
add_char:
break;
}
ctx->addText(c);
}
}
void Parser::xmlAssert(XmlContext* ctx, Token t, Escapement esc)
{
if (T0 != t)
compiler->syntaxError(position(), SYNTAXERR_XML_UNEXPECTED_TOKEN);
switch (t) {
case T_XmlProcessingInstruction:
case T_XmlComment:
case T_XmlCDATA:
case T_XmlLeftBrace:
case T_XmlName:
case T_XmlWhitespace:
case T_XmlText:
ctx->addText(V0.s);
break;
case T_XmlString:
switch (esc) {
case ESC_attributeValue:
case ESC_elementValue:
// Spec seems broken
/*
ctx->addText(V0.s->s[0]);
xmlEscape(ctx, V0.s->s + 1, V0.s->s + V0.s->length - 1, esc == ESC_attributeValue);
ctx->addText(V0.s->s[0]);
break;
*/
default:
ctx->addText(V0.s);
break;
}
break;
case T_XmlRightBrace:
// no text added
break;
case T_XmlRightAngle:
ctx->addText(">");
break;
case T_XmlSlashRightAngle:
ctx->addText("/>");
break;
case T_XmlEquals:
ctx->addText("=");
break;
case T_XmlLeftAngle:
ctx->addText("<");
break;
case T_XmlLeftAngleSlash:
ctx->addText("</");
break;
default:
compiler->internalError(position(), "Unexpected token in XML parsing");
}
}
void Parser::xmlAtomSkipSpace()
{
do {
xmlAtom();
} while (T0 == T_XmlWhitespace);
}
}
}
#endif // VMCFG_EVAL