5 # <b>A Hash subclass designed for mapping word lists to token types.</b>
7 # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
9 # License:: LGPL / ask the author
10 # Version:: 1.1 (2006-Oct-19)
12 # A WordList is a Hash with some additional features.
13 # It is intended to be used for keyword recognition.
15 # WordList is highly optimized to be used in Scanners,
16 # typically to decide whether a given ident is a special token.
18 # For case insensitive words use CaseIgnoringWordList.
22 # # define word arrays
23 # RESERVED_WORDS = %w[
24 # asm break case continue default do else
28 # PREDEFINED_TYPES = %w[
29 # int long short char void
33 # PREDEFINED_CONSTANTS = %w[
38 # IDENT_KIND = WordList.new(:ident).
39 # add(RESERVED_WORDS, :reserved).
40 # add(PREDEFINED_TYPES, :pre_type).
41 # add(PREDEFINED_CONSTANTS, :pre_constant)
45 # def scan_tokens tokens, options
48 # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
50 # kind = IDENT_KIND[match]
54 # Creates a new WordList with +default+ as default value.
56 # You can activate +caching+ to store the results for every [] request.
58 # With caching, methods like +include?+ or +delete+ may no longer behave
59 # as you expect. Therefore, it is recommended to use the [] method only.
60 def initialize default = false, caching = false, &block
62 raise ArgumentError, 'Can\'t combine block with caching.' if caching
67 h[k] = h.fetch k, default
75 # Add words to the list and associate them with +kind+.
77 # Returns +self+, so you can concat add calls.
78 def add words, kind = true
88 # A CaseIgnoringWordList is like a WordList, only that
89 # keys are compared case-insensitively.
91 # Ignoring the text case is realized by sending the +downcase+ message to
94 # Caching usually makes a CaseIgnoringWordList faster, but it has to be
95 # activated explicitely.
96 class CaseIgnoringWordList < WordList
98 # Creates a new case-insensitive WordList with +default+ as default value.
100 # You can activate caching to store the results for every [] request.
101 def initialize default = false, caching = false
103 super(default, false) do |h, k|
104 h[k] = h.fetch k.downcase, default
107 def self.[] key # :nodoc:
113 # Add +words+ to the list and associate them with +kind+.
114 def add words, kind = true
116 self[word.downcase] = kind