Class: MARC::Spec::Parsing::ClosedLcAlphaRange

Inherits:
Parslet::Atoms::Base
  • Object
show all
Defined in:
lib/marc/spec/parsing/closed_lc_alpha_range.rb

Constant Summary collapse

SIMPLE_CLASS_NAME =
ClosedLcAlphaRange.name.split('::').last
LCALPHA_RANGE_RE =
/^([a-z])-([a-z])/.freeze

Instance Method Summary collapse

Instance Method Details

#to_s_inner(_prec) ⇒ Object



22
23
24
# File 'lib/marc/spec/parsing/closed_lc_alpha_range.rb', line 22

def to_s_inner(_prec)
  SIMPLE_CLASS_NAME
end

#try(source, context, _consume_all) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/marc/spec/parsing/closed_lc_alpha_range.rb', line 11

def try(source, context, _consume_all)
  source_str = source.instance_variable_get(:@str) # TODO: something less hacky
  return context.err(self, source, 'Not a lower-case alphabetic range') unless (range_str = source_str.check(LCALPHA_RANGE_RE))

  s, e = range_str.match(LCALPHA_RANGE_RE)[1, 2]
  return context.err(self, source, "#{s} !<= #{e}") unless s < e

  sv, _, ev = [s.size, 1, e.size].map { |l| source.consume(l) } # discard hyphen
  succ(from: sv, to: ev)
end