Class: Ruco::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/ruco.rb

Direct Known Subclasses

Production

Instance Method Summary collapse

Constructor Details

#initialize(type = :normal, prodset = {}) ⇒ Group

Returns a new instance of Group.



69
70
71
72
73
# File 'lib/ruco.rb', line 69

def initialize(type=:normal, prodset={})
  @type = type
  @prodset = prodset
  @stuff = []
end

Instance Method Details

#convert_thing(thing) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruco.rb', line 79

def convert_thing(thing)
  if thing.is_a? String
    return LitString.new(thing, @prodset)
  end

  if thing.is_a? Identifier or thing.is_a? Variation

    thing.prodset = @prodset

    if !@prodset[thing.name]
      @prodset[thing.name] = {count: 0, type: :id}
    end
    @prodset[thing.name][:count] += 1
    @prodset[thing.name][:count] += 1 unless @type == :normal or @type == :either
  end

  if thing.is_a? Token
    if !@prodset[thing.name]
      @prodset[thing.name] = {count: 0, type: :token}
    end
    @prodset[thing.name][:count] += 1
    @prodset[thing.name][:count] += 1 unless @type == :normal or @type == :either
  end

  return thing
end

#either(*args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/ruco.rb', line 115

def either(*args)
  g = Group.new :either, @prodset

  g.instance_eval do
    args.each do |x|
      one x
    end
  end
  one g
end

#generate(indent = 0) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ruco.rb', line 171

def generate(indent=0)

  result = []

  @stuff.each do |x|
    result << "#{x.generate(indent+1)}"
  end

  openbrace = "("
  closebrace = ")"
  divider = ""

  openbrace = "[" if @type == :maybe
  closebrace = "]" if @type == :maybe

  openbrace = "{" if @type == :multiple
  closebrace = "}" if @type == :multiple


  divider = "|" if @type == :either

  ("\t"*indent) + openbrace + "\n" + result.join("#{divider}\n") + "\n" + ("\t"*indent) + closebrace
end

#group(&block) ⇒ Object



165
166
167
168
169
# File 'lib/ruco.rb', line 165

def group(&block)
  g = Group.new :normal, @prodset
  g.instance_eval(&block)
  g
end

#many(thing, options = nil) ⇒ Object



135
136
137
138
# File 'lib/ruco.rb', line 135

def many(thing, options=nil)
  one thing
  maybemany thing, options
end

#maybe(thing) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/ruco.rb', line 126

def maybe(thing)
  g = Group.new :maybe, @prodset
  g.instance_eval do
    one thing
  end
  g.multiply
  one g
end

#maybemany(thing, options = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ruco.rb', line 140

def maybemany(thing, options=nil)
  g = Group.new :multiple, @prodset
  g.instance_eval do
    if options[:separator].is_a? String
      one options[:separator]
    else
      puts "Separator needs to be a simple string"
    end if options 
    one thing
  end
  g.multiply
  one g
end

#multiplyObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/ruco.rb', line 154

def multiply
  @stuff.each do |x|
    if x.is_a? Group
      x.multiply
    elsif x.is_a? LitString
    else
      @prodset[x.name][:count] += 1
    end
  end
end

#one(thing) ⇒ Object



110
111
112
113
# File 'lib/ruco.rb', line 110

def one(thing)
  thing = convert_thing(thing)
  @stuff << thing
end

#syncObject



106
107
108
# File 'lib/ruco.rb', line 106

def sync
  @stuff << Sync.new
end