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.



210
211
212
213
214
# File 'lib/racc/grammar.rb', line 210

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



233
234
235
236
237
238
239
240
241
242
# File 'lib/racc/grammar.rb', line 233

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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/racc/grammar.rb', line 244

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)


264
265
266
# File 'lib/racc/grammar.rb', line 264

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

#_delayed_add(rule) ⇒ Object



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

def _delayed_add(rule)
  @delayed.push rule
end

#_intern(x) ⇒ Object



323
324
325
326
327
328
329
330
331
332
# File 'lib/racc/grammar.rb', line 323

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: _



284
285
286
287
288
# File 'lib/racc/grammar.rb', line 284

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

#flush_delayedObject



268
269
270
271
272
273
274
# File 'lib/racc/grammar.rb', line 268

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

#grammarObject



216
217
218
219
220
221
222
223
224
225
# File 'lib/racc/grammar.rb', line 216

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



298
299
300
301
302
303
# File 'lib/racc/grammar.rb', line 298

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



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

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



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

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

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



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

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

#precedence_table(&block) ⇒ Object



227
228
229
230
231
# File 'lib/racc/grammar.rb', line 227

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



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

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

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



316
317
318
319
320
321
# File 'lib/racc/grammar.rb', line 316

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



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

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