stark 0.5.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.
@@ -0,0 +1,330 @@
1
+ %% name = Stark::Parser
2
+
3
+ %% namespace = ast Namespace(lang, namespace)
4
+ %% include = ast Include(path)
5
+ %% struct = ast Struct(type, name, fields)
6
+ %% field = ast Field(index, type, name, value, options)
7
+ %% function = ast Function(name, return_type, arguments)
8
+ %% service = ast Service(name, functions)
9
+ %% comment = ast Comment(text)
10
+ %% enum = ast Enum(name, values)
11
+ %% map = ast Map(key,value)
12
+ %% set = ast Set(value)
13
+ %% list = ast List(value)
14
+ %% exception = ast Exception(name, fields)
15
+ %% const_int = ast ConstInt(value)
16
+ %% const_str = ast ConstString(value)
17
+ %% const_id = ast ConstIdentifier(value)
18
+ %% const_list = ast ConstList(values)
19
+ %% const_map = ast ConstMap(values)
20
+ %% const_dbl = ast ConstDouble(value)
21
+
22
+ intconstant = /([+-]?[0-9]+)/
23
+ hexconstant = /("0x"[0-9A-Fa-f]+)/
24
+ dubconstant = /([+-]?[0-9]+(\.[0-9]+)([eE][+-]?[0-9]+)?)/
25
+ | /([+-]?[0-9]+([eE][+-]?[0-9]+))/
26
+ | /([+-]?(\.[0-9]+)([eE][+-]?[0-9]+)?)/
27
+ # | /([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)/
28
+ identifier = /([a-zA-Z_][\.a-zA-Z_0-9]*)/
29
+ whitespace = /([ \t\r\n]*)/
30
+ # sillycomm = /("\/*""*"*"*\/")/
31
+ # multicomm = /("\/*"[^*]"\/"*([^*\/]|[^*]"\/"|"*"[^\/])*"*"*"*\/")/
32
+ # doctext = "\/**" (/[^\*\/]|[^\*]/ "\/"|"*" /[^\/]/)* "*"* "*\/"
33
+ # comment = /("//"[^\n]*)/
34
+ # unixcomment = /("#"[^\n]*)/
35
+ st_identifier = /([a-zA-Z-][\.a-zA-Z_0-9-]*)/
36
+ literal_begin = /(['\"])/
37
+
38
+ reserved =
39
+ "BEGIN" |
40
+ "END" |
41
+ "__CLASS__" |
42
+ "__DIR__" |
43
+ "__FILE__" |
44
+ "__FUNCTION__" |
45
+ "__LINE__" |
46
+ "__METHOD__" |
47
+ "__NAMESPACE__" |
48
+ "abstract" |
49
+ "alias" |
50
+ "and" |
51
+ "args" |
52
+ "as" |
53
+ "assert" |
54
+ "begin" |
55
+ "break" |
56
+ "case" |
57
+ "catch" |
58
+ "class" |
59
+ "clone" |
60
+ "continue" |
61
+ "declare" |
62
+ "def" |
63
+ "default" |
64
+ "del" |
65
+ "delete" |
66
+ "do" |
67
+ "dynamic" |
68
+ "elif" |
69
+ "else" |
70
+ "elseif" |
71
+ "elsif" |
72
+ "end" |
73
+ "enddeclare" |
74
+ "endfor" |
75
+ "endforeach" |
76
+ "endif" |
77
+ "endswitch" |
78
+ "endwhile" |
79
+ "ensure" |
80
+ "except" |
81
+ "exec" |
82
+ "finally" |
83
+ "float" |
84
+ "for" |
85
+ "foreach" |
86
+ "function" |
87
+ "global" |
88
+ "goto" |
89
+ "if" |
90
+ "implements" |
91
+ "import" |
92
+ "in" |
93
+ "inline" |
94
+ "instanceof" |
95
+ "interface" |
96
+ "is" |
97
+ "lambda" |
98
+ "module" |
99
+ "native" |
100
+ "new" |
101
+ "next" |
102
+ "nil" |
103
+ "not" |
104
+ "or" |
105
+ "pass" |
106
+ "public" |
107
+ "print" |
108
+ "private" |
109
+ "protected" |
110
+ "public" |
111
+ "raise" |
112
+ "redo" |
113
+ "rescue" |
114
+ "retry" |
115
+ "register" |
116
+ "return" |
117
+ "self" |
118
+ "sizeof" |
119
+ "static" |
120
+ "super" |
121
+ "switch" |
122
+ "synchronized" |
123
+ "then" |
124
+ "this" |
125
+ "throw" |
126
+ "transient" |
127
+ "try" |
128
+ "undef" |
129
+ "union" |
130
+ "unless" |
131
+ "unsigned" |
132
+ "until" |
133
+ "use" |
134
+ "var" |
135
+ "virtual" |
136
+ "volatile" |
137
+ "when" |
138
+ "while" |
139
+ "with" |
140
+ "xor" |
141
+ "yield"
142
+
143
+
144
+ tok_int_constant = < intconstant > { text.to_i }
145
+ | < hexconstant > { text.to_i }
146
+ | "false" { 0 }
147
+ | "true" { 1 }
148
+
149
+ tok_dub_constant = dubconstant:f { f.to_f }
150
+
151
+ tok_identifier = < identifier > ~text
152
+ tok_st_identifier = st_identifier
153
+
154
+ escapes = "\\r" { "\r" }
155
+ | "\\n" { "\n" }
156
+ | "\\t" { "\t" }
157
+ | "\\\"" { "\"" }
158
+ | "\\'" { "'" }
159
+ | "\\\\" { "\\" }
160
+
161
+ tok_literal = "\"" < (escapes | !"\"" . )* > "\"" ~text
162
+ | "'" < (escapes | !"'" . )* > "'" ~text
163
+
164
+ - = /[ \t]+/
165
+ osp = /[ \t]*/
166
+ bsp = /[\s]+/
167
+ obsp = /[\s]*/
168
+
169
+ root = Program !.
170
+
171
+ # Program = HeaderList - DefinitionList
172
+
173
+ Program = Element*:a { a }
174
+
175
+ CComment = "/*" < (!"*/" .)* > "*/" obsp ~comment(text)
176
+
177
+ HComment = "#" < (!"\n" .)* > bsp ~comment(text)
178
+
179
+ Comment = CComment | HComment
180
+
181
+ CaptureDocText = {}
182
+ DestroyDocText = {}
183
+ HeaderList = HeaderList Header
184
+ | Header
185
+
186
+ Element = Comment
187
+ | Header bsp
188
+ | Definition bsp
189
+
190
+ Header = Include | Namespace
191
+
192
+ Namespace = "namespace" - tok_identifier:l - tok_identifier:n ~namespace(l,n)
193
+ | "namespace" - "*" - tok_identifier:n ~namespace(nil,n)
194
+
195
+ Include = "include" - tok_literal:f ~include(f)
196
+
197
+ DefinitionList = DefinitionList CaptureDocText Definition
198
+
199
+ Definition = Const
200
+ | TypeDefinition
201
+ | Service
202
+
203
+ TypeDefinition = Typedef
204
+ | Enum
205
+ | Senum
206
+ | Struct
207
+ | Xception
208
+
209
+ Typedef = "typedef" - FieldType tok_identifier
210
+
211
+ CommaOrSemicolonOptional = ("," | ";")? obsp
212
+
213
+ Enum = "enum" - tok_identifier:name osp "{" obsp EnumDefList:vals obsp "}" ~enum(name, vals)
214
+
215
+ EnumDefList = EnumDefList:l EnumDef:e { l + [e] }
216
+ | EnumDef:e { [e] }
217
+
218
+ EnumDef = CaptureDocText tok_identifier "=" tok_int_constant CommaOrSemicolonOptional
219
+ | CaptureDocText tok_identifier CommaOrSemicolonOptional
220
+
221
+ Senum = "senum" - tok_identifier "{" SenumDefList "}"
222
+
223
+ SenumDefList = SenumDefList SenumDef
224
+ | nothing
225
+
226
+ SenumDef = tok_literal CommaOrSemicolonOptional
227
+
228
+ Const = "const" - FieldType tok_identifier "=" ConstValue CommaOrSemicolonOptional
229
+
230
+ ConstValue = tok_int_constant:i ~const_int(i)
231
+ | tok_literal:s ~const_str(s)
232
+ | tok_identifier:i ~const_id(i)
233
+ | ConstList
234
+ | ConstMap
235
+ | tok_dub_constant:d ~const_dbl(d)
236
+
237
+ ConstList = "[" osp ConstListContents*:l osp "]" ~const_list(l)
238
+
239
+ ConstListContents = ConstValue:i CommaOrSemicolonOptional osp ~i
240
+
241
+ ConstMap = "{" osp ConstMapContents*:m osp "}" ~const_map(m)
242
+
243
+ # ConstMapContents = ConstMapContents ConstValue osp ":" osp ConstValue CommaOrSemicolonOptional
244
+ # | ConstValue osp ":" osp ConstValue CommaOrSemicolonOptional
245
+ ConstMapContents = ConstValue:k osp ":" osp ConstValue:v CommaOrSemicolonOptional
246
+ { [k,v] }
247
+
248
+ StructHead = < "struct" | "union"> ~text
249
+
250
+ Struct = StructHead:t - tok_identifier:name - XsdAll? osp
251
+ "{" obsp Comment? FieldList?:list obsp "}" # TypeAnnotations
252
+ ~struct(t.to_sym,name,list)
253
+
254
+ XsdAll = "xsd_all"
255
+
256
+ XsdOptional = "xsd_optional" - | nothing
257
+
258
+ XsdNillable = "xsd_nillable" - | nothing
259
+
260
+ XsdAttributes = "xsd_attrs" - "{" FieldList "}"
261
+ | nothing
262
+
263
+ Xception = "exception" - tok_identifier:name osp "{" obsp FieldList?:list obsp "}" ~exception(name, list)
264
+
265
+ Service = "service" - tok_identifier:name - Extends? osp
266
+ "{" obsp FunctionList?:funcs obsp "}"
267
+ ~service(name, funcs)
268
+
269
+ Extends = "extends" - tok_identifier
270
+
271
+ FunctionList = FunctionList:l Function:f { l + [f] }
272
+ | Function:f { [f] }
273
+
274
+ Function = CaptureDocText OneWay? FunctionType:rt - tok_identifier:name
275
+ osp "(" FieldList?:args ")" Throws? CommaOrSemicolonOptional
276
+ ~function(name, rt, args)
277
+
278
+ OneWay = ("oneway" | "async") -
279
+
280
+ Throws = "throws" - "(" FieldList ")"
281
+
282
+ FieldList = FieldList:l Field:f { l + [f] }
283
+ | Field:f { [f] }
284
+
285
+ Field = CaptureDocText FieldIdentifier?:i osp FieldRequiredness?:req osp
286
+ FieldType:t osp tok_identifier:n osp FieldValue?:val
287
+ CommaOrSemicolonOptional
288
+ ~field(i,t,n,val,req)
289
+
290
+ # XsdOptional XsdNillable XsdAttributes TypeAnnotations
291
+ FieldIdentifier = tok_int_constant:n ":" ~n
292
+
293
+ FieldRequiredness = < ("required" | "optional") > { [text] }
294
+
295
+ FieldValue = "=" osp ConstValue:e ~e
296
+
297
+ FunctionType = FieldType | "void"
298
+
299
+ FieldType = ContainerType
300
+ | tok_identifier:n ~n
301
+
302
+ BaseType = SimpleBaseType:t TypeAnnotations ~t
303
+
304
+ SimpleBaseType = "string" { :string }
305
+ | "binary" { :binary }
306
+ | "slist" { :slist }
307
+ | "bool" { :bool }
308
+ | "byte" { :byte }
309
+ | "i16" { :i16 }
310
+ | "i32" { :i32 }
311
+ | "i64" { :i64 }
312
+ | "double" { :double }
313
+
314
+ ContainerType = SimpleContainerType
315
+
316
+ SimpleContainerType = MapType | SetType | ListType
317
+
318
+ MapType = "map" "<" FieldType:a osp "," osp FieldType:b ">" ~map(a,b)
319
+
320
+ SetType = "set" "<" FieldType:a ">" ~set(a)
321
+
322
+ ListType = "list" "<" FieldType:a ">" ~list(a)
323
+
324
+ CppType = tok_cpp_type tok_literal
325
+ | nothing
326
+
327
+ TypeAnnotationList = TypeAnnotationList TypeAnnotation
328
+
329
+ TypeAnnotation = tok_identifier "=" tok_literal CommaOrSemicolonOptional
330
+
data/lib/stark.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'thrift'
2
+
3
+ module Stark
4
+ VERSION = '0.5.0'
5
+
6
+ def self.pipe_transport
7
+ cr, cw = IO.pipe
8
+ sr, sw = IO.pipe
9
+
10
+ client_t = Thrift::IOStreamTransport.new sr, cw
11
+ server_t = Thrift::IOStreamTransport.new cr, sw
12
+
13
+ [client_t, server_t]
14
+ end
15
+
16
+ def self.materialize(file, namespace=Object)
17
+ require 'stark/parser'
18
+ require 'stark/ruby'
19
+ require 'stringio'
20
+
21
+ data = File.read file
22
+
23
+ tg = Stark::Parser.new data
24
+
25
+ unless tg.parse
26
+ tg.raise_error
27
+ end
28
+
29
+ stream = StringIO.new
30
+
31
+ ruby = Stark::Ruby.new stream
32
+
33
+ tg.result.each { |i| i.accept ruby }
34
+
35
+ namespace.module_eval stream.string
36
+ end
37
+ end
data/stark.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "stark"
5
+ s.version = "0.5.0.20130227233247"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Evan Phoenix"]
9
+ s.date = "2013-02-28"
10
+ s.description = "Optimized thrift bindings for ruby"
11
+ s.email = ["evan@phx.io"]
12
+ s.executables = ["stark"]
13
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
14
+ s.files = [".autotest", "History.txt", "Manifest.txt", "README.txt", "Rakefile", "bin/stark", "lib/stark.rb", "test/test_stark.rb", "test/test_client.rb", "test/test_parser.rb", "test/test_server.rb", ".gemtest"]
15
+ s.homepage = "http://github.com/evanphx/stark"
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = "stark"
19
+ s.rubygems_version = "1.8.25"
20
+ s.summary = "Optimized thrift bindings for ruby"
21
+ s.test_files = ["test/test_client.rb", "test/test_parser.rb", "test/test_server.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<thrift>, ["~> 0.9.0"])
28
+ s.add_development_dependency(%q<rdoc>, ["~> 3.10"])
29
+ s.add_development_dependency(%q<hoe>, ["~> 3.5"])
30
+ else
31
+ s.add_dependency(%q<thrift>, ["~> 0.9.0"])
32
+ s.add_dependency(%q<rdoc>, ["~> 3.10"])
33
+ s.add_dependency(%q<hoe>, ["~> 3.5"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<thrift>, ["~> 0.9.0"])
37
+ s.add_dependency(%q<rdoc>, ["~> 3.10"])
38
+ s.add_dependency(%q<hoe>, ["~> 3.5"])
39
+ end
40
+ end
@@ -0,0 +1,183 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ #
21
+ # Licensed to the Apache Software Foundation (ASF) under one
22
+ # or more contributor license agreements. See the NOTICE file
23
+ # distributed with this work for additional information
24
+ # regarding copyright ownership. The ASF licenses this file
25
+ # to you under the Apache License, Version 2.0 (the
26
+ # "License"); you may not use this file except in compliance
27
+ # with the License. You may obtain a copy of the License at
28
+ #
29
+ # http://www.apache.org/licenses/LICENSE-2.0
30
+ #
31
+ # Unless required by applicable law or agreed to in writing,
32
+ # software distributed under the License is distributed on an
33
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
34
+ # KIND, either express or implied. See the License for the
35
+ # specific language governing permissions and limitations
36
+ # under the License.
37
+ #
38
+
39
+ namespace rb SpecNamespace
40
+
41
+ struct Hello {
42
+ 1: string greeting = "hello world"
43
+ }
44
+
45
+ enum SomeEnum {
46
+ ONE
47
+ TWO
48
+ }
49
+
50
+ struct StructWithSomeEnum {
51
+ 1: SomeEnum some_enum;
52
+ }
53
+
54
+ union TestUnion {
55
+ /**
56
+ * A doc string
57
+ */
58
+ 1: string string_field;
59
+ 2: i32 i32_field;
60
+ 3: i32 other_i32_field;
61
+ 4: SomeEnum enum_field;
62
+ 5: binary binary_field;
63
+ }
64
+
65
+ struct Foo {
66
+ 1: i32 simple = 53,
67
+ 2: string words = "words",
68
+ 3: Hello hello = {'greeting' : "hello, world!"},
69
+ 4: list<i32> ints = [1, 2, 2, 3],
70
+ 5: map<i32, map<string, double>> complex,
71
+ 6: set<i16> shorts = [5, 17, 239],
72
+ 7: optional string opt_string
73
+ 8: bool my_bool
74
+ }
75
+
76
+ struct Foo2 {
77
+ 1: binary my_binary
78
+ }
79
+
80
+ struct BoolStruct {
81
+ 1: bool yesno = 1
82
+ }
83
+
84
+ struct SimpleList {
85
+ 1: list<bool> bools,
86
+ 2: list<byte> bytes,
87
+ 3: list<i16> i16s,
88
+ 4: list<i32> i32s,
89
+ 5: list<i64> i64s,
90
+ 6: list<double> doubles,
91
+ 7: list<string> strings,
92
+ 8: list<map<i16, i16>> maps,
93
+ 9: list<list<i16>> lists,
94
+ 10: list<set<i16>> sets,
95
+ 11: list<Hello> hellos
96
+ }
97
+
98
+ exception Xception {
99
+ 1: string message,
100
+ 2: i32 code = 1
101
+ }
102
+
103
+ service NonblockingService {
104
+ Hello greeting(1:bool english)
105
+ bool block()
106
+ oneway void unblock(1:i32 n)
107
+ oneway void shutdown()
108
+ void sleep(1:double seconds)
109
+ }
110
+
111
+ union My_union {
112
+ 1: bool im_true,
113
+ 2: byte a_bite,
114
+ 3: i16 integer16,
115
+ 4: i32 integer32,
116
+ 5: i64 integer64,
117
+ 6: double double_precision,
118
+ 7: string some_characters,
119
+ 8: i32 other_i32
120
+ 9: SomeEnum some_enum;
121
+ 10: map<SomeEnum, list<SomeEnum>> my_map;
122
+ }
123
+
124
+ struct Struct_with_union {
125
+ 1: My_union fun_union
126
+ 2: i32 integer32
127
+ 3: string some_characters
128
+ }
129
+
130
+ struct StructWithEnumMap {
131
+ 1: map<SomeEnum, list<SomeEnum>> my_map;
132
+ }
133
+
134
+ # Nested lists
135
+ struct NestedListInList {
136
+ 1: list<list<byte>> value
137
+ }
138
+
139
+ struct NestedListInSet {
140
+ 1: set<list<byte>> value
141
+ }
142
+
143
+ struct NestedListInMapKey {
144
+ 1: map<list<byte>, byte> value
145
+ }
146
+
147
+ struct NestedListInMapValue {
148
+ 1: map<byte, list<byte>> value
149
+ }
150
+
151
+ # Nested sets
152
+ struct NestedSetInList {
153
+ 1: list<set<byte>> value
154
+ }
155
+
156
+ struct NestedSetInSet {
157
+ 1: set<set<byte>> value
158
+ }
159
+
160
+ struct NestedSetInMapKey {
161
+ 1: map<set<byte>, byte> value
162
+ }
163
+
164
+ struct NestedSetInMapValue {
165
+ 1: map<byte, set<byte>> value
166
+ }
167
+
168
+ # Nested maps
169
+ struct NestedMapInList {
170
+ 1: list<map<byte, byte>> value
171
+ }
172
+
173
+ struct NestedMapInSet {
174
+ 1: set<map<byte, byte>> value
175
+ }
176
+
177
+ struct NestedMapInMapKey {
178
+ 2: map<map<byte, byte>, byte> value
179
+ }
180
+
181
+ struct NestedMapInMapValue {
182
+ 2: map<byte, map<byte, byte>> value
183
+ }
@@ -0,0 +1,8 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.8.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'profile_types'
8
+
@@ -0,0 +1,36 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.8.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+
8
+ module Status
9
+ ON = 0
10
+ OFF = 1
11
+ DEAD = 2
12
+ ALIVE = 3
13
+ VALUE_MAP = {0 => "ON", 1 => "OFF", 2 => "DEAD", 3 => "ALIVE"}
14
+ VALID_VALUES = Set.new([ON, OFF, DEAD, ALIVE]).freeze
15
+ end
16
+
17
+ class UserProfile
18
+ include ::Thrift::Struct, ::Thrift::Struct_Union
19
+ UID = 1
20
+ NAME = 2
21
+ BLURB = 3
22
+
23
+ FIELDS = {
24
+ UID => {:type => ::Thrift::Types::I32, :name => 'uid'},
25
+ NAME => {:type => ::Thrift::Types::STRING, :name => 'name'},
26
+ BLURB => {:type => ::Thrift::Types::STRING, :name => 'blurb'}
27
+ }
28
+
29
+ def struct_fields; FIELDS; end
30
+
31
+ def validate
32
+ end
33
+
34
+ ::Thrift::Struct.generate_accessors self
35
+ end
36
+