To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 5e / 5eb189ca68bafa164c9eaf8f70c4f655a815135e.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (1.77 KB)

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-2011 by murphy (Kornelius Kalnbach) <murphy rubychan de>
8
  #
9
  # License:: LGPL / ask the author
10
  # Version:: 2.0 (2011-05-08)
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 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 WordList::CaseIgnoring.
19
  #
20
  # Example:
21
  #
22
  #  # define word arrays
23
  #  RESERVED_WORDS = %w[
24
  #    asm break case continue default do else
25
  #  ]
26
  #  
27
  #  PREDEFINED_TYPES = %w[
28
  #    int long short char void
29
  #  ]
30
  #  
31
  #  # make a WordList
32
  #  IDENT_KIND = WordList.new(:ident).
33
  #    add(RESERVED_WORDS, :reserved).
34
  #    add(PREDEFINED_TYPES, :predefined_type)
35
  #  
36
  #  ...
37
  #  
38
  #  def scan_tokens tokens, options
39
  #    ...
40
  #    
41
  #    elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
42
  #      # use it
43
  #      kind = IDENT_KIND[match]
44
  #      ...
45
  class WordList < Hash
46
    
47
    # Create a new WordList with +default+ as default value.
48
    def initialize default = false
49
      super default
50
    end
51
    
52
    # Add words to the list and associate them with +value+.
53
    # 
54
    # Returns +self+, so you can concat add calls.
55
    def add words, value = true
56
      words.each { |word| self[word] = value }
57
      self
58
    end
59
    
60
  end
61
  
62
  
63
  # A CaseIgnoring WordList is like a WordList, only that
64
  # keys are compared case-insensitively (normalizing keys using +downcase+).
65
  class WordList::CaseIgnoring < WordList
66
    
67
    def [] key
68
      super key.downcase
69
    end
70
    
71
    def []= key, value
72
      super key.downcase, value
73
    end
74
    
75
  end
76
  
77
end