Module: Protomsg

Defined in:
lib/protomsg/parser.rb,
lib/protomsg/attribute.rb,
lib/protomsg/message_type.rb

Defined Under Namespace

Classes: Attribute, MessageType

Class Method Summary collapse

Class Method Details

.parse_file(path) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protomsg/parser.rb', line 2

def self.parse_file(path)
  File.open(path) do |file|
    # write the standard protomsg header
    FileUtils.cp File.join(File.dirname(__FILE__), '..', 'templates', 'protomsg.h'), 'protomsg.h'
    puts "Created protomsg.h"
    
    # extract each message type definition
    message_types = []
    data = file.read
    data.scan(/(\w+)\s*\{([^\}]+)\}/) do |message_type|
      message_types << MessageType.new(message_types.size + 1, message_type[0], message_type[1])
    end
    
    # write out each message type's header file
    message_types.each do |message_type|
      header_name = "#{message_type.name}_message.h"
      File.open(header_name, 'w') do |file|
        file.write message_type.to_header_file
      end
      puts "Created #{header_name}"
    end
  end
end