Module: Katuv::DSL

Defined in:
lib/katuv/dsl.rb

Defined Under Namespace

Classes: InvalidNodeTypeError, NonterminalInTerminalError, TerminalAlreadySetError

Instance Method Summary collapse

Instance Method Details

#_type_to_method_name(type) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/katuv/dsl.rb', line 37

def _type_to_method_name(type)
  if type.respond_to?(:name)
    type.name
  else
    type.to_s
  end.split('::').last.downcase
end

#multiple(type) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/katuv/dsl.rb', line 28

def multiple(type)
  #should store an entry in #children that is a list of all the instances
  #it sees of this type. eg, `file 'x'; file 'y' #=> children[File] = [File<@name=x>, File<@name=y>]
  define_method(_type_to_method_name(type)) do |name, opts={}, &block|
    children[type] ||= ObjectSet.new
    children[type] << type.new(name, opts.merge(parent: self), &block)
  end
end

#nonterminal(type) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/katuv/dsl.rb', line 3

def nonterminal(type)
  raise NonterminalInTerminalError if terminal?
  define_method(_type_to_method_name(type)) do |name=nil, opts={}, &block|
    if children.has_key?(type) and children[type]
      children[type].run!(&block)
    else
      children[type] = type.new(name, opts.merge(parent: self), &block)
    end
    children[type]
  end
end

#terminal(type) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/katuv/dsl.rb', line 15

def terminal(type)
  #should only allow one.
  raise InvalidNodeTypeError unless type.terminal?
  define_method(_type_to_method_name(type)) do |name=nil, opts={}, &block|
    raise TerminalAlreadySetError if children.has_key?(type)
    children[type] = type.new(name, opts.merge(parent: self), &block)
  end
end

#terminal!Object



23
24
25
# File 'lib/katuv/dsl.rb', line 23

def terminal!
  define_singleton_method(:terminal?) { true }
end

#terminal?Boolean

Returns:

  • (Boolean)


26
# File 'lib/katuv/dsl.rb', line 26

def terminal?; false end