OSDN Git Service

add Redmine trunk rev 3089
[redminele/redminele.git] / redmine / vendor / plugins / coderay-0.7.6.227 / lib / coderay / helpers / word_list.rb
1 module CodeRay
2
3 # = WordList
4
5 # <b>A Hash subclass designed for mapping word lists to token types.</b>
6
7 # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
8 #
9 # License:: LGPL / ask the author
10 # Version:: 1.1 (2006-Oct-19)
11 #
12 # A WordList is a Hash with some additional features.
13 # It is intended to be used for keyword recognition.
14 #
15 # WordList is highly optimized to be used in Scanners,
16 # typically to decide whether a given ident is a special token.
17 #
18 # For case insensitive words use CaseIgnoringWordList.
19 #
20 # Example:
21 #
22 #  # define word arrays
23 #  RESERVED_WORDS = %w[
24 #    asm break case continue default do else
25 #    ...
26 #  ]
27 #  
28 #  PREDEFINED_TYPES = %w[
29 #    int long short char void
30 #    ...
31 #  ]
32 #  
33 #  PREDEFINED_CONSTANTS = %w[
34 #    EOF NULL ...
35 #  ]
36 #  
37 #  # make a WordList
38 #  IDENT_KIND = WordList.new(:ident).
39 #    add(RESERVED_WORDS, :reserved).
40 #    add(PREDEFINED_TYPES, :pre_type).
41 #    add(PREDEFINED_CONSTANTS, :pre_constant)
42 #
43 #  ...
44 #
45 #  def scan_tokens tokens, options
46 #    ...
47 #    
48 #    elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
49 #      # use it
50 #      kind = IDENT_KIND[match]
51 #      ...
52 class WordList < Hash
53
54   # Creates a new WordList with +default+ as default value.
55   # 
56   # You can activate +caching+ to store the results for every [] request.
57   # 
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
61     if block
62       raise ArgumentError, 'Can\'t combine block with caching.' if caching
63       super(&block)
64     else
65       if caching
66         super() do |h, k|
67           h[k] = h.fetch k, default
68         end
69       else
70         super default
71       end
72     end
73   end
74
75   # Add words to the list and associate them with +kind+.
76   # 
77   # Returns +self+, so you can concat add calls.
78   def add words, kind = true
79     words.each do |word|
80       self[word] = kind
81     end
82     self
83   end
84
85 end
86
87
88 # A CaseIgnoringWordList is like a WordList, only that
89 # keys are compared case-insensitively.
90
91 # Ignoring the text case is realized by sending the +downcase+ message to
92 # all keys.
93
94 # Caching usually makes a CaseIgnoringWordList faster, but it has to be
95 # activated explicitely.
96 class CaseIgnoringWordList < WordList
97
98   # Creates a new case-insensitive WordList with +default+ as default value.
99   # 
100   # You can activate caching to store the results for every [] request.
101   def initialize default = false, caching = false
102     if caching
103       super(default, false) do |h, k|
104         h[k] = h.fetch k.downcase, default
105       end
106     else
107       def self.[] key  # :nodoc:
108         super(key.downcase)
109       end
110     end
111   end
112
113   # Add +words+ to the list and associate them with +kind+.
114   def add words, kind = true
115     words.each do |word|
116       self[word.downcase] = kind
117     end
118     self
119   end
120
121 end
122
123 end