SQLite Forum

lemon - optional namespace support
Login
Hi Richard - nice to read your response and willingness to discuss.

I'm not sure that I will immediately convince you (or indeed anyone) of the special problems associated with incorporating lemon into C++,
but I will kickoff with what I hope is less controversial:

- A new `-e` command line option to define the code extension.

By default this is `c`, but with this option can define something like `-ecxx` or even something like `-efoobar`.
Being able to specify a different extension (other than `.c`) provides a means of invoking a different compiler (eg, C++) or triggering a special-purpose make rule that does some additional work.

The total size of the patch needed and the future maintenance are both modest (IMO) and improve versatility in the same way that the `-d` option does (very useful if you need it, low overhead/maintenance if you do not).

I figured on `-e` for extension, but it could be any other available letter.

```
--- lemon.c.orig        2020-01-10 14:08:19.181711107 +0100
+++ lemon.c     2020-01-10 14:12:00.621402133 +0100
@@ -1559,6 +1560,23 @@
   lemon_strcpy(outputDir, z);
 }
 
+/* Remember the name of the code extension (automatically prefix '.')
+*/
+static char *code_ext = NULL;
+static void handle_e_option(char *z){
+  code_ext = (char *) malloc( lemonStrlen(z)+2 );
+  if( code_ext==0 ){
+    fprintf(stderr,"out of memory\n");
+    exit(1);
+  }
+  if(*z == '.'){
+    lemon_strcpy(code_ext, z);
+  } else {
+    code_ext[0] = '.';
+    lemon_strcpy(&(code_ext[1]), z);
+  }
+}
+
 static char *user_templatename = NULL;
 static void handle_T_option(char *z){
   user_templatename = (char *) malloc( lemonStrlen(z)+1 );
@@ -1647,6 +1665,7 @@
     {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
     {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory.  Default '.'"},
     {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
+    {OPT_FSTR, "e", (char*)&handle_e_option, "Output code extension.  Default 'c'"},
     {OPT_FSTR, "f", 0, "Ignored.  (Placeholder for -f compiler options.)"},
     {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
     {OPT_FSTR, "I", 0, "Ignored.  (Placeholder for '-I' compiler options.)"},
@@ -4187,7 +4272,8 @@
 
   in = tplt_open(lemp);
   if( in==0 ) return;
-  out = file_open(lemp,".c","wb");
+  if(code_ext) out = file_open(lemp,code_ext,"wb");
+  else out = file_open(lemp,".c","wb");
   if( out==0 ){
     fclose(in);
     return;
```

## Notes

The supplied extension can be with/without the dot. Can save a couple (2) of lines of code if we simply dictate that it is always without the dot.

Since we don't both with `free()` on any of the allocated option handlers, we could reduce even further, if that is desirable:

```
--- lemon.c.orig        2020-01-10 14:08:19.181711107 +0100
+++ lemon.c     2020-01-10 14:12:00.621402133 +0100
@@ -1559,6 +1560,23 @@
   lemon_strcpy(outputDir, z);
 }
 
+/* Remember the name of the code extension (automatically prefix '.')
+*/
+static char *code_ext = ".c";
+static void handle_e_option(char *z){
+  code_ext = (char *) malloc( lemonStrlen(z)+2 );
+  if( code_ext==0 ){
+    fprintf(stderr,"out of memory\n");
+    exit(1);
+  }
+  code_ext[0] = '.';
+  lemon_strcpy(&(code_ext[1]), z);
+}
+
 static char *user_templatename = NULL;
 static void handle_T_option(char *z){
   user_templatename = (char *) malloc( lemonStrlen(z)+1 );
@@ -1647,6 +1665,7 @@
     {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
     {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory.  Default '.'"},
     {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
+    {OPT_FSTR, "e", (char*)&handle_e_option, "Output code extension.  Default 'c'"},
     {OPT_FSTR, "f", 0, "Ignored.  (Placeholder for -f compiler options.)"},
     {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
     {OPT_FSTR, "I", 0, "Ignored.  (Placeholder for '-I' compiler options.)"},
@@ -4187,7 +4272,8 @@
 
   in = tplt_open(lemp);
   if( in==0 ) return;
-  out = file_open(lemp,".c","wb");
+  out = file_open(lemp,code_ext,"wb");
   if( out==0 ){
     fclose(in);
     return;
```