robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1 2#include <ctype.h> 3#include <limits.h> 4#include <stdio.h> 5 6 7#include "lptypes.h" 8#include "lpprint.h" 9#include "lpcode.h" 10 11 12#if defined(LPEG_DEBUG ) 13 14/* 15** {====================================================== 16** Printing patterns (for debugging) 17** ======================================================= 18*/ 19 20 21void printcharset (const byte * st ) { 22int i ; 23printf ("[" ); 24for (i = 0 ;i <=UCHAR_MAX ;i ++ ) { 25int first = i ; 26while (i <=UCHAR_MAX && testchar (st ,i ))i ++ ; 27if (i - 1 == first )/* unary range? */ 28printf ("(%02x)" ,first ); 29else if (i - 1 > first )/* non-empty range? */ 30printf ("(%02x-%02x)" ,first ,i - 1 ); 31 } 32printf ("]" ); 33} 34 35 36static void printIcharset (const Instruction * inst ,const byte * buff ) { 37byte cs [CHARSETSIZE ]; 38int i ; 39printf ("(%02x-%d) " ,inst -> i .aux2 .set .offset ,inst -> i .aux2 .set .size ); 40clearset (cs ); 41for (i = 0 ;i < CHARSETSIZE * 8 ;i ++ ) { 42if (charinset (inst ,buff ,i )) 43setchar (cs ,i ); 44 } 45printcharset (cs ); 46} 47 48 49static void printTcharset (TTree * tree ) { 50byte cs [CHARSETSIZE ]; 51int i ; 52printf ("(%02x-%d) " ,tree -> u .set .offset ,tree -> u .set .size ); 53fillset (cs ,tree -> u .set .deflt ); 54for (i = 0 ;i < tree -> u .set .size ;i ++ ) 55cs [tree -> u .set .offset + i ]= treebuffer (tree )[i ]; 56printcharset (cs ); 57} 58 59 60static const char * capkind (int kind ) { 61const char * const modes []= { 62"close" ,"position" ,"constant" ,"backref" , 63"argument" ,"simple" ,"table" ,"function" ,"accumulator" , 64"query" ,"string" ,"num" ,"substitution" ,"fold" , 65"runtime" ,"group" }; 66return modes [kind ]; 67} 68 69 70static void printjmp (const Instruction * op ,const Instruction * p ) { 71printf ("-> %d" , (int )(p + (p + 1 )-> offset - op )); 72} 73 74 75void printinst (const Instruction * op ,const Instruction * p ) { 76const char * const names []= { 77"any" ,"char" ,"set" , 78"testany" ,"testchar" ,"testset" , 79"span" ,"utf-range" ,"behind" , 80"ret" ,"end" , 81"choice" ,"jmp" ,"call" ,"open_call" , 82"commit" ,"partial_commit" ,"back_commit" ,"failtwice" ,"fail" ,"giveup" , 83"fullcapture" ,"opencapture" ,"closecapture" ,"closeruntime" , 84"--" 85 }; 86printf ("%02ld: %s " , (long )(p - op ),names [p -> i .code ]); 87switch ((Opcode )p -> i .code ) { 88case IChar : { 89printf ("'%c' (%02x)" ,p -> i .aux1 ,p -> i .aux1 ); 90break ; 91 } 92case ITestChar : { 93printf ("'%c' (%02x)" ,p -> i .aux1 ,p -> i .aux1 );printjmp (op ,p ); 94break ; 95 } 96case IUTFR : { 97printf ("%d - %d" ,p [1 ].offset ,utf_to (p )); 98break ; 99 } 100case IFullCapture : { 101printf ("%s (size = %d) (idx = %d)" , 102capkind (getkind (p )),getoff (p ),p -> i .aux2 .key ); 103break ; 104 } 105case IOpenCapture : { 106printf ("%s (idx = %d)" ,capkind (getkind (p )),p -> i .aux2 .key ); 107break ; 108 } 109case ISet : { 110printIcharset (p , (p + 1 )-> buff ); 111break ; 112 } 113case ITestSet : { 114printIcharset (p , (p + 2 )-> buff );printjmp (op ,p ); 115break ; 116 } 117case ISpan : { 118printIcharset (p , (p + 1 )-> buff ); 119break ; 120 } 121case IOpenCall : { 122printf ("-> %d" , (p + 1 )-> offset ); 123break ; 124 } 125case IBehind : { 126printf ("%d" ,p -> i .aux1 ); 127break ; 128 } 129case IJmp :case ICall :case ICommit :case IChoice : 130case IPartialCommit :case IBackCommit :case ITestAny : { 131printjmp (op ,p ); 132break ; 133 } 134default :break ; 135 } 136printf ("\n" ); 137} 138 139 140void printpatt (Instruction * p ) { 141Instruction * op = p ; 142uint n = op [-1 ].codesize - 1 ; 143while (p < op + n ) { 144printinst (op ,p ); 145p += sizei (p ); 146 } 147} 148 149 150static void printcap (Capture * cap ,int ident ) { 151while (ident -- )printf (" " ); 152printf ("%s (idx: %d - size: %d) -> %lu (%p)\n" , 153capkind (cap -> kind ),cap -> idx ,cap -> siz , (long )cap -> index , (void * )cap ); 154} 155 156 157/* 158** Print a capture and its nested captures 159*/ 160static Capture * printcap2close (Capture * cap ,int ident ) { 161Capture * head = cap ++ ; 162printcap (head ,ident );/* print head capture */ 163while (capinside (head ,cap )) 164cap = printcap2close (cap ,ident + 2 );/* print nested captures */ 165if (isopencap (head )) { 166assert (isclosecap (cap )); 167printcap (cap ++ ,ident );/* print and skip close capture */ 168 } 169return cap ; 170} 171 172 173void printcaplist (Capture * cap ) { 174 {/* for debugging, print first a raw list of captures */ 175Capture * c = cap ; 176while (c -> index != MAXINDT ) {printcap (c ,0 );c ++ ; } 177 } 178printf (">======\n" ); 179while (!isclosecap (cap )) 180cap = printcap2close (cap ,0 ); 181printf ("=======\n" ); 182} 183 184/* }====================================================== */ 185 186 187/* 188** {====================================================== 189** Printing trees (for debugging) 190** ======================================================= 191*/ 192 193static const char * tagnames []= { 194"char" ,"set" ,"any" , 195"true" ,"false" ,"utf8.range" , 196"rep" , 197"seq" ,"choice" , 198"not" ,"and" , 199"call" ,"opencall" ,"rule" ,"xinfo" ,"grammar" , 200"behind" , 201"capture" ,"run-time" 202}; 203 204 205void printtree (TTree * tree ,int ident ) { 206int i ; 207int sibs = numsiblings [tree -> tag ]; 208for (i = 0 ;i < ident ;i ++ )printf (" " ); 209printf ("%s" ,tagnames [tree -> tag ]); 210switch (tree -> tag ) { 211case TChar : { 212int c = tree -> u .n ; 213if (isprint (c )) 214printf (" '%c'\n" ,c ); 215else 216printf (" (%02X)\n" ,c ); 217break ; 218 } 219case TSet : { 220printTcharset (tree ); 221printf ("\n" ); 222break ; 223 } 224case TUTFR : { 225assert (sib1 (tree )-> tag == TXInfo ); 226printf (" %d (%02x %d) - %d (%02x %d) \n" , 227tree -> u .n ,tree -> key ,tree -> cap , 228sib1 (tree )-> u .n ,sib1 (tree )-> key ,sib1 (tree )-> cap ); 229break ; 230 } 231case TOpenCall :case TCall : { 232assert (sib1 (sib2 (tree ))-> tag == TXInfo ); 233printf (" key: %d (rule: %d)\n" ,tree -> key ,sib1 (sib2 (tree ))-> u .n ); 234break ; 235 } 236case TBehind : { 237printf (" %d\n" ,tree -> u .n ); 238break ; 239 } 240case TCapture : { 241printf (" kind: '%s' key: %d\n" ,capkind (tree -> cap ),tree -> key ); 242break ; 243 } 244case TRule : { 245printf (" key: %d\n" ,tree -> key ); 246sibs = 1 ;/* do not print 'sib2' (next rule) as a sibling */ 247break ; 248 } 249case TXInfo : { 250printf (" n: %d\n" ,tree -> u .n ); 251break ; 252 } 253case TGrammar : { 254TTree * rule = sib1 (tree ); 255printf (" %d\n" ,tree -> u .n );/* number of rules */ 256for (i = 0 ;i < tree -> u .n ;i ++ ) { 257printtree (rule ,ident + 2 ); 258rule = sib2 (rule ); 259 } 260assert (rule -> tag == TTrue );/* sentinel */ 261sibs = 0 ;/* siblings already handled */ 262break ; 263 } 264default : 265printf ("\n" ); 266break ; 267 } 268if (sibs >=1 ) { 269printtree (sib1 (tree ),ident + 2 ); 270if (sibs >=2 ) 271printtree (sib2 (tree ),ident + 2 ); 272 } 273} 274 275 276void printktable (lua_State * L ,int idx ) { 277int n ,i ; 278lua_getuservalue (L ,idx ); 279if (lua_isnil (L ,-1 ))/* no ktable? */ 280return ; 281n = lua_rawlen (L ,-1 ); 282printf ("[" ); 283for (i = 1 ;i <=n ;i ++ ) { 284printf ("%d = " ,i ); 285lua_rawgeti (L ,-1 ,i ); 286if (lua_isstring (L ,-1 )) 287printf ("%s " ,lua_tostring (L ,-1 )); 288else 289printf ("%s " ,lua_typename (L ,lua_type (L ,-1 ))); 290lua_pop (L ,1 ); 291 } 292printf ("]\n" ); 293/* leave ktable at the stack */ 294} 295 296/* }====================================================== */ 297 298#endif