ruby_protobuf 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +6 -1
- data/Manifest.txt +23 -2
- data/bin/rprotoc +5 -5
- data/examples/addressbook.proto +24 -0
- data/examples/addressbook.rb +30 -0
- data/examples/reading_a_message.rb +32 -0
- data/examples/writing_a_message.rb +46 -0
- data/lib/protobuf/compiler/compiler.rb +38 -74
- data/lib/protobuf/compiler/compiler_old.rb +123 -0
- data/lib/protobuf/compiler/nodes.rb +208 -0
- data/lib/protobuf/compiler/proto.y +198 -0
- data/lib/protobuf/compiler/proto2.ebnf +79 -0
- data/lib/protobuf/compiler/proto_parser.rb +1259 -0
- data/lib/protobuf/compiler/template/rpc_bin.erb +4 -0
- data/lib/protobuf/compiler/template/rpc_client.erb +18 -0
- data/lib/protobuf/compiler/template/rpc_service.erb +25 -0
- data/lib/protobuf/compiler/visitors.rb +132 -0
- data/lib/protobuf/message/decoder.rb +8 -3
- data/lib/protobuf/message/enum.rb +4 -0
- data/lib/protobuf/message/field.rb +31 -5
- data/lib/protobuf/message/message.rb +130 -14
- data/lib/protobuf/rpc/client.rb +19 -0
- data/lib/protobuf/rpc/handler.rb +17 -0
- data/lib/protobuf/rpc/server.rb +39 -0
- data/lib/ruby_protobuf.rb +1 -20
- data/test/addressbook.proto +6 -0
- data/test/addressbook.rb +17 -14
- data/test/addressbook_base.proto +26 -0
- data/test/addressbook_base.rb +62 -0
- data/test/addressbook_ext.proto +6 -0
- data/test/addressbook_ext.rb +12 -0
- data/test/rpc.proto +6 -0
- data/test/test_addressbook.rb +3 -2
- data/test/test_compiler.rb +98 -3
- data/test/test_extension.rb +40 -0
- data/test/test_standard_message.rb +83 -0
- data/test/test_types.rb +3 -3
- metadata +31 -8
- data/lib/protobuf/compiler/parser.y +0 -138
- data/test/data/data2.bin +0 -3
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'protobuf/message/message'
|
2
|
+
require 'protobuf/message/enum'
|
3
|
+
require 'protobuf/message/service'
|
4
|
+
require 'protobuf/message/extend'
|
5
|
+
|
6
|
+
module TutorialExt
|
7
|
+
|
8
|
+
class Person < ::Protobuf::Message
|
9
|
+
required :string, :name, 1
|
10
|
+
required :int32, :id, 2
|
11
|
+
optional :string, :email, 3
|
12
|
+
|
13
|
+
class PhoneType < ::Protobuf::Enum
|
14
|
+
MOBILE = 0
|
15
|
+
HOME = 1
|
16
|
+
WORK = 2
|
17
|
+
end
|
18
|
+
|
19
|
+
class PhoneNumber < ::Protobuf::Message
|
20
|
+
required :string, :number, 1
|
21
|
+
optional :PhoneType, :type, 2, {:default => :HOME}
|
22
|
+
end
|
23
|
+
|
24
|
+
repeated :PhoneNumber, :phone, 4
|
25
|
+
|
26
|
+
extensions 100..200
|
27
|
+
end
|
28
|
+
|
29
|
+
# see: addressbool_ext.rb
|
30
|
+
#class Person < ::Protobuf::Message
|
31
|
+
# optional :int32, :age, 100, :extension => true
|
32
|
+
#end
|
33
|
+
|
34
|
+
class AddressBook < ::Protobuf::Message
|
35
|
+
repeated :Person, :person, 1
|
36
|
+
end
|
37
|
+
|
38
|
+
#class SearchService < Protobuf::Service
|
39
|
+
# rpc :Search => :SearchRequest, :returns => :SearchResponse
|
40
|
+
#end
|
41
|
+
|
42
|
+
#Protobuf::OPTIONS[:optimize_for] = :SPEED
|
43
|
+
#Protobuf::OPTIONS[:java_package] = :'com.example.foo'
|
44
|
+
end
|
45
|
+
|
46
|
+
=begin
|
47
|
+
tutorial = Object.const_set :Tutorial, Module.new
|
48
|
+
person = tutorial.const_set :Person, Class.new(Protobuf::Message)
|
49
|
+
person.required :string, :name, 1
|
50
|
+
person.required :int32, :id, 2
|
51
|
+
person.optional :string, :email, 3
|
52
|
+
phone_type = person.const_set :PhoneType, Class.new(Protobuf::Enum)
|
53
|
+
phone_type.const_set :MOBILE, 0
|
54
|
+
phone_type.const_set :HOME, 1
|
55
|
+
phone_type.const_set :WORK, 2
|
56
|
+
phone_number = person.const_set :PhoneNumber, Class.new(Protobuf::Message)
|
57
|
+
phone_number.required :string, :number, 1
|
58
|
+
phone_number.optional :PhoneType, :type, 2, {:default => :HOME}
|
59
|
+
person.repeated :PhoneNumber, :phone, 4
|
60
|
+
address_book = tutorial.const_set :AddressBook, Class.new(Protobuf::Message)
|
61
|
+
address_book.repeated :Person, :person, 1
|
62
|
+
=end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'protobuf/message/message'
|
2
|
+
require 'protobuf/message/enum'
|
3
|
+
require 'protobuf/message/service'
|
4
|
+
require 'protobuf/message/extend'
|
5
|
+
|
6
|
+
require 'addressbook_base'
|
7
|
+
|
8
|
+
module TutorialExt
|
9
|
+
class Person < ::Protobuf::Message
|
10
|
+
optional :int32, :age, 100, :extension => true
|
11
|
+
end
|
12
|
+
end
|
data/test/rpc.proto
ADDED
data/test/test_addressbook.rb
CHANGED
@@ -19,9 +19,10 @@ class AddressbookTest < Test::Unit::TestCase
|
|
19
19
|
|
20
20
|
def test_initial_value
|
21
21
|
person = Tutorial::Person.new
|
22
|
-
|
23
|
-
|
22
|
+
assert_nil person.name
|
23
|
+
assert_nil person.id
|
24
24
|
assert_equal [], person.phone
|
25
|
+
assert_equal '', person.email
|
25
26
|
end
|
26
27
|
|
27
28
|
def test_repeatable
|
data/test/test_compiler.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'test/unit'
|
2
|
+
#require 'protobuf/compiler/compiler_old'
|
2
3
|
require 'protobuf/compiler/compiler'
|
3
4
|
|
4
5
|
class CompilerTest < Test::Unit::TestCase
|
5
|
-
def
|
6
|
-
assert_equal <<-eos.strip, Protobuf::Compiler.
|
6
|
+
def test_create_message
|
7
|
+
assert_equal <<-eos.gsub(/^\s*\n/, '').strip, Protobuf::Compiler.new.create_message('test/addressbook.proto', '.', '.', false).gsub(/^\s*\n/, '').strip
|
7
8
|
require 'protobuf/message/message'
|
8
9
|
require 'protobuf/message/enum'
|
9
10
|
require 'protobuf/message/service'
|
@@ -24,10 +25,16 @@ module Tutorial
|
|
24
25
|
|
25
26
|
class PhoneNumber < ::Protobuf::Message
|
26
27
|
required :string, :number, 1
|
27
|
-
optional :PhoneType, :type, 2,
|
28
|
+
optional :PhoneType, :type, 2, :default => :HOME
|
28
29
|
end
|
29
30
|
|
30
31
|
repeated :PhoneNumber, :phone, 4
|
32
|
+
|
33
|
+
extensions 100..200
|
34
|
+
end
|
35
|
+
|
36
|
+
class Person < ::Protobuf::Message
|
37
|
+
optional :int32, :age, 100, :extension => true
|
31
38
|
end
|
32
39
|
|
33
40
|
class AddressBook < ::Protobuf::Message
|
@@ -36,4 +43,92 @@ module Tutorial
|
|
36
43
|
end
|
37
44
|
eos
|
38
45
|
end
|
46
|
+
|
47
|
+
def test_create_rpc
|
48
|
+
file_contents = Protobuf::Compiler.new.create_rpc('test/rpc.proto', '.', '.', false)
|
49
|
+
|
50
|
+
assert_source <<-eos, file_contents['./address_book_service.rb']
|
51
|
+
require 'protobuf/rpc/server'
|
52
|
+
require 'protobuf/rpc/handler'
|
53
|
+
require 'test/rpc'
|
54
|
+
|
55
|
+
class Tutorial::SearchHandler < Protobuf::Rpc::Handler
|
56
|
+
request Tutorial::Person
|
57
|
+
response Tutorial::AddressBook
|
58
|
+
|
59
|
+
def self.process_request(request, response)
|
60
|
+
# TODO: edit this method
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Tutorial::AddHandler < Protobuf::Rpc::Handler
|
65
|
+
request Tutorial::Person
|
66
|
+
response Tutorial::Person
|
67
|
+
|
68
|
+
def self.process_request(request, response)
|
69
|
+
# TODO: edit this method
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Tutorial::AddressBookService < Protobuf::Rpc::Server
|
74
|
+
def setup_handlers
|
75
|
+
@handlers = {
|
76
|
+
:search => Tutorial::SearchHandler,
|
77
|
+
:add => Tutorial::AddHandler,
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
eos
|
82
|
+
|
83
|
+
assert_source <<-eos, file_contents['./start_address_book_service']
|
84
|
+
#!/usr/bin/ruby
|
85
|
+
require 'address_book_service'
|
86
|
+
|
87
|
+
Tutorial::AddressBookService.new(:port => 9999).start
|
88
|
+
eos
|
89
|
+
|
90
|
+
assert_source <<-eos, file_contents['./client_search.rb']
|
91
|
+
#!/usr/bin/ruby
|
92
|
+
require 'protobuf/rpc/client'
|
93
|
+
require 'test/rpc'
|
94
|
+
|
95
|
+
# build request
|
96
|
+
request = Tutorial::Person.new
|
97
|
+
# TODO: setup a request
|
98
|
+
raise StandardError.new('setup a request')
|
99
|
+
|
100
|
+
# create blunk response
|
101
|
+
response = Tutorial::AddressBook.new
|
102
|
+
|
103
|
+
# execute rpc
|
104
|
+
Protobuf::Rpc::Client.new('localhost', 9999).call :search, request, response
|
105
|
+
|
106
|
+
# show response
|
107
|
+
puts response
|
108
|
+
eos
|
109
|
+
|
110
|
+
assert_source <<-eos, file_contents['./client_add.rb']
|
111
|
+
#!/usr/bin/ruby
|
112
|
+
require 'protobuf/rpc/client'
|
113
|
+
require 'test/rpc'
|
114
|
+
|
115
|
+
# build request
|
116
|
+
request = Tutorial::Person.new
|
117
|
+
# TODO: setup a request
|
118
|
+
raise StandardError.new('setup a request')
|
119
|
+
|
120
|
+
# create blunk response
|
121
|
+
response = Tutorial::Person.new
|
122
|
+
|
123
|
+
# execute rpc
|
124
|
+
Protobuf::Rpc::Client.new('localhost', 9999).call :add, request, response
|
125
|
+
|
126
|
+
# show response
|
127
|
+
puts response
|
128
|
+
eos
|
129
|
+
end
|
130
|
+
|
131
|
+
def assert_source(ideal, real)
|
132
|
+
assert_equal ideal.strip.gsub(/^\s*\n/, '').gsub(/\s+\n/, "\n"), real.strip.gsub(/^\s*\n/, '').gsub(/\s+\n/, "\n")
|
133
|
+
end
|
39
134
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/addressbook_ext.rb'
|
3
|
+
|
4
|
+
class ExtensionTest < Test::Unit::TestCase
|
5
|
+
def test_accessor
|
6
|
+
assert TutorialExt::Person.extension_fields.to_a.map{|t, f| f.name}.include?(:age)
|
7
|
+
person = TutorialExt::Person.new
|
8
|
+
assert_nothing_raised {person.age = 100}
|
9
|
+
assert 100, person.age
|
10
|
+
#assert 100, person.extension.age
|
11
|
+
#assert_nothing_raised {person.extension.age = 200}
|
12
|
+
#assert 200, person.age
|
13
|
+
#assert 200, person.extension.age
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_serialize
|
17
|
+
# serialize to string
|
18
|
+
person = TutorialExt::Person.new
|
19
|
+
person.id = 1234
|
20
|
+
person.age = 70
|
21
|
+
person.name = 'John Doe'
|
22
|
+
person.email = 'jdoe@example.com'
|
23
|
+
phone = TutorialExt::Person::PhoneNumber.new
|
24
|
+
phone.number = '555-4321'
|
25
|
+
phone.type = TutorialExt::Person::PhoneType::HOME
|
26
|
+
person.phone << phone
|
27
|
+
serialized_string = person.serialize_to_string
|
28
|
+
|
29
|
+
# parse the serialized string
|
30
|
+
person2 = TutorialExt::Person.new
|
31
|
+
person2.parse_from_string serialized_string
|
32
|
+
assert_equal 1234, person2.id
|
33
|
+
assert_equal 70, person2.age
|
34
|
+
assert_equal 'John Doe', person2.name
|
35
|
+
assert_equal 'jdoe@example.com', person2.email
|
36
|
+
assert_equal 1, person2.phone.size
|
37
|
+
assert_equal '555-4321', person2.phone[0].number
|
38
|
+
assert_equal TutorialExt::Person::PhoneType::HOME, person2.phone[0].type
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'protobuf/message/message'
|
3
|
+
require 'protobuf/message/enum'
|
4
|
+
require 'test/addressbook'
|
5
|
+
|
6
|
+
class StandardMessageTest < Test::Unit::TestCase
|
7
|
+
def test_initialized
|
8
|
+
person = Tutorial::Person.new
|
9
|
+
assert !person.initialized?
|
10
|
+
person.name = 'name'
|
11
|
+
assert !person.initialized?
|
12
|
+
person.id = 12
|
13
|
+
assert person.initialized?
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_clear
|
17
|
+
person = Tutorial::Person.new
|
18
|
+
person.name = 'name'
|
19
|
+
person.id = 1234
|
20
|
+
person.email = 'abc@cde.fgh'
|
21
|
+
person.phone << Tutorial::Person::PhoneNumber.new
|
22
|
+
person.clear!
|
23
|
+
|
24
|
+
assert_nil person.name
|
25
|
+
assert_nil person.id
|
26
|
+
assert_equal '', person.email
|
27
|
+
assert_equal 0, person.phone.size
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_dup
|
31
|
+
person = Tutorial::Person.new
|
32
|
+
person.name = 'name'
|
33
|
+
person.id = 1234
|
34
|
+
person.email = 'abc@cde.fgh'
|
35
|
+
person.phone << Tutorial::Person::PhoneNumber.new
|
36
|
+
person.phone.last.number = '123-456'
|
37
|
+
person.phone.last.type = Tutorial::Person::PhoneType::MOBILE
|
38
|
+
person.phone << Tutorial::Person::PhoneNumber.new
|
39
|
+
person.phone.last.number = '456-123'
|
40
|
+
person.phone.last.type = Tutorial::Person::PhoneType::WORK
|
41
|
+
|
42
|
+
person2 = person.dup
|
43
|
+
assert_not_equal person, person2
|
44
|
+
assert_equal person.name, person2.name
|
45
|
+
assert_equal person.id, person2.id
|
46
|
+
assert_equal person.email, person2.email
|
47
|
+
assert_equal person.phone.size, person2.phone.size
|
48
|
+
assert_not_equal person.phone.first, person2.phone.first
|
49
|
+
assert_equal person.phone.first.number, person2.phone.first.number
|
50
|
+
assert_equal person.phone.first.type, person2.phone.first.type
|
51
|
+
assert_not_equal person.phone.last, person2.phone.last
|
52
|
+
assert_equal person.phone.last.number, person2.phone.last.number
|
53
|
+
assert_equal person.phone.last.type, person2.phone.last.type
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_to_s
|
57
|
+
person = Tutorial::Person.new
|
58
|
+
person.name = 'name'
|
59
|
+
person.id = 1234
|
60
|
+
person.email = 'abc@cde.fgh'
|
61
|
+
person.phone << Tutorial::Person::PhoneNumber.new
|
62
|
+
person.phone.last.number = '123-456'
|
63
|
+
person.phone.last.type = Tutorial::Person::PhoneType::MOBILE
|
64
|
+
person.phone << Tutorial::Person::PhoneNumber.new
|
65
|
+
person.phone.last.number = '456-123'
|
66
|
+
person.phone.last.type = Tutorial::Person::PhoneType::WORK
|
67
|
+
|
68
|
+
assert_equal <<-eos, person.to_s
|
69
|
+
name: "name"
|
70
|
+
id: 1234
|
71
|
+
email: "abc@cde.fgh"
|
72
|
+
phone {
|
73
|
+
number: "123-456"
|
74
|
+
type: MOBILE
|
75
|
+
}
|
76
|
+
phone {
|
77
|
+
number: "456-123"
|
78
|
+
type: WORK
|
79
|
+
}
|
80
|
+
eos
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
data/test/test_types.rb
CHANGED
@@ -31,7 +31,7 @@ class TypesTest < Test::Unit::TestCase
|
|
31
31
|
assert_equal(-10, types2.type8)
|
32
32
|
assert_equal 10000, types2.type9
|
33
33
|
assert_equal 100000, types2.type10
|
34
|
-
|
34
|
+
assert !types2.type11
|
35
35
|
assert_equal 'hello all types', types2.type12
|
36
36
|
#types2.type13
|
37
37
|
end
|
@@ -152,7 +152,7 @@ class TypesTest < Test::Unit::TestCase
|
|
152
152
|
types = Test::Types::TestTypes.new
|
153
153
|
assert_nothing_raised do types.type11 = true end
|
154
154
|
assert_nothing_raised do types.type11 = false end
|
155
|
-
|
155
|
+
assert_nothing_raised do types.type11 = nil end
|
156
156
|
assert_raise TypeError do types.type11 = 0 end
|
157
157
|
assert_raise TypeError do types.type11 = '' end
|
158
158
|
end
|
@@ -161,7 +161,7 @@ class TypesTest < Test::Unit::TestCase
|
|
161
161
|
types = Test::Types::TestTypes.new
|
162
162
|
assert_nothing_raised do types.type12 = '' end
|
163
163
|
assert_nothing_raised do types.type12 = 'hello' end
|
164
|
-
|
164
|
+
assert_nothing_raised do types.type12 = nil end
|
165
165
|
assert_raise TypeError do types.type12 = 0 end
|
166
166
|
assert_raise TypeError do types.type12 = true end
|
167
167
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_protobuf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ANDO Yasushi
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-18 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -38,9 +38,21 @@ files:
|
|
38
38
|
- README.txt
|
39
39
|
- Rakefile
|
40
40
|
- bin/rprotoc
|
41
|
+
- examples/addressbook.proto
|
42
|
+
- examples/addressbook.rb
|
43
|
+
- examples/reading_a_message.rb
|
44
|
+
- examples/writing_a_message.rb
|
41
45
|
- lib/protobuf/common/wire_type.rb
|
42
46
|
- lib/protobuf/compiler/compiler.rb
|
43
|
-
- lib/protobuf/compiler/
|
47
|
+
- lib/protobuf/compiler/compiler_old.rb
|
48
|
+
- lib/protobuf/compiler/nodes.rb
|
49
|
+
- lib/protobuf/compiler/proto.y
|
50
|
+
- lib/protobuf/compiler/proto2.ebnf
|
51
|
+
- lib/protobuf/compiler/proto_parser.rb
|
52
|
+
- lib/protobuf/compiler/template/rpc_bin.erb
|
53
|
+
- lib/protobuf/compiler/template/rpc_client.erb
|
54
|
+
- lib/protobuf/compiler/template/rpc_service.erb
|
55
|
+
- lib/protobuf/compiler/visitors.rb
|
44
56
|
- lib/protobuf/descriptor/descriptor.proto
|
45
57
|
- lib/protobuf/descriptor/descriptor.rb
|
46
58
|
- lib/protobuf/descriptor/descriptor_builder.rb
|
@@ -55,22 +67,31 @@ files:
|
|
55
67
|
- lib/protobuf/message/field.rb
|
56
68
|
- lib/protobuf/message/message.rb
|
57
69
|
- lib/protobuf/message/service.rb
|
70
|
+
- lib/protobuf/rpc/client.rb
|
71
|
+
- lib/protobuf/rpc/handler.rb
|
72
|
+
- lib/protobuf/rpc/server.rb
|
58
73
|
- lib/ruby_protobuf.rb
|
59
74
|
- test/addressbook.proto
|
60
75
|
- test/addressbook.rb
|
76
|
+
- test/addressbook_base.proto
|
77
|
+
- test/addressbook_base.rb
|
78
|
+
- test/addressbook_ext.proto
|
79
|
+
- test/addressbook_ext.rb
|
61
80
|
- test/check_unbuild.rb
|
62
81
|
- test/data/data.bin
|
63
|
-
- test/data/data2.bin
|
64
82
|
- test/data/data_source.py
|
65
83
|
- test/data/types.bin
|
66
84
|
- test/data/types_source.py
|
85
|
+
- test/rpc.proto
|
67
86
|
- test/test_addressbook.rb
|
68
87
|
- test/test_compiler.rb
|
69
88
|
- test/test_descriptor.rb
|
89
|
+
- test/test_extension.rb
|
70
90
|
- test/test_message.rb
|
71
91
|
- test/test_parse.rb
|
72
92
|
- test/test_ruby_protobuf.rb
|
73
93
|
- test/test_serialize.rb
|
94
|
+
- test/test_standard_message.rb
|
74
95
|
- test/test_types.rb
|
75
96
|
- test/types.proto
|
76
97
|
- test/types.rb
|
@@ -102,11 +123,13 @@ signing_key:
|
|
102
123
|
specification_version: 2
|
103
124
|
summary: Protocol Buffers for Ruby
|
104
125
|
test_files:
|
105
|
-
- test/
|
106
|
-
- test/test_addressbook.rb
|
126
|
+
- test/test_descriptor.rb
|
107
127
|
- test/test_message.rb
|
128
|
+
- test/test_ruby_protobuf.rb
|
129
|
+
- test/test_extension.rb
|
108
130
|
- test/test_parse.rb
|
109
|
-
- test/test_types.rb
|
110
131
|
- test/test_serialize.rb
|
132
|
+
- test/test_types.rb
|
111
133
|
- test/test_compiler.rb
|
112
|
-
- test/
|
134
|
+
- test/test_addressbook.rb
|
135
|
+
- test/test_standard_message.rb
|
@@ -1,138 +0,0 @@
|
|
1
|
-
class Protobuf::Parser
|
2
|
-
rule
|
3
|
-
stmt_list : stmt
|
4
|
-
| stmt_list stmt
|
5
|
-
|
6
|
-
stmt : package_stmt
|
7
|
-
| import_stmt
|
8
|
-
| option_stmt
|
9
|
-
| message_def
|
10
|
-
| extend_def
|
11
|
-
| enum_def
|
12
|
-
| service_def
|
13
|
-
|
14
|
-
package_stmt : PACKAGE fqcn_package ';'
|
15
|
-
|
16
|
-
fqcn_package : ident
|
17
|
-
| fqcn_package '.' ident
|
18
|
-
|
19
|
-
import_stmt : IMPORT STRING ';'
|
20
|
-
|
21
|
-
option_stmt : OPTION ident '=' STRING ';'
|
22
|
-
| OPTION ident '=' ident ';'
|
23
|
-
|
24
|
-
message_def : MESSAGE ident '{' message_item_list '}'
|
25
|
-
|
26
|
-
message_item_list : message_item
|
27
|
-
| message_item_list message_item
|
28
|
-
|
29
|
-
message_item : RULE type ident '=' INTEGER ';'
|
30
|
-
| RULE type ident '=' INTEGER '[' DEFAULT '=' ident ']' ';'
|
31
|
-
| RULE type ident '=' INTEGER '[' DEFAULT '=' number ']' ';'
|
32
|
-
| option_stmt
|
33
|
-
| extend_stmt
|
34
|
-
| message_def
|
35
|
-
| extend_def
|
36
|
-
| enum_def
|
37
|
-
|
38
|
-
type : TYPE | IDENT
|
39
|
-
|
40
|
-
number : INTEGER | FLOAT
|
41
|
-
|
42
|
-
extend_stmt | EXTENSIONS INTEGER TO INTEGER ';'
|
43
|
-
|
44
|
-
extend_def : EXTEND ident '{' message_item_list '}'
|
45
|
-
|
46
|
-
enum_def : ENUM ident '{' enum_item_list '}'
|
47
|
-
|
48
|
-
enum_item_list : enum_item
|
49
|
-
| enum_item_list enum_item
|
50
|
-
|
51
|
-
enum_item : ident '=' INTEGER ';'
|
52
|
-
|
53
|
-
service_def : SERVICE ident '{' RPC ident '(' ident ')' RETURNS '(' ident ')' '}' ';'
|
54
|
-
|
55
|
-
ident : PACKAGE | IDENT | MESSAGE | RULE | TYPE | EXTENSIONS | EXTEND | ENUM | SERVICE | RPC | RETURNS | TO | DEFAULT
|
56
|
-
|
57
|
-
---- inner
|
58
|
-
|
59
|
-
def initialize
|
60
|
-
@indent = 0
|
61
|
-
end
|
62
|
-
|
63
|
-
RESERVED = {
|
64
|
-
'package' => :PACKAGE,
|
65
|
-
'message' => :MESSAGE,
|
66
|
-
'extensions' => :EXTENSIONS,
|
67
|
-
'extend' => :EXTEND,
|
68
|
-
'enum' => :ENUM,
|
69
|
-
'service' => :SERVICE,
|
70
|
-
'rpc' => :RPC,
|
71
|
-
'returns' => :RETURNS,
|
72
|
-
'to' => :TO,
|
73
|
-
'default' => :DEFAULT,
|
74
|
-
'required' => :RULE,
|
75
|
-
'optional' => :RULE,
|
76
|
-
'repeated' => :RULE,
|
77
|
-
'double' => :TYPE,
|
78
|
-
'float' => :TYPE,
|
79
|
-
'int32' => :TYPE,
|
80
|
-
'int64' => :TYPE,
|
81
|
-
'uint32' => :TYPE,
|
82
|
-
'uint64' => :TYPE,
|
83
|
-
'sint32' => :TYPE,
|
84
|
-
'sint64' => :TYPE,
|
85
|
-
'fixed32' => :TYPE,
|
86
|
-
'fixed64' => :TYPE,
|
87
|
-
'sfixed32' => :TYPE,
|
88
|
-
'sfixed64' => :TYPE,
|
89
|
-
'bool' => :TYPE,
|
90
|
-
'string' => :TYPE,
|
91
|
-
'bytes' => :TYPE,
|
92
|
-
}
|
93
|
-
|
94
|
-
def parse(f)
|
95
|
-
@q = []
|
96
|
-
lineno = 1
|
97
|
-
f.each do |line|
|
98
|
-
line.strip!
|
99
|
-
until line.empty? do
|
100
|
-
case line
|
101
|
-
when /\A\s+/, /\A\/\/.*/
|
102
|
-
;
|
103
|
-
when /\A[a-zA-Z_]\w*/
|
104
|
-
word = $&
|
105
|
-
@q.push [RESERVED[word] || :IDENT, [lineno, word.to_sym]]
|
106
|
-
when /\A\d+\.\d+/
|
107
|
-
@q.push [:FLOAT, [lineno, $&.to_f]]
|
108
|
-
when /\A\d+/
|
109
|
-
@q.push [:INTEGER, [lineno, $&.to_i]]
|
110
|
-
when /\A"(?:[^"\\]+|\\.)*"/, /\A'(?:[^'\\]+|\\.)*'/
|
111
|
-
@q.push [:STRING, [lineno, eval($&)]]
|
112
|
-
when /\A./
|
113
|
-
@q.push [$&, [lineno, $&]]
|
114
|
-
else
|
115
|
-
raise RuntimeError, 'must not happen'
|
116
|
-
end
|
117
|
-
line = $'
|
118
|
-
end
|
119
|
-
lineno += 1
|
120
|
-
end
|
121
|
-
@q.push [false, '$']
|
122
|
-
|
123
|
-
do_parse
|
124
|
-
end
|
125
|
-
|
126
|
-
def next_token
|
127
|
-
@q.shift
|
128
|
-
end
|
129
|
-
|
130
|
-
def on_error(t, v, values)
|
131
|
-
raise Racc::ParseError, "syntax error on #{v[1].inspect} at line.#{v[0]}"
|
132
|
-
end
|
133
|
-
|
134
|
-
---- footer
|
135
|
-
|
136
|
-
File.open(ARGV.shift, 'r') do |f|
|
137
|
-
Protobuf::Parser.new.parse f
|
138
|
-
end
|
data/test/data/data2.bin
DELETED