Blame SOURCES/7.4.036

22c937
To: vim_dev@googlegroups.com
22c937
Subject: Patch 7.4.036
22c937
Fcc: outbox
22c937
From: Bram Moolenaar <Bram@moolenaar.net>
22c937
Mime-Version: 1.0
22c937
Content-Type: text/plain; charset=UTF-8
22c937
Content-Transfer-Encoding: 8bit
22c937
------------
22c937
22c937
Patch 7.4.036
22c937
Problem:    NFA engine does not capture group correctly when using \@>. (ZyX)
22c937
Solution:   Copy submatches before doing the recursive match.
22c937
Files:	    src/regexp_nfa.c, src/testdir/test64.in, src/testdir/test64.ok
22c937
22c937
22c937
*** ../vim-7.4.035/src/regexp_nfa.c	2013-09-22 13:57:19.000000000 +0200
22c937
--- src/regexp_nfa.c	2013-09-25 16:35:54.000000000 +0200
22c937
***************
22c937
*** 36,42 ****
22c937
  {
22c937
      NFA_SPLIT = -1024,
22c937
      NFA_MATCH,
22c937
!     NFA_SKIP_CHAR,		    /* matches a 0-length char */
22c937
  
22c937
      NFA_START_COLL,		    /* [abc] start */
22c937
      NFA_END_COLL,		    /* [abc] end */
22c937
--- 36,42 ----
22c937
  {
22c937
      NFA_SPLIT = -1024,
22c937
      NFA_MATCH,
22c937
!     NFA_EMPTY,			    /* matches 0-length */
22c937
  
22c937
      NFA_START_COLL,		    /* [abc] start */
22c937
      NFA_END_COLL,		    /* [abc] end */
22c937
***************
22c937
*** 2005,2012 ****
22c937
  	    {
22c937
  		/* Ignore result of previous call to nfa_regatom() */
22c937
  		post_ptr = post_start + my_post_start;
22c937
! 		/* NFA_SKIP_CHAR has 0-length and works everywhere */
22c937
! 		EMIT(NFA_SKIP_CHAR);
22c937
  		return OK;
22c937
  	    }
22c937
  
22c937
--- 2005,2012 ----
22c937
  	    {
22c937
  		/* Ignore result of previous call to nfa_regatom() */
22c937
  		post_ptr = post_start + my_post_start;
22c937
! 		/* NFA_EMPTY is 0-length and works everywhere */
22c937
! 		EMIT(NFA_EMPTY);
22c937
  		return OK;
22c937
  	    }
22c937
  
22c937
***************
22c937
*** 2170,2185 ****
22c937
  	old_post_pos = (int)(post_ptr - post_start);
22c937
  	if (nfa_regconcat() == FAIL)
22c937
  	    return FAIL;
22c937
! 	/* if concat is empty, skip a input char. But do emit a node */
22c937
  	if (old_post_pos == (int)(post_ptr - post_start))
22c937
! 	    EMIT(NFA_SKIP_CHAR);
22c937
  	EMIT(NFA_CONCAT);
22c937
  	ch = peekchr();
22c937
      }
22c937
  
22c937
!     /* Even if a branch is empty, emit one node for it */
22c937
      if (old_post_pos == (int)(post_ptr - post_start))
22c937
! 	EMIT(NFA_SKIP_CHAR);
22c937
  
22c937
      return OK;
22c937
  }
22c937
--- 2170,2185 ----
22c937
  	old_post_pos = (int)(post_ptr - post_start);
22c937
  	if (nfa_regconcat() == FAIL)
22c937
  	    return FAIL;
22c937
! 	/* if concat is empty do emit a node */
22c937
  	if (old_post_pos == (int)(post_ptr - post_start))
22c937
! 	    EMIT(NFA_EMPTY);
22c937
  	EMIT(NFA_CONCAT);
22c937
  	ch = peekchr();
22c937
      }
22c937
  
22c937
!     /* if a branch is empty, emit one node for it */
22c937
      if (old_post_pos == (int)(post_ptr - post_start))
22c937
! 	EMIT(NFA_EMPTY);
22c937
  
22c937
      return OK;
22c937
  }
22c937
***************
22c937
*** 2423,2429 ****
22c937
  	case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
22c937
  	case NFA_QUEST:		STRCPY(code, "NFA_QUEST"); break;
22c937
  	case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
22c937
! 	case NFA_SKIP_CHAR:	STRCPY(code, "NFA_SKIP_CHAR"); break;
22c937
  	case NFA_OR:		STRCPY(code, "NFA_OR"); break;
22c937
  
22c937
  	case NFA_START_COLL:	STRCPY(code, "NFA_START_COLL"); break;
22c937
--- 2423,2429 ----
22c937
  	case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
22c937
  	case NFA_QUEST:		STRCPY(code, "NFA_QUEST"); break;
22c937
  	case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
22c937
! 	case NFA_EMPTY:		STRCPY(code, "NFA_EMPTY"); break;
22c937
  	case NFA_OR:		STRCPY(code, "NFA_OR"); break;
22c937
  
22c937
  	case NFA_START_COLL:	STRCPY(code, "NFA_START_COLL"); break;
22c937
***************
22c937
*** 3067,3073 ****
22c937
  	    case NFA_ZSTART:
22c937
  	    case NFA_ZEND:
22c937
  	    case NFA_OPT_CHARS:
22c937
! 	    case NFA_SKIP_CHAR:
22c937
  	    case NFA_START_PATTERN:
22c937
  	    case NFA_END_PATTERN:
22c937
  	    case NFA_COMPOSING:
22c937
--- 3067,3073 ----
22c937
  	    case NFA_ZSTART:
22c937
  	    case NFA_ZEND:
22c937
  	    case NFA_OPT_CHARS:
22c937
! 	    case NFA_EMPTY:
22c937
  	    case NFA_START_PATTERN:
22c937
  	    case NFA_END_PATTERN:
22c937
  	    case NFA_COMPOSING:
22c937
***************
22c937
*** 3265,3279 ****
22c937
  	    PUSH(frag(e1.start, e2.out));
22c937
  	    break;
22c937
  
22c937
! 	case NFA_SKIP_CHAR:
22c937
! 	    /* Symbol of 0-length, Used in a repetition
22c937
! 	     * with max/min count of 0 */
22c937
  	    if (nfa_calc_size == TRUE)
22c937
  	    {
22c937
  		nstate++;
22c937
  		break;
22c937
  	    }
22c937
! 	    s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
22c937
  	    if (s == NULL)
22c937
  		goto theend;
22c937
  	    PUSH(frag(s, list1(&s->out)));
22c937
--- 3265,3278 ----
22c937
  	    PUSH(frag(e1.start, e2.out));
22c937
  	    break;
22c937
  
22c937
! 	case NFA_EMPTY:
22c937
! 	    /* 0-length, used in a repetition with max/min count of 0 */
22c937
  	    if (nfa_calc_size == TRUE)
22c937
  	    {
22c937
  		nstate++;
22c937
  		break;
22c937
  	    }
22c937
! 	    s = alloc_state(NFA_EMPTY, NULL, NULL);
22c937
  	    if (s == NULL)
22c937
  		goto theend;
22c937
  	    PUSH(frag(s, list1(&s->out)));
22c937
***************
22c937
*** 4209,4215 ****
22c937
  	case NFA_MOPEN:
22c937
  	case NFA_ZEND:
22c937
  	case NFA_SPLIT:
22c937
! 	case NFA_SKIP_CHAR:
22c937
  	    /* These nodes are not added themselves but their "out" and/or
22c937
  	     * "out1" may be added below.  */
22c937
  	    break;
22c937
--- 4208,4214 ----
22c937
  	case NFA_MOPEN:
22c937
  	case NFA_ZEND:
22c937
  	case NFA_SPLIT:
22c937
! 	case NFA_EMPTY:
22c937
  	    /* These nodes are not added themselves but their "out" and/or
22c937
  	     * "out1" may be added below.  */
22c937
  	    break;
22c937
***************
22c937
*** 4337,4343 ****
22c937
  	    subs = addstate(l, state->out1, subs, pim, off);
22c937
  	    break;
22c937
  
22c937
! 	case NFA_SKIP_CHAR:
22c937
  	case NFA_NOPEN:
22c937
  	case NFA_NCLOSE:
22c937
  	    subs = addstate(l, state->out, subs, pim, off);
22c937
--- 4336,4342 ----
22c937
  	    subs = addstate(l, state->out1, subs, pim, off);
22c937
  	    break;
22c937
  
22c937
! 	case NFA_EMPTY:
22c937
  	case NFA_NOPEN:
22c937
  	case NFA_NCLOSE:
22c937
  	    subs = addstate(l, state->out, subs, pim, off);
22c937
***************
22c937
*** 5604,5612 ****
22c937
  		    {
22c937
  			int in_use = m->norm.in_use;
22c937
  
22c937
! 			/* Copy submatch info for the recursive call, so that
22c937
! 			 * \1 can be matched. */
22c937
  			copy_sub_off(&m->norm, &t->subs.norm);
22c937
  
22c937
  			/*
22c937
  			 * First try matching the invisible match, then what
22c937
--- 5603,5615 ----
22c937
  		    {
22c937
  			int in_use = m->norm.in_use;
22c937
  
22c937
! 			/* Copy submatch info for the recursive call, opposite
22c937
! 			 * of what happens on success below. */
22c937
  			copy_sub_off(&m->norm, &t->subs.norm);
22c937
+ #ifdef FEAT_SYN_HL
22c937
+ 			if (nfa_has_zsubexpr)
22c937
+ 			    copy_sub_off(&m->synt, &t->subs.synt);
22c937
+ #endif
22c937
  
22c937
  			/*
22c937
  			 * First try matching the invisible match, then what
22c937
***************
22c937
*** 5713,5718 ****
22c937
--- 5716,5728 ----
22c937
  #endif
22c937
  		    break;
22c937
  		}
22c937
+ 		/* Copy submatch info to the recursive call, opposite of what
22c937
+ 		 * happens afterwards. */
22c937
+ 		copy_sub_off(&m->norm, &t->subs.norm);
22c937
+ #ifdef FEAT_SYN_HL
22c937
+ 		if (nfa_has_zsubexpr)
22c937
+ 		    copy_sub_off(&m->synt, &t->subs.synt);
22c937
+ #endif
22c937
  
22c937
  		/* First try matching the pattern. */
22c937
  		result = recursive_regmatch(t->state, NULL, prog,
22c937
*** ../vim-7.4.035/src/testdir/test64.in	2013-09-22 13:57:19.000000000 +0200
22c937
--- src/testdir/test64.in	2013-09-25 15:51:12.000000000 +0200
22c937
***************
22c937
*** 430,435 ****
22c937
--- 430,436 ----
22c937
  :call add(tl, [2, '\(a*\)\@>a', 'aaaa'])
22c937
  :call add(tl, [2, '\(a*\)\@>b', 'aaab', 'aaab', 'aaa'])
22c937
  :call add(tl, [2, '^\(.\{-}b\)\@>.', '  abcbd', '  abc', '  ab'])
22c937
+ :call add(tl, [2, '\(.\{-}\)\(\)\@>$', 'abc', 'abc', 'abc', ''])
22c937
  :" TODO: BT engine does not restore submatch after failure
22c937
  :call add(tl, [1, '\(a*\)\@>a\|a\+', 'aaaa', 'aaaa'])
22c937
  :"
22c937
*** ../vim-7.4.035/src/testdir/test64.ok	2013-09-22 13:57:19.000000000 +0200
22c937
--- src/testdir/test64.ok	2013-09-25 16:39:31.000000000 +0200
22c937
***************
22c937
*** 992,997 ****
22c937
--- 992,1000 ----
22c937
  OK 0 - ^\(.\{-}b\)\@>.
22c937
  OK 1 - ^\(.\{-}b\)\@>.
22c937
  OK 2 - ^\(.\{-}b\)\@>.
22c937
+ OK 0 - \(.\{-}\)\(\)\@>$
22c937
+ OK 1 - \(.\{-}\)\(\)\@>$
22c937
+ OK 2 - \(.\{-}\)\(\)\@>$
22c937
  OK 0 - \(a*\)\@>a\|a\+
22c937
  OK 2 - \(a*\)\@>a\|a\+
22c937
  OK 0 - \_[^8-9]\+
22c937
*** ../vim-7.4.035/src/version.c	2013-09-22 15:43:34.000000000 +0200
22c937
--- src/version.c	2013-09-25 16:40:01.000000000 +0200
22c937
***************
22c937
*** 740,741 ****
22c937
--- 740,743 ----
22c937
  {   /* Add new patch number below this line */
22c937
+ /**/
22c937
+     36,
22c937
  /**/
22c937
22c937
-- 
22c937
There is a fine line between courage and foolishness.
22c937
Unfortunately, it's not a fence.
22c937
22c937
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
22c937
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
22c937
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
22c937
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///