Chris@909
|
1 module CodeRay
|
Chris@909
|
2
|
Chris@909
|
3 # = WordList
|
Chris@909
|
4 #
|
Chris@909
|
5 # <b>A Hash subclass designed for mapping word lists to token types.</b>
|
Chris@909
|
6 #
|
Chris@909
|
7 # Copyright (c) 2006-2011 by murphy (Kornelius Kalnbach) <murphy rubychan de>
|
Chris@909
|
8 #
|
Chris@909
|
9 # License:: LGPL / ask the author
|
Chris@909
|
10 # Version:: 2.0 (2011-05-08)
|
Chris@909
|
11 #
|
Chris@909
|
12 # A WordList is a Hash with some additional features.
|
Chris@909
|
13 # It is intended to be used for keyword recognition.
|
Chris@909
|
14 #
|
Chris@909
|
15 # WordList is optimized to be used in Scanners,
|
Chris@909
|
16 # typically to decide whether a given ident is a special token.
|
Chris@909
|
17 #
|
Chris@909
|
18 # For case insensitive words use WordList::CaseIgnoring.
|
Chris@909
|
19 #
|
Chris@909
|
20 # Example:
|
Chris@909
|
21 #
|
Chris@909
|
22 # # define word arrays
|
Chris@909
|
23 # RESERVED_WORDS = %w[
|
Chris@909
|
24 # asm break case continue default do else
|
Chris@909
|
25 # ]
|
Chris@909
|
26 #
|
Chris@909
|
27 # PREDEFINED_TYPES = %w[
|
Chris@909
|
28 # int long short char void
|
Chris@909
|
29 # ]
|
Chris@909
|
30 #
|
Chris@909
|
31 # # make a WordList
|
Chris@909
|
32 # IDENT_KIND = WordList.new(:ident).
|
Chris@909
|
33 # add(RESERVED_WORDS, :reserved).
|
Chris@909
|
34 # add(PREDEFINED_TYPES, :predefined_type)
|
Chris@909
|
35 #
|
Chris@909
|
36 # ...
|
Chris@909
|
37 #
|
Chris@909
|
38 # def scan_tokens tokens, options
|
Chris@909
|
39 # ...
|
Chris@909
|
40 #
|
Chris@909
|
41 # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
|
Chris@909
|
42 # # use it
|
Chris@909
|
43 # kind = IDENT_KIND[match]
|
Chris@909
|
44 # ...
|
Chris@909
|
45 class WordList < Hash
|
Chris@909
|
46
|
Chris@909
|
47 # Create a new WordList with +default+ as default value.
|
Chris@909
|
48 def initialize default = false
|
Chris@909
|
49 super default
|
Chris@909
|
50 end
|
Chris@909
|
51
|
Chris@909
|
52 # Add words to the list and associate them with +value+.
|
Chris@909
|
53 #
|
Chris@909
|
54 # Returns +self+, so you can concat add calls.
|
Chris@909
|
55 def add words, value = true
|
Chris@909
|
56 words.each { |word| self[word] = value }
|
Chris@909
|
57 self
|
Chris@909
|
58 end
|
Chris@909
|
59
|
Chris@909
|
60 end
|
Chris@909
|
61
|
Chris@909
|
62
|
Chris@909
|
63 # A CaseIgnoring WordList is like a WordList, only that
|
Chris@909
|
64 # keys are compared case-insensitively (normalizing keys using +downcase+).
|
Chris@909
|
65 class WordList::CaseIgnoring < WordList
|
Chris@909
|
66
|
Chris@909
|
67 def [] key
|
Chris@909
|
68 super key.downcase
|
Chris@909
|
69 end
|
Chris@909
|
70
|
Chris@909
|
71 def []= key, value
|
Chris@909
|
72 super key.downcase, value
|
Chris@909
|
73 end
|
Chris@909
|
74
|
Chris@909
|
75 end
|
Chris@909
|
76
|
Chris@909
|
77 end
|