RPA Toolkit
added rule tree dump
authorMartin Stoilov <martin@rpasearch.com>
Tue, 22 Mar 2011 06:17:53 +0000 (23:17 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Tue, 22 Mar 2011 06:17:53 +0000 (23:17 -0700)
arch/linux/x86_64/rtypes.h
rpa2/rpaparseinfo.c
rpa2/rpaparseinfo.h
rpa2/rparecord.c
rpa2/rparecord.h
testrpa2/rpaparser-test.c

index fc289a2..214a056 100644 (file)
@@ -67,6 +67,8 @@ typedef struct {ruint32 p1; ruint32 p2;} rpair_t;
 #define R_DEBUG_BRAKE __asm__ ("int $3")
 #define ASSERT(__a__) do {if (!(__a__)) R_DEBUG_BRAKE; } while (0)
 #define R_SIZE_ALIGN(s, n) ((((s) + (n) - 1) / (n)) * (n))
+#define R_MIN(a, b) ((a) < (b) ? (a): (b))
+#define R_MAX(a, b) ((a) > (b) ? (a): (b))
 
 #ifndef NULL
 #ifdef __cplusplus
index 3c9f753..3269828 100644 (file)
@@ -107,7 +107,7 @@ static void rpa_parseinfo_buildruleinfo(rpa_parseinfo_t *pi)
                                continue;
                        namerec = (rparecord_t *)r_array_slot(pi->records, nrec);
                        if ((namerec->userid == RPA_PRODUCTION_RULENAME) && (namerec->type & RPA_RECORD_END)) {
-                               r_harray_add(rules, namerec->input, namerec->inputsiz, &info);
+                               r_harray_replace(rules, namerec->input, namerec->inputsiz, &info);
                                i += info.sizerecs - 1;
                        }
                }
@@ -208,3 +208,31 @@ void rpa_parseinfo_dump_records(rpa_parseinfo_t *pi)
                rpa_record_dump(pi->records, i);
        }
 }
+
+
+static void rpa_parseinfo_dumptree(rpa_parseinfo_t *pi, rlong rec, rint level)
+{
+       rparecord_t *prec = (rparecord_t *)r_array_slot(pi->records, rec);
+       if (prec && prec->userid == RPA_PRODUCTION_RULENAME)
+               return;
+       rpa_record_dumpindented(pi->records, rpa_recordtree_get(pi->records, rec, RPA_RECORD_END), level);
+       prec = (rparecord_t *)r_array_slot(pi->records, rec);
+       if (prec && (prec->userid == RPA_PRODUCTION_AREF || prec->userid == RPA_PRODUCTION_CREF))
+               return;
+       if (prec && (prec->userid == RPA_PRODUCTION_CHARRNG || prec->userid == RPA_PRODUCTION_NUMRNG))
+               return;
+       for (rec = rpa_recordtree_firstchild(pi->records, rec, RPA_RECORD_START); rec >= 0; rec = rpa_recordtree_next(pi->records, rec, RPA_RECORD_START)) {
+               rpa_parseinfo_dumptree(pi, rec, level + 1);
+       }
+}
+
+
+rint rpa_parseinfo_dump_ruletree(rpa_parseinfo_t *pi, const char *rulename)
+{
+       rpa_ruleinfo_t *info = (rpa_ruleinfo_t *)r_harray_get(pi->rules, r_harray_lookup_s(pi->rules, rulename));
+
+       if (!info)
+               return -1;
+       rpa_parseinfo_dumptree(pi, info->startrec, 0);
+       return 0;
+}
index ecf8002..5987b05 100644 (file)
@@ -31,6 +31,7 @@ rpa_parseinfo_t *rpa_parseinfo_create(rpastat_t *stat);
 void rpa_parseinfo_destroy(rpa_parseinfo_t *pi);
 void rpa_parseinfo_dump(rpa_parseinfo_t *pi);
 void rpa_parseinfo_dump_records(rpa_parseinfo_t *pi);
+rint rpa_parseinfo_dump_ruletree(rpa_parseinfo_t *pi, const char *rulename);
 
 #ifdef __cplusplus
 }
index 62befe8..a7faeda 100644 (file)
@@ -178,15 +178,54 @@ rlong rpa_record_getusertype(rarray_t *records, rlong rec)
 }
 
 
+static rint rpa_record_optchar(rparecord_t *prec, rint defc)
+{
+       rint optc = defc;
+
+       if ((prec->usertype & RPA_MATCH_MASK) == RPA_MATCH_OPTIONAL)
+               optc = '?';
+       else if ((prec->usertype & RPA_MATCH_MASK) == RPA_MATCH_MULTIPLE)
+               optc = '+';
+       else if ((prec->usertype & RPA_MATCH_MASK) == RPA_MATCH_MULTIOPT)
+               optc = '*';
+       else
+               optc = defc;
+       return optc;
+}
+
+
+static rint rpa_record_loopchar(rparecord_t *prec, rint defc)
+{
+       rint loopc = defc;
+
+       if ((prec->usertype & RPA_LOOP_PATH) && (prec->usertype & RPA_NONLOOP_PATH)) {
+               /*
+                * This is an error, should never happen
+                */
+               loopc = 'R';
+       } else if ((prec->usertype & RPA_LOOP_PATH)) {
+               loopc = 'L';
+       } else if ((prec->usertype & RPA_NONLOOP_PATH)) {
+               loopc = 'N';
+       } else {
+               loopc = defc;
+       }
+       return loopc;
+}
+
+
 void rpa_record_dump(rarray_t *records, rlong rec)
 {
-       rparecord_t *prec = (rparecord_t *)r_array_slot(records, rec);
+       rparecord_t *prec;
        rlong start, end, first, last, next, prev, parent;
        rchar buf[240];
        rint bufsize = sizeof(buf) - 1;
        rint n = 0, size;
        rchar optc = ' ';
 
+       if (rec < 0 || rec >= r_array_length(records))
+               return;
+       prec = (rparecord_t *)r_array_slot(records, rec);
        if (prec->type & RPA_RECORD_END) {
                if ((prec->usertype & RPA_MATCH_MASK) == RPA_MATCH_OPTIONAL)
                        optc = '?';
@@ -235,3 +274,31 @@ void rpa_record_dump(rarray_t *records, rlong rec)
 
        r_printf("%s\n", buf);
 }
+
+
+void rpa_record_dumpindented(rarray_t *records, rlong rec, rint level)
+{
+       rchar buffer[160];
+       rparecord_t *prec;
+       rint i, size;
+
+       if (rec < 0 || rec >= r_array_length(records))
+               return;
+       r_memset(buffer, 0, sizeof(buffer));
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       for (i = 0; i < level; i++)
+               r_printf("   ");
+       r_printf("(");
+       r_printf("%s, %c, %c", prec->rule, rpa_record_optchar(prec, 'x'), rpa_record_loopchar(prec, 'x'));
+       r_printf(")");
+       rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
+       prec = (rparecord_t *)r_array_slot(records, rec);
+       size = R_MIN(prec->inputsiz, sizeof(buffer) - 1);
+       r_strncpy(buffer, prec->input, size);
+
+       if (size == (sizeof(buffer) - 1))
+               r_printf(" %s ..\n", buffer);
+       else
+               r_printf(" %s\n", buffer);
+       return;
+}
index 375013c..c50ba60 100644 (file)
@@ -41,6 +41,7 @@ rlong rpa_recordtree_next(rarray_t *records, rlong rec, rulong type);
 rlong rpa_recordtree_prev(rarray_t *records, rlong rec, rulong type);
 rlong rpa_recordtree_parent(rarray_t *records, rlong rec, rulong type);
 
+void rpa_record_dumpindented(rarray_t *records, rlong rec, rint level);
 void rpa_record_dump(rarray_t *records, rlong rec);
 void rpa_record_setusertype(rarray_t *records, rlong rec, ruint32 usertype, rvalset_t op);
 rlong rpa_record_getusertype(rarray_t *records, rlong rec);
index d18b44e..fa4244b 100644 (file)
@@ -58,6 +58,17 @@ int main(int argc, char *argv[])
        }
 
 
+       for (i = 1; i < argc; i++) {
+               if (r_strcmp(argv[i], "-d") == 0) {
+                       if (++i < argc) {
+                               rpa_parseinfo_t *pi = rpa_parseinfo_create(pa->stat);
+                               rpa_parseinfo_dump_ruletree(pi, argv[i]);
+                               rpa_parseinfo_destroy(pi);
+                       }
+               }
+       }
+
+
        rpa_parser_destroy(pa);
        r_printf("Parsed Size = %d\n", ret);
        r_printf("Cache hit = %d\n", pa->stat->cache.hit);