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