[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-core
Subject:    %y{ ... } patch for 1.8.0
From:       why the lucky stiff <ruby-core () whytheluckystiff ! net>
Date:       2003-08-22 18:05:52
[Download RAW message or body]

hey, y'alls.

this morning, i patched my 1.8.0 to use %y{ ... } for loading YAML strings:

  invoice = %y{
    ---
    id: 34843
    date   : 2001-01-23
    bill-to: Chris Dumars
    product:
      - item    : Super Hoop
        quantity: 1
      - item    : Basketball
        quantity: 4
      - item    : Big Shoes
        quantity: 1
  }

kinda fun.

here's a simple patch if anyone wants to play with it.  i know i need to clean 
up the yacc entry.  this is just proof of concept.  surprisingly,
YAML's awareness of indentation doesn't pose much of an issue.

_why

["ruby-1.8.0-yamlstr.patch" (text/plain)]

diff -wur ruby-1.8.0/eval.c ruby-1.8.0-yamlstr/eval.c
--- ruby-1.8.0/eval.c	Sun Aug  3 23:28:50 2003
+++ ruby-1.8.0-yamlstr/eval.c	Fri Aug 22 11:24:46 2003
@@ -3399,6 +3399,11 @@
 	result = rb_funcall(self, '`', 1, node->nd_lit);
 	break;
 
+      case NODE_YAMLSTR:
+    rb_require("yaml");
+	result = rb_funcall(rb_const_get_at(rb_cObject, rb_intern("YAML")), \
rb_intern("load"), 1, node->nd_lit); +	break;
+
       case NODE_LIT:
 	result = node->nd_lit;
 	break;
diff -wur ruby-1.8.0/gc.c ruby-1.8.0-yamlstr/gc.c
--- ruby-1.8.0/gc.c	Sat Aug  2 00:45:57 2003
+++ ruby-1.8.0-yamlstr/gc.c	Fri Aug 22 11:28:31 2003
@@ -738,6 +738,7 @@
 	  case NODE_LIT:
 	  case NODE_STR:
 	  case NODE_XSTR:
+	  case NODE_YAMLSTR:
 	  case NODE_DEFINED:
 	  case NODE_MATCH:
 	  case NODE_RETURN:
diff -wur ruby-1.8.0/node.h ruby-1.8.0-yamlstr/node.h
--- ruby-1.8.0/node.h	Wed Jul 16 01:11:27 2003
+++ ruby-1.8.0-yamlstr/node.h	Fri Aug 22 11:27:15 2003
@@ -125,6 +125,7 @@
     NODE_IFUNC,
     NODE_DSYM,
     NODE_ATTRASGN,
+	NODE_YAMLSTR,
     NODE_LAST
 };
 
@@ -298,6 +299,7 @@
 #define NEW_STR(s) NEW_NODE(NODE_STR,s,0,0)
 #define NEW_DSTR(s) NEW_NODE(NODE_DSTR,s,0,0)
 #define NEW_XSTR(s) NEW_NODE(NODE_XSTR,s,0,0)
+#define NEW_YAMLSTR(s) NEW_NODE(NODE_YAMLSTR,s,0,0)
 #define NEW_DXSTR(s) NEW_NODE(NODE_DXSTR,s,0,0)
 #define NEW_DSYM(s) NEW_NODE(NODE_DSYM,s,0,0)
 #define NEW_EVSTR(n) NEW_NODE(NODE_EVSTR,0,(n),0)
diff -wur ruby-1.8.0/parse.y ruby-1.8.0-yamlstr/parse.y
--- ruby-1.8.0/parse.y	Wed Jul 30 00:38:32 2003
+++ ruby-1.8.0-yamlstr/parse.y	Fri Aug 22 11:29:24 2003
@@ -247,7 +247,7 @@
 %token <node> tNTH_REF tBACK_REF
 %token <num>  tREGEXP_END
 
-%type <node> singleton strings string string1 xstring regexp
+%type <node> singleton strings string string1 xstring regexp yaml
 %type <node> string_contents xstring_contents string_content
 %type <node> words qwords word_list qword_list word
 %type <node> literal numeric dsym cpath
@@ -290,7 +290,7 @@
 %token tLBRACE_ARG	/* { */
 %token tSTAR		/* * */
 %token tAMPER		/* & */
-%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
+%token tSYMBEG tSTRING_BEG tXSTRING_BEG tYAML_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
 %token tSTRING_DBEG tSTRING_DVAR tSTRING_END
 
 /*
@@ -1399,6 +1399,7 @@
 primary		: literal
 		| strings
 		| xstring
+		| yaml
 		| regexp
 		| words
 		| qwords
@@ -1922,6 +1923,30 @@
 		    }
 		;
 
+yaml		: tYAML_BEG xstring_contents tSTRING_END
+		    {
+			NODE *node = $2;
+            rb_require("yaml");
+			if (!node) {
+			    node = NEW_YAMLSTR(rb_str_new(0, 0));
+			}
+			else {
+			    switch (nd_type(node)) {
+			      case NODE_STR:
+				nd_set_type(node, NODE_YAMLSTR);
+				break;
+			      case NODE_DSTR:
+				nd_set_type(node, NODE_YAMLSTR);
+				break;
+			      default:
+				node = NEW_NODE(NODE_YAMLSTR, rb_str_new(0, 0), 1, NEW_LIST(node));
+				break;
+			    }
+			}
+			$$ = node;
+		    }
+		;
+
 regexp		: tREGEXP_BEG xstring_contents tREGEXP_END
 		    {
 			int options = $3;
@@ -2936,6 +2961,7 @@
     str_squote = (0),
     str_dquote = (STR_FUNC_EXPAND),
     str_xquote = (STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
+    str_yaml   = (STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
     str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
     str_sword  = (STR_FUNC_QWORDS),
     str_dword  = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
@@ -4146,6 +4172,10 @@
 	      case 'x':
 		lex_strterm = NEW_STRTERM(str_xquote, term, paren);
 		return tXSTRING_BEG;
+
+	      case 'y':
+		lex_strterm = NEW_STRTERM(str_yaml, term, paren);
+		return tYAML_BEG;
 
 	      case 'r':
 		lex_strterm = NEW_STRTERM(str_regexp, term, paren);



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic