rubysl-yaml 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,257 +0,0 @@
1
- /*
2
- * yaml2byte.c
3
- *
4
- * $Author: shyouhei $
5
- * $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6
- *
7
- * Copyright (C) 2003 why the lucky stiff, clark evans
8
- *
9
- * WARNING WARNING WARNING --- THIS IS *NOT JUST* PLAYING
10
- * ANYMORE! -- WHY HAS EMBRACED THIS AS THE REAL THING!
11
- */
12
- #include "ruby.h"
13
- #include <syck.h>
14
- #include <assert.h>
15
- #define YAMLBYTE_UTF8
16
- #include "yamlbyte.h"
17
-
18
- #include <stdio.h>
19
- #define TRACE0(a) \
20
- do { printf(a); printf("\n"); fflush(stdout); } while(0)
21
- #define TRACE1(a,b) \
22
- do { printf(a,b); printf("\n"); fflush(stdout); } while(0)
23
- #define TRACE2(a,b,c) \
24
- do { printf(a,b,c); printf("\n"); fflush(stdout); } while(0)
25
- #define TRACE3(a,b,c,d) \
26
- do { printf(a,b,c,d); printf("\n"); fflush(stdout); } while(0)
27
-
28
- /* Reinvent the wheel... */
29
- #define CHUNKSIZE 64
30
- #define HASH ((long)0xCAFECAFE)
31
- typedef struct {
32
- long hash;
33
- char *buffer;
34
- long length;
35
- long remaining;
36
- int printed;
37
- } bytestring_t;
38
- bytestring_t *bytestring_alloc() {
39
- bytestring_t *ret;
40
- /*TRACE0("bytestring_alloc()");*/
41
- ret = S_ALLOC(bytestring_t);
42
- ret->hash = HASH;
43
- ret->length = CHUNKSIZE;
44
- ret->remaining = ret->length;
45
- ret->buffer = S_ALLOC_N(char, ret->length + 1 );
46
- ret->buffer[0] = 0;
47
- ret->printed = 0;
48
- return ret;
49
- }
50
- void bytestring_append(bytestring_t *str, char code,
51
- char *start, char *finish)
52
- {
53
- long grow;
54
- long length = 2; /* CODE + LF */
55
- char *curr;
56
- assert(str && HASH == str->hash);
57
- /*TRACE0("bytestring_append()");*/
58
- if(start) {
59
- if(!finish)
60
- finish = start + strlen(start);
61
- length += (finish-start);
62
- }
63
- if(length > str->remaining) {
64
- grow = (length - str->remaining) + CHUNKSIZE;
65
- str->remaining += grow;
66
- str->length += grow;
67
- str->buffer = S_REALLOC_N( str->buffer, char, str->length + 1 );
68
- assert(str->buffer);
69
- }
70
- curr = str->buffer + (str->length - str->remaining);
71
- *curr = code;
72
- curr += 1;
73
- if(start)
74
- while(start < finish)
75
- *curr ++ = *start ++;
76
- *curr = '\n';
77
- curr += 1;
78
- *curr = 0;
79
- str->remaining = str->remaining - length;
80
- assert( (str->buffer + str->length) - str->remaining );
81
- }
82
- void bytestring_extend(bytestring_t *str, bytestring_t *ext)
83
- {
84
- char *from;
85
- char *curr;
86
- char *stop;
87
- long grow;
88
- long length;
89
- assert(str && HASH == str->hash);
90
- assert(ext && HASH == ext->hash);
91
- if(ext->printed) {
92
- assert(ext->buffer[0] ==YAMLBYTE_ANCHOR);
93
- curr = ext->buffer;
94
- while( '\n' != *curr)
95
- curr++;
96
- bytestring_append(str, YAMLBYTE_ALIAS, ext->buffer + 1, curr);
97
- } else {
98
- ext->printed = 1;
99
- length = (ext->length - ext->remaining);
100
- if(length > str->remaining) {
101
- grow = (length - str->remaining) + CHUNKSIZE;
102
- str->remaining += grow;
103
- str->length += grow;
104
- str->buffer = S_REALLOC_N( str->buffer, char, str->length + 1 );
105
- }
106
- curr = str->buffer + (str->length - str->remaining);
107
- from = ext->buffer;
108
- stop = ext->buffer + length;
109
- while( from < stop )
110
- *curr ++ = *from ++;
111
- *curr = 0;
112
- str->remaining = str->remaining - length;
113
- assert( (str->buffer + str->length) - str->remaining );
114
- }
115
- }
116
-
117
- /* convert SyckNode into yamlbyte_buffer_t objects */
118
- SYMID
119
- syck_yaml2byte_handler(p, n)
120
- SyckParser *p;
121
- SyckNode *n;
122
- {
123
- SYMID oid;
124
- long i;
125
- char ch;
126
- char nextcode;
127
- char *start;
128
- char *current;
129
- char *finish;
130
- bytestring_t *val = NULL;
131
- bytestring_t *sav = NULL;
132
- /*TRACE0("syck_yaml2byte_handler()");*/
133
- val = bytestring_alloc();
134
- if(n->anchor) bytestring_append(val,YAMLBYTE_ANCHOR, n->anchor, NULL);
135
- if ( n->type_id )
136
- {
137
- if ( p->taguri_expansion )
138
- {
139
- bytestring_append(val,YAMLBYTE_TRANSFER, n->type_id, NULL);
140
- }
141
- else
142
- {
143
- char *type_tag = S_ALLOC_N( char, strlen( n->type_id ) + 1 );
144
- type_tag[0] = '\0';
145
- strcat( type_tag, "!" );
146
- strcat( type_tag, n->type_id );
147
- bytestring_append( val, YAMLBYTE_TRANSFER, type_tag, NULL);
148
- S_FREE(type_tag);
149
- }
150
- }
151
- switch (n->kind)
152
- {
153
- case syck_str_kind:
154
- nextcode = YAMLBYTE_SCALAR;
155
- start = n->data.str->ptr;
156
- finish = start + n->data.str->len - 1;
157
- current = start;
158
- /*TRACE2("SCALAR: %s %d", start, n->data.str->len); */
159
- while(1) {
160
- ch = *current;
161
- if('\n' == ch || 0 == ch || current > finish) {
162
- if(current >= start) {
163
- bytestring_append(val, nextcode, start, current);
164
- nextcode = YAMLBYTE_CONTINUE;
165
- }
166
- start = current + 1;
167
- if(current > finish)
168
- {
169
- break;
170
- }
171
- else if('\n' == ch )
172
- {
173
- bytestring_append(val,YAMLBYTE_NEWLINE,NULL,NULL);
174
- }
175
- else if(0 == ch)
176
- {
177
- bytestring_append(val,YAMLBYTE_NULLCHAR,NULL,NULL);
178
- }
179
- else
180
- {
181
- assert("oops");
182
- }
183
- }
184
- current += 1;
185
- }
186
- break;
187
- case syck_seq_kind:
188
- bytestring_append(val,YAMLBYTE_SEQUENCE,NULL,NULL);
189
- for ( i = 0; i < n->data.list->idx; i++ )
190
- {
191
- oid = syck_seq_read( n, i );
192
- syck_lookup_sym( p, oid, (char **)&sav );
193
- bytestring_extend(val, sav);
194
- }
195
- bytestring_append(val,YAMLBYTE_END_BRANCH,NULL,NULL);
196
- break;
197
- case syck_map_kind:
198
- bytestring_append(val,YAMLBYTE_MAPPING,NULL,NULL);
199
- for ( i = 0; i < n->data.pairs->idx; i++ )
200
- {
201
- oid = syck_map_read( n, map_key, i );
202
- syck_lookup_sym( p, oid, (char **)&sav );
203
- bytestring_extend(val, sav);
204
- oid = syck_map_read( n, map_value, i );
205
- syck_lookup_sym( p, oid, (char **)&sav );
206
- bytestring_extend(val, sav);
207
- }
208
- bytestring_append(val,YAMLBYTE_END_BRANCH,NULL,NULL);
209
- break;
210
- }
211
- oid = syck_add_sym( p, (char *) val );
212
- /*TRACE1("Saving: %s", val->buffer );*/
213
- return oid;
214
- }
215
-
216
- char *
217
- syck_yaml2byte(char *yamlstr)
218
- {
219
- SYMID oid;
220
- char *ret;
221
- bytestring_t *sav;
222
-
223
- SyckParser *parser = syck_new_parser();
224
- syck_parser_str_auto( parser, yamlstr, NULL );
225
- syck_parser_handler( parser, syck_yaml2byte_handler );
226
- syck_parser_error_handler( parser, NULL );
227
- syck_parser_implicit_typing( parser, 1 );
228
- syck_parser_taguri_expansion( parser, 1 );
229
- oid = syck_parse( parser );
230
-
231
- if ( syck_lookup_sym( parser, oid, (char **)&sav ) == 1 ) {
232
- ret = S_ALLOC_N( char, strlen( sav->buffer ) + 3 );
233
- ret[0] = '\0';
234
- strcat( ret, "D\n" );
235
- strcat( ret, sav->buffer );
236
- }
237
- else
238
- {
239
- ret = NULL;
240
- }
241
-
242
- syck_free_parser( parser );
243
- return ret;
244
- }
245
-
246
- #ifdef TEST_YBEXT
247
- #include <stdio.h>
248
- int main() {
249
- char *yaml = "test: 1\nand: \"with new\\nline\\n\"\nalso: &3 three\nmore: *3";
250
- printf("--- # YAML \n");
251
- printf(yaml);
252
- printf("\n...\n");
253
- printf(syck_yaml2byte(yaml));
254
- return 0;
255
- }
256
- #endif
257
-
@@ -1,170 +0,0 @@
1
- /* yamlbyte.h
2
- *
3
- * The YAML bytecode "C" interface header file. See the YAML bytecode
4
- * reference for bytecode sequence rules and for the meaning of each
5
- * bytecode.
6
- */
7
-
8
- #ifndef YAMLBYTE_H
9
- #define YAMLBYTE_H
10
- #include <stddef.h>
11
-
12
- /* define what a character is */
13
- typedef unsigned char yamlbyte_utf8_t;
14
- typedef unsigned short yamlbyte_utf16_t;
15
- #ifdef YAMLBYTE_UTF8
16
- #ifdef YAMLBYTE_UTF16
17
- #error Must only define YAMLBYTE_UTF8 or YAMLBYTE_UTF16
18
- #endif
19
- typedef yamlbyte_utf8_t yamlbyte_char_t;
20
- #else
21
- #ifdef YAMLBYTE_UTF16
22
- typedef yamlbyte_utf16_t yamlbyte_char_t;
23
- #else
24
- #error Must define YAMLBYTE_UTF8 or YAMLBYTE_UTF16
25
- #endif
26
- #endif
27
-
28
- /* specify list of bytecodes */
29
- #define YAMLBYTE_FINISH ((yamlbyte_char_t) 0)
30
- #define YAMLBYTE_DOCUMENT ((yamlbyte_char_t)'D')
31
- #define YAMLBYTE_DIRECTIVE ((yamlbyte_char_t)'V')
32
- #define YAMLBYTE_PAUSE ((yamlbyte_char_t)'P')
33
- #define YAMLBYTE_MAPPING ((yamlbyte_char_t)'M')
34
- #define YAMLBYTE_SEQUENCE ((yamlbyte_char_t)'Q')
35
- #define YAMLBYTE_END_BRANCH ((yamlbyte_char_t)'E')
36
- #define YAMLBYTE_SCALAR ((yamlbyte_char_t)'S')
37
- #define YAMLBYTE_CONTINUE ((yamlbyte_char_t)'C')
38
- #define YAMLBYTE_NEWLINE ((yamlbyte_char_t)'N')
39
- #define YAMLBYTE_NULLCHAR ((yamlbyte_char_t)'Z')
40
- #define YAMLBYTE_ANCHOR ((yamlbyte_char_t)'A')
41
- #define YAMLBYTE_ALIAS ((yamlbyte_char_t)'R')
42
- #define YAMLBYTE_TRANSFER ((yamlbyte_char_t)'T')
43
- /* formatting bytecodes */
44
- #define YAMLBYTE_COMMENT ((yamlbyte_char_t)'c')
45
- #define YAMLBYTE_INDENT ((yamlbyte_char_t)'i')
46
- #define YAMLBYTE_STYLE ((yamlbyte_char_t)'s')
47
- /* other bytecodes */
48
- #define YAMLBYTE_LINE_NUMBER ((yamlbyte_char_t)'#')
49
- #define YAMLBYTE_WHOLE_SCALAR ((yamlbyte_char_t)'<')
50
- #define YAMLBYTE_NOTICE ((yamlbyte_char_t)'!')
51
- #define YAMLBYTE_SPAN ((yamlbyte_char_t)')')
52
- #define YAMLBYTE_ALLOC ((yamlbyte_char_t)'@')
53
-
54
- /* second level style bytecodes, ie "s>" */
55
- #define YAMLBYTE_FLOW ((yamlbyte_char_t)'>')
56
- #define YAMLBYTE_LITERAL ((yamlbyte_char_t)'|')
57
- #define YAMLBYTE_BLOCK ((yamlbyte_char_t)'b')
58
- #define YAMLBYTE_PLAIN ((yamlbyte_char_t)'p')
59
- #define YAMLBYTE_INLINE_MAPPING ((yamlbyte_char_t)'{')
60
- #define YAMLBYTE_INLINE_SEQUENCE ((yamlbyte_char_t)'[')
61
- #define YAMLBYTE_SINGLE_QUOTED ((yamlbyte_char_t)39)
62
- #define YAMLBYTE_DOUBLE_QUOTED ((yamlbyte_char_t)'"')
63
-
64
- /*
65
- * The "C" API has two variants, one based on instructions,
66
- * with events delivered via pointers; and the other one
67
- * is character based where one or more instructions are
68
- * serialized into a buffer.
69
- *
70
- * Note: In the instruction based API, WHOLE_SCALAR does
71
- * not have the '<here' marshalling stuff.
72
- */
73
-
74
- typedef void * yamlbyte_consumer_t;
75
- typedef void * yamlbyte_producer_t;
76
-
77
- /* push and pull APIs need a way to communicate results */
78
- typedef enum {
79
- YAMLBYTE_OK = 0, /* proceed */
80
- YAMLBYTE_E_MEMORY = 'M', /* could not allocate memory */
81
- YAMLBYTE_E_READ = 'R', /* input stream read error */
82
- YAMLBYTE_E_WRITE = 'W', /* output stream write error */
83
- YAMLBYTE_E_OTHER = '?', /* some other error condition */
84
- YAMLBYTE_E_PARSE = 'P', /* parse error, check bytecodes */
85
- } yamlbyte_result_t;
86
-
87
- typedef const yamlbyte_char_t *yamlbyte_buff_t;
88
-
89
- /*
90
- * The "Instruction" API
91
- */
92
-
93
- typedef struct yaml_instruction {
94
- yamlbyte_char_t bytecode;
95
- yamlbyte_buff_t start;
96
- yamlbyte_buff_t finish; /* open range, *finish is _not_ part */
97
- } *yamlbyte_inst_t;
98
-
99
- /* producer pushes the instruction with one bytecode event to the
100
- * consumer; if the consumer's result is not YAMLBYTE_OK, then
101
- * the producer should stop */
102
- typedef
103
- yamlbyte_result_t
104
- (*yamlbyte_push_t)(
105
- yamlbyte_consumer_t self,
106
- yamlbyte_inst_t inst
107
- );
108
-
109
- /* consumer pulls a bytecode instruction from the producer; in this
110
- * case the instruction (and is buffer) are owned by the producer and
111
- * will remain valid till the pull function is called once again;
112
- * if the instruction is NULL, then there are no more results; and
113
- * it is important to call the pull function till it returns NULL so
114
- * that the producer can clean up its memory allocations */
115
- typedef
116
- yamlbyte_result_t
117
- (*yamlbyte_pull_t)(
118
- yamlbyte_producer_t self,
119
- yamlbyte_inst_t *inst /* to be filled in by the producer */
120
- );
121
-
122
- /*
123
- * Buffer based API
124
- */
125
-
126
- /* producer pushes a null terminated buffer filled with one or more
127
- * bytecode events to the consumer; if the consumer's result is not
128
- * YAMLBYTE_OK, then the producer should stop */
129
- typedef
130
- yamlbyte_result_t
131
- (*yamlbyte_pushbuff_t)(
132
- yamlbyte_consumer_t self,
133
- yamlbyte_buff_t buff
134
- );
135
-
136
- /* consumer pulls bytecode events from the producer; in this case
137
- * the buffer is owned by the producer, and will remain valid till
138
- * the pull function is called once again; if the buffer pointer
139
- * is set to NULL, then there are no more results; it is important
140
- * to call the pull function till it returns NULL so that the
141
- * producer can clean up its memory allocations */
142
- typedef
143
- yamlbyte_result_t
144
- (*yamlbyte_pullbuff_t)(
145
- yamlbyte_producer_t self,
146
- yamlbyte_buff_t *buff /* to be filled in by the producer */
147
- );
148
-
149
- /* convert a pull interface to a push interface; the reverse process
150
- * requires threads and thus is language dependent */
151
- #define YAMLBYTE_PULL2PUSH(pull,producer,push,consumer,result) \
152
- do { \
153
- yamlbyte_pullbuff_t _pull = (pull); \
154
- yamlbyte_pushbuff_t _push = (push); \
155
- yamlbyte_result_t _result = YAMLBYTE_OK; \
156
- yamlbyte_producer_t _producer = (producer); \
157
- yamlbyte_consumer_t _consumer = (consumer); \
158
- while(1) { \
159
- yamlbyte_buff_t buff = NULL; \
160
- _result = _pull(_producer,&buff); \
161
- if(YAMLBYTE_OK != result || NULL == buff) \
162
- break; \
163
- _result = _push(_consumer,buff); \
164
- if(YAMLBYTE_OK != result) \
165
- break; \
166
- } \
167
- (result) = _result; \
168
- } while(0)
169
-
170
- #endif
@@ -1,247 +0,0 @@
1
- #
2
- # BaseEmitter
3
- #
4
-
5
- require 'yaml/constants'
6
- require 'yaml/encoding'
7
- require 'yaml/error'
8
-
9
- module YAML
10
-
11
- module BaseEmitter
12
-
13
- def options( opt = nil )
14
- if opt
15
- @options[opt] || YAML::DEFAULTS[opt]
16
- else
17
- @options
18
- end
19
- end
20
-
21
- def options=( opt )
22
- @options = opt
23
- end
24
-
25
- #
26
- # Emit binary data
27
- #
28
- def binary_base64( value )
29
- self << "!binary "
30
- self.node_text( [value].pack("m"), '|' )
31
- end
32
-
33
- #
34
- # Emit plain, normal flowing text
35
- #
36
- def node_text( value, block = nil )
37
- @seq_map = false
38
- valx = value.dup
39
- unless block
40
- block =
41
- if options(:UseBlock)
42
- '|'
43
- elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
44
- '|'
45
- else
46
- '>'
47
- end
48
-
49
- indt = $&.to_i if block =~ /\d+/
50
- if valx =~ /(\A\n*[ \t#]|^---\s+)/
51
- indt = options(:Indent) unless indt.to_i > 0
52
- block += indt.to_s
53
- end
54
-
55
- block +=
56
- if valx =~ /\n\Z\n/
57
- "+"
58
- elsif valx =~ /\Z\n/
59
- ""
60
- else
61
- "-"
62
- end
63
- end
64
- block += "\n"
65
- if block[0] == ?"
66
- esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
67
- valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp
68
- self << '"' + indent_text( valx, indt, false )
69
- else
70
- if block[0] == ?>
71
- valx = fold( valx )
72
- end
73
- #p [block, indt]
74
- self << block + indent_text( valx, indt )
75
- end
76
- end
77
-
78
- #
79
- # Emit a simple, unqouted string
80
- #
81
- def simple( value )
82
- @seq_map = false
83
- self << value.to_s
84
- end
85
-
86
- #
87
- # Emit double-quoted string
88
- #
89
- def double( value )
90
- "\"#{YAML.escape( value )}\""
91
- end
92
-
93
- #
94
- # Emit single-quoted string
95
- #
96
- def single( value )
97
- "'#{value}'"
98
- end
99
-
100
- #
101
- # Write a text block with the current indent
102
- #
103
- def indent_text( text, mod, first_line = true )
104
- return "" if text.to_s.empty?
105
- spacing = indent( mod )
106
- text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
107
- return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
108
- end
109
-
110
- #
111
- # Write a current indent
112
- #
113
- def indent( mod = nil )
114
- #p [ self.id, level, mod, :INDENT ]
115
- if level <= 0
116
- mod ||= 0
117
- else
118
- mod ||= options(:Indent)
119
- mod += ( level - 1 ) * options(:Indent)
120
- end
121
- return " " * mod
122
- end
123
-
124
- #
125
- # Add indent to the buffer
126
- #
127
- def indent!
128
- self << indent
129
- end
130
-
131
- #
132
- # Folding paragraphs within a column
133
- #
134
- def fold( value )
135
- value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do
136
- $1 || $2 + ( $3 || "\n" )
137
- end
138
- end
139
-
140
- #
141
- # Quick mapping
142
- #
143
- def map( type, &e )
144
- val = Mapping.new
145
- e.call( val )
146
- self << "#{type} " if type.length.nonzero?
147
-
148
- #
149
- # Empty hashes
150
- #
151
- if val.length.zero?
152
- self << "{}"
153
- @seq_map = false
154
- else
155
- # FIXME
156
- # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
157
- # @headless = 1
158
- # end
159
-
160
- defkey = @options.delete( :DefaultKey )
161
- if defkey
162
- seq_map_shortcut
163
- self << "= : "
164
- defkey.to_yaml( :Emitter => self )
165
- end
166
-
167
- #
168
- # Emit the key and value
169
- #
170
- val.each { |v|
171
- seq_map_shortcut
172
- if v[0].is_complex_yaml?
173
- self << "? "
174
- end
175
- v[0].to_yaml( :Emitter => self )
176
- if v[0].is_complex_yaml?
177
- self << "\n"
178
- indent!
179
- end
180
- self << ": "
181
- v[1].to_yaml( :Emitter => self )
182
- }
183
- end
184
- end
185
-
186
- def seq_map_shortcut
187
- # FIXME: seq_map needs to work with the new anchoring system
188
- # if @seq_map
189
- # @anchor_extras[@buffer.length - 1] = "\n" + indent
190
- # @seq_map = false
191
- # else
192
- self << "\n"
193
- indent!
194
- # end
195
- end
196
-
197
- #
198
- # Quick sequence
199
- #
200
- def seq( type, &e )
201
- @seq_map = false
202
- val = Sequence.new
203
- e.call( val )
204
- self << "#{type} " if type.length.nonzero?
205
-
206
- #
207
- # Empty arrays
208
- #
209
- if val.length.zero?
210
- self << "[]"
211
- else
212
- # FIXME
213
- # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
214
- # @headless = 1
215
- # end
216
-
217
- #
218
- # Emit the key and value
219
- #
220
- val.each { |v|
221
- self << "\n"
222
- indent!
223
- self << "- "
224
- @seq_map = true if v.class == Hash
225
- v.to_yaml( :Emitter => self )
226
- }
227
- end
228
- end
229
-
230
- end
231
-
232
- #
233
- # Emitter helper classes
234
- #
235
- class Mapping < Array
236
- def add( k, v )
237
- push [k, v]
238
- end
239
- end
240
-
241
- class Sequence < Array
242
- def add( v )
243
- push v
244
- end
245
- end
246
-
247
- end