Class: Racc::Grammar::DefinitionEnv

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

Instance Method Summary collapse

Constructor Details

#initializeDefinitionEnv

Returns a new instance of DefinitionEnv.


205
206
207
208
209
# File 'lib/racc/grammar.rb', line 205

def initialize
  @grammar = Grammar.new
  @seqs = Hash.new(0)
  @delayed = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args, &block) ⇒ Object


228
229
230
231
232
233
234
235
236
237
# File 'lib/racc/grammar.rb', line 228

def method_missing(mid, *args, &block)
  unless mid.to_s[-1,1] == '='
    super   # raises NoMethodError
  end
  target = @grammar.intern(mid.to_s.chop.intern)
  unless args.size == 1
    raise ArgumentError, "too many arguments for #{mid} (#{args.size} for 1)"
  end
  _add target, args.first
end

Instance Method Details

#_add(target, x) ⇒ Object


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/racc/grammar.rb', line 239

def _add(target, x)
  case x
  when Sym
    @delayed.each do |rule|
      rule.replace x, target if rule.target == x
    end
    @grammar.symboltable.delete x
  else
    x.each_rule do |r|
      r.target = target
      @grammar.add r
    end
  end
  flush_delayed
end

#_added?(sym) ⇒ Boolean

Returns:

  • (Boolean)

259
260
261
# File 'lib/racc/grammar.rb', line 259

def _added?(sym)
  @grammar.added?(sym) or @delayed.detect {|r| r.target == sym }
end

#_delayed_add(rule) ⇒ Object


255
256
257
# File 'lib/racc/grammar.rb', line 255

def _delayed_add(rule)
  @delayed.push rule
end

#_intern(x) ⇒ Object


318
319
320
321
322
323
324
325
326
327
# File 'lib/racc/grammar.rb', line 318

def _intern(x)
  case x
  when Symbol, String
    @grammar.intern(x)
  when Racc::Sym
    x
  else
    raise TypeError, "wrong type #{x.class} (expected Symbol/String/Racc::Sym)"
  end
end

#action(&block) ⇒ Object Also known as: _


279
280
281
282
283
# File 'lib/racc/grammar.rb', line 279

def action(&block)
  id = "@#{@seqs["action"] += 1}".intern
  _delayed_add Rule.new(@grammar.intern(id), [], UserAction.proc(block))
  id
end

#flush_delayedObject


263
264
265
266
267
268
269
# File 'lib/racc/grammar.rb', line 263

def flush_delayed
  return if @delayed.empty?
  @delayed.each do |rule|
    @grammar.add rule
  end
  @delayed.clear
end

#grammarObject


211
212
213
214
215
216
217
218
219
220
# File 'lib/racc/grammar.rb', line 211

def grammar
  flush_delayed
  @grammar.each do |rule|
    if rule.specified_prec
      rule.specified_prec = @grammar.intern(rule.specified_prec)
    end
  end
  @grammar.init
  @grammar
end

#many(sym, &block) ⇒ Object


293
294
295
296
297
298
# File 'lib/racc/grammar.rb', line 293

def many(sym, &block)
  _defmetasyntax("many", _intern(sym), block) {|target|
      seq() { [] }\
    | seq(target, sym) {|list, x| list.push x; list }
  }
end

#many1(sym, &block) ⇒ Object


300
301
302
303
304
305
# File 'lib/racc/grammar.rb', line 300

def many1(sym, &block)
  _defmetasyntax("many1", _intern(sym), block) {|target|
      seq(sym) {|x| [x] }\
    | seq(target, sym) {|list, x| list.push x; list }
  }
end

#null(&block) ⇒ Object


275
276
277
# File 'lib/racc/grammar.rb', line 275

def null(&block)
  seq(&block)
end

#option(sym, default = nil, &block) ⇒ Object


287
288
289
290
291
# File 'lib/racc/grammar.rb', line 287

def option(sym, default = nil, &block)
  _defmetasyntax("option", _intern(sym), block) {|target|
    seq() { default } | seq(sym)
  }
end

#precedence_table(&block) ⇒ Object


222
223
224
225
226
# File 'lib/racc/grammar.rb', line 222

def precedence_table(&block)
  env = PrecedenceDefinitionEnv.new(@grammar)
  env.instance_eval(&block)
  @grammar.end_precedence_declaration env.reverse
end

#separated_by(sep, sym, &block) ⇒ Object


307
308
309
# File 'lib/racc/grammar.rb', line 307

def separated_by(sep, sym, &block)
  option(separated_by1(sep, sym), [], &block)
end

#separated_by1(sep, sym, &block) ⇒ Object


311
312
313
314
315
316
# File 'lib/racc/grammar.rb', line 311

def separated_by1(sep, sym, &block)
  _defmetasyntax("separated_by1", _intern(sym), block) {|target|
      seq(sym) {|x| [x] }\
    | seq(target, sep, sym) {|list, _, x| list.push x; list }
  }
end

#seq(*list, &block) ⇒ Object


271
272
273
# File 'lib/racc/grammar.rb', line 271

def seq(*list, &block)
  Rule.new(nil, list.map {|x| _intern(x) }, UserAction.proc(block))
end