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

ruby-changes:48921

From: yui-knk <ko1@a...>
Date: Wed, 6 Dec 2017 12:10:10 +0900 (JST)
Subject: [ruby-changes:48921] yui-knk:r61039 (trunk): parse.y: Fix the first location of heredoc identifier

yui-knk	2017-12-06 12:09:55 +0900 (Wed, 06 Dec 2017)

  New Revision: 61039

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

  Log:
    parse.y: Fix the first location of heredoc identifier
    
    * parse.y (parser_heredoc_identifier):
      Put length of term at the head of rb_strterm_heredoc_struct.term.
    
    * parse.y (rb_parser_set_location_from_strterm_heredoc):
      Use length of term to calculate first_loc.column.
    
      e.g. The locations of the NODE_DSTR is fixed:
    
      ```
      a <<STR
      123
      #{:a}
      STR
      ```
    
      * Before
    
      ```
      NODE_DSTR (line: 3, code_range: (1,3)-(1,7))
      ```
    
      * After
    
      ```
      NODE_DSTR (line: 3, code_range: (1,2)-(1,7))
      ```

  Modified files:
    trunk/parse.y
Index: parse.y
===================================================================
--- parse.y	(revision 61038)
+++ parse.y	(revision 61039)
@@ -6625,7 +6625,7 @@ parser_parse_string(struct parser_params https://github.com/ruby/ruby/blob/trunk/parse.y#L6625
 static enum yytokentype
 parser_heredoc_identifier(struct parser_params *parser)
 {
-    int c = nextc(), term, func = 0;
+    int c = nextc(), term, func = 0, term_len = 2; /* length of "<<" */
     enum yytokentype token = tSTRING_BEG;
     long len;
     int newline = 0;
@@ -6633,24 +6633,31 @@ parser_heredoc_identifier(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L6633
 
     if (c == '-') {
 	c = nextc();
+	term_len++;
 	func = STR_FUNC_INDENT;
     }
     else if (c == '~') {
 	c = nextc();
+	term_len++;
 	func = STR_FUNC_INDENT;
 	indent = INT_MAX;
     }
     switch (c) {
       case '\'':
+	term_len++;
 	func |= str_squote; goto quoted;
       case '"':
+	term_len++;
 	func |= str_dquote; goto quoted;
       case '`':
+	term_len++;
 	token = tXSTRING_BEG;
 	func |= str_xquote; goto quoted;
 
       quoted:
+	term_len++;
 	newtok();
+	tokadd(term_len);
 	tokadd(func);
 	term = c;
 	while ((c = nextc()) != -1 && c != term) {
@@ -6682,6 +6689,7 @@ parser_heredoc_identifier(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L6689
 	    return 0;
 	}
 	newtok();
+	tokadd(term_len);
 	tokadd(func |= str_dquote);
 	do {
 	    if (tokadd_mbchar(c) == -1) return 0;
@@ -6690,6 +6698,7 @@ parser_heredoc_identifier(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L6698
 	break;
     }
 
+    tokenbuf[0] = tokenbuf[0] + toklen() - 2;
     tokfix();
     dispatch_scan_event(tHEREDOC_BEG);
     len = lex_p - lex_pbeg;
@@ -6925,7 +6934,8 @@ parser_here_document(struct parser_param https://github.com/ruby/ruby/blob/trunk/parse.y#L6934
     rb_encoding *enc = current_enc;
 
     eos = RSTRING_PTR(here->term);
-    len = RSTRING_LEN(here->term) - 1;
+    len = RSTRING_LEN(here->term) - 2; /* here->term includes term_len and func */
+    eos++; /* skip term_len */
     indent = (func = *eos++) & STR_FUNC_INDENT;
 
     if ((c = nextc()) == -1) {
@@ -9841,8 +9851,11 @@ rb_parser_fatal(struct parser_params *pa https://github.com/ruby/ruby/blob/trunk/parse.y#L9851
 void
 rb_parser_set_location_from_strterm_heredoc(struct parser_params *parser, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
 {
+    const char *eos = RSTRING_PTR(here->term);
+    int term_len = (int)eos[0];
+
     yylloc->first_loc.lineno = (int)here->sourceline;
-    yylloc->first_loc.column = (int)(here->u3.lastidx - RSTRING_LEN(here->term));
+    yylloc->first_loc.column = (int)(here->u3.lastidx - term_len);
     yylloc->last_loc.lineno  = (int)here->sourceline;
     yylloc->last_loc.column  = (int)(here->u3.lastidx);
 }

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

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