Class: Panini::Nonterminal

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Nonterminal

Initialize a nonterminal. Optionally specify a name.



8
9
10
11
# File 'lib/nonterminal.rb', line 8

def initialize(name=nil)
  @productions = []
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/nonterminal.rb', line 5

def name
  @name
end

Instance Method Details

#add_production(production) ⇒ Object

Add a production to the nonterminal. It must be an array, but the array can contain any type of Ruby object.

nonterminal.add_production([1, 'a', lambda { ...} ])

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/nonterminal.rb', line 18

def add_production(production)
  raise ArgumentError, "The production must be an Array." unless production.class == Array
  @productions << production.dup
  nil
end

#productionsObject

The productions for the nonterminal.



25
26
27
# File 'lib/nonterminal.rb', line 25

def productions
  @productions.dup
end

#to_sObject



29
30
31
# File 'lib/nonterminal.rb', line 29

def to_s
  name.nil? ? super : @name
end