Class: CodeRay::WordList
- Inherits:
-
Hash
- Object
- Hash
- CodeRay::WordList
- Defined in:
- lib/coderay/helpers/word_list.rb
Overview
WordList
A Hash subclass designed for mapping word lists to token types.
Copyright © 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
- License
-
LGPL / ask the author
- Version
-
1.1 (2006-Oct-19)
A WordList is a Hash with some additional features. It is intended to be used for keyword recognition.
WordList is highly optimized to be used in Scanners, typically to decide whether a given ident is a special token.
For case insensitive words use CaseIgnoringWordList.
Example:
# define word arrays
RESERVED_WORDS = %w[
asm break case continue default do else
...
]
PREDEFINED_TYPES = %w[
int long short char void
...
]
PREDEFINED_CONSTANTS = %w[
EOF NULL ...
]
# make a WordList
IDENT_KIND = WordList.new(:ident).
add(RESERVED_WORDS, :reserved).
add(PREDEFINED_TYPES, :pre_type).
add(PREDEFINED_CONSTANTS, :pre_constant)
...
def scan_tokens tokens, options
...
elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
# use it
kind = IDENT_KIND[match]
...
Direct Known Subclasses
Instance Method Summary collapse
-
#add(words, kind = true) ⇒ Object
Add words to the list and associate them with
kind
. -
#initialize(default = false, caching = false, &block) ⇒ WordList
constructor
Creates a new WordList with
default
as default value.
Constructor Details
#initialize(default = false, caching = false, &block) ⇒ WordList
Creates a new WordList with default
as default value.
You can activate caching
to store the results for every [] request.
With caching, methods like include?
or delete
may no longer behave as you expect. Therefore, it is recommended to use the [] method only.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coderay/helpers/word_list.rb', line 60 def initialize default = false, caching = false, &block if block raise ArgumentError, 'Can\'t combine block with caching.' if caching super(&block) else if caching super() do |h, k| h[k] = h.fetch k, default end else super default end end end |
Instance Method Details
#add(words, kind = true) ⇒ Object
Add words to the list and associate them with kind
.
Returns self
, so you can concat add calls.
78 79 80 81 82 83 |
# File 'lib/coderay/helpers/word_list.rb', line 78 def add words, kind = true words.each do |word| self[word] = kind end self end |