Module: Parsr::Rules::Hash
- Defined in:
- lib/parsr/rules/hash.rb
Defined Under Namespace
Classes: MissingSeparator, MissingValue, Unterminated
Class Method Summary
collapse
Class Method Details
.match(scanner, &block) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/parsr/rules/hash.rb', line 17
def match(scanner, &block)
if scanner.scan(/\{\s*/)
hash = []
while pair = parse_pair(scanner, &block)
hash << pair
break unless scanner.scan(/\s*\,\s*/)
end
raise Unterminated.new(scanner) unless scanner.scan(/\s*\}\s*/)
Parsr::Token.new(Hash[hash])
end
end
|
.parse_pair(scanner, key = nil) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/parsr/rules/hash.rb', line 29
def parse_pair(scanner, key=nil)
unless key.is_a?(Parsr::Token)
if scanner.scan(/[a-zA-Z_][0-9a-zA-Z_]*\:/)
key = Parsr::Token.new(scanner.matched.chomp(':').to_sym)
elsif key = yield and key.is_a?(Parsr::Token)
raise MissingSeparator.new(scanner) unless scanner.scan(/\s*=>\s*/)
else
return false
end
end
value = yield
raise MissingValue.new(scanner) unless value && value.is_a?(Parsr::Token)
[key, value].map(&:value)
end
|