File: parser.c

package info (click to toggle)
lua-yaml 5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,896 kB
  • ctags: 673
  • sloc: sh: 11,895; ansic: 1,013; makefile: 34
file content (412 lines) | stat: -rw-r--r-- 10,719 bytes parent folder | download
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * parser.c, libyaml parser binding for Lua
 * Written by Gary V. Vaughan, 2013
 *
 * Copyright (c) 2013-2014 Gary V. Vaughan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <config.h>

#include "lyaml.h"

typedef struct {
   lua_State	 *L;
   yaml_parser_t  parser;
   yaml_event_t	  event;
   char		  validevent;
   int		  document_count;
} lyaml_parser;


static void
parser_delete_event (lyaml_parser *parser)
{
   if (parser->validevent)
   {
      yaml_event_delete (&parser->event);
      parser->validevent = 0;
   }
}

/* With the event result table on the top of the stack, insert
   a mark entry. */
static void
parser_set_mark (lua_State *L, const char *k, yaml_mark_t mark)
{
   lua_pushstring  (L, k);
   lua_createtable (L, 0, 3);
#define MENTRY(_s)	RAWSET_INTEGER(#_s, mark._s)
        MENTRY( index	);
        MENTRY( line	);
        MENTRY( column	);
#undef MENTRY
   lua_rawset (L, -3);
}

/* Push a new event table, pre-populated with shared elements. */
static void
parser_push_eventtable (lyaml_parser *parser, const char *v, int n)
{
   lua_State *L = parser->L;

   lua_createtable (L, 0, n + 3);
   RAWSET_STRING   ("type", v);
#define MENTRY(_s)	parser_set_mark (L, #_s, parser->event._s)
        MENTRY( start_mark	);
        MENTRY( end_mark	);
#undef MENTRY
}

static void
parse_STREAM_START (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.stream_start._f)
   lua_State *L = parser->L;
   const char *encoding;

   switch (EVENTF (encoding))
   {
#define MENTRY(_s)		\
      case YAML_##_s##_ENCODING: encoding = #_s; break

        MENTRY( ANY	);
        MENTRY( UTF8	);
        MENTRY( UTF16LE	);
        MENTRY( UTF16BE	);
#undef MENTRY

      default:
         lua_pushfstring (L, "invalid encoding %d", EVENTF (encoding));
         lua_error (L);
   }

   parser_push_eventtable (parser, "STREAM_START", 1);
   RAWSET_STRING ("encoding", encoding);
#undef EVENTF
}

/* With the tag list on the top of the stack, append TAG. */
static void
parser_append_tag (lua_State *L, yaml_tag_directive_t tag)
{
   lua_createtable (L, 0, 2);
#define MENTRY(_s)	RAWSET_STRING(#_s, tag._s)
        MENTRY( handle	);
        MENTRY( prefix	);
#undef MENTRY
   lua_rawseti (L, -2, lua_objlen (L, -2) + 1);
}

static void
parse_DOCUMENT_START (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.document_start._f)
   lua_State *L = parser->L;

   /* increment document count */
   parser->document_count++;

   parser_push_eventtable (parser, "DOCUMENT_START", 1);
   RAWSET_BOOLEAN ("implicit", EVENTF (implicit));

   /* version_directive = { major = M, minor = N } */
   if (EVENTF (version_directive))
   {
      lua_pushliteral (L, "version_directive");
      lua_createtable (L, 0, 2);
#define MENTRY(_s)	RAWSET_INTEGER(#_s, EVENTF (version_directive->_s))
        MENTRY( major	);
        MENTRY( minor	);
#undef MENTRY
      lua_rawset (L, -3);
   }

   /* tag_directives = { {handle = H1, prefix = P1}, ... } */
   if (EVENTF (tag_directives.start) &&
       EVENTF (tag_directives.end)) {
      yaml_tag_directive_t *cur;

      lua_pushliteral (L, "tag_directives");
      lua_newtable (L);
      for (cur = EVENTF (tag_directives.start);
           cur != EVENTF (tag_directives.end);
           cur = cur + 1)
      {
         parser_append_tag (L, *cur);
      }
      lua_rawset (L, -3);
   }
#undef EVENTF
}

static void
parse_DOCUMENT_END (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.document_end._f)
   lua_State *L = parser->L;

   parser_push_eventtable (parser, "DOCUMENT_END", 1);
   RAWSET_BOOLEAN ("implicit", EVENTF (implicit));
#undef EVENTF
}

static void
parse_ALIAS (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.alias._f)
   lua_State *L = parser->L;

   parser_push_eventtable (parser, "ALIAS", 1);
   RAWSET_EVENTF (anchor);
#undef EVENTF
}

static void
parse_SCALAR (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.scalar._f)
   lua_State *L = parser->L;
   const char *style;

   switch (EVENTF (style))
   {
#define MENTRY(_s)		\
      case YAML_##_s##_SCALAR_STYLE: style = #_s; break

        MENTRY( ANY		);
        MENTRY( PLAIN		);
        MENTRY( SINGLE_QUOTED	);
        MENTRY( DOUBLE_QUOTED	);
        MENTRY( LITERAL		);
        MENTRY( FOLDED		);
#undef MENTRY

      default:
         lua_pushfstring (L, "invalid sequence style %d", EVENTF (style));
         lua_error (L);
   }


   parser_push_eventtable (parser, "SCALAR", 6);
   RAWSET_EVENTF (anchor);
   RAWSET_EVENTF (tag);
   RAWSET_EVENTF (value);

   RAWSET_BOOLEAN ("plain_implicit", EVENTF (plain_implicit));
   RAWSET_BOOLEAN ("quoted_implicit", EVENTF (quoted_implicit));
   RAWSET_STRING  ("style", style);
#undef EVENTF
}

static void
parse_SEQUENCE_START (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.sequence_start._f)
   lua_State *L = parser->L;
   const char *style;

   switch (EVENTF (style))
   {
#define MENTRY(_s)		\
      case YAML_##_s##_SEQUENCE_STYLE: style = #_s; break

        MENTRY( ANY	);
        MENTRY( BLOCK	);
        MENTRY( FLOW	);
#undef MENTRY

      default:
         lua_pushfstring (L, "invalid sequence style %d", EVENTF (style));
         lua_error (L);
   }

   parser_push_eventtable (parser, "SEQUENCE_START", 4);
   RAWSET_EVENTF (anchor);
   RAWSET_EVENTF (tag);
   RAWSET_BOOLEAN ("implicit", EVENTF (implicit));
   RAWSET_STRING ("style", style);
#undef EVENTF
}

static void
parse_MAPPING_START (lyaml_parser *parser)
{
#define EVENTF(_f)	(parser->event.data.mapping_start._f)
   lua_State *L = parser->L;
   const char *style;

   switch (EVENTF (style))
   {
#define MENTRY(_s)		\
      case YAML_##_s##_MAPPING_STYLE: style = #_s; break

        MENTRY( ANY	);
        MENTRY( BLOCK	);
        MENTRY( FLOW	);
#undef MENTRY

      default:
         lua_pushfstring (L, "invalid mapping style %d", EVENTF (style));
         lua_error (L);
   }

   parser_push_eventtable (parser, "MAPPING_START", 4);
   RAWSET_EVENTF (anchor);
   RAWSET_EVENTF (tag);
   RAWSET_BOOLEAN ("implicit", EVENTF (implicit));
   RAWSET_STRING ("style", style);
#undef EVENTF
}

static void
parser_generate_error_message (lyaml_parser *parser)
{
   yaml_parser_t *P = &parser->parser;
   char buf[256];
   luaL_Buffer b;

   luaL_buffinit (parser->L, &b);
   luaL_addstring (&b, P->problem ? P->problem : "A problem");
   snprintf (buf, sizeof (buf), " at document: %d", parser->document_count);
   luaL_addstring (&b, buf);

   if (P->problem_mark.line || P->problem_mark.column)
   {
      snprintf (buf, sizeof (buf), ", line: %lu, column: %lu",
         (unsigned long) P->problem_mark.line + 1,
         (unsigned long) P->problem_mark.column + 1);
      luaL_addstring (&b, buf);
   }
   luaL_addstring (&b, "\n");

   if (P->context)
   {
      snprintf (buf, sizeof (buf), "%s at line: %lu, column: %lu\n",
         P->context,
         (unsigned long) P->context_mark.line + 1,
         (unsigned long) P->context_mark.column + 1);
      luaL_addstring (&b, buf);
   }

   luaL_pushresult (&b);
}

static int
event_iter (lua_State *L)
{
   lyaml_parser *parser = (lyaml_parser *)lua_touserdata(L, lua_upvalueindex(1));
   char *str;

   parser_delete_event (parser);
   if (yaml_parser_parse (&parser->parser, &parser->event) != 1)
   {
      parser_generate_error_message (parser);
      return lua_error (L);
   }

   parser->validevent = 1;

   lua_newtable    (L);
   lua_pushliteral (L, "type");

   switch (parser->event.type)
   {
      /* First the simple events, generated right here... */
#define MENTRY(_s)		\
      case YAML_##_s##_EVENT: parser_push_eventtable (parser, #_s, 0); break
         MENTRY( STREAM_END	);
         MENTRY( SEQUENCE_END   );
         MENTRY( MAPPING_END	);
#undef MENTRY

      /* ...then the complex events, generated by a function call. */
#define MENTRY(_s)		\
      case YAML_##_s##_EVENT: parse_##_s (parser); break
         MENTRY( STREAM_START	);
         MENTRY( DOCUMENT_START	);
         MENTRY( DOCUMENT_END	);
         MENTRY( ALIAS		);
         MENTRY( SCALAR		);
         MENTRY( SEQUENCE_START	);
         MENTRY( MAPPING_START	);
#undef MENTRY

      case YAML_NO_EVENT:
         lua_pushnil (L);
         break;
      default:
         lua_pushfstring  (L, "invalid event %d", parser->event.type);
         return lua_error (L);
   }

   return 1;
}

static int
parser_gc (lua_State *L)
{
   lyaml_parser *parser = (lyaml_parser *) lua_touserdata (L, 1);

   if (parser)
   {
      parser_delete_event (parser);
      yaml_parser_delete (&parser->parser);
   }
   return 0;
}

void
parser_init (lua_State *L)
{
   luaL_newmetatable(L, "lyaml.parser");
   lua_pushcfunction(L, parser_gc);
   lua_setfield(L, -2, "__gc");
}

int
Pparser (lua_State *L)
{
   lyaml_parser *parser;
   const unsigned char *str;

   /* requires a single string type argument */
   luaL_argcheck (L, lua_isstring (L, 1), 1, "must provide a string argument");
   str = (const unsigned char *) lua_tostring (L, 1);

   /* create a user datum to store the parser */
   parser = (lyaml_parser *) lua_newuserdata (L, sizeof (*parser));
   memset ((void *) parser, 0, sizeof (*parser));
   parser->L = L;

   /* set its metatable */
   luaL_getmetatable (L, "lyaml.parser");
   lua_setmetatable  (L, -2);

   /* try to initialize the parser */
   if (yaml_parser_initialize (&parser->parser) == 0)
      luaL_error (L, "cannot initialize parser for %s", str);
   yaml_parser_set_input_string (&parser->parser, str, lua_strlen (L, 1));

   /* create and return the iterator function, with the loader userdatum as
      its sole upvalue */
   lua_pushcclosure (L, event_iter, 1);
   return 1;
}