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