]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Use Bison's api.token.raw feature.
authorNick Bowler <nbowler@draconx.ca>
Sat, 8 Jul 2023 14:04:31 +0000 (10:04 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sat, 8 Jul 2023 14:04:31 +0000 (10:04 -0400)
By default, Bison numbers user-defined tokens starting from 257.
If the scanner never returns plain characters (which is the case
for libcdecl), this is inefficient.

Recent versions of Bison provide the api.token.raw option to improve
things.  Let's turn it on, if configure detects that it is supported.

This requires a very minor tweak to spec_string in order for the
encoding of tokens into single-byte values to work regardless of
which mode Bison is run in.

configure.ac
src/gen-specstr.awk

index f1bd702e331e1c9c2246fa94a10968a788b30b45..b9633244cd13b551b049ae9e3ed4f2d3f80cb96e 100644 (file)
@@ -98,6 +98,26 @@ AS_IF([test x"$dx_cv_bison_warn_no_deprecated" = x"yes"],
   [AS_VAR_APPEND([BISON_COMPAT], ["${BISON_COMPAT:+ }-Wno-deprecated"])])
 AC_SUBST([BISON_COMPAT])
 
+AS_IF([test x"$dx_cv_bison_define_cmdline" != x"yes"],
+  [dx_cv_bison_api_token_raw=no])
+AC_CACHE_CHECK([whether $BISON supports api.token.raw],
+  [dx_cv_bison_api_token_raw], [dx_cv_bison_api_token_raw=no
+cat >conftest.y <<'EOF'
+%token T_TEST
+%{
+int yylex(void) { return 0; }
+void yyerror(const char *msg) { }
+%}
+%%
+input:
+EOF
+AS_IF([$BISON --define api.token.raw conftest.y >&AS_MESSAGE_LOG_FD 2>&1],
+[AC_COMPUTE_INT([testval], [T_TEST], [#include "conftest.tab.c"
+])
+AS_IF([test "$testval" -lt 256], [dx_cv_bison_api_token_raw=yes])])])
+AS_IF([test x"$dx_cv_bison_api_token_raw" = x"yes"],
+  [AS_VAR_APPEND([BISON_COMPAT], [" --define api.token.raw"])])
+
 AH_TOP([#include <conf_pre.h>])
 AH_BOTTOM([#include <conf_post.h>])
 
index c07856ec93518c2d0a6a99d0dbab64414754ddca..68316676714f371a1ec6cba97e9c31a349dc9ea8 100755 (executable)
@@ -79,7 +79,7 @@ END {
     if (specs[i] == "IDENT")
       s = "0";
     else
-      s = "T_" substr(specs[i] "                ", 1, maxwidth) " - 256";
+      s = "T_" substr(specs[i] "                ", 1, maxwidth) " & 0xff";
     offset_table = offset_table s suffix;
   }
 
@@ -93,6 +93,8 @@ END {
   print "\tassert(x < sizeof idx);";
   print "\tif (!(x = idx[x]))";
   print "\t\treturn \"\";";
-  print "\treturn cdecl__token_name(x + 256);";
+  print "\tif (T_" specs[0] " >= 256)";
+  print "\t\tx += 256;";
+  print "\treturn cdecl__token_name(x);";
   print "}";
 }