[前][次][番号順一覧][スレッド一覧]

ruby-changes:40377

From: nobu <ko1@a...>
Date: Thu, 5 Nov 2015 13:04:18 +0900 (JST)
Subject: [ruby-changes:40377] nobu:r52458 (trunk): id.def: token_ops

nobu	2015-11-05 13:04:00 +0900 (Thu, 05 Nov 2015)

  New Revision: 52458

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52458

  Log:
    id.def: token_ops
    
    * defs/id.def (token_ops): gather associations between IDs,
      operators, and parser tokens.

  Modified files:
    trunk/ChangeLog
    trunk/defs/id.def
    trunk/symbol.c
    trunk/template/id.c.tmpl
    trunk/template/id.h.tmpl
Index: symbol.c
===================================================================
--- symbol.c	(revision 52457)
+++ symbol.c	(revision 52458)
@@ -30,55 +30,6 @@ static ID register_static_symid_str(ID, https://github.com/ruby/ruby/blob/trunk/symbol.c#L30
 
 #define is_identchar(p,e,enc) (rb_enc_isalnum((unsigned char)(*(p)),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
 
-#define tUPLUS  RUBY_TOKEN(UPLUS)
-#define tUMINUS RUBY_TOKEN(UMINUS)
-#define tPOW    RUBY_TOKEN(POW)
-#define tCMP    RUBY_TOKEN(CMP)
-#define tEQ     RUBY_TOKEN(EQ)
-#define tEQQ    RUBY_TOKEN(EQQ)
-#define tNEQ    RUBY_TOKEN(NEQ)
-#define tGEQ    RUBY_TOKEN(GEQ)
-#define tLEQ    RUBY_TOKEN(LEQ)
-#define tMATCH  RUBY_TOKEN(MATCH)
-#define tNMATCH RUBY_TOKEN(NMATCH)
-#define tDOT2   RUBY_TOKEN(DOT2)
-#define tDOT3   RUBY_TOKEN(DOT3)
-#define tAREF   RUBY_TOKEN(AREF)
-#define tASET   RUBY_TOKEN(ASET)
-#define tLSHFT  RUBY_TOKEN(LSHFT)
-#define tRSHFT  RUBY_TOKEN(RSHFT)
-#define tCOLON2 RUBY_TOKEN(COLON2)
-#define tANDOP  RUBY_TOKEN(ANDOP)
-#define tOROP   RUBY_TOKEN(OROP)
-#define tDOTQ   RUBY_TOKEN(DOTQ)
-
-static const struct {
-    unsigned short token;
-    const char name[3], term;
-} op_tbl[] = {
-    {tDOT2,	".."},
-    {tDOT3,	"..."},
-    {tPOW,	"**"},
-    {tUPLUS,	"+@"},
-    {tUMINUS,	"-@"},
-    {tCMP,	"<=>"},
-    {tGEQ,	">="},
-    {tLEQ,	"<="},
-    {tEQ,	"=="},
-    {tEQQ,	"==="},
-    {tNEQ,	"!="},
-    {tMATCH,	"=~"},
-    {tNMATCH,	"!~"},
-    {tAREF,	"[]"},
-    {tASET,	"[]="},
-    {tLSHFT,	"<<"},
-    {tRSHFT,	">>"},
-    {tCOLON2,   "::"},
-    {tANDOP,    "&&"},
-    {tOROP,     "||"},
-    {tDOTQ,     ".?"},
-};
-
 #define op_tbl_count numberof(op_tbl)
 STATIC_ASSERT(op_tbl_name_size, sizeof(op_tbl[0].name) == 3);
 #define op_tbl_len(i) (!op_tbl[i].name[1] ? 1 : !op_tbl[i].name[2] ? 2 : 3)
Index: defs/id.def
===================================================================
--- defs/id.def	(revision 52457)
+++ defs/id.def	(revision 52458)
@@ -65,6 +65,42 @@ firstline, predefined = __LINE__+1, %[\ https://github.com/ruby/ruby/blob/trunk/defs/id.def#L65
   -                                                     debug#created_line
 ]
 
+# VM ID         OP      Parser Token
+token_ops = %[\
+  Dot2          ..      DOT2
+  Dot3          ...     DOT3
+  UPlus         +@      UPLUS
+  UMinus        -@      UMINUS
+  Pow           **      POW
+  DSTAR         **
+  Cmp           <=>     CMP
+  PLUS          +
+  MINUS         -
+  MULT          *
+  DIV           /
+  MOD           %
+  LTLT          <<      LSHFT
+  GTGT          >>      RSHFT
+  LT            <
+  LE            <=      LEQ
+  GT            >
+  GE            >=      GEQ
+  Eq            ==      EQ
+  Eqq           ===     EQQ
+  Neq           !=      NEQ
+  Not           !
+  Backquote     `
+  EqTilde       =~      MATCH
+  NeqTilde      !~      NMATCH
+  AREF          []
+  ASET          []=
+  COLON2        ::
+  COLON3        ::
+  ANDOP         &&
+  OROP          ||
+  DOTQ          .?
+]
+
 class KeywordError < RuntimeError
   def self.raise(mesg, line)
     super(self, mesg, ["#{__FILE__}:#{line}", *caller])
@@ -79,6 +115,7 @@ global_ids = [] https://github.com/ruby/ruby/blob/trunk/defs/id.def#L115
 const_ids = []
 class_ids = []
 attrset_ids = []
+token_op_ids = []
 names = {}
 predefined.split(/^/).each_with_index do |line, num|
   next if /^#/ =~ line
@@ -117,6 +154,14 @@ predefined.split(/^/).each_with_index do https://github.com/ruby/ruby/blob/trunk/defs/id.def#L154
   end << token
   predefined_ids[token] = name
 end
+token_ops.split(/^/).each do |line|
+  next if /^#/ =~ line
+  line.sub!(/\s+#.*/, '')
+  id, op, token = line.split
+  next unless id and op
+  token ||= (id unless /\A\W\z/ =~ op)
+  token_op_ids << [id, op, token]
+end
 {
   "LOCAL" => local_ids,
   "INSTANCE" => instance_ids,
@@ -126,4 +171,5 @@ end https://github.com/ruby/ruby/blob/trunk/defs/id.def#L171
   "ATTRSET" => attrset_ids,
   :preserved => preserved_ids,
   :predefined => predefined_ids,
+  :token_op => token_op_ids,
 }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52457)
+++ ChangeLog	(revision 52458)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Nov  5 13:03:58 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* defs/id.def (token_ops): gather associations between IDs,
+	  operators, and parser tokens.
+
 Thu Nov  5 10:17:17 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/socket/socket.c (make_addrinfo): use RARRAY_ASET for
Index: template/id.c.tmpl
===================================================================
--- template/id.c.tmpl	(revision 52457)
+++ template/id.c.tmpl	(revision 52458)
@@ -13,7 +13,23 @@ https://github.com/ruby/ruby/blob/trunk/template/id.c.tmpl#L13
 <%
 defs = File.join(File.dirname(File.dirname(erb.filename)), "defs/id.def")
 ids = eval(File.read(defs), binding, defs)
+ops = ids[:token_op].uniq {|id, op, token| token && op}
 %>
+% ops.each do |_id, _op, token|
+%   next unless token
+#define t<%=token%> RUBY_TOKEN(<%=token%>)
+% end
+
+static const struct {
+    unsigned short token;
+    const char name[3], term;
+} op_tbl[] = {
+% ops.each do |_id, op, token|
+%   next unless token
+    {t<%=token%>, "<%=op%>"},
+% end
+};
+
 static void
 Init_id(void)
 {
Index: template/id.h.tmpl
===================================================================
--- template/id.h.tmpl	(revision 52457)
+++ template/id.h.tmpl	(revision 52458)
@@ -15,12 +15,6 @@ require 'optparse' https://github.com/ruby/ruby/blob/trunk/template/id.h.tmpl#L15
 
 op_id_offset = 128
 
-token_op_ids = %w[
-  tDOT2 tDOT3 tUPLUS tUMINUS tPOW tDSTAR tCMP tLSHFT tRSHFT
-  tLEQ tGEQ tEQ tEQQ tNEQ tMATCH tNMATCH tAREF tASET
-  tCOLON2 tCOLON3 tANDOP tOROP tDOTQ
-]
-
 defs = File.join(File.dirname(File.dirname(erb.filename)), "defs/id.def")
 ids = eval(File.read(defs), binding, defs)
 types = ids.keys.grep(/^[A-Z]/)
@@ -57,43 +51,19 @@ enum ruby_id_types { https://github.com/ruby/ruby/blob/trunk/template/id.h.tmpl#L51
 #define symIFUNC ID2SYM(idIFUNC)
 #define symCFUNC ID2SYM(idCFUNC)
 
-% token_op_ids.each_with_index do |token, index|
-#define RUBY_TOKEN_<%=token[/\At(.+)\z/, 1]%> <%=op_id_offset + index%>
+% index = op_id_offset
+% ids[:token_op].each do |_id, _op, token|
+%   next unless token
+#define RUBY_TOKEN_<%=token%> <%=index%>
+%   index += 1
 % end
 #define RUBY_TOKEN(t) RUBY_TOKEN_##t
 
 enum ruby_method_ids {
-    idDot2 = RUBY_TOKEN(DOT2),
-    idDot3 = RUBY_TOKEN(DOT3),
-    idUPlus = RUBY_TOKEN(UPLUS),
-    idUMinus = RUBY_TOKEN(UMINUS),
-    idPow = RUBY_TOKEN(POW),
-    idCmp = RUBY_TOKEN(CMP),
-    idPLUS = '+',
-    idMINUS = '-',
-    idMULT = '*',
-    idDIV = '/',
-    idMOD = '%',
-    idLT = '<',
-    idLTLT = RUBY_TOKEN(LSHFT),
-    idLE = RUBY_TOKEN(LEQ),
-    idGT = '>',
-    idGTGT = RUBY_TOKEN(RSHFT),
-    idGE = RUBY_TOKEN(GEQ),
-    idEq = RUBY_TOKEN(EQ),
-    idEqq = RUBY_TOKEN(EQQ),
-    idNeq = RUBY_TOKEN(NEQ),
-    idNot = '!',
-    idBackquote = '`',
-    idEqTilde = RUBY_TOKEN(MATCH),
-    idNeqTilde = RUBY_TOKEN(NMATCH),
-    idAREF = RUBY_TOKEN(AREF),
-    idASET = RUBY_TOKEN(ASET),
-    idCOLON2 = RUBY_TOKEN(COLON2),
-    idANDOP = RUBY_TOKEN(ANDOP),
-    idOROP = RUBY_TOKEN(OROP),
-    idDOTQ = RUBY_TOKEN(DOTQ),
-    tPRESERVED_ID_BEGIN = <%=op_id_offset + token_op_ids.size - 1%>,
+% ids[:token_op].uniq {|_, op| op}.each do |id, op, token|
+    id<%=id%> = <%=token ? "RUBY_TOKEN(#{token})" : "'#{op}'"%>,
+% end
+    tPRESERVED_ID_BEGIN = <%=index-1%>,
 % ids[:preserved].each do |token|
     id<%=token%>,
 % end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]