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,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
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module Syck
3
+ VERSION = "1.0.1"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "rubysl/syck/version"
2
+
3
+ module Rubysl
4
+ module Syck
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/syck/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-syck"
6
+ spec.version = RubySL::Syck::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library syck.}
10
+ spec.summary = %q{Ruby standard library syck.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-syck"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.extensions = ["ext/rubysl/syck/extconf.rb"]
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-syck
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Ruby standard library syck.
42
+ email:
43
+ - brixen@gmail.com
44
+ executables: []
45
+ extensions:
46
+ - ext/rubysl/syck/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - ext/rubysl/syck/bytecode.c
55
+ - ext/rubysl/syck/emitter.c
56
+ - ext/rubysl/syck/extconf.rb
57
+ - ext/rubysl/syck/gram.c
58
+ - ext/rubysl/syck/gram.h
59
+ - ext/rubysl/syck/handler.c
60
+ - ext/rubysl/syck/implicit.c
61
+ - ext/rubysl/syck/node.c
62
+ - ext/rubysl/syck/rubyext.c
63
+ - ext/rubysl/syck/st.c
64
+ - ext/rubysl/syck/st.h
65
+ - ext/rubysl/syck/syck.c
66
+ - ext/rubysl/syck/syck.h
67
+ - ext/rubysl/syck/token.c
68
+ - ext/rubysl/syck/yaml2byte.c
69
+ - ext/rubysl/syck/yamlbyte.h
70
+ - lib/rubysl/syck.rb
71
+ - lib/rubysl/syck/version.rb
72
+ - rubysl-syck.gemspec
73
+ homepage: https://github.com/rubysl/rubysl-syck
74
+ licenses:
75
+ - BSD
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.7
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Ruby standard library syck.
97
+ test_files: []