Class: Musa::GenerativeGrammar::Implementation::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/generative/generative-grammar.rb

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Object



101
102
103
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 101

def [](index)
  options[index].to_serie.to_node
end

#_options(parent: nil, &condition) ⇒ Object

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 114

def _options(parent: nil, &condition)
  raise NotImplementedError
end

#limit(attribute = nil, after_collect_operation = nil, comparison_method = nil, comparison_value = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 58

def limit(attribute = nil, after_collect_operation = nil, comparison_method = nil, comparison_value = nil, &block)
  raise ArgumentError, 'Cannot use simplified arguments and yield block at the same time' if (attribute || after_collect_operation || comparison_method || comparison_value) && @block

  block ||= generate_simple_condition_block(attribute, after_collect_operation, comparison_method, comparison_value)

  ConditionNode.new(self, &block)
end

#next(other) ⇒ Object Also known as: +



66
67
68
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 66

def next(other)
  NextNode.new(self, other)
end

#options(attribute = nil, after_collect_operation = nil, comparison_method = nil, comparison_value = nil, raw: nil, content: nil, &condition) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 72

def options(attribute = nil,
            after_collect_operation = nil,
            comparison_method = nil,
            comparison_value = nil,
            raw: nil,
            content: nil,
            &condition)

  raise ArgumentError, 'Cannot use simplified arguments and yield block at the same time' if (attribute || after_collect_operation || comparison_method || comparison_value) && @condition
  raise ArgumentError, 'Cannot use raw: true and content: option at the same time' if raw && content

  raw ||= false
  content ||= :itself

  condition ||= generate_simple_condition_block(attribute, after_collect_operation, comparison_method, comparison_value)

  if raw
    _options(&condition)
  else
    _options(&condition).collect { |o| o.collect(&:content).send(content) }
  end
end

#or(other) ⇒ Object Also known as: |



30
31
32
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 30

def or(other)
  OrNode.new(self, other)
end

#repeat(exactly = nil, min: nil, max: nil) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 36

def repeat(exactly = nil, min: nil, max: nil)
  raise ArgumentError, 'Only exactly value or min/max values are allowed' if exactly && (min || max)

  min = max = exactly if exactly

  if min && min > 0
    pre = self

    (min - 1).times do
      pre += self
    end
  end

  if pre && max == min
    pre
  elsif pre && max > min
    pre + RepeatNode.new(self, max - min)
  else
    RepeatNode.new(self, max)
  end
end

#sizeObject Also known as: length



95
96
97
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 95

def size
  options.size
end

#to_serie(flatten: true, &condition) ⇒ Object Also known as: s



105
106
107
108
109
110
# File 'lib/musa-dsl/generative/generative-grammar.rb', line 105

def to_serie(flatten: true, &condition)
  serie = _options(&condition).collect { |o| o.collect(&:content) }.to_serie(of_series: true).merge
  serie = serie.flatten if flatten

  serie.prototype
end