Class: KPeg::RuleReference

Inherits:
Operator show all
Defined in:
lib/kpeg/grammar.rb

Instance Attribute Summary collapse

Attributes inherited from Operator

#action

Instance Method Summary collapse

Methods inherited from Operator

#detect_tags, #inspect_type, #prune_values, #set_action, #|

Constructor Details

#initialize(name, grammar = nil, args = nil) ⇒ RuleReference

Returns a new instance of RuleReference.



389
390
391
392
393
394
# File 'lib/kpeg/grammar.rb', line 389

def initialize(name, grammar=nil, args=nil)
  super()
  @rule_name = name
  @grammar = grammar
  @arguments = args
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



396
397
398
# File 'lib/kpeg/grammar.rb', line 396

def arguments
  @arguments
end

#rule_nameObject (readonly)

Returns the value of attribute rule_name.



396
397
398
# File 'lib/kpeg/grammar.rb', line 396

def rule_name
  @rule_name
end

Instance Method Details

#==(obj) ⇒ Object



412
413
414
415
416
417
418
419
# File 'lib/kpeg/grammar.rb', line 412

def ==(obj)
  case obj
  when RuleReference
    @rule_name == obj.rule_name and @arguments == obj.arguments
  else
    super
  end
end

#inspectObject



421
422
423
424
425
426
427
428
# File 'lib/kpeg/grammar.rb', line 421

def inspect
  if @arguments
    body = "#{@rule_name} #{@arguments}"
  else
    body = @rule_name
  end
  inspect_type "ref", body
end

#match(x) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/kpeg/grammar.rb', line 398

def match(x)
  if @grammar and @grammar != x.grammar
    x.switch_grammar(@grammar) do
      rule = @grammar.find(@rule_name)
      raise "Unknown rule: '#{@rule_name}'" unless rule
      x.apply rule
    end
  else
    rule = x.grammar.find(@rule_name)
    raise "Unknown rule: '#{@rule_name}'" unless rule
    x.apply rule
  end
end