[前][次][番号順一覧][スレッド一覧]

ruby-changes:41016

From: nobu <ko1@a...>
Date: Sun, 13 Dec 2015 23:46:37 +0900 (JST)
Subject: [ruby-changes:41016] nobu:r53095 (trunk): parse.y: lex_state trace by yydebug

nobu	2015-12-13 23:46:09 +0900 (Sun, 13 Dec 2015)

  New Revision: 53095

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53095

  Log:
    parse.y: lex_state trace by yydebug
    
    * parse.y (trace_lex_state): trace lex_state changes if yydebug is
      set, and send the messages to rb_stdout.
    * parse.y (rb_parser_printf): store YYPRINTF messages per lines
      so that lex_state traces do not mix.
    * tool/ytab.sed: add parser argument to yy_stack_print too.

  Modified files:
    trunk/ChangeLog
    trunk/node.h
    trunk/parse.y
    trunk/test/ruby/test_rubyoptions.rb
    trunk/tool/ytab.sed
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53094)
+++ ChangeLog	(revision 53095)
@@ -1,3 +1,23 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Dec 13 23:46:10 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (trace_lex_state): trace lex_state changes if yydebug is
+	  set, and send the messages to rb_stdout.
+
+	* parse.y (rb_parser_printf): store YYPRINTF messages per lines
+	  so that lex_state traces do not mix.
+
+	* tool/ytab.sed: add parser argument to yy_stack_print too.
+
+Sun Dec 13 23:45:19 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (trace_lex_state): trace lex_state changes if yydebug is
+	  set, and send the messages to rb_stdout.
+
+	* parse.y (rb_parser_printf): store YYPRINTF messages per lines
+	  so that lex_state traces do not mix.
+
+	* tool/ytab.sed: add parser argument to yy_stack_print too.
+
 Sun Dec 13 20:41:16 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* parse.y (build_lex_state_name, trace_lex_state): lex_state is
Index: parse.y
===================================================================
--- parse.y	(revision 53094)
+++ parse.y	(revision 53095)
@@ -44,6 +44,12 @@ https://github.com/ruby/ruby/blob/trunk/parse.y#L44
 #define YYREALLOC(ptr, size)	rb_parser_realloc(parser, (ptr), (size))
 #define YYCALLOC(nelem, size)	rb_parser_calloc(parser, (nelem), (size))
 #define YYFREE(ptr)		rb_parser_free(parser, (ptr))
+#ifdef HAVE_VA_ARGS_MACRO
+# define YYFPRINTF(f, fmt, ...) rb_parser_printf(parser, fmt, ##__VA_ARGS__)
+#else
+# define YYFPRINTF		rb_parser_printf
+# define stderr 		parser
+#endif
 #undef malloc
 #undef realloc
 #undef calloc
@@ -95,11 +101,9 @@ enum lex_state_e { https://github.com/ruby/ruby/blob/trunk/parse.y#L101
 
 # define SET_LEX_STATE(ls) \
     (lex_state = trace_lex_state(lex_state, (ls), __LINE__))
-#if PARSER_DEBUG
 static enum lex_state_e trace_lex_state(enum lex_state_e from, enum lex_state_e to, int line);
-#else
-# define trace_lex_state(from, to, line) (to)
-#endif
+# define trace_lex_state(from, to, line) \
+    (yydebug ? trace_lex_state(from, to, line) : (to))
 
 typedef VALUE stack_type;
 
@@ -274,6 +278,8 @@ struct parser_params { https://github.com/ruby/ruby/blob/trunk/parse.y#L278
     token_info *token_info;
     VALUE compile_option;
 
+    VALUE debug_buffer;
+
     ID cur_arg;
 
     int last_cr_line;
@@ -9179,54 +9185,47 @@ id_is_var_gen(struct parser_params *pars https://github.com/ruby/ruby/blob/trunk/parse.y#L9185
 }
 #endif /* !RIPPER */
 
-#if PARSER_DEBUG
 static const char lex_state_names[][13] = {
     "EXPR_BEG",    "EXPR_END",    "EXPR_ENDARG", "EXPR_ENDFN",  "EXPR_ARG",
     "EXPR_CMDARG", "EXPR_MID",    "EXPR_FNAME",  "EXPR_DOT",    "EXPR_CLASS",
     "EXPR_LABEL",  "EXPR_LABELED",
 };
 
-static const char *
-build_lex_state_name(enum lex_state_e state, char *buf, size_t size)
+static VALUE
+append_lex_state_name(enum lex_state_e state, VALUE buf)
 {
     int i, sep = 0;
-    char *p = buf;
     unsigned int mask = 1;
-    size_t n;
     static const char none[] = "EXPR_NONE";
 
     for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
 	if ((unsigned)state & mask) {
 	    if (sep) {
-		if (size < 2) break;
-		--size;
-		*p++ = '|';
+		rb_str_cat(buf, "|", 1);
 	    }
 	    sep = 1;
-	    n = strlcpy(p, lex_state_names[i], size);
-	    if (n >= size) break;
-	    size -= n;
-	    p += n;
+	    rb_str_cat_cstr(buf, lex_state_names[i]);
 	}
     }
-    if (p == buf && size >= sizeof(none)) {
-	n = strlcpy(buf, none, size);
-	p += n;
+    if (!sep) {
+	rb_str_cat(buf, none, sizeof(none)-1);
     }
-    *p = '\0';
     return buf;
 }
 
+#undef trace_lex_state
 static enum lex_state_e
 trace_lex_state(enum lex_state_e from, enum lex_state_e to, int line)
 {
-    char buf1[sizeof(lex_state_names)], buf2[sizeof(lex_state_names)];
-    build_lex_state_name(from, buf1, sizeof(buf1));
-    build_lex_state_name(to, buf2, sizeof(buf2));
-    printf("lex_state: %s -> %s at L%d\n", buf1, buf2, line);
+    VALUE mesg;
+    mesg = rb_str_new_cstr("lex_state: ");
+    append_lex_state_name(from, mesg);
+    rb_str_cat_cstr(mesg, " -> ");
+    append_lex_state_name(to, mesg);
+    rb_str_catf(mesg, " at line %d\n", line);
+    rb_io_write(rb_stdout, mesg);
     return to;
 }
-#endif
 
 #ifdef RIPPER
 static VALUE
@@ -10776,6 +10775,7 @@ parser_initialize(struct parser_params * https://github.com/ruby/ruby/blob/trunk/parse.y#L10775
     parser->result = Qnil;
     parser->parsing_thread = Qnil;
 #endif
+    parser->debug_buffer = Qnil;
     parser->enc = rb_utf8_encoding();
 }
 
@@ -11034,6 +11034,21 @@ rb_parser_free(struct parser_params *par https://github.com/ruby/ruby/blob/trunk/parse.y#L11034
     xfree(ptr);
 }
 #endif
+
+void
+rb_parser_printf(struct parser_params *parser, const char *fmt, ...)
+{
+    va_list ap;
+    VALUE mesg = parser->debug_buffer;
+
+    if (NIL_P(mesg)) parser->debug_buffer = mesg = rb_str_new(0, 0);
+    va_start(ap, fmt);
+    rb_str_vcatf(mesg, fmt, ap);
+    va_end(ap);
+    if (RSTRING_END(mesg)[-1] == '\n') {
+	rb_io_write(rb_stdout, mesg);
+    }
+}
 #endif
 
 #ifdef RIPPER
Index: tool/ytab.sed
===================================================================
--- tool/ytab.sed	(revision 53094)
+++ tool/ytab.sed	(revision 53095)
@@ -14,7 +14,7 @@ a\ https://github.com/ruby/ruby/blob/trunk/tool/ytab.sed#L14
 /^yydestruct.*yymsg/,/#endif/{
   /^yydestruct/{
     /parser/!{
-      h
+      H
       s/^/ruby_parser_&/
       s/)$/, parser)/
       /\*/s/parser)$/struct parser_params *&/
@@ -22,7 +22,7 @@ a\ https://github.com/ruby/ruby/blob/trunk/tool/ytab.sed#L22
   }
   /^#endif/{
     x
-    /^./{
+    /yydestruct/{
       i\
     struct parser_params *parser;
       a\
@@ -31,6 +31,22 @@ a\ https://github.com/ruby/ruby/blob/trunk/tool/ytab.sed#L31
     x
   }
 }
+/^yy_stack_print/{
+  /parser/!{
+    H
+    s/)$/, parser)/
+    /\*/s/parser)$/struct parser_params *&/
+  }
+}
+/yy_stack_print.*;/{
+  x
+  /yy_stack_print/{
+    x
+    s/\(yy_stack_print *\)(\(.*\));/\1(\2, parser);/
+    x
+  }
+  x
+}
 s/^\([ 	]*\)\(yyerror[ 	]*([ 	]*parser,\)/\1parser_\2/
 s!^ *extern char \*getenv();!/* & */!
 s/^\(#.*\)".*\.tab\.c"/\1"parse.c"/
Index: test/ruby/test_rubyoptions.rb
===================================================================
--- test/ruby/test_rubyoptions.rb	(revision 53094)
+++ test/ruby/test_rubyoptions.rb	(revision 53095)
@@ -191,13 +191,13 @@ class TestRubyOptions < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L191
 
   def test_yydebug
     assert_in_out_err(["-ye", ""]) do |r, e|
-      assert_equal([], r)
-      assert_not_equal([], e)
+      assert_not_equal([], r)
+      assert_equal([], e)
     end
 
     assert_in_out_err(%w(--yydebug -e) + [""]) do |r, e|
-      assert_equal([], r)
-      assert_not_equal([], e)
+      assert_not_equal([], r)
+      assert_equal([], e)
     end
   end
 
Index: node.h
===================================================================
--- node.h	(revision 53094)
+++ node.h	(revision 53095)
@@ -506,6 +506,7 @@ void *rb_parser_malloc(struct parser_par https://github.com/ruby/ruby/blob/trunk/node.h#L506
 void *rb_parser_realloc(struct parser_params *, void *, size_t);
 void *rb_parser_calloc(struct parser_params *, size_t, size_t);
 void rb_parser_free(struct parser_params *, void *);
+void rb_parser_printf(struct parser_params *parser, const char *fmt, ...);
 
 RUBY_SYMBOL_EXPORT_END
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]