rubysl-syck 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,458 @@
1
+ /*
2
+ * syck.h
3
+ *
4
+ * $Author: shyouhei $
5
+ * $Date: 2007-08-21 18:16:17 -0700 (Tue, 21 Aug 2007) $
6
+ *
7
+ * Copyright (C) 2003 why the lucky stiff
8
+ */
9
+
10
+ #ifndef SYCK_H
11
+ #define SYCK_H
12
+
13
+ #define SYCK_YAML_MAJOR 1
14
+ #define SYCK_YAML_MINOR 0
15
+
16
+ #define SYCK_VERSION "0.60"
17
+ #define YAML_DOMAIN "yaml.org,2002"
18
+
19
+ #include <stdio.h>
20
+ #include <stdlib.h>
21
+ #include <ctype.h>
22
+ #include "st.h"
23
+
24
+ #if defined(__cplusplus)
25
+ extern "C" {
26
+ #endif
27
+
28
+ /*
29
+ * Memory Allocation
30
+ */
31
+ #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
32
+ #include <alloca.h>
33
+ #endif
34
+
35
+ #if DEBUG
36
+ void syck_assert( char *, unsigned );
37
+ # define ASSERT(f) \
38
+ if ( f ) \
39
+ {} \
40
+ else \
41
+ syck_assert( __FILE__, __LINE__ )
42
+ #else
43
+ # define ASSERT(f)
44
+ #endif
45
+
46
+ #ifndef NULL
47
+ # define NULL (void *)0
48
+ #endif
49
+
50
+ #define ALLOC_CT 8
51
+ #define SYCK_BUFFERSIZE 4096
52
+ #define S_ALLOC_N(type,n) (type*)malloc(sizeof(type)*(n))
53
+ #define S_ALLOC(type) (type*)malloc(sizeof(type))
54
+ #define S_REALLOC_N(var,type,n) (var)=(type*)realloc((char*)(var),sizeof(type)*(n))
55
+ #define S_FREE(n) if (n) { free(n); n = NULL; }
56
+
57
+ #define S_ALLOCA_N(type,n) (type*)alloca(sizeof(type)*(n))
58
+
59
+ #define S_MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(n))
60
+ #define S_MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n))
61
+ #define S_MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n))
62
+ #define S_MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n))
63
+
64
+ #define BLOCK_FOLD 10
65
+ #define BLOCK_LIT 20
66
+ #define BLOCK_PLAIN 30
67
+ #define NL_CHOMP 40
68
+ #define NL_KEEP 50
69
+
70
+ /*
71
+ * Node definitions
72
+ */
73
+ #ifndef ST_DATA_T_DEFINED
74
+ typedef long st_data_t;
75
+ #endif
76
+
77
+ #define SYMID unsigned long
78
+
79
+ typedef struct _syck_node SyckNode;
80
+
81
+ enum syck_kind_tag {
82
+ syck_map_kind,
83
+ syck_seq_kind,
84
+ syck_str_kind
85
+ };
86
+
87
+ enum map_part {
88
+ map_key,
89
+ map_value
90
+ };
91
+
92
+ enum map_style {
93
+ map_none,
94
+ map_inline
95
+ };
96
+
97
+ enum seq_style {
98
+ seq_none,
99
+ seq_inline
100
+ };
101
+
102
+ enum scalar_style {
103
+ scalar_none,
104
+ scalar_1quote,
105
+ scalar_2quote,
106
+ scalar_fold,
107
+ scalar_literal,
108
+ scalar_plain
109
+ };
110
+
111
+ /*
112
+ * Node metadata struct
113
+ */
114
+ struct _syck_node {
115
+ /* Symbol table ID */
116
+ SYMID id;
117
+ /* Underlying kind */
118
+ enum syck_kind_tag kind;
119
+ /* Fully qualified tag-uri for type */
120
+ char *type_id;
121
+ /* Anchor name */
122
+ char *anchor;
123
+ union {
124
+ /* Storage for map data */
125
+ struct SyckMap {
126
+ enum map_style style;
127
+ SYMID *keys;
128
+ SYMID *values;
129
+ long capa;
130
+ long idx;
131
+ } *pairs;
132
+ /* Storage for sequence data */
133
+ struct SyckSeq {
134
+ enum seq_style style;
135
+ SYMID *items;
136
+ long capa;
137
+ long idx;
138
+ } *list;
139
+ /* Storage for string data */
140
+ struct SyckStr {
141
+ enum scalar_style style;
142
+ char *ptr;
143
+ long len;
144
+ } *str;
145
+ } data;
146
+ /* Shortcut node */
147
+ void *shortcut;
148
+ };
149
+
150
+ /*
151
+ * Parser definitions
152
+ */
153
+ typedef struct _syck_parser SyckParser;
154
+ typedef struct _syck_file SyckIoFile;
155
+ typedef struct _syck_str SyckIoStr;
156
+ typedef struct _syck_level SyckLevel;
157
+
158
+ typedef SYMID (*SyckNodeHandler)(SyckParser *, SyckNode *);
159
+ typedef void (*SyckErrorHandler)(SyckParser *, char *);
160
+ typedef SyckNode * (*SyckBadAnchorHandler)(SyckParser *, char *);
161
+ typedef long (*SyckIoFileRead)(char *, SyckIoFile *, long, long);
162
+ typedef long (*SyckIoStrRead)(char *, SyckIoStr *, long, long);
163
+
164
+ enum syck_io_type {
165
+ syck_io_str,
166
+ syck_io_file
167
+ };
168
+
169
+ enum syck_parser_input {
170
+ syck_yaml_utf8,
171
+ syck_yaml_utf16,
172
+ syck_yaml_utf32,
173
+ syck_bytecode_utf8
174
+ };
175
+
176
+ enum syck_level_status {
177
+ syck_lvl_header,
178
+ syck_lvl_doc,
179
+ syck_lvl_open,
180
+ syck_lvl_seq,
181
+ syck_lvl_map,
182
+ syck_lvl_block,
183
+ syck_lvl_str,
184
+ syck_lvl_iseq,
185
+ syck_lvl_imap,
186
+ syck_lvl_end,
187
+ syck_lvl_pause,
188
+ syck_lvl_anctag,
189
+ syck_lvl_mapx,
190
+ syck_lvl_seqx
191
+ };
192
+
193
+ /*
194
+ * Parser structs
195
+ */
196
+ struct _syck_file {
197
+ /* File pointer */
198
+ FILE *ptr;
199
+ /* Function which FILE -> buffer */
200
+ SyckIoFileRead read;
201
+ };
202
+
203
+ struct _syck_str {
204
+ /* String buffer pointers */
205
+ char *beg, *ptr, *end;
206
+ /* Function which string -> buffer */
207
+ SyckIoStrRead read;
208
+ };
209
+
210
+ struct _syck_level {
211
+ /* Indent */
212
+ int spaces;
213
+ /* Counts nodes emitted at this level, useful for parsing
214
+ * keys and pairs in bytecode */
215
+ int ncount;
216
+ /* Does node have anchors or tags? */
217
+ int anctag;
218
+ /* Domain prefixing at the given level */
219
+ char *domain;
220
+ /* Keeps a node status */
221
+ enum syck_level_status status;
222
+ };
223
+
224
+ struct _syck_parser {
225
+ /* Root node */
226
+ SYMID root, root_on_error;
227
+ /* Implicit typing flag */
228
+ int implicit_typing, taguri_expansion;
229
+ /* Scripting language function to handle nodes */
230
+ SyckNodeHandler handler;
231
+ /* Error handler */
232
+ SyckErrorHandler error_handler;
233
+ /* InvalidAnchor handler */
234
+ SyckBadAnchorHandler bad_anchor_handler;
235
+ /* Parser input type */
236
+ enum syck_parser_input input_type;
237
+ /* IO type */
238
+ enum syck_io_type io_type;
239
+ /* Custom buffer size */
240
+ size_t bufsize;
241
+ /* Buffer pointers */
242
+ char *buffer, *linectptr, *lineptr, *toktmp, *token, *cursor, *marker, *limit;
243
+ /* Line counter */
244
+ int linect;
245
+ /* Last token from yylex() */
246
+ int last_token;
247
+ /* Force a token upon next call to yylex() */
248
+ int force_token;
249
+ /* EOF flag */
250
+ int eof;
251
+ union {
252
+ SyckIoFile *file;
253
+ SyckIoStr *str;
254
+ } io;
255
+ /* Symbol table for anchors */
256
+ st_table *anchors, *bad_anchors;
257
+ /* Optional symbol table for SYMIDs */
258
+ st_table *syms;
259
+ /* Levels of indentation */
260
+ SyckLevel *levels;
261
+ int lvl_idx;
262
+ int lvl_capa;
263
+ /* Pointer for extension's use */
264
+ void *bonus;
265
+ };
266
+
267
+ /*
268
+ * Emitter definitions
269
+ */
270
+ typedef struct _syck_emitter SyckEmitter;
271
+ typedef struct _syck_emitter_node SyckEmitterNode;
272
+
273
+ typedef void (*SyckOutputHandler)(SyckEmitter *, char *, long);
274
+ typedef void (*SyckEmitterHandler)(SyckEmitter *, st_data_t);
275
+
276
+ enum doc_stage {
277
+ doc_open,
278
+ doc_processing
279
+ };
280
+
281
+ /*
282
+ * Emitter struct
283
+ */
284
+ struct _syck_emitter {
285
+ /* Headerless doc flag */
286
+ int headless;
287
+ /* Force header? */
288
+ int use_header;
289
+ /* Force version? */
290
+ int use_version;
291
+ /* Sort hash keys */
292
+ int sort_keys;
293
+ /* Anchor format */
294
+ char *anchor_format;
295
+ /* Explicit typing on all collections? */
296
+ int explicit_typing;
297
+ /* Best width on folded scalars */
298
+ int best_width;
299
+ /* Use literal[1] or folded[2] blocks on all text? */
300
+ enum scalar_style style;
301
+ /* Stage of written document */
302
+ enum doc_stage stage;
303
+ /* Level counter */
304
+ int level;
305
+ /* Default indentation */
306
+ int indent;
307
+ /* Object ignore ID */
308
+ SYMID ignore_id;
309
+ /* Symbol table for anchors */
310
+ st_table *markers, *anchors, *anchored;
311
+ /* Custom buffer size */
312
+ size_t bufsize;
313
+ /* Buffer */
314
+ char *buffer, *marker;
315
+ /* Absolute position of the buffer */
316
+ long bufpos;
317
+ /* Handler for emitter nodes */
318
+ SyckEmitterHandler emitter_handler;
319
+ /* Handler for output */
320
+ SyckOutputHandler output_handler;
321
+ /* Levels of indentation */
322
+ SyckLevel *levels;
323
+ int lvl_idx;
324
+ int lvl_capa;
325
+ /* Pointer for extension's use */
326
+ void *bonus;
327
+ };
328
+
329
+ /*
330
+ * Emitter node metadata struct
331
+ */
332
+ struct _syck_emitter_node {
333
+ /* Node buffer position */
334
+ long pos;
335
+ /* Current indent */
336
+ long indent;
337
+ /* Collection? */
338
+ int is_shortcut;
339
+ };
340
+
341
+ /*
342
+ * Handler prototypes
343
+ */
344
+ SYMID syck_hdlr_add_node( SyckParser *, SyckNode * );
345
+ SyckNode *syck_hdlr_add_anchor( SyckParser *, char *, SyckNode * );
346
+ void syck_hdlr_remove_anchor( SyckParser *, char * );
347
+ SyckNode *syck_hdlr_get_anchor( SyckParser *, char * );
348
+ void syck_add_transfer( char *, SyckNode *, int );
349
+ char *syck_xprivate( char *, int );
350
+ char *syck_taguri( char *, char *, int );
351
+ int syck_tagcmp( char *, char * );
352
+ int syck_add_sym( SyckParser *, char * );
353
+ int syck_lookup_sym( SyckParser *, SYMID, char ** );
354
+ int syck_try_implicit( SyckNode * );
355
+ char *syck_type_id_to_uri( char * );
356
+ void try_tag_implicit( SyckNode *, int );
357
+ char *syck_match_implicit( char *, size_t );
358
+
359
+ /*
360
+ * API prototypes
361
+ */
362
+ char *syck_strndup( char *, long );
363
+ long syck_io_file_read( char *, SyckIoFile *, long, long );
364
+ long syck_io_str_read( char *, SyckIoStr *, long, long );
365
+ char *syck_base64enc( char *, long );
366
+ char *syck_base64dec( char *, long );
367
+ SyckEmitter *syck_new_emitter();
368
+ SYMID syck_emitter_mark_node( SyckEmitter *, st_data_t );
369
+ void syck_emitter_ignore_id( SyckEmitter *, SYMID );
370
+ void syck_output_handler( SyckEmitter *, SyckOutputHandler );
371
+ void syck_emitter_handler( SyckEmitter *, SyckEmitterHandler );
372
+ void syck_free_emitter( SyckEmitter * );
373
+ void syck_emitter_clear( SyckEmitter * );
374
+ void syck_emitter_write( SyckEmitter *, char *, long );
375
+ void syck_emitter_escape( SyckEmitter *, char *, long );
376
+ void syck_emitter_flush( SyckEmitter *, long );
377
+ void syck_emit( SyckEmitter *, st_data_t );
378
+ void syck_emit_scalar( SyckEmitter *, char *, enum scalar_style, int, int, char, char *, long );
379
+ void syck_emit_1quoted( SyckEmitter *, int, char *, long );
380
+ void syck_emit_2quoted( SyckEmitter *, int, char *, long );
381
+ void syck_emit_folded( SyckEmitter *, int, char, char *, long );
382
+ void syck_emit_literal( SyckEmitter *, char, char *, long );
383
+ void syck_emit_seq( SyckEmitter *, char *, enum seq_style );
384
+ void syck_emit_item( SyckEmitter *, st_data_t );
385
+ void syck_emit_map( SyckEmitter *, char *, enum map_style );
386
+ void syck_emit_end( SyckEmitter * );
387
+ void syck_emit_tag( SyckEmitter *, char *, char * );
388
+ void syck_emit_indent( SyckEmitter * );
389
+ SyckLevel *syck_emitter_current_level( SyckEmitter * );
390
+ SyckLevel *syck_emitter_parent_level( SyckEmitter * );
391
+ void syck_emitter_pop_level( SyckEmitter * );
392
+ void syck_emitter_add_level( SyckEmitter *, int, enum syck_level_status );
393
+ void syck_emitter_reset_levels( SyckEmitter * );
394
+ SyckParser *syck_new_parser();
395
+ void syck_free_parser( SyckParser * );
396
+ void syck_parser_set_root_on_error( SyckParser *, SYMID );
397
+ void syck_parser_implicit_typing( SyckParser *, int );
398
+ void syck_parser_taguri_expansion( SyckParser *, int );
399
+ int syck_scan_scalar( int, char *, long );
400
+ void syck_parser_handler( SyckParser *, SyckNodeHandler );
401
+ void syck_parser_error_handler( SyckParser *, SyckErrorHandler );
402
+ void syck_parser_bad_anchor_handler( SyckParser *, SyckBadAnchorHandler );
403
+ void syck_parser_set_input_type( SyckParser *, enum syck_parser_input );
404
+ void syck_parser_file( SyckParser *, FILE *, SyckIoFileRead );
405
+ void syck_parser_str( SyckParser *, char *, long, SyckIoStrRead );
406
+ void syck_parser_str_auto( SyckParser *, char *, SyckIoStrRead );
407
+ SyckLevel *syck_parser_current_level( SyckParser * );
408
+ void syck_parser_add_level( SyckParser *, int, enum syck_level_status );
409
+ void syck_parser_pop_level( SyckParser * );
410
+ void free_any_io( SyckParser * );
411
+ long syck_parser_read( SyckParser * );
412
+ long syck_parser_readlen( SyckParser *, long );
413
+ SYMID syck_parse( SyckParser * );
414
+ void syck_default_error_handler( SyckParser *, char * );
415
+ SYMID syck_yaml2byte_handler( SyckParser *, SyckNode * );
416
+ char *syck_yaml2byte( char * );
417
+
418
+ /*
419
+ * Allocation prototypes
420
+ */
421
+ SyckNode *syck_alloc_map();
422
+ SyckNode *syck_alloc_seq();
423
+ SyckNode *syck_alloc_str();
424
+ void syck_free_node( SyckNode * );
425
+ void syck_free_members( SyckNode * );
426
+ SyckNode *syck_new_str( char *, enum scalar_style );
427
+ SyckNode *syck_new_str2( char *, long, enum scalar_style );
428
+ void syck_replace_str( SyckNode *, char *, enum scalar_style );
429
+ void syck_replace_str2( SyckNode *, char *, long, enum scalar_style );
430
+ void syck_str_blow_away_commas( SyckNode * );
431
+ char *syck_str_read( SyckNode * );
432
+ SyckNode *syck_new_map( SYMID, SYMID );
433
+ void syck_map_empty( SyckNode * );
434
+ void syck_map_add( SyckNode *, SYMID, SYMID );
435
+ SYMID syck_map_read( SyckNode *, enum map_part, long );
436
+ void syck_map_assign( SyckNode *, enum map_part, long, SYMID );
437
+ long syck_map_count( SyckNode * );
438
+ void syck_map_update( SyckNode *, SyckNode * );
439
+ SyckNode *syck_new_seq( SYMID );
440
+ void syck_seq_empty( SyckNode * );
441
+ void syck_seq_add( SyckNode *, SYMID );
442
+ void syck_seq_assign( SyckNode *, long, SYMID );
443
+ SYMID syck_seq_read( SyckNode *, long );
444
+ long syck_seq_count( SyckNode * );
445
+
446
+ /*
447
+ * Lexer prototypes
448
+ */
449
+ void syckerror( char * );
450
+ int syckparse( void * );
451
+ union YYSTYPE;
452
+ int sycklex( union YYSTYPE *, SyckParser * );
453
+
454
+ #if defined(__cplusplus)
455
+ } /* extern "C" { */
456
+ #endif
457
+
458
+ #endif /* ifndef SYCK_H */