1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/syck/rubyext.c: YAML::Syck::compile method.

* ext/syck/syck.c: Buffer edge bug.

* ext/syck/yaml2byte.c: YAML to bytecode converter.

* ext/syck/bytecode.c: Bytecode parser fixes to empty collections
  and empty strings.

* ext/syck/token.c: Ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4799 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
why 2003-10-17 20:53:39 +00:00
parent 23f6b5bdc4
commit 71a8bebf5a
9 changed files with 4111 additions and 1558 deletions

View file

@ -1,3 +1,16 @@
Sat Oct 18 05:48:59 2003 why the lucky stiff <ruby-cvs@whytheluckystiff.net>
* ext/syck/rubyext.c: YAML::Syck::compile method.
* ext/syck/syck.c: Buffer edge bug.
* ext/syck/yaml2byte.c: YAML to bytecode converter.
* ext/syck/bytecode.c: Bytecode parser fixes to empty collections
and empty strings.
* ext/syck/token.c: Ditto.
Fri Oct 17 23:07:38 2003 Akinori MUSHA <knu@iDaemons.org>
* ext/enumerator/enumerator.c, ext/enumerator/enumerator.txt:

View file

@ -11,3 +11,4 @@ rubyext.c
syck.c
syck.h
token.c
yaml2byte.c

View file

@ -1,4 +1,4 @@
/* Generated by re2c 0.5 on Tue Oct 14 15:44:30 2003 */
/* Generated by re2c 0.5 on Fri Oct 17 12:13:58 2003 */
#line 1 "bytecode.re"
/*
* bytecode.re
@ -62,6 +62,11 @@ char *get_inline( SyckParser *parser );
ADD_LEVEL(len, syck_lvl_open); \
YYPOS(0); \
return '-'; \
\
case syck_lvl_map: \
lvl->ncount++; \
ADD_LEVEL(len, s); \
break; \
\
case syck_lvl_open: \
lvl->status = s; \
@ -139,7 +144,7 @@ sycklex_bytecode_utf8( YYSTYPE *sycklval, SyckParser *parser )
return t;
}
#line 163
#line 168
lvl = CURRENT_LEVEL();
@ -160,19 +165,24 @@ yy1: ++YYCURSOR;
yy0:
if((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
yych = *YYCURSOR;
if(yych <= '\000') goto yy2;
if(yych == 'D') goto yy3;
goto yy5;
switch(yych){
case '\000': goto yy2;
case 'D': goto yy3;
default: goto yy5;
}
yy2: YYCURSOR = YYMARKER;
switch(yyaccept){
case 0: goto yy4;
}
yy3: yyaccept = 0;
yych = *(YYMARKER = ++YYCURSOR);
if(yych == '\n') goto yy6;
if(yych == '\r') goto yy8;
switch(yych){
case '\n': goto yy6;
case '\r': goto yy8;
default: goto yy4;
}
yy4:
#line 189
#line 194
{ YYPOS(0);
goto Document;
}
@ -180,7 +190,7 @@ yy5: yych = *++YYCURSOR;
goto yy4;
yy6: yych = *++YYCURSOR;
yy7:
#line 177
#line 182
{ if ( lvl->status == syck_lvl_header )
{
goto Directive;
@ -193,10 +203,12 @@ yy7:
}
}
yy8: yych = *++YYCURSOR;
if(yych == '\n') goto yy6;
goto yy2;
switch(yych){
case '\n': goto yy6;
default: goto yy2;
}
}
#line 193
#line 198
lvl->status = syck_lvl_doc;
@ -219,71 +231,61 @@ yy10: ++YYCURSOR;
yy9:
if((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
yych = *YYCURSOR;
if(yych <= 'E'){
if(yych <= '\r'){
if(yych <= '\t'){
if(yych <= '\000') goto yy30;
} else {
if(yych <= '\n') goto yy27;
if(yych >= '\r') goto yy29;
}
} else {
if(yych <= 'A'){
if(yych >= 'A') goto yy19;
} else {
if(yych <= 'C') goto yy11;
if(yych <= 'D') goto yy12;
goto yy16;
}
}
} else {
if(yych <= 'Q'){
if(yych <= 'M'){
if(yych >= 'M') goto yy14;
} else {
if(yych <= 'O') goto yy11;
if(yych <= 'P') goto yy13;
goto yy15;
}
} else {
if(yych <= 'T'){
if(yych <= 'R') goto yy21;
if(yych <= 'S') goto yy17;
goto yy23;
} else {
if(yych == 'c') goto yy25;
}
}
switch(yych){
case '\000': goto yy30;
case '\n': goto yy27;
case '\r': goto yy29;
case 'A': goto yy19;
case 'D': goto yy12;
case 'E': goto yy16;
case 'M': goto yy14;
case 'P': goto yy13;
case 'Q': goto yy15;
case 'R': goto yy21;
case 'S': goto yy17;
case 'T': goto yy23;
case 'c': goto yy25;
default: goto yy11;
}
yy11:yy12: yych = *++YYCURSOR;
if(yych == '\n') goto yy41;
if(yych == '\r') goto yy44;
goto yy11;
switch(yych){
case '\n': goto yy41;
case '\r': goto yy44;
default: goto yy11;
}
yy13: yych = *++YYCURSOR;
if(yych == '\n') goto yy41;
if(yych == '\r') goto yy43;
goto yy11;
switch(yych){
case '\n': goto yy41;
case '\r': goto yy43;
default: goto yy11;
}
yy14: yych = *++YYCURSOR;
if(yych == '\n') goto yy38;
if(yych == '\r') goto yy40;
goto yy11;
switch(yych){
case '\n': goto yy38;
case '\r': goto yy40;
default: goto yy11;
}
yy15: yych = *++YYCURSOR;
if(yych == '\n') goto yy35;
if(yych == '\r') goto yy37;
goto yy11;
switch(yych){
case '\n': goto yy35;
case '\r': goto yy37;
default: goto yy11;
}
yy16: yych = *++YYCURSOR;
if(yych == '\n') goto yy32;
if(yych == '\r') goto yy34;
goto yy11;
switch(yych){
case '\n': goto yy32;
case '\r': goto yy34;
default: goto yy11;
}
yy17: yych = *++YYCURSOR;
yy18:
#line 243
#line 282
{ ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_str);
goto Scalar;
}
yy19: yych = *++YYCURSOR;
yy20:
#line 247
#line 286
{ ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_open);
sycklval->name = get_inline( parser );
syck_hdlr_remove_anchor( parser, sycklval->name );
@ -291,7 +293,7 @@ yy20:
}
yy21: yych = *++YYCURSOR;
yy22:
#line 253
#line 292
{ ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_str);
sycklval->name = get_inline( parser );
POP_LEVEL();
@ -300,8 +302,10 @@ yy22:
}
yy23: yych = *++YYCURSOR;
yy24:
#line 260
{ char *qstr = get_inline( parser );
#line 299
{ char *qstr;
ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_open);
qstr = get_inline( parser );
if ( qstr[0] == '!' )
{
int qidx = strlen( qstr );
@ -347,6 +351,7 @@ yy24:
else
{
sycklval->name = S_ALLOC_N( char, strlen( qstr ) );
sycklval->name[0] = '\0';
S_MEMCPY( sycklval->name, qstr + 1, char, strlen( qstr ) );
free( qstr );
}
@ -358,37 +363,53 @@ yy24:
}
yy25: yych = *++YYCURSOR;
yy26:
#line 316
#line 358
{ goto Comment; }
yy27: yych = *++YYCURSOR;
yy28:
#line 318
#line 360
{ if ( lvl->status == syck_lvl_seq )
{
return YAML_INDENT;
}
else if ( lvl->status == syck_lvl_map )
{
lvl->ncount++;
if ( lvl->ncount % 2 == 1 ) return ':';
else return YAML_INDENT;
}
goto Document;
}
yy29: yych = *++YYCURSOR;
if(yych == '\n') goto yy27;
goto yy11;
switch(yych){
case '\n': goto yy27;
default: goto yy11;
}
yy30: yych = *++YYCURSOR;
yy31:
#line 331
#line 372
{ ENSURE_YAML_IEND(lvl, -1);
YYPOS(0);
return 0;
}
yy32: yych = *++YYCURSOR;
yy33:
#line 222
{ POP_LEVEL();
#line 247
{ if ( lvl->status == syck_lvl_seq && lvl->ncount == 0 )
{
lvl->ncount++;
YYPOS(0);
FORCE_NEXT_TOKEN( ']' );
return '[';
}
else if ( lvl->status == syck_lvl_map && lvl->ncount == 0 )
{
lvl->ncount++;
YYPOS(0);
FORCE_NEXT_TOKEN( '}' );
return '{';
}
POP_LEVEL();
lvl = CURRENT_LEVEL();
if ( lvl->status == syck_lvl_seq )
{
@ -396,7 +417,6 @@ yy33:
}
else if ( lvl->status == syck_lvl_map )
{
lvl->ncount++;
if ( lvl->ncount % 2 == 1 )
{
FORCE_NEXT_TOKEN(':');
@ -409,41 +429,71 @@ yy33:
return YAML_IEND;
}
yy34: yych = *++YYCURSOR;
if(yych == '\n') goto yy32;
goto yy11;
switch(yych){
case '\n': goto yy32;
default: goto yy11;
}
yy35: yych = *++YYCURSOR;
yy36:
#line 218
{ ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_seq);
#line 233
{ int complex = 0;
if ( lvl->ncount % 2 == 0 && ( lvl->status == syck_lvl_map || lvl->status == syck_lvl_seq ) )
{
complex = 1;
}
ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_seq);
if ( complex )
{
FORCE_NEXT_TOKEN( YAML_IOPEN );
return '?';
}
return YAML_IOPEN;
}
yy37: yych = *++YYCURSOR;
if(yych == '\n') goto yy35;
goto yy11;
switch(yych){
case '\n': goto yy35;
default: goto yy11;
}
yy38: yych = *++YYCURSOR;
yy39:
#line 214
{ ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_map);
#line 219
{ int complex = 0;
if ( lvl->ncount % 2 == 0 && ( lvl->status == syck_lvl_map || lvl->status == syck_lvl_seq ) )
{
complex = 1;
}
ADD_BYTE_LEVEL(lvl, lvl->spaces + 1, syck_lvl_map);
if ( complex )
{
FORCE_NEXT_TOKEN( YAML_IOPEN );
return '?';
}
return YAML_IOPEN;
}
yy40: yych = *++YYCURSOR;
if(yych == '\n') goto yy38;
goto yy11;
switch(yych){
case '\n': goto yy38;
default: goto yy11;
}
yy41: yych = *++YYCURSOR;
yy42:
#line 209
#line 214
{ ENSURE_YAML_IEND(lvl, -1);
YYPOS(0);
return 0;
}
yy43: yych = *++YYCURSOR;
if(yych == '\n') goto yy41;
goto yy11;
switch(yych){
case '\n': goto yy41;
default: goto yy11;
}
yy44: yych = *++YYCURSOR;
if(yych == '\n') goto yy41;
goto yy11;
switch(yych){
case '\n': goto yy41;
default: goto yy11;
}
}
#line 336
#line 377
}
@ -460,20 +510,97 @@ yy46: ++YYCURSOR;
yy45:
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
if(yych <= '\000') goto yy47;
if(yych == 'V') goto yy48;
goto yy50;
switch(yych){
case '\000': goto yy47;
case 'V': goto yy48;
default: goto yy50;
}
yy47: YYCURSOR = YYMARKER;
switch(yyaccept){
case 0: goto yy49;
}
yy48: yyaccept = 0;
yych = *(YYMARKER = ++YYCURSOR);
if(yych <= '-') goto yy49;
if(yych == '`') goto yy49;
if(yych <= 'z') goto yy51;
switch(yych){
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case '\\':
case ']':
case '^':
case '_': case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy51;
default: goto yy49;
}
yy49:
#line 348
#line 389
{ YYCURSOR = YYTOKTMP;
return YAML_DOCSEP;
}
@ -482,44 +609,256 @@ yy50: yych = *++YYCURSOR;
yy51: ++YYCURSOR;
if(YYLIMIT == YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
yy52: if(yych <= ':'){
if(yych <= '-') goto yy47;
if(yych <= '9') goto yy51;
} else {
if(yych == '`') goto yy47;
if(yych <= 'z') goto yy51;
goto yy47;
yy52: switch(yych){
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case '\\':
case ']':
case '^':
case '_': case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy51;
case ':': goto yy53;
default: goto yy47;
}
yy53: yych = *++YYCURSOR;
if(yych <= '-') goto yy47;
if(yych == '`') goto yy47;
if(yych >= '{') goto yy47;
switch(yych){
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case '\\':
case ']':
case '^':
case '_': case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy54;
default: goto yy47;
}
yy54: ++YYCURSOR;
if(YYLIMIT == YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
yy55: if(yych <= '\r'){
if(yych == '\n') goto yy56;
if(yych <= '\f') goto yy47;
goto yy58;
} else {
if(yych <= '_'){
if(yych <= '-') goto yy47;
goto yy54;
} else {
if(yych <= '`') goto yy47;
if(yych <= 'z') goto yy54;
goto yy47;
}
yy55: switch(yych){
case '\n': goto yy56;
case '\r': goto yy58;
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case '\\':
case ']':
case '^':
case '_': case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy54;
default: goto yy47;
}
yy56: yych = *++YYCURSOR;
yy57:
#line 346
#line 387
{ goto Directive; }
yy58: yych = *++YYCURSOR;
if(yych == '\n') goto yy56;
goto yy47;
switch(yych){
case '\n': goto yy56;
default: goto yy47;
}
}
#line 351
#line 392
}
@ -536,29 +875,30 @@ yy60: ++YYCURSOR;
yy59:
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
if(yych <= '\n'){
if(yych <= '\000') goto yy61;
if(yych <= '\t') goto yy66;
goto yy62;
} else {
if(yych == '\r') goto yy64;
goto yy66;
switch(yych){
case '\000': goto yy61;
case '\n': goto yy62;
case '\r': goto yy64;
default: goto yy66;
}
yy61:yy62: yych = *++YYCURSOR;
yy63:
#line 361
#line 402
{ goto Document; }
yy64: yych = *++YYCURSOR;
if(yych == '\n') goto yy67;
switch(yych){
case '\n': goto yy67;
default: goto yy65;
}
yy65:
#line 363
#line 404
{ goto Comment; }
yy66: yych = *++YYCURSOR;
goto yy65;
yy67: yych = *++YYCURSOR;
goto yy63;
}
#line 365
#line 406
}
@ -570,6 +910,8 @@ Scalar:
char *str = S_ALLOC_N( char, cap );
char *tok;
str[0] = '\0';
Scalar2:
tok = YYCURSOR;
@ -581,59 +923,71 @@ yy69: ++YYCURSOR;
yy68:
if((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
yych = *YYCURSOR;
if(yych <= '\n'){
if(yych <= '\000') goto yy74;
if(yych <= '\t') goto yy76;
} else {
if(yych == '\r') goto yy72;
goto yy76;
switch(yych){
case '\000': goto yy74;
case '\n': goto yy70;
case '\r': goto yy72;
default: goto yy76;
}
yy70: yych = *++YYCURSOR;
if(yych <= 'M'){
if(yych == 'C') goto yy78;
} else {
if(yych <= 'N') goto yy80;
if(yych == 'Z') goto yy83;
switch(yych){
case 'C': goto yy78;
case 'N': goto yy80;
case 'Z': goto yy83;
default: goto yy71;
}
yy71:
#line 404
#line 447
{ YYCURSOR = tok;
goto ScalarEnd;
}
yy72: yych = *++YYCURSOR;
if(yych == '\n') goto yy77;
switch(yych){
case '\n': goto yy77;
default: goto yy73;
}
yy73:
#line 412
#line 455
{ CAT(str, cap, idx, tok[0]);
goto Scalar2;
}
yy74: yych = *++YYCURSOR;
yy75:
#line 408
#line 451
{ YYCURSOR = tok;
goto ScalarEnd;
}
yy76: yych = *++YYCURSOR;
goto yy73;
yy77: yych = *++YYCURSOR;
if(yych <= 'M'){
if(yych != 'C') goto yy71;
} else {
if(yych <= 'N') goto yy80;
if(yych == 'Z') goto yy83;
goto yy71;
switch(yych){
case 'C': goto yy78;
case 'N': goto yy80;
case 'Z': goto yy83;
default: goto yy71;
}
yy78: yych = *++YYCURSOR;
yy79:
#line 381
#line 424
{ goto Scalar2; }
yy80: ++YYCURSOR;
if(YYLIMIT == YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
yy81: if(yych <= '/') goto yy82;
if(yych <= '9') goto yy80;
yy81: switch(yych){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': goto yy80;
default: goto yy82;
}
yy82:
#line 383
#line 426
{ if ( tok + 2 < YYCURSOR )
{
char *count = tok + 2;
@ -652,12 +1006,12 @@ yy82:
}
yy83: yych = *++YYCURSOR;
yy84:
#line 400
#line 443
{ CAT(str, cap, idx, '\0');
goto Scalar2;
}
}
#line 416
#line 459
ScalarEnd:
@ -685,6 +1039,8 @@ get_inline( SyckParser *parser )
char *str = S_ALLOC_N( char, cap );
char *tok;
str[0] = '\0';
Inline:
{
tok = YYCURSOR;
@ -697,27 +1053,29 @@ yy86: ++YYCURSOR;
yy85:
if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
if(yych <= '\n'){
if(yych <= '\000') goto yy91;
if(yych <= '\t') goto yy93;
} else {
if(yych == '\r') goto yy89;
goto yy93;
switch(yych){
case '\000': goto yy91;
case '\n': goto yy87;
case '\r': goto yy89;
default: goto yy93;
}
yy87: yych = *++YYCURSOR;
yy88:
#line 449
#line 494
{ return str; }
yy89: yych = *++YYCURSOR;
if(yych == '\n') goto yy94;
switch(yych){
case '\n': goto yy94;
default: goto yy90;
}
yy90:
#line 455
#line 500
{ CAT(str, cap, idx, tok[0]);
goto Inline;
}
yy91: yych = *++YYCURSOR;
yy92:
#line 451
#line 496
{ YYCURSOR = tok;
return str;
}
@ -726,7 +1084,7 @@ yy93: yych = *++YYCURSOR;
yy94: yych = *++YYCURSOR;
goto yy88;
}
#line 459
#line 504
}

File diff suppressed because it is too large Load diff

View file

@ -35,8 +35,20 @@ typedef struct RVALUE {
} as;
} RVALUE;
typedef struct {
long hash;
char *buffer;
long length;
long remaining;
int printed;
} bytestring_t;
#define RUBY_DOMAIN "ruby.yaml.org,2002"
#ifndef StringValue
#define StringValue(v)
#endif
/*
* symbols and constants
*/
@ -71,6 +83,40 @@ struct parser_xtra {
int taint;
};
/*
* Convert YAML to bytecode
*/
VALUE
rb_syck_compile(self, port)
VALUE self, port;
{
SYMID oid;
int taint;
char *ret;
VALUE bc;
bytestring_t *sav;
SyckParser *parser = syck_new_parser();
taint = syck_parser_assign_io(parser, port);
syck_parser_handler( parser, syck_yaml2byte_handler );
syck_parser_error_handler( parser, NULL );
syck_parser_implicit_typing( parser, 0 );
syck_parser_taguri_expansion( parser, 0 );
oid = syck_parse( parser );
syck_lookup_sym( parser, oid, (char **)&sav );
ret = S_ALLOC_N( char, strlen( sav->buffer ) + 3 );
ret[0] = '\0';
strcat( ret, "D\n" );
strcat( ret, sav->buffer );
syck_free_parser( parser );
bc = rb_str_new2( ret );
if ( taint ) OBJ_TAINT( bc );
return bc;
}
/*
* read from io.
*/
@ -1278,6 +1324,7 @@ Init_syck()
VALUE rb_yaml = rb_define_module( "YAML" );
VALUE rb_syck = rb_define_module_under( rb_yaml, "Syck" );
rb_define_const( rb_syck, "VERSION", rb_str_new2( SYCK_VERSION ) );
rb_define_module_function( rb_syck, "compile", rb_syck_compile, 1 );
/*
* Global symbols

View file

@ -9,8 +9,8 @@
#include <stdio.h>
#include <string.h>
#include "ruby.h"
#include "syck.h"
#include "ruby.h"
void syck_parser_pop_level( SyckParser * );
@ -88,7 +88,7 @@ syck_io_str_read( char *buf, SyckIoStr *str, long max_size, long skip )
}
if ( beg < str->ptr )
{
len = str->ptr - beg;
len = ( str->ptr - beg ) + 1;
S_MEMCPY( buf + skip, beg, char, len );
}
len += skip;

View file

@ -17,6 +17,7 @@
#define YAML_DOMAIN "yaml.org,2002"
#include <stdio.h>
#include <ctype.h>
#include "st.h"
#if defined(__cplusplus)
@ -355,6 +356,8 @@ long syck_parser_readlen( SyckParser *, long );
void syck_parser_init( SyckParser *, int );
SYMID syck_parse( SyckParser * );
void syck_default_error_handler( SyckParser *, char * );
SYMID syck_yaml2byte_handler( SyckParser *, SyckNode * );
char *syck_yaml2byte( char * );
/*
* Allocation prototypes

File diff suppressed because it is too large Load diff

251
ext/syck/yaml2byte.c Normal file
View file

@ -0,0 +1,251 @@
//
// ybext.c
//
// $Author$
// $Date$
//
// Copyright (C) 2003 why the lucky stiff, clark evans
//
// WARNING WARNING WARNING --- THIS IS *NOT JUST* PLAYING
// ANYMORE! -- WHY HAS EMBRACED THIS AS THE REAL THING!
//
#include <syck.h>
#include <assert.h>
#define YAMLBYTE_UTF8
#include "yamlbyte.h"
#include "ruby.h"
#include <stdio.h>
#define TRACE0(a) \
do { printf(a); printf("\n"); fflush(stdout); } while(0)
#define TRACE1(a,b) \
do { printf(a,b); printf("\n"); fflush(stdout); } while(0)
#define TRACE2(a,b,c) \
do { printf(a,b,c); printf("\n"); fflush(stdout); } while(0)
#define TRACE3(a,b,c,d) \
do { printf(a,b,c,d); printf("\n"); fflush(stdout); } while(0)
/* Reinvent the wheel... */
#define CHUNKSIZE 64
#define HASH ((long)0xCAFECAFECAFECAFE)
typedef struct {
long hash;
char *buffer;
long length;
long remaining;
int printed;
} bytestring_t;
bytestring_t *bytestring_alloc() {
bytestring_t *ret;
//TRACE0("bytestring_alloc()");
ret = S_ALLOC(bytestring_t);
ret->hash = HASH;
ret->length = CHUNKSIZE;
ret->remaining = ret->length;
ret->buffer = S_ALLOC_N(char, ret->length + 1 );
ret->buffer[0] = 0;
ret->printed = 0;
return ret;
}
void bytestring_append(bytestring_t *str, char code,
char *start, char *finish)
{
long grow;
long length = 2; /* CODE + LF */
char *curr;
assert(str && HASH == str->hash);
//TRACE0("bytestring_append()");
if(start) {
if(!finish)
finish = start + strlen(start);
length += (finish-start);
}
if(length > str->remaining) {
grow = (length - str->remaining) + CHUNKSIZE;
str->remaining += grow;
str->length += grow;
str->buffer = S_REALLOC_N( str->buffer, char, str->length + 1 );
assert(str->buffer);
}
curr = str->buffer + (str->length - str->remaining);
*curr = code;
curr += 1;
if(start)
while(start < finish)
*curr ++ = *start ++;
*curr = '\n';
curr += 1;
*curr = 0;
str->remaining = str->remaining - length;
assert( (str->buffer + str->length) - str->remaining );
}
void bytestring_extend(bytestring_t *str, bytestring_t *ext)
{
char *from;
char *curr;
char *stop;
long grow;
long length;
assert(str && HASH == str->hash);
assert(ext && HASH == ext->hash);
if(ext->printed) {
assert(ext->buffer[0] ==YAMLBYTE_ANCHOR);
curr = ext->buffer;
while( '\n' != *curr)
curr++;
bytestring_append(str, YAMLBYTE_ALIAS, ext->buffer + 1, curr);
} else {
ext->printed = 1;
length = (ext->length - ext->remaining);
if(length > str->remaining) {
grow = (length - str->remaining) + CHUNKSIZE;
str->remaining += grow;
str->length += grow;
str->buffer = S_REALLOC_N( str->buffer, char, str->length + 1 );
}
curr = str->buffer + (str->length - str->remaining);
from = ext->buffer;
stop = ext->buffer + length;
while( from < stop )
*curr ++ = *from ++;
*curr = 0;
str->remaining = str->remaining - length;
assert( (str->buffer + str->length) - str->remaining );
}
}
/* convert SyckNode into yamlbyte_buffer_t objects */
SYMID
syck_yaml2byte_handler(p, n)
SyckParser *p;
SyckNode *n;
{
SYMID oid;
long i;
char ch;
char nextcode;
char *start;
char *current;
char *finish;
bytestring_t *val = NULL;
bytestring_t *sav = NULL;
//TRACE0("syck_yaml2byte_handler()");
val = bytestring_alloc();
if(n->anchor) bytestring_append(val,YAMLBYTE_ANCHOR, n->anchor, NULL);
if ( n->type_id )
{
if ( p->taguri_expansion )
{
bytestring_append(val,YAMLBYTE_TRANSFER, n->type_id, NULL);
}
else
{
char *type_tag = S_ALLOC_N( char, strlen( n->type_id ) + 1 );
type_tag[0] = '\0';
strcat( type_tag, "!" );
strcat( type_tag, n->type_id );
bytestring_append( val, YAMLBYTE_TRANSFER, type_tag, NULL);
}
}
switch (n->kind)
{
case syck_str_kind:
nextcode = YAMLBYTE_SCALAR;
start = n->data.str->ptr;
finish = start + n->data.str->len - 1;
current = start;
//TRACE2("SCALAR: %s %d", start, n->data.str->len);
while(1) {
ch = *current;
if('\n' == ch || 0 == ch || current > finish) {
if(current >= start) {
bytestring_append(val, nextcode, start, current);
nextcode = YAMLBYTE_CONTINUE;
}
start = current + 1;
if(current > finish)
{
break;
}
else if('\n' == ch )
{
bytestring_append(val,YAMLBYTE_NEWLINE,NULL,NULL);
}
else if(0 == ch)
{
bytestring_append(val,YAMLBYTE_NULLCHAR,NULL,NULL);
}
else
{
assert("oops");
}
}
current += 1;
}
break;
case syck_seq_kind:
bytestring_append(val,YAMLBYTE_SEQUENCE,NULL,NULL);
for ( i = 0; i < n->data.list->idx; i++ )
{
oid = syck_seq_read( n, i );
syck_lookup_sym( p, oid, (char **)&sav );
bytestring_extend(val, sav);
}
bytestring_append(val,YAMLBYTE_END_BRANCH,NULL,NULL);
break;
case syck_map_kind:
bytestring_append(val,YAMLBYTE_MAPPING,NULL,NULL);
for ( i = 0; i < n->data.pairs->idx; i++ )
{
oid = syck_map_read( n, map_key, i );
syck_lookup_sym( p, oid, (char **)&sav );
bytestring_extend(val, sav);
oid = syck_map_read( n, map_value, i );
syck_lookup_sym( p, oid, (char **)&sav );
bytestring_extend(val, sav);
}
bytestring_append(val,YAMLBYTE_END_BRANCH,NULL,NULL);
break;
}
oid = syck_add_sym( p, (char *) val );
//TRACE1("Saving: %s", val->buffer );
return oid;
}
char *
syck_yaml2byte(char *yamlstr)
{
SYMID oid;
char *ret;
bytestring_t *sav;
SyckParser *parser = syck_new_parser();
syck_parser_str_auto( parser, yamlstr, NULL );
syck_parser_handler( parser, syck_yaml2byte_handler );
syck_parser_error_handler( parser, NULL );
syck_parser_implicit_typing( parser, 1 );
syck_parser_taguri_expansion( parser, 1 );
oid = syck_parse( parser );
syck_lookup_sym( parser, oid, (char **)&sav );
ret = S_ALLOC_N( char, strlen( sav->buffer ) + 3 );
ret[0] = '\0';
strcat( ret, "D\n" );
strcat( ret, sav->buffer );
syck_free_parser( parser );
return ret;
}
#ifdef TEST_YBEXT
#include <stdio.h>
int main() {
char *yaml = "test: 1\nand: \"with new\\nline\\n\"\nalso: &3 three\nmore: *3";
printf("--- # YAML \n");
printf(yaml);
printf("\n...\n");
printf(syck_yaml2byte(yaml));
return 0;
}
#endif