Hi all

I have been using lemon successfully to generate a parser used in a
library, and I'd like some source-level way of limiting the visibility
of some lempar symbols.

I threw together a proof of concept for a macro that can be user defined
to allow setting `static` or e.g.
`__attribute___((visibility("hidden")))` for the `Parse.*` functions. By
default it is empty which maintains the existing behaviour.

My proof of concept patch is included below using the macro name
`YYAPIFUNC`.

I feel this is of general utility, and if you find it useful, please
feel free to adopt as you see fit under the usual sqlite terms.

All the Best

Luke

---
Index: tool/lempar.c
==================================================================
--- tool/lempar.c
+++ tool/lempar.c
@@ -231,10 +231,18 @@
 #include <stdio.h>
 static FILE *yyTraceFILE = 0;
 static char *yyTracePrompt = 0;
 #endif /* NDEBUG */
 
+/*
+** Any required function qualifiers for public parts of the parser should be
+** defined ** here, e.g. static, declspec, etc.
+*/
+#ifndef YYAPIFUNC
+#define YYAPIFUNC /* default is empty */
+#endif
+
 #ifndef NDEBUG
 /* 
 ** Turn parser tracing on by giving a stream to which to write the trace
 ** and a prompt to preface each trace message.  Tracing is turned off
 ** by making either argument NULL 
@@ -249,11 +257,11 @@
 ** </ul>
 **
 ** Outputs:
 ** None.
 */
-void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
+YYAPIFUNC void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
   yyTraceFILE = TraceFILE;
   yyTracePrompt = zTracePrompt;
   if( yyTraceFILE==0 ) yyTracePrompt = 0;
   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
 }
@@ -318,11 +326,11 @@
 # define YYMALLOCARGTYPE size_t
 #endif
 
 /* Initialize a new parser that has already been allocated.
 */
-void ParseInit(void *yypRawParser ParseCTX_PDECL){
+YYAPIFUNC void ParseInit(void *yypRawParser ParseCTX_PDECL){
   yyParser *yypParser = (yyParser*)yypRawParser;
   ParseCTX_STORE
 #ifdef YYTRACKMAXSTACKDEPTH
   yypParser->yyhwm = 0;
 #endif
@@ -357,11 +365,11 @@
 **
 ** Outputs:
 ** A pointer to a parser.  This pointer is used in subsequent calls
 ** to Parse and ParseFree.
 */
-void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){
+YYAPIFUNC void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) 
ParseCTX_PDECL){
   yyParser *yypParser;
   yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
   if( yypParser ){
     ParseCTX_STORE
     ParseInit(yypParser ParseCTX_PARAM);
@@ -425,11 +433,11 @@
 }
 
 /*
 ** Clear all secondary memory allocations from the parser
 */
-void ParseFinalize(void *p){
+YYAPIFUNC void ParseFinalize(void *p){
   yyParser *pParser = (yyParser*)p;
   while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
 #if YYSTACKDEPTH<=0
   if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
 #endif
@@ -442,11 +450,11 @@
 **
 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
 ** is defined in a %include section of the input grammar) then it is
 ** assumed that the input pointer is never NULL.
 */
-void ParseFree(
+YYAPIFUNC void ParseFree(
   void *p,                    /* The parser to be deleted */
   void (*freeProc)(void*)     /* Function used to reclaim memory */
 ){
 #ifndef YYPARSEFREENEVERNULL
   if( p==0 ) return;
@@ -458,11 +466,11 @@
 
 /*
 ** Return the peak depth of the stack for a parser.
 */
 #ifdef YYTRACKMAXSTACKDEPTH
-int ParseStackPeak(void *p){
+YYAPIFUNC int ParseStackPeak(void *p){
   yyParser *pParser = (yyParser*)p;
   return pParser->yyhwm;
 }
 #endif
 
@@ -482,11 +490,11 @@
 **   (2)  is not a syntax error.
 **
 ** Return the number of missed state/lookahead combinations.
 */
 #if defined(YYCOVERAGE)
-int ParseCoverage(FILE *out){
+YYAPIFUNC int ParseCoverage(FILE *out){
   int stateno, iLookAhead, i;
   int nMissed = 0;
   for(stateno=0; stateno<YYNSTATE; stateno++){
     i = yy_shift_ofst[stateno];
     for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
@@ -889,11 +897,11 @@
 ** </ul>
 **
 ** Outputs:
 ** None.
 */
-void Parse(
+YYAPIFUNC void Parse(
   void *yyp,                   /* The parser */
   int yymajor,                 /* The major token code number */
   ParseTOKENTYPE yyminor       /* The value for the token */
   ParseARG_PDECL               /* Optional %extra_argument parameter */
 ){
@@ -1063,14 +1071,14 @@
 
 /*
 ** Return the fallback token corresponding to canonical token iToken, or
 ** 0 if iToken has no fallback.
 */
-int ParseFallback(int iToken){
+YYAPIFUNC int ParseFallback(int iToken){
 #ifdef YYFALLBACK
   assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
   return yyFallback[iToken];
 #else
   (void)iToken;
   return 0;
 #endif
 }

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to