Class: Keyword

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_parser_extras.rb

Defined Under Namespace

Classes: KWtable

Constant Summary collapse

TOTAL_KEYWORDS =
40
MIN_WORD_LENGTH =
2
MAX_WORD_LENGTH =
8
MIN_HASH_VALUE =
6
MAX_HASH_VALUE =
55
ASSO_VALUES =

maximum key range = 50, duplicates = 0

[
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 11, 56, 56, 36, 56,  1, 37,
 31,  1, 56, 56, 56, 56, 29, 56,  1, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56,  1, 56, 32,  1,  2,
 1,   1,  4, 23, 56, 17, 56, 20,  9,  2,
 9,  26, 14, 56,  5,  1,  1, 16, 56, 21,
 20,  9, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
 56, 56, 56, 56, 56, 56
]
WORDLIST =

:expr_beg = ignore newline, /- is a sign. :expr_end = newline significant, /- is a operator. :expr_arg = newline significant, /- is a operator. :expr_cmdarg = newline significant, /- is a operator. :expr_endarg = newline significant, /- is a operator. :expr_mid = newline significant, /- is a operator. :expr_fname = ignore newline, no reserved words. :expr_dot = right after . or ::, no reserved words. :expr_class = immediate after class, no here document.

[
 [""], [""], [""], [""], [""], [""],
 ["end",      [:kEND,      :kEND        ], :expr_end   ],
 ["else",     [:kELSE,     :kELSE       ], :expr_beg   ],
 ["case",     [:kCASE,     :kCASE       ], :expr_beg   ],
 ["ensure",   [:kENSURE,   :kENSURE     ], :expr_beg   ],
 ["module",   [:kMODULE,   :kMODULE     ], :expr_beg   ],
 ["elsif",    [:kELSIF,    :kELSIF      ], :expr_beg   ],
 ["def",      [:kDEF,      :kDEF        ], :expr_fname ],
 ["rescue",   [:kRESCUE,   :kRESCUE_MOD ], :expr_mid   ],
 ["not",      [:kNOT,      :kNOT        ], :expr_beg   ],
 ["then",     [:kTHEN,     :kTHEN       ], :expr_beg   ],
 ["yield",    [:kYIELD,    :kYIELD      ], :expr_arg   ],
 ["for",      [:kFOR,      :kFOR        ], :expr_beg   ],
 ["self",     [:kSELF,     :kSELF       ], :expr_end   ],
 ["false",    [:kFALSE,    :kFALSE      ], :expr_end   ],
 ["retry",    [:kRETRY,    :kRETRY      ], :expr_end   ],
 ["return",   [:kRETURN,   :kRETURN     ], :expr_mid   ],
 ["true",     [:kTRUE,     :kTRUE       ], :expr_end   ],
 ["if",       [:kIF,       :kIF_MOD     ], :expr_beg   ],
 ["defined?", [:kDEFINED,  :kDEFINED    ], :expr_arg   ],
 ["super",    [:kSUPER,    :kSUPER      ], :expr_arg   ],
 ["undef",    [:kUNDEF,    :kUNDEF      ], :expr_fname ],
 ["break",    [:kBREAK,    :kBREAK      ], :expr_mid   ],
 ["in",       [:kIN,       :kIN         ], :expr_beg   ],
 ["do",       [:kDO,       :kDO         ], :expr_beg   ],
 ["nil",      [:kNIL,      :kNIL        ], :expr_end   ],
 ["until",    [:kUNTIL,    :kUNTIL_MOD  ], :expr_beg   ],
 ["unless",   [:kUNLESS,   :kUNLESS_MOD ], :expr_beg   ],
 ["or",       [:kOR,       :kOR         ], :expr_beg   ],
 ["next",     [:kNEXT,     :kNEXT       ], :expr_mid   ],
 ["when",     [:kWHEN,     :kWHEN       ], :expr_beg   ],
 ["redo",     [:kREDO,     :kREDO       ], :expr_end   ],
 ["and",      [:kAND,      :kAND        ], :expr_beg   ],
 ["begin",    [:kBEGIN,    :kBEGIN      ], :expr_beg   ],
 ["__LINE__", [:k__LINE__, :k__LINE__   ], :expr_end   ],
 ["class",    [:kCLASS,    :kCLASS      ], :expr_class ],
 ["__FILE__", [:k__FILE__, :k__FILE__   ], :expr_end   ],
 ["END",      [:klEND,     :klEND       ], :expr_end   ],
 ["BEGIN",    [:klBEGIN,   :klBEGIN     ], :expr_end   ],
 ["while",    [:kWHILE,    :kWHILE_MOD  ], :expr_beg   ],
 [""], [""], [""], [""], [""], [""], [""], [""], [""],
 [""],
 ["alias",    [:kALIAS,    :kALIAS      ], :expr_fname ],
].map { |args| KWtable.new(*args) }

Class Method Summary collapse

Class Method Details

.hash_keyword(str, len) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/ruby_parser_extras.rb', line 559

def self.hash_keyword(str, len)
  hval = len

  case hval
  when 2, 1 then
    hval += ASSO_VALUES[str[0].ord]
  else
    hval += ASSO_VALUES[str[2].ord]
    hval += ASSO_VALUES[str[0].ord]
  end

  hval += ASSO_VALUES[str[len - 1].ord]
  return hval
end

.keyword(str, len = str.size) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
# File 'lib/ruby_parser_extras.rb', line 574

def self.keyword(str, len = str.size)
  if len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH then
    key = hash_keyword(str, len)
    if key <= MAX_HASH_VALUE && key >= 0 then
      s = WORDLIST[key].name
      return WORDLIST[key] if str == s
    end
  end

  return nil
end