bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RFC: help me with Java enums please


From: Akim Demaille
Subject: Re: RFC: help me with Java enums please
Date: Sat, 4 Apr 2020 19:19:30 +0200


> Le 4 avr. 2020 à 09:12, Akim Demaille <address@hidden> a écrit :
> [...]
> This generates the following enum (don't pay too much attention to its name, 
> it's likely to change from SymbolType to symbolKind):

(I meant SymbolKind here, not symbolKind).

> So I would definitely need some feedback.  Is this the right move for Java?  
> Or should we stick to some plain integral type (on which we would have no 
> type checking).

Paolo Bonzini, the original author of lalr1.java, and a friend of mine, David 
Gageot, validated this change (see https://github.com/akimd/bison/pull/34).

So I installed this patch, plus the following one on top.

commit f6dcecb287970ff7618b8be866dd6324c8d43563
Author: Akim Demaille <address@hidden>
Date:   Sat Apr 4 17:11:02 2020 +0200

    java: fixes in SymbolType
    
    Reported by Paolo Bonzini.
    https://github.com/akimd/bison/pull/34#issuecomment-609029634
    
    * data/skeletons/java.m4 (SymbolType): Use 'final' where possible.
    (get): Rewrite on top of an array instead of a switch.

diff --git a/data/skeletons/java.m4 b/data/skeletons/java.m4
index d5e021f3..d7d7df8a 100644
--- a/data/skeletons/java.m4
+++ b/data/skeletons/java.m4
@@ -163,10 +163,6 @@ m4_define([b4_symbol_enum],
                     b4_symbol([$1], [number]))])])
 
 
-m4_define([b4_case_code_symbol],
-[[        case $1: return SymbolType.]b4_symbol([$1], [sid]);
-])
-
 # b4_declare_symbol_enum
 # ----------------------
 # The definition of the symbol internal numbers as an enum.
@@ -176,22 +172,25 @@ m4_define([b4_declare_symbol_enum],
     ]m4_join([,
     ],
              ]b4_symbol_sid([-2])[(-2),
-             b4_symbol_map([b4_symbol_enum]),
-             [YYNTOKENS(]b4_tokens_number[); ///< Number of tokens.])[
+             b4_symbol_map([b4_symbol_enum]))[;
 
-    private int code;
+    private final int code_;
 
     SymbolType (int n) {
-      this.code = n;
+      this.code_ = n;
     }
-    static SymbolType get (int code) {
-      switch (code) {
-        default: return YYSYMBOL_YYUNDEF;
-]b4_symbol_foreach([b4_case_code_symbol])[
-      }
+
+    private static final SymbolType[] values_ = {
+        ]m4_map_args_sep([b4_symbol_sid(], [)], [,
+        ], b4_symbol_numbers)[
+    };
+
+    static final SymbolType get (int code) {
+      return values_[code];
     }
-    int getCode () {
-      return this.code;
+
+    public final int getCode () {
+      return this.code_;
     }
   };
 ]])])





reply via email to

[Prev in Thread] Current Thread [Next in Thread]