Class: ABNF::Compiler::Grammar::Repetition

Inherits:
Object
  • Object
show all
Includes:
Compiler
Defined in:
lib/abnf/compiler/grammar/repetition.rb

Constant Summary collapse

PATTERN =
%r{\A
  (?:
    (?<range>(?<minimum>[[:digit:]]*)\*(?<maximum>[[:digit:]]*))
  |
    (?<fixed_number>[[:digit:]]+)
  )?
}nx

Instance Attribute Summary

Attributes included from Compiler

#match_data, #stream

Instance Method Summary collapse

Methods included from Compiler

included, #initialize

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/abnf/compiler/grammar/repetition.rb', line 15

def call
  element = Element.compile stream
  return unless element

  if no_repeat?
    min = 1
    max = 1
  elsif fixed_number
    min = fixed_number.to_i
    max = min
  else
    min = minimum.to_i
    max = if maximum.empty? then Float::INFINITY else maximum.to_i end
  end

  return element if min == 1 and max == 1
  return if min > max

  ABNF::Compiler::Rule::Repetition.new min..max, element
end

#fixed_numberObject



36
37
38
# File 'lib/abnf/compiler/grammar/repetition.rb', line 36

def fixed_number
  match_data['fixed_number'.freeze]
end

#maximumObject



40
41
42
# File 'lib/abnf/compiler/grammar/repetition.rb', line 40

def maximum
  match_data['maximum'.freeze]
end

#minimumObject



44
45
46
# File 'lib/abnf/compiler/grammar/repetition.rb', line 44

def minimum
  match_data['minimum'.freeze]
end

#no_repeat?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/abnf/compiler/grammar/repetition.rb', line 48

def no_repeat?
  not fixed_number and not range?
end

#range?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/abnf/compiler/grammar/repetition.rb', line 52

def range?
  match_data['range'.freeze]
end