protobuf 1.4.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +4 -0
  2. data/Rakefile +7 -7
  3. data/bin/rpc_server +2 -188
  4. data/bin/rprotoc +44 -38
  5. data/examples/addressbook.pb.rb +2 -2
  6. data/ext/Makefile +11 -0
  7. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  8. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  9. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  10. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  11. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  12. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  13. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  92. data/ext/ruby_generator/RubyGenerator.cpp +435 -0
  93. data/ext/ruby_generator/RubyGenerator.h +180 -0
  94. data/ext/ruby_generator/extconf.rb +24 -0
  95. data/lib/protobuf.rb +55 -33
  96. data/lib/protobuf/cli.rb +176 -0
  97. data/lib/protobuf/common/logger.rb +11 -11
  98. data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
  99. data/lib/protobuf/evented.rb +22 -0
  100. data/lib/protobuf/field.rb +57 -0
  101. data/lib/protobuf/field/base_field.rb +210 -0
  102. data/lib/protobuf/field/bool_field.rb +24 -0
  103. data/lib/protobuf/field/bytes_field.rb +38 -0
  104. data/lib/protobuf/field/double_field.rb +19 -0
  105. data/lib/protobuf/field/enum_field.rb +58 -0
  106. data/lib/protobuf/field/extension_fields.rb +24 -0
  107. data/lib/protobuf/field/field_array.rb +55 -0
  108. data/lib/protobuf/field/fixed32_field.rb +19 -0
  109. data/lib/protobuf/field/fixed64_field.rb +22 -0
  110. data/lib/protobuf/field/float_field.rb +29 -0
  111. data/lib/protobuf/field/int32_field.rb +10 -0
  112. data/lib/protobuf/field/int64_field.rb +10 -0
  113. data/lib/protobuf/field/integer_field.rb +19 -0
  114. data/lib/protobuf/field/message_field.rb +51 -0
  115. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  116. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  117. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  118. data/lib/protobuf/field/sint32_field.rb +10 -0
  119. data/lib/protobuf/field/sint64_field.rb +10 -0
  120. data/lib/protobuf/field/string_field.rb +14 -0
  121. data/lib/protobuf/field/uint32_field.rb +10 -0
  122. data/lib/protobuf/field/uint64_field.rb +10 -0
  123. data/lib/protobuf/field/varint_field.rb +63 -0
  124. data/lib/protobuf/message.rb +376 -0
  125. data/lib/protobuf/message/message.rb +1 -475
  126. data/lib/protobuf/rpc/buffer.rb +1 -1
  127. data/lib/protobuf/rpc/client.rb +26 -26
  128. data/lib/protobuf/rpc/connector.rb +15 -14
  129. data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
  130. data/lib/protobuf/rpc/rpc.pb.rb +39 -28
  131. data/lib/protobuf/rpc/server.rb +7 -28
  132. data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
  133. data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
  134. data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
  135. data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
  136. data/lib/protobuf/socket.rb +22 -0
  137. data/lib/protobuf/version.rb +2 -1
  138. data/lib/protobuf/zmq.rb +21 -0
  139. data/proto/rpc.pb.rb +48 -0
  140. data/protobuf.gemspec +14 -6
  141. data/spec/benchmark/tasks.rb +42 -23
  142. data/spec/functional/evented_server_spec.rb +2 -0
  143. data/spec/functional/socket_server_spec.rb +22 -26
  144. data/spec/functional/zmq_server_spec.rb +22 -26
  145. data/spec/lib/protobuf/cli_spec.rb +182 -0
  146. data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
  147. data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
  148. data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
  149. data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
  150. data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
  151. data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
  152. data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
  153. data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
  154. data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
  155. data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
  156. data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
  157. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
  158. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
  159. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
  160. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
  161. data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
  162. data/spec/proto/test.pb.rb +48 -25
  163. data/spec/spec_helper.rb +9 -15
  164. data/spec/support/all.rb +7 -0
  165. data/spec/{helper → support}/server.rb +5 -7
  166. data/spec/{helper → support}/silent_constants.rb +4 -0
  167. data/spec/support/test_app_file.rb +2 -0
  168. data/spec/{helper → support}/tolerance_matcher.rb +0 -0
  169. data/test/proto/types.pb.rb +1 -1
  170. data/test/test_addressbook.rb +1 -1
  171. data/test/test_enum_value.rb +1 -1
  172. data/test/test_standard_message.rb +1 -1
  173. metadata +243 -107
  174. data/lib/protobuf/common/util.rb +0 -59
  175. data/lib/protobuf/compiler/compiler.rb +0 -52
  176. data/lib/protobuf/compiler/nodes.rb +0 -323
  177. data/lib/protobuf/compiler/proto.y +0 -216
  178. data/lib/protobuf/compiler/proto2.ebnf +0 -79
  179. data/lib/protobuf/compiler/proto_parser.rb +0 -1425
  180. data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
  181. data/lib/protobuf/compiler/visitors.rb +0 -282
  182. data/lib/protobuf/descriptor/descriptor.proto +0 -286
  183. data/lib/protobuf/descriptor/descriptor.rb +0 -55
  184. data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
  185. data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
  186. data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
  187. data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
  188. data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
  189. data/lib/protobuf/message/field.rb +0 -684
  190. data/lib/protobuf/message/protoable.rb +0 -38
  191. data/spec/helper/all.rb +0 -7
  192. data/spec/unit/common/util_spec.rb +0 -17
  193. data/spec/unit/rpc/connector_spec.rb +0 -31
  194. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
  195. data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
  196. data/test/check_unbuild.rb +0 -30
  197. data/test/test_compiler.rb +0 -325
  198. data/test/test_descriptor.rb +0 -122
@@ -1,59 +0,0 @@
1
- module Protobuf
2
- module Util
3
- module_function
4
-
5
- # Takes a string or symbol and camelizes it:
6
- # Expects: some_long_name
7
- # Returns: SomeLongName
8
- def camelize(str)
9
- if (str.is_a? Array)
10
- str.map{|p| camelize(p.to_s) }.join('::')
11
- else
12
- str.to_s.gsub(/(?:\A|_)(\w)/) { $1.upcase }
13
- end
14
- end
15
-
16
- # Expects: SomeLongName, SOMELongName
17
- # Returns: some_long_name
18
- def underscore(str)
19
- str.to_s.gsub(/\B[A-Z](?:[a-z])/, '_\&').downcase
20
- end
21
-
22
- # Expects: SomeModule::Path
23
- # Returns: some_module/path
24
- def module_to_path(str)
25
- pkg = str.to_s.split('::')
26
- pkg.map{|e| underscore(e) }.join('/')
27
- end
28
-
29
- # Expects: PackageA.PackageB
30
- # Returns: package_a/package_b
31
- def package_to_path(str)
32
- str.to_s.split('.').map{|e| underscore(e) }.join('/')
33
- end
34
-
35
- # Takes a class constant and converts it to a string resembling a java package path
36
- # Expects: ModA::ModB::MyService
37
- # Returns: mod_a.mod_b.MyService
38
- def packagize(klass)
39
- klass = klass.to_s.split('::') unless klass.is_a? Array
40
- klass_name = klass.pop
41
- klass.map{|e| underscore(e) }.join('.') + ".#{klass_name}"
42
- end
43
-
44
- # The reverse of packagize. Takes a string resembling a java package path
45
- # and converts it into a module constant
46
- # Expects: mod_a.mod_b.MyService
47
- # Returns: ModA::ModB::MyService
48
- def moduleize(str)
49
- str = str.join('.') if str.is_a? Array
50
- str.split('.').map{|e| camelize(e) }.join('::')
51
- end
52
-
53
- def constantize(klass)
54
- constants = moduleize(klass).split('::')
55
- constants.inject(Module.const_get(constants.shift)) {|const, obj| const.const_get(obj) }
56
- end
57
-
58
- end
59
- end
@@ -1,52 +0,0 @@
1
- require 'fileutils'
2
- require 'protobuf/compiler/proto_parser'
3
- require 'protobuf/compiler/nodes'
4
- require 'protobuf/compiler/visitors'
5
-
6
- module Protobuf
7
- class Compiler
8
- def self.compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
9
- self.new.compile(proto_file, proto_dir, out_dir, file_create)
10
- end
11
-
12
- def compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
13
- create_message(proto_file, proto_dir, out_dir, file_create)
14
- create_rpc(proto_file, proto_dir, out_dir, file_create)
15
- end
16
-
17
- def create_message(proto_file, proto_dir='.', out_dir='.', file_create=true)
18
- rb_file = File.join(out_dir, File.basename(proto_file).sub(/\.[^\0]*\z/, '') + '.pb.rb')
19
- proto_path = validate_existence(proto_file, proto_dir)
20
-
21
- message_visitor = Visitor::CreateMessageVisitor.new(proto_file, proto_dir, out_dir)
22
- File.open(proto_path) do |file|
23
- message_visitor.visit(ProtoParser.new.parse(file))
24
- end
25
- message_visitor.create_files(rb_file, out_dir, file_create)
26
- end
27
-
28
- def create_rpc(proto_file, proto_dir='.', out_dir='.', file_create=true)
29
- message_file = File.join(out_dir, File.basename(proto_file).sub(/\.[^\0]*\z/, '') + '.pb.rb')
30
- proto_path = validate_existence(proto_file, proto_dir)
31
-
32
- rpc_visitor = Visitor::CreateRpcVisitor.new
33
- File.open(proto_path) do |file|
34
- rpc_visitor.visit(ProtoParser.new.parse(file))
35
- end
36
- rpc_visitor.create_files(message_file, out_dir, file_create)
37
- end
38
-
39
- def validate_existence(path, base_dir)
40
- if File.exist?(path)
41
- path
42
- else
43
- newpath = File.join(base_dir, path)
44
- if File.exist?(newpath)
45
- newpath
46
- else
47
- raise ArgumentError, "File does not exist: #{path}"
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,323 +0,0 @@
1
- require 'protobuf/common/util'
2
- require 'protobuf/descriptor/descriptor_proto'
3
-
4
- module Protobuf
5
- module Node
6
- class Base
7
- def define_in_the_file(visitor)
8
- visitor.write("defined_in __FILE__") if visitor.attach_proto?
9
- end
10
-
11
- def accept_message_visitor(visitor)
12
- end
13
-
14
- def accept_rpc_visitor(vistor)
15
- end
16
-
17
- def accept_descriptor_visitor(visitor)
18
- end
19
- end
20
-
21
- class ProtoNode < Base
22
- attr_reader :children
23
-
24
- def initialize(children)
25
- @children = children || []
26
- end
27
-
28
- def accept_message_visitor(visitor)
29
- visitor.write('### Generated by rprotoc. DO NOT EDIT!')
30
- visitor.write("### <proto file: #{visitor.proto_file}>") if visitor.attach_proto?
31
- visitor.write(visitor.commented_proto_contents) if visitor.attach_proto?
32
- visitor.write(<<-EOS)
33
- require 'protobuf/message/message'
34
- require 'protobuf/message/enum'
35
- require 'protobuf/message/extend'
36
- EOS
37
- @children.each {|child| child.accept_message_visitor(visitor) }
38
- visitor.close_ruby
39
- end
40
-
41
- def accept_rpc_visitor(visitor)
42
- @children.each {|child| child.accept_rpc_visitor(visitor) }
43
- end
44
-
45
- def accept_descriptor_visitor(visitor)
46
- descriptor = Google::Protobuf::FileDescriptorProto.new(:name => visitor.filename)
47
- visitor.file_descriptor = descriptor
48
- visitor.in_context(descriptor) do
49
- @children.each {|child| child.accept_descriptor_visitor(visitor) }
50
- end
51
- end
52
- end
53
-
54
- class ImportNode < Base
55
- def initialize(path)
56
- @path = path
57
- end
58
-
59
- def accept_message_visitor(visitor)
60
- visitor.write("require '#{visitor.required_message_from_proto(@path)}'")
61
- end
62
-
63
- def accept_descriptor_visitor(visitor)
64
- visitor.current_descriptor.dependency << @path
65
- end
66
- end
67
-
68
- class PackageNode < Base
69
- def initialize(path_list)
70
- @path_list = path_list
71
- end
72
-
73
- def accept_message_visitor(visitor)
74
- visitor.package = @path_list.dup
75
- @path_list.each do |path|
76
- visitor.write("module #{Util.camelize(path)}")
77
- visitor.increment
78
- end
79
- end
80
-
81
- def accept_rpc_visitor(visitor)
82
- visitor.package = @path_list.dup
83
- end
84
-
85
- def accept_descriptor_visitor(visitor)
86
- visitor.current_descriptor.package = @path_list.join('.')
87
- end
88
- end
89
-
90
- class OptionNode < Base
91
- def initialize(name_list, value)
92
- @name_list, @value = name_list, value
93
- end
94
-
95
- def accept_message_visitor(visitor)
96
- visitor.write("::Protobuf::OPTIONS[:#{@name_list.join('.').inspect}] = #{@value.inspect}")
97
- end
98
-
99
- def accept_descriptor_visitor(visitor)
100
- visitor.add_option(@name_list.join('.'), @value)
101
- end
102
- end
103
-
104
- class MessageNode < Base
105
- def initialize(name, children)
106
- @name, @children = name, children
107
- end
108
-
109
- def accept_message_visitor(visitor)
110
- class_name = @name.to_s
111
- class_name.gsub!(/\A[a-z]/) {|c| c.upcase}
112
- visitor.write("class #{class_name} < ::Protobuf::Message")
113
- visitor.in_context(self.class) do
114
- define_in_the_file(visitor)
115
- @children.each {|child| child.accept_message_visitor(visitor) }
116
- end
117
- visitor.write('end')
118
- end
119
-
120
- def accept_descriptor_visitor(visitor)
121
- descriptor = Google::Protobuf::DescriptorProto.new(:name => @name.to_s)
122
- visitor.descriptor = descriptor
123
- visitor.in_context(descriptor) do
124
- @children.each {|child| child.accept_descriptor_visitor(visitor) }
125
- end
126
- end
127
- end
128
-
129
- class ExtendNode < Base
130
- def initialize(name, children)
131
- @name, @children = name, children
132
- end
133
-
134
- def accept_message_visitor(visitor)
135
- name = @name.is_a?(Array) ? @name.join : name.to_s
136
- visitor.write("class #{name} < ::Protobuf::Message")
137
- visitor.in_context(self.class) do
138
- define_in_the_file(visitor)
139
- @children.each {|child| child.accept_message_visitor(visitor) }
140
- end
141
- visitor.write('end')
142
- end
143
-
144
- def accept_descriptor_visitor(visitor)
145
- # TODO: how should i handle this?
146
- end
147
- end
148
-
149
- class EnumNode < Base
150
- def initialize(name, children)
151
- @name, @children = name, children
152
- end
153
-
154
- def accept_message_visitor(visitor)
155
- visitor.write("class #{@name} < ::Protobuf::Enum")
156
- visitor.in_context(self.class) do
157
- define_in_the_file(visitor)
158
- @children.each {|child| child.accept_message_visitor(visitor) }
159
- end
160
- visitor.write('end')
161
- end
162
-
163
- def accept_descriptor_visitor(visitor)
164
- descriptor = Google::Protobuf::EnumDescriptorProto.new(:name => @name.to_s)
165
- visitor.enum_descriptor = descriptor
166
- visitor.in_context(descriptor) do
167
- @children.each {|child| child.accept_descriptor_visitor(visitor) }
168
- end
169
- end
170
- end
171
-
172
- class EnumFieldNode < Base
173
- def initialize(name, value)
174
- @name, @value = name, value
175
- end
176
-
177
- def accept_message_visitor(visitor)
178
- visitor.write("define :#{@name}, #{@value}")
179
- end
180
-
181
- def accept_descriptor_visitor(visitor)
182
- descriptor = Google::Protobuf::EnumValueDescriptorProto.new(:name => @name.to_s, :number => @value)
183
- visitor.enum_value_descriptor = descriptor
184
- end
185
- end
186
-
187
- class ServiceNode < Base
188
- def initialize(name, children)
189
- @name, @children = name, children
190
- end
191
-
192
- def accept_message_visitor(visitor)
193
- # do nothing
194
- end
195
-
196
- def accept_rpc_visitor(visitor)
197
- visitor.current_service = @name
198
- @children.each {|child| child.accept_rpc_visitor(visitor) }
199
- end
200
-
201
- def accept_descriptor_visitor(visitor)
202
- descriptor = Google::Protobuf::ServiceDescriptorProto.new(:name => @name.to_s)
203
- visitor.service_descriptor = descriptor
204
- visitor.in_context(descriptor) do
205
- @children.each {|child| child.accept_descriptor_visitor(visitor) }
206
- end
207
- end
208
- end
209
-
210
- class RpcNode < Base
211
- def initialize(name, request, response)
212
- @name, @request, @response = name, request, response
213
- end
214
-
215
- def accept_message_visitor(visitor)
216
- # do nothing
217
- end
218
-
219
- def accept_rpc_visitor(visitor)
220
- visitor.add_rpc(@name, @request, @response)
221
- end
222
-
223
- def accept_descriptor_visitor(visitor)
224
- descriptor = Google::Protobuf::MethodDescriptorProto.new(:name => @name.to_s, :input_type => @request.to_s, :output_type => @response.to_s)
225
- visitor.method_descriptor = descriptor
226
- end
227
- end
228
-
229
- class GroupNode < Base
230
- def initialize(label, name, value, children)
231
- @label, @name, @value, @children = label, name, value, children
232
- end
233
-
234
- def accept_message_visitor(visitor)
235
- raise NotImplementedError
236
- end
237
-
238
- def accept_descriptor_visitor(visitor)
239
- raise NotImplementedError
240
- end
241
- end
242
-
243
- class FieldNode < Base
244
- def initialize(label, type, name, value, opts={})
245
- @label, @type, @name, @value, @opts = label, type, name, value, opts
246
- end
247
-
248
- def accept_message_visitor(visitor)
249
- opts = @opts.empty? ? '' : ", #{@opts.map{|k, v| ":#{k} => #{v.inspect}" }.join(', ')}"
250
- if visitor.context.first == ExtendNode
251
- opts << ', :extension => true'
252
- end
253
- type = if @type.is_a?(Array)
254
- then (@type.size > 1) ? "'#{@type.map{|e| Util.camelize(e) }.join('::')}'" : @type[0]
255
- else @type
256
- end
257
- visitor.write("#{@label} :#{type}, :#{@name}, #{@value}#{opts}")
258
- end
259
-
260
- def accept_descriptor_visitor(visitor)
261
- descriptor = Google::Protobuf::FieldDescriptorProto.new(:name => @name.to_s, :number => @value)
262
- descriptor.label = Google::Protobuf::FieldDescriptorProto::Label.const_get("LABEL_#{@label.to_s.upcase}")
263
- descriptor.type = Google::Protobuf::FieldDescriptorProto::Type.const_get("TYPE_#{@type.to_s.upcase}") if predefined_type?
264
- descriptor.type_name = @type.is_a?(Array) ? @type.join : @type.to_s
265
- @opts.each do |key, val|
266
- case key.to_sym
267
- when :default then
268
- descriptor.default_value = val.to_s
269
- end
270
- end
271
- visitor.field_descriptor = descriptor
272
- end
273
-
274
- private
275
-
276
- def predefined_type?
277
- # TODO: constantize
278
- %w{double float int64 uint64 int32 fixed64 fixed32 bool string group message bytes uint32 enum sfixed32 sfixed64 sint32 sint64}.include?(@type.to_s)
279
- end
280
- end
281
-
282
- class ExtensionsNode < Base
283
- def initialize(range)
284
- @range = range
285
- end
286
-
287
- def accept_message_visitor(visitor)
288
- visitor.write("extensions #{@range.first.to_s}")
289
- end
290
-
291
- def accept_descriptor_visitor(visitor)
292
- descriptor = Google::Protobuf::DescriptorProto::ExtensionRange.new(:start => @range.first.low)
293
- case @range.first.high
294
- when NilClass then # ignore
295
- when :max then descriptor.end = 1
296
- else descriptor.end = @range.first.high
297
- end
298
- visitor.extension_range_descriptor = descriptor
299
- end
300
- end
301
-
302
- class ExtensionRangeNode < Base
303
- attr_reader :low, :high
304
-
305
- def initialize(low, high=nil)
306
- @low, @high = low, high
307
- end
308
-
309
- #def accept_message_visitor(visitor)
310
- #end
311
-
312
- def to_s
313
- if @high.nil?
314
- @low.to_s
315
- elsif @high == :max
316
- "#{@low}..::Protobuf::Extend::MAX"
317
- else
318
- "#{@low}..#{@high}"
319
- end
320
- end
321
- end
322
- end
323
- end
@@ -1,216 +0,0 @@
1
- class Protobuf::ProtoParser
2
- rule
3
- proto : proto_item
4
- { result = Protobuf::Node::ProtoNode.new(val) }
5
- | proto proto_item
6
- { result.children << val[1] if val[1]}
7
-
8
- proto_item : message
9
- | extend
10
- | enum
11
- | import
12
- | package
13
- | option
14
- | service
15
- | ';' { result = nil }
16
-
17
- import : 'import' STRING_LITERAL ';'
18
- { result = Protobuf::Node::ImportNode.new(val[1]) }
19
-
20
- package : 'package' IDENT dot_ident_list ';'
21
- { result = Protobuf::Node::PackageNode.new(val[2].unshift(val[1])) }
22
-
23
- dot_ident_list :
24
- { result = [] }
25
- | dot_ident_list '.' IDENT
26
- { result << val[2] }
27
-
28
- option : 'option' option_body ';'
29
- { result = Protobuf::Node::OptionNode.new(*val[1]) }
30
-
31
- option_body : IDENT dot_ident_list '=' constant
32
- { result = [val[1].unshift(val[0]), val[3]] }
33
-
34
- message : 'message' IDENT message_body
35
- { result = Protobuf::Node::MessageNode.new(val[1], val[2]) }
36
-
37
- extend : 'extend' user_type '{' extend_body_list '}'
38
- { result = Protobuf::Node::ExtendNode.new(val[1], val[3]) }
39
-
40
- extend_body_list :
41
- { result = [] }
42
- | extend_body_list extend_body
43
- { result << val[1] if val[1] }
44
-
45
- extend_body : field
46
- | group
47
- | ';' { result = nil }
48
-
49
- enum : 'enum' IDENT '{' enum_body_list '}'
50
- { result = Protobuf::Node::EnumNode.new(val[1], val[3]) }
51
-
52
- enum_body_list :
53
- { result = [] }
54
- | enum_body_list enum_body
55
- { result << val[1] if val[1] }
56
-
57
- enum_body : option
58
- | enum_field
59
- | ';' { result = nil }
60
-
61
- enum_field : IDENT '=' integer_literal ';'
62
- { result = Protobuf::Node::EnumFieldNode.new(val[0], val[2]) }
63
-
64
- service : 'service' IDENT '{' service_body_list '}'
65
- { result = Protobuf::Node::ServiceNode.new(val[1], val[3]) }
66
-
67
- service_body_list :
68
- { result = [] }
69
- | service_body_list service_body
70
- { result << val[1] if val[1] }
71
-
72
- service_body : option
73
- | rpc
74
- | ';' { result = nil}
75
-
76
- rpc : 'rpc' IDENT '(' rpc_arg ')' 'returns' '(' rpc_arg ')' ';'
77
- { result = Protobuf::Node::RpcNode.new(val[1], val[3], val[7]) }
78
-
79
- rpc_arg :
80
- | user_type
81
-
82
- message_body : '{' message_body_body_list '}'
83
- { result = val[1] }
84
-
85
- message_body_body_list :
86
- { result = [] }
87
- | message_body_body_list message_body_body
88
- { result << val[1] if val[1] }
89
-
90
- message_body_body : field
91
- | enum
92
- | message
93
- | extend
94
- | extensions
95
- | group
96
- | option
97
- | ';' { result = nil }
98
-
99
- group : label 'group' CAMEL_IDENT '=' integer_literal message_body
100
- { result = Protobuf::Node::GroupNode.new(val[0], val[2], val[4], val[5]) }
101
-
102
- field : label type field_name '=' integer_literal ';'
103
- { result = Protobuf::Node::FieldNode.new(val[0], val[1], val[2], val[4]) }
104
- | label type field_name '=' integer_literal '[' field_option_list ']' ';'
105
- { result = Protobuf::Node::FieldNode.new(val[0], val[1], val[2], val[4], val[6]) }
106
-
107
- field_name : IDENT | "required" | "optional" | "repeated" | "import" | "package" | "option" | "message" | "extend" | "enum" | "service" | "rpc" | "returns" | "group" | "default" | "extensions" | "to" | "max" | "double" | "float" | "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64" | "bool" | "string" | "bytes"
108
-
109
- field_option_list : field_option
110
- { result = val }
111
- | field_option_list ',' field_option
112
- { result << val[2] }
113
-
114
- field_option : option_body
115
- | 'default' '=' constant
116
- { result = [:default, val[2]] }
117
-
118
- extensions : 'extensions' extension comma_extension_list ';'
119
- { result = Protobuf::Node::ExtensionsNode.new(val[2].unshift(val[1])) }
120
-
121
- comma_extension_list :
122
- { result = [] }
123
- | ',' extension
124
- { result << val[1] }
125
-
126
- extension : integer_literal
127
- { result = Protobuf::Node::ExtensionRangeNode.new(val[0]) }
128
- | integer_literal 'to' integer_literal
129
- { result = Protobuf::Node::ExtensionRangeNode.new(val[0], val[2]) }
130
- | integer_literal 'to' 'max'
131
- { result = Protobuf::Node::ExtensionRangeNode.new(val[0], :max) }
132
-
133
- label : 'required'
134
- | 'optional'
135
- | 'repeated'
136
-
137
- type : 'double' | 'float' | 'int32' | 'int64' | 'uint32' | 'uint64'
138
- | 'sint32' | 'sint64' | 'fixed32' | 'fixed64' | 'sfixed32' | 'sfixed64'
139
- | 'bool' | 'string' | 'bytes' | user_type
140
-
141
- user_type : IDENT dot_ident_list
142
- { result = val[1].unshift(val[0]) }
143
- | '.' IDENT dot_ident_list
144
- { result = val[1].unshift(val[0]) }
145
-
146
- constant : IDENT
147
- | integer_literal
148
- | FLOAT_LITERAL
149
- | STRING_LITERAL
150
- | BOOLEAN_LITERAL
151
-
152
- integer_literal : DEC_INTEGER
153
- | HEX_INTEGER
154
- | OCT_INTEGER
155
- end
156
-
157
- ---- inner
158
-
159
- require 'strscan'
160
-
161
- def parse(f)
162
- @scanner = StringScanner.new(f.read)
163
- yyparse(self, :scan)
164
- end
165
-
166
- def scan_debug
167
- scan do |token, value|
168
- p [token, value]
169
- yield [token, value]
170
- end
171
- end
172
-
173
- def scan
174
- until @scanner.eos?
175
- case
176
- when match(/\s+/, /\/\/.*/)
177
- # skip
178
- when match(/\/\*/)
179
- # C-like comment
180
- raise 'EOF inside block comment' until @scanner.scan_until(/\*\//)
181
- when match(/(?:required|optional|repeated|import|package|option|message|extend|enum|service|rpc|returns|group|default|extensions|to|max|double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/)
182
- yield [@token, @token.to_sym]
183
- when match(/[+-]?\d*\.\d+([Ee][\+-]?\d+)?/)
184
- yield [:FLOAT_LITERAL, @token.to_f]
185
- when match(/[+-]?[1-9]\d*(?!\.)/, /0(?![.xX0-9])/)
186
- yield [:DEC_INTEGER, @token.to_i]
187
- when match(/0[xX]([A-Fa-f0-9])+/)
188
- yield [:HEX_INTEGER, @token.to_i(0)]
189
- when match(/0[0-7]+/)
190
- yield [:OCT_INTEGER, @token.to_i(0)]
191
- when match(/(true|false)\b/)
192
- yield [:BOOLEAN_LITERAL, @token == 'true']
193
- when match(/"(?:[^"\\]+|\\.)*"/, /'(?:[^'\\]+|\\.)*'/)
194
- yield [:STRING_LITERAL, eval(@token)]
195
- when match(/[a-zA-Z_]\w*/)
196
- yield [:IDENT, @token.to_sym]
197
- when match(/[A-Z]\w*/)
198
- yield [:CAMEL_IDENT, @token.to_sym]
199
- when match(/./)
200
- yield [@token, @token]
201
- else
202
- raise "parse error around #{@scanner.string[@scanner.pos, 32].inspect}"
203
- end
204
- end
205
- yield [false, nil]
206
- end
207
-
208
- def match(*regular_expressions)
209
- regular_expressions.each do |re|
210
- if @scanner.scan(re)
211
- @token = @scanner[0]
212
- return true
213
- end
214
- end
215
- false
216
- end