rubysl-yaml 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,257 @@
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
+
@@ -0,0 +1,170 @@
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,5 +1,5 @@
1
1
  module RubySL
2
2
  module YAML
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
data/rubysl-yaml.gemspec CHANGED
@@ -13,12 +13,14 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.files = `git ls-files`.split($/)
15
15
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.extensions = ["ext/rubysl/syck/extconf.rb"]
16
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
18
  spec.require_paths = ["lib"]
18
19
 
19
- spec.add_runtime_dependency "rubysl-syck", "~> 1.0"
20
+ spec.required_ruby_version = "~> 1.8.7"
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rake", "~> 10.0"
23
24
  spec.add_development_dependency "mspec", "~> 1.5"
25
+ spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
24
26
  end