i18n-inflector-3 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ChangeLog +1674 -0
- data/LGPL-LICENSE +169 -0
- data/README.md +259 -0
- data/docs/COPYING +57 -0
- data/docs/EXAMPLES +357 -0
- data/docs/HISTORY +280 -0
- data/docs/LEGAL +10 -0
- data/docs/LGPL +166 -0
- data/docs/TODO +8 -0
- data/docs/USAGE +967 -0
- data/docs/rdoc.css +20 -0
- data/lib/i18n-inflector/api.rb +753 -0
- data/lib/i18n-inflector/api_strict.rb +671 -0
- data/lib/i18n-inflector/backend.rb +352 -0
- data/lib/i18n-inflector/config.rb +289 -0
- data/lib/i18n-inflector/errors.rb +226 -0
- data/lib/i18n-inflector/hset.rb +24 -0
- data/lib/i18n-inflector/inflection_data.rb +357 -0
- data/lib/i18n-inflector/inflection_data_strict.rb +300 -0
- data/lib/i18n-inflector/inflector.rb +38 -0
- data/lib/i18n-inflector/interpolate.rb +546 -0
- data/lib/i18n-inflector/lazy_enum.rb +267 -0
- data/lib/i18n-inflector/long_comments.rb +57 -0
- data/lib/i18n-inflector/options.rb +329 -0
- data/lib/i18n-inflector/version.rb +27 -0
- data/lib/i18n-inflector.rb +19 -0
- metadata +87 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
|
5
|
+
# Copyright:: (c) 2011,2012,2013 by Paweł Wilk
|
|
6
|
+
# License:: This program is licensed under the terms of {file:docs/LGPL GNU Lesser General Public License} or {file:docs/COPYING Ruby License}.
|
|
7
|
+
#
|
|
8
|
+
# This file contains error reporting classes for I18n::Backend::Inflector module.
|
|
9
|
+
|
|
10
|
+
module I18n
|
|
11
|
+
# @abstract It is a parent class for all exceptions
|
|
12
|
+
# related to inflections.
|
|
13
|
+
class InflectionException < I18n::ArgumentError
|
|
14
|
+
attr_accessor :token, :kind, :key
|
|
15
|
+
|
|
16
|
+
def initialize(locale, token, kind)
|
|
17
|
+
@locale = locale
|
|
18
|
+
@token = token
|
|
19
|
+
@kind = kind
|
|
20
|
+
@key = nil
|
|
21
|
+
super()
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @abstract It is a parent class for all exceptions
|
|
26
|
+
# related to inflection patterns that are processed.
|
|
27
|
+
class InflectionPatternException < InflectionException
|
|
28
|
+
attr_accessor :pattern
|
|
29
|
+
|
|
30
|
+
def initialize(locale, pattern, token, kind)
|
|
31
|
+
super(locale, token, kind)
|
|
32
|
+
@pattern = pattern
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def message
|
|
36
|
+
mkey = @key.nil? ? '' : ".#{@key}"
|
|
37
|
+
@pattern.nil? ? '' : "#{@locale}#{mkey}: #{@pattern} - "
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @abstract It is a parent class for all exceptions
|
|
42
|
+
# related to configuration data of inflections that is processed.
|
|
43
|
+
class InflectionConfigurationException < InflectionException
|
|
44
|
+
attr_accessor :locale
|
|
45
|
+
|
|
46
|
+
def message
|
|
47
|
+
mkey = @key.nil? ? ".i18n.inflections.#{@kind}" : ".#{@key}"
|
|
48
|
+
"#{@locale}#{mkey}: "
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# @abstract It is a parent class for exceptions raised when
|
|
53
|
+
# inflection option is bad or missing.
|
|
54
|
+
class InvalidOptionForKind < InflectionPatternException
|
|
55
|
+
attr_accessor :option
|
|
56
|
+
|
|
57
|
+
def initialize(locale, pattern, token, kind, option)
|
|
58
|
+
super(locale, pattern, token, kind)
|
|
59
|
+
@option = option
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# This is raised when there is no kind given in options. The kind
|
|
64
|
+
# is determined by looking at token placed in a pattern.
|
|
65
|
+
class InflectionOptionNotFound < InvalidOptionForKind
|
|
66
|
+
def message
|
|
67
|
+
kind = @kind.to_s
|
|
68
|
+
unless kind.empty?
|
|
69
|
+
kindmsg = if kind[0..0] == I18n::Inflector::Config::Markers::STRICT_KIND
|
|
70
|
+
":#{kind} (or :#{kind[1..]})"
|
|
71
|
+
else
|
|
72
|
+
kind.to_sym.inspect
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
'' << super <<
|
|
76
|
+
"required option #{kindmsg} was not found"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# This exception will be raised when a required option, describing token selected
|
|
81
|
+
# for a kind contains a token that is not of the given kind.
|
|
82
|
+
class InflectionOptionIncorrect < InvalidOptionForKind
|
|
83
|
+
def message
|
|
84
|
+
'' << super <<
|
|
85
|
+
"required value #{@option.inspect} of option #{@kind.inspect} " \
|
|
86
|
+
'does not match any token'
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# This is raised when a token given in a pattern is invalid (empty or has no
|
|
91
|
+
# kind assigned).
|
|
92
|
+
class InvalidInflectionToken < InflectionPatternException
|
|
93
|
+
def initialize(locale, pattern, token, kind = nil)
|
|
94
|
+
super
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def message
|
|
98
|
+
badkind = ''
|
|
99
|
+
if !@token.to_s.empty? && !kind.nil?
|
|
100
|
+
kind = @kind.to_s.empty? ? '' : @kind.to_sym
|
|
101
|
+
badkind = " (processed kind: #{kind.inspect})"
|
|
102
|
+
end
|
|
103
|
+
'' << super << ("token #{@token.to_s.inspect} is invalid" + badkind)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# This is raised when an inflection option name is invalid (contains
|
|
108
|
+
# reserved symbols).
|
|
109
|
+
class InvalidInflectionOption < InflectionPatternException
|
|
110
|
+
def initialize(locale, pattern, option)
|
|
111
|
+
super(locale, pattern, nil, option)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def message
|
|
115
|
+
'' << super << "inflection option #{@kind.inspect} is invalid"
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# This is raised when a kind given in a pattern is invalid (empty, reserved
|
|
120
|
+
# or containing a reserved character).
|
|
121
|
+
class InvalidInflectionKind < InflectionPatternException
|
|
122
|
+
def initialize(locale, pattern, kind)
|
|
123
|
+
super(locale, pattern, nil, kind)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def message
|
|
127
|
+
'' << super << "kind #{@kind.to_s.inspect} is invalid"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# This is raised when an inflection token used in a pattern does not match
|
|
132
|
+
# an assumed kind determined by reading previous tokens from that pattern
|
|
133
|
+
# or by the given strict kind of a named pattern.
|
|
134
|
+
class MisplacedInflectionToken < InflectionPatternException
|
|
135
|
+
def message
|
|
136
|
+
'' << super <<
|
|
137
|
+
"token #{@token.to_s.inspect} " \
|
|
138
|
+
"is not of the expected kind #{@kind.inspect}"
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# This is raised when a complex inflection pattern is malformed
|
|
143
|
+
# and cannot be reduced to set of regular patterns.
|
|
144
|
+
class ComplexPatternMalformed < InflectionPatternException
|
|
145
|
+
def initialize(locale, pattern, token, complex_kind)
|
|
146
|
+
unless pattern.include?(I18n::Inflector::Config::Markers::PATTERN)
|
|
147
|
+
pattern = I18n::Inflector::Config::Markers::PATTERN + "#{complex_kind}{#{pattern}}"
|
|
148
|
+
end
|
|
149
|
+
super
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def message
|
|
153
|
+
'' << super << 'pattern is malformed; token count differs from kind count'
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# This is raised when an inflection token of the same name is already defined in
|
|
158
|
+
# inflections tree of translation data.
|
|
159
|
+
class DuplicatedInflectionToken < InflectionConfigurationException
|
|
160
|
+
attr_accessor :original_kind
|
|
161
|
+
|
|
162
|
+
def initialize(locale, token, kind, original_kind)
|
|
163
|
+
super(locale, token, kind)
|
|
164
|
+
@original_kind = original_kind
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def message
|
|
168
|
+
'' << super <<
|
|
169
|
+
"token #{@token.inspect} " \
|
|
170
|
+
"was already assigned to the kind #{@original_kind.inspect}"
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# This is raised when an alias for an inflection token points to a token that
|
|
175
|
+
# doesn't exists. It is also raised when default token of some kind points
|
|
176
|
+
# to a non-existent token.
|
|
177
|
+
class BadInflectionAlias < InflectionConfigurationException
|
|
178
|
+
attr_accessor :pointer
|
|
179
|
+
|
|
180
|
+
def initialize(locale, token, kind, pointer)
|
|
181
|
+
super(locale, token, kind)
|
|
182
|
+
@pointer = pointer
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def message
|
|
186
|
+
what = (token == :default) ? 'default token' : "alias #{@token.inspect}"
|
|
187
|
+
'' << super <<
|
|
188
|
+
"the #{what} " \
|
|
189
|
+
"points to an unknown token #{@pointer.inspect}"
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# This is raised when an inflection token or its description has a bad name. This
|
|
194
|
+
# includes an empty name or a name containing prohibited characters.
|
|
195
|
+
class BadInflectionToken < InflectionConfigurationException
|
|
196
|
+
attr_accessor :description
|
|
197
|
+
|
|
198
|
+
def initialize(locale, token, kind = nil, description = nil)
|
|
199
|
+
super(locale, token, kind)
|
|
200
|
+
@description = description
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def message
|
|
204
|
+
'' << super << if @description.nil?
|
|
205
|
+
"inflection token #{@token.inspect} " \
|
|
206
|
+
'has a bad name'
|
|
207
|
+
else
|
|
208
|
+
"inflection token #{@token.inspect} " \
|
|
209
|
+
"has a bad description #{@description.inspect}"
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# This is raised when an inflection kind has a bad name
|
|
215
|
+
# or is not a root for a tree of tokens.
|
|
216
|
+
class BadInflectionKind < InflectionConfigurationException
|
|
217
|
+
def initialize(locale, kind)
|
|
218
|
+
super(locale, nil, kind)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def message
|
|
222
|
+
'' << super <<
|
|
223
|
+
"inflection kind #{@kind.inspect} has bad name or type"
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
|
5
|
+
# Copyright:: (c) 2011,2012,2013 by Paweł Wilk
|
|
6
|
+
# License:: This program is licensed under the terms of {file:docs/LGPL GNU Lesser General Public License} or {file:docs/COPYING Ruby License}.
|
|
7
|
+
#
|
|
8
|
+
# This file contains more intuitive version of Set.
|
|
9
|
+
|
|
10
|
+
module I18n
|
|
11
|
+
module Inflector
|
|
12
|
+
# This class keeps sets of data with hash-like access
|
|
13
|
+
class HSet < Set
|
|
14
|
+
# This method performs a fast check
|
|
15
|
+
# if an element exists in a set using hash-like syntax.
|
|
16
|
+
#
|
|
17
|
+
# @param [Object] k the element to check
|
|
18
|
+
# @return [Boolean] true if element exists in set
|
|
19
|
+
def [](k)
|
|
20
|
+
include?(k)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
|
5
|
+
# Copyright:: (c) 2011,2012,2013 by Paweł Wilk
|
|
6
|
+
# License:: This program is licensed under the terms of {file:docs/LGPL GNU Lesser General Public License} or {file:docs/COPYING Ruby License}.
|
|
7
|
+
#
|
|
8
|
+
# This file contains class that is used to keep
|
|
9
|
+
# inflection data.
|
|
10
|
+
|
|
11
|
+
# @abstract This namespace is shared with I18n subsystem.
|
|
12
|
+
module I18n
|
|
13
|
+
module Inflector
|
|
14
|
+
# This class contains structures for keeping parsed translation data
|
|
15
|
+
# and basic operations for performing on them.
|
|
16
|
+
class InflectionData < InflectionData_Strict
|
|
17
|
+
# Initializes internal structures.
|
|
18
|
+
#
|
|
19
|
+
# @param [Symbol,nil] locale the locale identifier for the object to be labeled with
|
|
20
|
+
def initialize(locale = nil)
|
|
21
|
+
@kinds = Hash.new(false)
|
|
22
|
+
@tokens = Hash.new(DUMMY_TOKEN)
|
|
23
|
+
@lazy_tokens = LazyHashEnumerator.for(@tokens)
|
|
24
|
+
@lazy_kinds = LazyArrayEnumerator.for(@kinds)
|
|
25
|
+
@defaults = {}
|
|
26
|
+
@locale = locale
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Adds an alias (overwriting an existing alias).
|
|
30
|
+
#
|
|
31
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
|
32
|
+
# (in case of bad or +nil+ names or non-existent targets)
|
|
33
|
+
# @overload add_alias(name, target)
|
|
34
|
+
# Adds an alias (overwriting an existing alias).
|
|
35
|
+
# @param [Symbol] name the name of an alias
|
|
36
|
+
# @param [Symbol] target the target token for the given +alias+
|
|
37
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
|
38
|
+
# (in case of bad or +nil+ names or non-existent targets)
|
|
39
|
+
# @overload add_alias(name, target, kind)
|
|
40
|
+
# Adds an alias (overwriting an existing alias) if the given
|
|
41
|
+
# +kind+ matches the kind of the given target.
|
|
42
|
+
# @param [Symbol] name the name of an alias
|
|
43
|
+
# @param [Symbol] target the target token for the given +alias+
|
|
44
|
+
# @param [Symbol] kind the optional kind of a taget
|
|
45
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
|
46
|
+
# (in case of bad or +nil+ names or non-existent targets)
|
|
47
|
+
def add_alias(name, target, kind = nil)
|
|
48
|
+
target = target.to_s
|
|
49
|
+
name = name.to_s
|
|
50
|
+
return false if name.empty? || target.empty?
|
|
51
|
+
|
|
52
|
+
kind = nil if !kind.nil? && !kind.nil? && kind.to_s.empty?
|
|
53
|
+
name = name.to_sym
|
|
54
|
+
target = target.to_sym
|
|
55
|
+
t_kind = get_kind(target)
|
|
56
|
+
return false if t_kind.nil? || (!kind.nil? && t_kind != kind)
|
|
57
|
+
|
|
58
|
+
@tokens[name] = {}
|
|
59
|
+
@tokens[name][:kind] = kind
|
|
60
|
+
@tokens[name][:target] = target
|
|
61
|
+
@tokens[name][:description] = @tokens[target][:description]
|
|
62
|
+
true
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Adds a token (overwriting existing token).
|
|
66
|
+
#
|
|
67
|
+
# @param [Symbol] token the name of a token to add
|
|
68
|
+
# @param [Symbol] kind the kind of a token
|
|
69
|
+
# @param [String] description the description of a token
|
|
70
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
|
71
|
+
# (in case of bad names or non-existent targets)
|
|
72
|
+
def add_token(token, kind, description)
|
|
73
|
+
return false if token.to_s.empty? || kind.to_s.empty? || description.nil?
|
|
74
|
+
|
|
75
|
+
token = token.to_sym
|
|
76
|
+
@tokens[token] = {}
|
|
77
|
+
@tokens[token][:kind] = kind.to_sym
|
|
78
|
+
@tokens[token][:description] = description.to_s
|
|
79
|
+
@kinds[kind] = true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Tests if the token is a true token.
|
|
83
|
+
#
|
|
84
|
+
# @overload has_true_token?(token)
|
|
85
|
+
# Tests if the token is a true token.
|
|
86
|
+
# @param [Symbol] token the identifier of a token
|
|
87
|
+
# @return [Boolean] +true+ if the given +token+ is
|
|
88
|
+
# a token and not an alias, +false+ otherwise
|
|
89
|
+
# @overload has_true_token?(token, kind)
|
|
90
|
+
# Tests if the token is a true token.
|
|
91
|
+
# The kind will work as the expectation filter.
|
|
92
|
+
# @param [Symbol] token the identifier of a token
|
|
93
|
+
# @param [Symbol] kind the identifier of a kind
|
|
94
|
+
# @return [Boolean] +true+ if the given +token+ is
|
|
95
|
+
# a token and not an alias, and is a kind of
|
|
96
|
+
# the given kind, +false+ otherwise
|
|
97
|
+
def has_true_token?(token, kind = nil)
|
|
98
|
+
o = @tokens[token]
|
|
99
|
+
k = o[:kind]
|
|
100
|
+
return false if k.nil? || !o[:target].nil?
|
|
101
|
+
|
|
102
|
+
kind.nil? || k == kind
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Tests if a token (or alias) is present.
|
|
106
|
+
#
|
|
107
|
+
# @overload has_token(token)
|
|
108
|
+
# Tests if a token (or alias) is present.
|
|
109
|
+
# @param [Symbol] token the identifier of a token
|
|
110
|
+
# @return [Boolean] +true+ if the given +token+
|
|
111
|
+
# (which may be an alias) exists
|
|
112
|
+
# @overload has_token(token, kind)
|
|
113
|
+
# Tests if a token (or alias) is present.
|
|
114
|
+
# The kind will work as the expectation filter.
|
|
115
|
+
# @param [Symbol] token the identifier of a token
|
|
116
|
+
# @param [Symbol] kind the identifier of a kind
|
|
117
|
+
# @return [Boolean] +true+ if the given +token+
|
|
118
|
+
# (which may be an alias) exists and if kind of
|
|
119
|
+
# the given kind
|
|
120
|
+
def has_token?(token, kind = nil)
|
|
121
|
+
k = @tokens[token][:kind]
|
|
122
|
+
kind.nil? ? !k.nil? : k == kind
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Tests if a kind exists.
|
|
126
|
+
#
|
|
127
|
+
# @param [Symbol] kind the identifier of a kind
|
|
128
|
+
# @return [Boolean] +true+ if the given +kind+ exists
|
|
129
|
+
def has_kind?(kind)
|
|
130
|
+
@kinds.key?(kind)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Tests if a kind has a default token assigned.
|
|
134
|
+
#
|
|
135
|
+
# @param [Symbol] kind the identifier of a kind
|
|
136
|
+
# @return [Boolean] +true+ if there is a default
|
|
137
|
+
# token of the given kind
|
|
138
|
+
def has_default_token?(kind)
|
|
139
|
+
@defaults.key?(kind)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Tests if the given alias is really an alias.
|
|
143
|
+
#
|
|
144
|
+
# @overload has_alias?(alias_name)
|
|
145
|
+
# Tests if the given alias is really an alias.
|
|
146
|
+
# @param [Symbol] alias_name the identifier of an alias
|
|
147
|
+
# @return [Boolean] +true+ if the given alias is really an alias,
|
|
148
|
+
# +false+ otherwise
|
|
149
|
+
# @overload has_alias?(alias_name, kind)
|
|
150
|
+
# Tests if the given alias is really an alias.
|
|
151
|
+
# The kind will work as the expectation filter.
|
|
152
|
+
# @param [Symbol] alias_name the identifier of an alias
|
|
153
|
+
# @param [Symbol] kind the identifier of a kind
|
|
154
|
+
# @return [Boolean] +true+ if the given alias is really an alias
|
|
155
|
+
# being a kind of the given kind, +false+ otherwise
|
|
156
|
+
def has_alias?(alias_name, kind = nil)
|
|
157
|
+
o = @tokens[alias_name]
|
|
158
|
+
return false if o[:target].nil?
|
|
159
|
+
|
|
160
|
+
kind.nil? || o[:kind] == kind
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Iterates through all the true tokens (not aliases).
|
|
164
|
+
#
|
|
165
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
166
|
+
# @yield [token, description] optional block in which each token will be yielded
|
|
167
|
+
# @yieldparam [Symbol] token a token
|
|
168
|
+
# @yieldparam [String] description a description string for a token
|
|
169
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
170
|
+
# @overload each_true_token
|
|
171
|
+
# Reads all the true tokens (not aliases).
|
|
172
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
173
|
+
# @overload each_true_token(kind)
|
|
174
|
+
# Reads all the true tokens (not aliases) of the given +kind+.
|
|
175
|
+
# @param [Symbol] kind the identifier of a kind
|
|
176
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
177
|
+
def each_true_token(kind = nil, &)
|
|
178
|
+
t = @lazy_tokens
|
|
179
|
+
t = t.select { |_token, data| data[:kind] == kind } unless kind.nil?
|
|
180
|
+
t.select { |_token, data| data[:target].nil? }
|
|
181
|
+
.map { |_token, data| data[:description] }.each(&)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Iterates through all the aliases.
|
|
185
|
+
#
|
|
186
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>alias => target</tt>)
|
|
187
|
+
# @yield [alias, target] optional block in which each alias will be yielded
|
|
188
|
+
# @yieldparam [Symbol] alias an alias
|
|
189
|
+
# @yieldparam [Symbol] target a name of the target token
|
|
190
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
191
|
+
# @overload each_alias
|
|
192
|
+
# Reads all the aliases.
|
|
193
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>alias => target</tt>)
|
|
194
|
+
# @overload each_alias(kind)
|
|
195
|
+
# Reads all the aliases of the given +kind+.
|
|
196
|
+
# @param [Symbol] kind the identifier of a kind
|
|
197
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>alias => target</tt>)
|
|
198
|
+
def each_alias(kind = nil, &)
|
|
199
|
+
t = @lazy_tokens
|
|
200
|
+
t = t.select { |_token, data| data[:kind] == kind } unless kind.nil?
|
|
201
|
+
t.reject { |_token, data| data[:target].nil? }
|
|
202
|
+
.map { |_token, data| data[:target] }.each(&)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Iterates through all the tokens in a way that it is possible to
|
|
206
|
+
# distinguish true tokens from aliases.
|
|
207
|
+
#
|
|
208
|
+
# @note True tokens have descriptions (String) and aliases
|
|
209
|
+
# have targets (Symbol) assigned.
|
|
210
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
|
|
211
|
+
# @yield [token, value] optional block in which each token will be yielded
|
|
212
|
+
# @yieldparam [Symbol] token a token
|
|
213
|
+
# @yieldparam [Symbol, String] value a description string for a token or a target (if alias)
|
|
214
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
215
|
+
# @overload each_raw_token
|
|
216
|
+
# Reads all the tokens in a way that it is possible to
|
|
217
|
+
# distinguish true tokens from aliases.
|
|
218
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
|
|
219
|
+
# @overload each_raw_token(kind)
|
|
220
|
+
# Reads all the tokens of the given +kind+ in a way
|
|
221
|
+
# that it is possible to distinguish true tokens from aliases.
|
|
222
|
+
# @param [Symbol] kind the identifier of a kind
|
|
223
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
|
|
224
|
+
def each_raw_token(kind = nil, &)
|
|
225
|
+
t = @lazy_tokens
|
|
226
|
+
t = t.select { |_token, data| data[:kind] == kind } unless kind.nil?
|
|
227
|
+
t.map { |_token, data| data[:target] || data[:description] }
|
|
228
|
+
.each(&)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Iterates through all the tokens (including aliases).
|
|
232
|
+
#
|
|
233
|
+
# @note Use {#each_raw_token} if you want to distinguish
|
|
234
|
+
# true tokens from aliases.
|
|
235
|
+
# @return return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
236
|
+
# @yield [token, description] optional block in which each token will be yielded
|
|
237
|
+
# @yieldparam [Symbol] token a token
|
|
238
|
+
# @yieldparam [String] description a description string for a token
|
|
239
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
240
|
+
# @overload each_token
|
|
241
|
+
# Reads all the tokens (including aliases).
|
|
242
|
+
# @return return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
243
|
+
# @overload each_token(kind)
|
|
244
|
+
# Reads all the tokens (including aliases) of the
|
|
245
|
+
# given +kind+.
|
|
246
|
+
# @param [Symbol] kind the identifier of a kind
|
|
247
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
248
|
+
def each_token(kind = nil, &)
|
|
249
|
+
t = @lazy_tokens
|
|
250
|
+
t = t.select { |_token, data| data[:kind] == kind } unless kind.nil?
|
|
251
|
+
t.map { |_token, data| data[:description] }.each(&)
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# Gets a target token for the alias.
|
|
255
|
+
#
|
|
256
|
+
# @return [Symbol,nil] the token that the given alias points to
|
|
257
|
+
# or +nil+ if it isn't really an alias
|
|
258
|
+
# @overload get_target_for_alias(alias_name)
|
|
259
|
+
# Gets a target token for the alias.
|
|
260
|
+
# @param [Symbol] alias_name the identifier of an alias
|
|
261
|
+
# @return [Symbol,nil] the token that the given alias points to
|
|
262
|
+
# or +nil+ if it isn't really an alias
|
|
263
|
+
# @overload get_target_for_alias(alias_name, kind)
|
|
264
|
+
# Gets a target token for the alias that's +kind+ is given.
|
|
265
|
+
# @param [Symbol] alias_name the identifier of an alias
|
|
266
|
+
# @param [Symbol] kind the identifier of a kind
|
|
267
|
+
# @return [Symbol,nil] the token that the given alias points to
|
|
268
|
+
# or +nil+ if it isn't really an alias
|
|
269
|
+
def get_target_for_alias(alias_name, _kind = nil)
|
|
270
|
+
@tokens[alias_name][:target]
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Gets a kind of the given token or alias.
|
|
274
|
+
#
|
|
275
|
+
# @return [Symbol,nil] the kind of the given +token+
|
|
276
|
+
# or +nil+ if the token is unknown
|
|
277
|
+
# @overload get_kind(token)
|
|
278
|
+
# Gets a kind of the given token or alias.
|
|
279
|
+
# @param [Symbol] token identifier of a token
|
|
280
|
+
# @return [Symbol,nil] the kind of the given +token+
|
|
281
|
+
# or +nil+ if the token is unknown
|
|
282
|
+
# @overload get_kind(token, kind)
|
|
283
|
+
# Gets a kind of the given token or alias.
|
|
284
|
+
# The kind will work as the expectation filter.
|
|
285
|
+
# @param [Symbol] token identifier of a token
|
|
286
|
+
# @param [Symbol] kind the identifier of a kind
|
|
287
|
+
# @return [Symbol,nil] the kind of the given +token+
|
|
288
|
+
# or +nil+ if the token is unknown
|
|
289
|
+
def get_kind(token, kind = nil)
|
|
290
|
+
k = @tokens[token][:kind]
|
|
291
|
+
return k if kind.nil? || kind == k
|
|
292
|
+
|
|
293
|
+
nil
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# Gets a true token for the given identifier.
|
|
297
|
+
#
|
|
298
|
+
# @note If the given +token+ is really an alias it will
|
|
299
|
+
# be resolved and the real token pointed by that alias
|
|
300
|
+
# will be returned.
|
|
301
|
+
# @return [Symbol,nil] the true token for the given +token+
|
|
302
|
+
# or +nil+
|
|
303
|
+
# @overload get_true_token(token)
|
|
304
|
+
# Gets a true token for the given +token+ identifier.
|
|
305
|
+
# @param [Symbol] token the identifier of a token
|
|
306
|
+
# @return [Symbol,nil] the true token for the given +token+
|
|
307
|
+
# or +nil+ if the token is unknown
|
|
308
|
+
# @overload get_true_token(token, kind)
|
|
309
|
+
# Gets a true token for the given +token+ identifier and the
|
|
310
|
+
# given +kind+. The kind will work as the expectation filter.
|
|
311
|
+
# @param [Symbol] token the identifier of a token
|
|
312
|
+
# @param [Symbol] kind the identifier of a kind
|
|
313
|
+
# @return [Symbol,nil] the true token for the given +token+
|
|
314
|
+
# or +nil+ if the token is unknown or is not a kind of the
|
|
315
|
+
# given +kind+
|
|
316
|
+
def get_true_token(token, kind = nil)
|
|
317
|
+
o = @tokens[token]
|
|
318
|
+
k = o[:kind]
|
|
319
|
+
return nil if k.nil?
|
|
320
|
+
|
|
321
|
+
r = o[:target] || token
|
|
322
|
+
return r if kind.nil?
|
|
323
|
+
|
|
324
|
+
(k == kind) ? r : nil
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# Reads the default token of a kind.
|
|
328
|
+
#
|
|
329
|
+
# @note It will always return true token (not an alias).
|
|
330
|
+
# @param [Symbol] kind the identifier of a kind
|
|
331
|
+
# @return [Symbol,nil] the default token of the given +kind+
|
|
332
|
+
# or +nil+ if there is no default token set
|
|
333
|
+
def get_default_token(kind)
|
|
334
|
+
@defaults[kind]
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Gets a description of a token or an alias.
|
|
338
|
+
# @note If the token is really an alias it will resolve the alias first.
|
|
339
|
+
# @return [String,nil] the string containing description of the given
|
|
340
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
|
341
|
+
# @overload get_description(token)
|
|
342
|
+
# Gets a description of a token or an alias.
|
|
343
|
+
# @param [Symbol] token the identifier of a token
|
|
344
|
+
# @return [String,nil] the string containing description of the given
|
|
345
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
|
346
|
+
# @overload get_description(token, kind)
|
|
347
|
+
# Gets a description of a token or an alias of the given +kind+
|
|
348
|
+
# @param [Symbol] token the identifier of a token
|
|
349
|
+
# @param [Symbol] kind the identifier of a kind
|
|
350
|
+
# @return [String,nil] the string containing description of the given
|
|
351
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
|
352
|
+
def get_description(token, kind = nil)
|
|
353
|
+
@tokens[token][:description] if kind.nil? || @tokens[token][:kind] == kind
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
end
|