I wrote a patch to make NFA state dump more pretty.

Before patch:

> (11) NFA_MOPEN(0) (-991) (id=11)
> (10)     NFA_SPLIT  (-1024) (id=10)
> ( 8)         NFA_MOPEN(1) (-990) (id=8)
> ( 7)             NFA_SPLIT  (-1024) (id=7)
> ( 1)                 CHAR(f) (102) (id=1)
> ( 2)                     CHAR(o) (111) (id=2)
> ( 3)                         CHAR(o) (111) (id=3)
> ( 9)                             NFA_MCLOSE(1) (-980) (id=9)
> (10)                                 NFA_SPLIT  (-1024) (id=10)
> ( 4)                 CHAR(b) (98) (id=4)
> ( 5)                     CHAR(a) (97) (id=5)
> ( 6)                         CHAR(r) (114) (id=6)
> ( 9)                             NFA_MCLOSE(1) (-980) (id=9)
> (12)         NFA_MCLOSE(0) (-981) (id=12)
> ( 0)             NFA_MATCH  (-1023) (id=0)

After patch:

> (11) NFA_MOPEN(0) (-991) (id=11)
> (10) +-NFA_SPLIT  (-1024) (id=10)
> ( 8)   +-NFA_MOPEN(1) (-990) (id=8)
> ( 7)   | +-NFA_SPLIT  (-1024) (id=7)
> ( 1)   |   +-CHAR(f) (102) (id=1)
> ( 2)   |   | +-CHAR(o) (111) (id=2)
> ( 3)   |   |   +-CHAR(o) (111) (id=3)
> ( 9)   |   |     +-NFA_MCLOSE(1) (-980) (id=9)
> (10)   |   |       +-NFA_SPLIT  (-1024) (id=10)
> ( 4)   |   +-CHAR(b) (98) (id=4)
> ( 5)   |     +-CHAR(a) (97) (id=5)
> ( 6)   |       +-CHAR(r) (114) (id=6)
> ( 9)   |         +-NFA_MCLOSE(1) (-980) (id=9)
> (12)   +-NFA_MCLOSE(0) (-981) (id=12)
> ( 0)     +-NFA_MATCH  (-1023) (id=0)

This will be great help to debugging NFA with complex patterns.

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


# HG changeset patch
# Parent e782f5be8263bed448060c473adaec560ec83aed

diff -r e782f5be8263 src/regexp_nfa.c
--- a/src/regexp_nfa.c	Thu May 23 22:43:08 2013 +0200
+++ b/src/regexp_nfa.c	Sat May 25 08:54:21 2013 +0900
@@ -170,7 +170,8 @@
 #ifdef DEBUG
 static void nfa_set_code __ARGS((int c));
 static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
-static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state, int ident));
+static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
+static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
 static void nfa_dump __ARGS((nfa_regprog_T *prog));
 #endif
 static int *re2post __ARGS((void));
@@ -1841,29 +1842,75 @@
  * Print the NFA starting with a root node "state".
  */
     static void
-nfa_print_state(debugf, state, ident)
+nfa_print_state(debugf, state)
     FILE *debugf;
     nfa_state_T *state;
-    int ident;
 {
-    int i;
+    garray_T indent;
+
+    ga_init2(&indent, 1, 64);
+    ga_append(&indent, '\0');
+    nfa_print_state2(debugf, state, &indent);
+    fclose(debugf);
+    ga_clear(&indent);
+}
+
+    static void
+nfa_print_state2(debugf, state, indent)
+    FILE *debugf;
+    nfa_state_T *state;
+    garray_T *indent;
+{
+    char_u  *p;
 
     if (state == NULL)
 	return;
 
     fprintf(debugf, "(%2d)", abs(state->id));
-    for (i = 0; i < ident; i++)
-	fprintf(debugf, "%c", ' ');
+
+    /* Output indent */
+    p = (char_u *)indent->ga_data;
+    if (indent->ga_len >= 3)
+    {
+	int	last = indent->ga_len - 3;
+	char_u	save[2];
+
+	STRNCPY(save, &p[last], 2);
+	STRNCPY(&p[last], "+-", 2);
+	fprintf(debugf, " %s", p);
+	STRNCPY(&p[last], save, 2);
+    }
+    else
+	fprintf(debugf, " %s", p);
 
     nfa_set_code(state->c);
-    fprintf(debugf, "%s %s (%d) (id=%d)\n",
-		 state->negated ? "NOT" : "", code, state->c, abs(state->id));
+    fprintf(debugf, "%s%s (%d) (id=%d)\n",
+		 state->negated ? "NOT " : "", code, state->c, abs(state->id));
     if (state->id < 0)
 	return;
 
     state->id = abs(state->id) * -1;
-    nfa_print_state(debugf, state->out, ident + 4);
-    nfa_print_state(debugf, state->out1, ident + 4);
+
+    /* grow indent for state->out */
+    indent->ga_len -= 1;
+    if (state->out1)
+	ga_concat(indent, "| ");
+    else
+	ga_concat(indent, "  ");
+    ga_append(indent, '\0');
+
+    nfa_print_state2(debugf, state->out, indent);
+
+    /* replace last part of indent for state->out1 */
+    indent->ga_len -= 3;
+    ga_concat(indent, "  ");
+    ga_append(indent, '\0');
+
+    nfa_print_state2(debugf, state->out1, indent);
+
+    /* shrink indent */
+    indent->ga_len -= 3;
+    ga_append(indent, '\0');
 }
 
 /*
@@ -1876,10 +1923,7 @@
     FILE *debugf = fopen("LOG.log", "a");
 
     if (debugf != NULL)
-    {
-	nfa_print_state(debugf, prog->start, 0);
-	fclose(debugf);
-    }
+	nfa_print_state(debugf, prog->start);
 }
 #endif	    /* ENABLE_LOG */
 #endif	    /* DEBUG */
@@ -3519,7 +3563,7 @@
 #endif
 	fprintf(f, "\tInput text is \"%s\" \n", reginput);
 	fprintf(f, "		=======================================================\n\n\n\n\n\n\n");
-	nfa_print_state(f, start, 0);
+	nfa_print_state(f, start);
 	fprintf(f, "\n\n");
 	fclose(f);
     }

Raspunde prin e-mail lui