From: drh Date: Sat, 11 Jan 2014 03:27:37 +0000 (+0000) Subject: Add the "%token_class" directive to the LEMON parser generator. This opens up X-Git-Tag: version-3.8.3~53 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ecaa9d399c15e49ed7f75293ae0e1391a7d24a8a;p=thirdparty%2Fsqlite.git Add the "%token_class" directive to the LEMON parser generator. This opens up the possibility of simplifying the parser. Also remove all calls to sprintf(), strcpy(), and strcat() from LEMON to avoid compiler warnings on OpenBSD. (Aside: It is this change to avoid harmless compiler warnings that was the cause of the reason spat of bugs.) FossilOrigin-Name: 8eb48c04bd0a14031488b3160fde67307eb8b35d --- ecaa9d399c15e49ed7f75293ae0e1391a7d24a8a diff --cc manifest index 78da6bddd4,b56fe09692..7c63bcd427 --- a/manifest +++ b/manifest @@@ -1,5 -1,5 +1,5 @@@ - C Remove\sunused\sstructure\sdefinition\sfrom\sparse.y. - D 2014-01-10T20:51:16.498 -C In\sLEMON,\sfix\sa\sbug\sin\sthe\stext\sformatter\sintroduced\sby\sthe\sprevious\ncommit.\s\sAlso\sadd\sthe\snew\s"%token_class"\sdirective\sfor\sdefining\ssymbolic\nnames\sthat\sstand\sany\sone\sof\sa\scollection\sof\stokens. -D 2014-01-11T03:06:18.172 ++C Add\sthe\s"%token_class"\sdirective\sto\sthe\sLEMON\sparser\sgenerator.\s\sThis\sopens\sup\nthe\spossibility\sof\ssimplifying\sthe\sparser.\s\sAlso\sremove\sall\scalls\sto\s\nsprintf(),\sstrcpy(),\sand\sstrcat()\sfrom\sLEMON\sto\savoid\scompiler\swarnings\non\sOpenBSD.\s\s(Aside:\s\sIt\sis\sthis\schange\sto\savoid\sharmless\scompiler\swarnings\nthat\swas\sthe\scause\sof\sthe\sreason\sspat\sof\sbugs.) ++D 2014-01-11T03:27:37.624 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@@ -1109,7 -1109,7 +1109,7 @@@ F tool/fragck.tcl 5265a95126abcf6ab357f F tool/genfkey.README cf68fddd4643bbe3ff8e31b8b6d8b0a1b85e20f4 F tool/genfkey.test 4196a8928b78f51d54ef58e99e99401ab2f0a7e5 F tool/getlock.c f4c39b651370156cae979501a7b156bdba50e7ce - F tool/lemon.c 796930d5fc2036c7636f3f1ee12f9ae03719a2eb -F tool/lemon.c 4a3d4a579c5dff6a42785e97d1f2b59789f3b8dd ++F tool/lemon.c 624b24c5dc048e09979f88a03e148bc728c70b73 F tool/lempar.c 01ca97f87610d1dac6d8cd96ab109ab1130e76dc F tool/logest.c 7ad625cac3d54012b27d468b7af6612f78b9ba75 F tool/mkautoconfamal.sh f8d8dbf7d62f409ebed5134998bf5b51d7266383 @@@ -1148,7 -1148,7 +1148,7 @@@ F tool/vdbe-compress.tcl 0cf56e9263a152 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff - P c9ea7d199f06a7801ab639e7ac98ebeb98706f24 - R b8682b1b95c61647eceb7cac2c7aa836 -P e43c522dde01e134f1adc94f534d2b3eda74afc2 -R 498b2eca2a4d9bdf3c22bba6f4386191 ++P 7f1e7ae313c7625ef2623d78883dce776eecca30 da7890ca6b1d8e511377a469047120220e8c3b2d ++R 4ba60b3fb47af12fdb784f3275ccdc2b U drh - Z 2b3078d680e621e51a9cb7dd08f4a256 -Z 303f39885598d1865b7eb401105ea9ea ++Z 11bef21bd681893c7b5fd9fec7621236 diff --cc manifest.uuid index 2faf2ec7b2,2d1234c95c..f08dc2b5ea --- a/manifest.uuid +++ b/manifest.uuid @@@ -1,1 -1,1 +1,1 @@@ - 7f1e7ae313c7625ef2623d78883dce776eecca30 -da7890ca6b1d8e511377a469047120220e8c3b2d ++8eb48c04bd0a14031488b3160fde67307eb8b35d diff --cc tool/lemon.c index 58f13880f0,25787abf8d..c4adc20864 --- a/tool/lemon.c +++ b/tool/lemon.c @@@ -50,6 -50,107 +50,107 @@@ static char *msort(char*,char**,int(*)( */ #define lemonStrlen(X) ((int)strlen(X)) + /* + ** Compilers are starting to complain about the use of sprintf() and strcpy(), + ** saying they are unsafe. So we define our own versions of those routines too. + ** + ** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and + ** lemon_addtext(). The first two are replacements for sprintf() and vsprintf(). + ** The third is a helper routine for vsnprintf() that adds texts to the end of a + ** buffer, making sure the buffer is always zero-terminated. + ** + ** The string formatter is a minimal subset of stdlib sprintf() supporting only + ** a few simply conversions: + ** + ** %d + ** %s + ** %.*s + ** + */ + static void lemon_addtext( + char *zBuf, /* The buffer to which text is added */ + int *pnUsed, /* Slots of the buffer used so far */ + const char *zIn, /* Text to add */ + int nIn, /* Bytes of text to add. -1 to use strlen() */ + int iWidth /* Field width. Negative to left justify */ + ){ + if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){} - while( iWidth>nIn ){ zBuf[*(pnUsed++)] = ' '; iWidth--; } ++ while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; } + if( nIn==0 ) return; + memcpy(&zBuf[*pnUsed], zIn, nIn); + *pnUsed += nIn; - while( (-iWidth)>nIn ){ zBuf[*(pnUsed++)] = ' '; iWidth++; } ++ while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; } + zBuf[*pnUsed] = 0; + } + static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){ + int i, j, k, c, size; + int nUsed = 0; + const char *z; + char zTemp[50]; + str[0] = 0; + for(i=j=0; (c = zFormat[i])!=0; i++){ + if( c=='%' ){ + int iWidth = 0; + lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); + c = zFormat[++i]; + if( isdigit(c) || (c=='-' && isdigit(zFormat[i+1])) ){ + if( c=='-' ) i++; + while( isdigit(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0'; + if( c=='-' ) iWidth = -iWidth; + c = zFormat[i]; + } + if( c=='d' ){ + int v = va_arg(ap, int); + if( v<0 ){ + lemon_addtext(str, &nUsed, "-", 1, iWidth); + v = -v; + }else if( v==0 ){ + lemon_addtext(str, &nUsed, "0", 1, iWidth); + } + k = 0; + while( v>0 ){ + k++; + zTemp[sizeof(zTemp)-k] = (v%10) + '0'; + v /= 10; + } + lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth); + }else if( c=='s' ){ + z = va_arg(ap, const char*); + lemon_addtext(str, &nUsed, z, -1, iWidth); + }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){ + i += 2; + k = va_arg(ap, int); + z = va_arg(ap, const char*); + lemon_addtext(str, &nUsed, z, k, iWidth); + }else if( c=='%' ){ + lemon_addtext(str, &nUsed, "%", 1, 0); + }else{ + fprintf(stderr, "illegal format\n"); + exit(1); + } + j = i+1; + } + } + lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); + return nUsed; + } + static int lemon_sprintf(char *str, const char *format, ...){ + va_list ap; + int rc; + va_start(ap, format); + rc = lemon_vsprintf(str, format, ap); + va_end(ap); + return rc; + } + static void lemon_strcpy(char *dest, const char *src){ + while( (*(dest++) = *(src++))!=0 ){} + } + static void lemon_strcat(char *dest, const char *src){ + while( *dest ) dest++; + lemon_strcpy(dest, src); + } + + /* a few forward declarations... */ struct rule; struct lemon;