aaronp-zomg 1.0.2.20080827232412

Sign up to get free protection for your applications and to get access to all the features.
Files changed (236) hide show
  1. data/History.txt +25 -0
  2. data/Manifest.txt +238 -0
  3. data/README.txt +59 -0
  4. data/Rakefile +43 -0
  5. data/bin/omfg +56 -0
  6. data/lib/scanner.rex +169 -0
  7. data/lib/yacc.y +861 -0
  8. data/lib/zomg.rb +22 -0
  9. data/lib/zomg/idl.rb +14 -0
  10. data/lib/zomg/idl/nodes/attribute.rb +15 -0
  11. data/lib/zomg/idl/nodes/binary.rb +19 -0
  12. data/lib/zomg/idl/nodes/constant.rb +15 -0
  13. data/lib/zomg/idl/nodes/interface.rb +13 -0
  14. data/lib/zomg/idl/nodes/interface_header.rb +14 -0
  15. data/lib/zomg/idl/nodes/member.rb +14 -0
  16. data/lib/zomg/idl/nodes/node.rb +54 -0
  17. data/lib/zomg/idl/nodes/operation.rb +17 -0
  18. data/lib/zomg/idl/nodes/parameter.rb +18 -0
  19. data/lib/zomg/idl/nodes/typedef.rb +13 -0
  20. data/lib/zomg/idl/nodes/union.rb +14 -0
  21. data/lib/zomg/idl/parser.rb +54 -0
  22. data/lib/zomg/idl/visitable.rb +15 -0
  23. data/lib/zomg/idl/visitors/duhr.rb +24 -0
  24. data/lib/zomg/idl/visitors/ruby_sexp.rb +204 -0
  25. data/lib/zomg/idl/visitors/sexp.rb +274 -0
  26. data/lib/zomg/version.rb +3 -0
  27. data/test/assets/array/array1.idl +60 -0
  28. data/test/assets/array/array2.idl +38 -0
  29. data/test/assets/array/array3.idl +8 -0
  30. data/test/assets/array/array4.idl +7 -0
  31. data/test/assets/array/array5.idl +80 -0
  32. data/test/assets/attribute/grid_0attribs_2methods.idl +16 -0
  33. data/test/assets/attribute/grid_1ROattribs_1attribs_0methods.idl +19 -0
  34. data/test/assets/attribute/grid_1ROattribs_1attribs_1methods.idl +19 -0
  35. data/test/assets/attribute/grid_1ROattribs_1attribs_2methods.idl +17 -0
  36. data/test/assets/attribute/grid_1ROattribs_1methods.idl +16 -0
  37. data/test/assets/attribute/grid_1ROattribs_2methods.idl +16 -0
  38. data/test/assets/attribute/grid_1attribs_1methods.idl +17 -0
  39. data/test/assets/attribute/grid_1attribs_2methods.idl +17 -0
  40. data/test/assets/attribute/grid_2ROattribs_0methods.idl +16 -0
  41. data/test/assets/attribute/grid_2ROattribs_1methods.idl +16 -0
  42. data/test/assets/attribute/grid_2ROattribs_2methods.idl +16 -0
  43. data/test/assets/attribute/grid_2attribs_0methods.idl +19 -0
  44. data/test/assets/attribute/grid_2attribs_1methods.idl +19 -0
  45. data/test/assets/attribute/grid_2attribs_2methods.idl +19 -0
  46. data/test/assets/comment/c_style_comments.idl +63 -0
  47. data/test/assets/comment/comment1.idl +57 -0
  48. data/test/assets/comment/cpp_style_comments.idl +168 -0
  49. data/test/assets/comment/shortform.idl +0 -0
  50. data/test/assets/complex/TestCodeSets.idl +10 -0
  51. data/test/assets/complex/TestIntfContext.idl +13 -0
  52. data/test/assets/complex/TestIntfLongLong.idl +51 -0
  53. data/test/assets/complex/TestIntfWChar.idl +52 -0
  54. data/test/assets/complex/any1.idl +22 -0
  55. data/test/assets/complex/complex1.idl +495 -0
  56. data/test/assets/complex/complex2.idl +730 -0
  57. data/test/assets/complex/complex3.idl +181 -0
  58. data/test/assets/complex/complex4.idl +181 -0
  59. data/test/assets/complex/interop_wchar.idl +60 -0
  60. data/test/assets/constant/constant1.idl +37 -0
  61. data/test/assets/constant/constant2.idl +29 -0
  62. data/test/assets/constant/constant3.idl +50 -0
  63. data/test/assets/constant/constant4.idl +21 -0
  64. data/test/assets/constant/constant5.idl +65 -0
  65. data/test/assets/constant/constant6.idl +144 -0
  66. data/test/assets/constant/constant7.idl +30 -0
  67. data/test/assets/enum/enum1.idl +13 -0
  68. data/test/assets/exception/TestIntfExceptions.idl +69 -0
  69. data/test/assets/exception/TestIntfExceptionsExt_2_0.idl +34 -0
  70. data/test/assets/exception/TestIntfExceptionsExt_2_3.idl +24 -0
  71. data/test/assets/exception/ex1.idl +6 -0
  72. data/test/assets/exception/ex2.idl +18 -0
  73. data/test/assets/exception/ex3.idl +10 -0
  74. data/test/assets/exception/ex4.idl +20 -0
  75. data/test/assets/exception/sc.idl +0 -0
  76. data/test/assets/exception/simple1.idl +18 -0
  77. data/test/assets/exception/simple2.idl +25 -0
  78. data/test/assets/factory/factory1.idl +77 -0
  79. data/test/assets/factory/objref.idl +25 -0
  80. data/test/assets/forward/forward.idl +15 -0
  81. data/test/assets/forward/forward1.idl +13 -0
  82. data/test/assets/forward/fwd_incl1.idl +6 -0
  83. data/test/assets/forward/fwd_incl2.idl +7 -0
  84. data/test/assets/include/a.idl +23 -0
  85. data/test/assets/include/b.idl +16 -0
  86. data/test/assets/include/employee.idl +12 -0
  87. data/test/assets/include/include1.idl +10 -0
  88. data/test/assets/include/include2.idl +5 -0
  89. data/test/assets/include/manager.idl +10 -0
  90. data/test/assets/include/pragma.idl +41 -0
  91. data/test/assets/include/rookie.idl +13 -0
  92. data/test/assets/include/rookiemanager.idl +15 -0
  93. data/test/assets/include/sample.idl +58 -0
  94. data/test/assets/include/sample_incl.idl +406 -0
  95. data/test/assets/inherit/IDL1.idl +18 -0
  96. data/test/assets/inherit/IDL2.idl +18 -0
  97. data/test/assets/inherit/IDL3.idl +18 -0
  98. data/test/assets/inherit/IDL4.idl +17 -0
  99. data/test/assets/inherit/diamond_inheritance.idl +31 -0
  100. data/test/assets/inherit/inherit.idl +27 -0
  101. data/test/assets/limit/longidentifiers.idl +25 -0
  102. data/test/assets/limit/namelens.idl +26 -0
  103. data/test/assets/limit/tpztest.idl +21 -0
  104. data/test/assets/module/module1.idl +29 -0
  105. data/test/assets/module/module2.idl +34 -0
  106. data/test/assets/module/module3.idl +20 -0
  107. data/test/assets/module/module4.idl +15 -0
  108. data/test/assets/nested/nested1.idl +5 -0
  109. data/test/assets/nested/nested2.idl +11 -0
  110. data/test/assets/operations/Benchmark.idl +331 -0
  111. data/test/assets/operations/CCS.idl +82 -0
  112. data/test/assets/operations/TestIntf.idl +40 -0
  113. data/test/assets/operations/TestIntfBasic.idl +653 -0
  114. data/test/assets/operations/atomic.idl +40 -0
  115. data/test/assets/operations/dif2.idl +48 -0
  116. data/test/assets/operations/interface1.idl +16 -0
  117. data/test/assets/operations/parameters.idl +111 -0
  118. data/test/assets/operations/perf.idl +84 -0
  119. data/test/assets/operations/timer.idl +104 -0
  120. data/test/assets/preprocess/base_for_diamond_inheritance_with_macros.idl +17 -0
  121. data/test/assets/preprocess/basic_macro_marshalling.idl +54 -0
  122. data/test/assets/preprocess/diamond_inheritance_with_macros.idl +74 -0
  123. data/test/assets/preprocess/macro1.idl +52 -0
  124. data/test/assets/preprocess/preprocess1.idl +246 -0
  125. data/test/assets/preprocess/preprocess2.idl +3 -0
  126. data/test/assets/recursion/loop.idl +28 -0
  127. data/test/assets/recursion/recursion1.idl +47 -0
  128. data/test/assets/recursion/recursion2.idl +74 -0
  129. data/test/assets/scenarios/filestat.idl +69 -0
  130. data/test/assets/scenarios/gateway.idl +176 -0
  131. data/test/assets/scenarios/library.idl +20 -0
  132. data/test/assets/scenarios/phone.idl +24 -0
  133. data/test/assets/scenarios/ticketservice.idl +118 -0
  134. data/test/assets/scope/scope01.idl +58 -0
  135. data/test/assets/scope/scope02.idl +25 -0
  136. data/test/assets/scope/scope03.idl +22 -0
  137. data/test/assets/scope/scope04.idl +27 -0
  138. data/test/assets/scope/scope05.idl +30 -0
  139. data/test/assets/scope/scope06.idl +51 -0
  140. data/test/assets/scope/scope07.idl +20 -0
  141. data/test/assets/scope/scope08.idl +88 -0
  142. data/test/assets/scope/scope09.idl +21 -0
  143. data/test/assets/scope/scope10.idl +95 -0
  144. data/test/assets/scope/scope11.idl +62 -0
  145. data/test/assets/scope/scope12.idl +117 -0
  146. data/test/assets/scope/scope13.idl +117 -0
  147. data/test/assets/scope/scope14.idl +117 -0
  148. data/test/assets/scope/scope15.idl +117 -0
  149. data/test/assets/scope/scope16.idl +117 -0
  150. data/test/assets/scope/scope17.idl +117 -0
  151. data/test/assets/scope/scope18.idl +117 -0
  152. data/test/assets/scope/scope19.idl +117 -0
  153. data/test/assets/scope/scope20.idl +215 -0
  154. data/test/assets/scope/scope21.idl +215 -0
  155. data/test/assets/scope/scope22.idl +205 -0
  156. data/test/assets/scope/scope23.idl +261 -0
  157. data/test/assets/scope/scope24.idl +376 -0
  158. data/test/assets/scope/scope25.idl +266 -0
  159. data/test/assets/scope/scope26.idl +180 -0
  160. data/test/assets/scope/scope27.idl +184 -0
  161. data/test/assets/scope/scope28.idl +182 -0
  162. data/test/assets/scope/scope29.idl +182 -0
  163. data/test/assets/scope/scope30.idl +40 -0
  164. data/test/assets/scope/scope31.idl +33 -0
  165. data/test/assets/scope/scope32.idl +37 -0
  166. data/test/assets/scope/scope33.idl +50 -0
  167. data/test/assets/scope/scope34.idl +97 -0
  168. data/test/assets/scope/scope35.idl +72 -0
  169. data/test/assets/scope/scope36.idl +167 -0
  170. data/test/assets/scope/scope37.idl +57 -0
  171. data/test/assets/scope/scope38.idl +38 -0
  172. data/test/assets/scope/scope39.idl +38 -0
  173. data/test/assets/scope/scope40.idl +38 -0
  174. data/test/assets/scope/scope41.idl +24 -0
  175. data/test/assets/scope/scope42.idl +64 -0
  176. data/test/assets/scope/scope43.idl +66 -0
  177. data/test/assets/scope/scope44.idl +52 -0
  178. data/test/assets/sequence/sequence1.idl +24 -0
  179. data/test/assets/sequence/sequence2.idl +9 -0
  180. data/test/assets/simple/basictypes1.idl +80 -0
  181. data/test/assets/simple/basictypes2.idl +197 -0
  182. data/test/assets/simple/basictypes3.idl +23 -0
  183. data/test/assets/simple/basictypes4.idl +73 -0
  184. data/test/assets/simple/basictypes5.idl +29 -0
  185. data/test/assets/simple/basictypes6.idl +63 -0
  186. data/test/assets/simple/simple1.idl +45 -0
  187. data/test/assets/simple/simple2.idl +61 -0
  188. data/test/assets/simple/simple3.idl +36 -0
  189. data/test/assets/string/string1.idl +11 -0
  190. data/test/assets/string/string2.idl +29 -0
  191. data/test/assets/struct/struct1.idl +20 -0
  192. data/test/assets/struct/struct2.idl +21 -0
  193. data/test/assets/struct/struct3.idl +42 -0
  194. data/test/assets/struct/struct4.idl +57 -0
  195. data/test/assets/struct/struct5.idl +71 -0
  196. data/test/assets/typecode/orb.idl +0 -0
  197. data/test/assets/typecode/pseudo.idl +25 -0
  198. data/test/assets/typedef/shorthand_typedef_struct_insideIF.idl +17 -0
  199. data/test/assets/typedef/shorthand_typedef_struct_insideIF_noMO.idl +16 -0
  200. data/test/assets/typedef/shorthand_typedef_struct_insideMO.idl +17 -0
  201. data/test/assets/typedef/shorthand_typedef_struct_outsideMO.idl +17 -0
  202. data/test/assets/typedef/typedef1.idl +8 -0
  203. data/test/assets/typedef/typedef2.idl +22 -0
  204. data/test/assets/typedef/typedef_struct_insideIF.idl +18 -0
  205. data/test/assets/typedef/typedef_struct_insideMO.idl +18 -0
  206. data/test/assets/typedef/typedef_struct_outsideMO.idl +18 -0
  207. data/test/assets/union/union1.idl +31 -0
  208. data/test/assets/union/union2.idl +22 -0
  209. data/test/assets/union/union3.idl +26 -0
  210. data/test/conversions/test_attribute.rb +46 -0
  211. data/test/conversions/test_enum.rb +25 -0
  212. data/test/conversions/test_exception.rb +24 -0
  213. data/test/conversions/test_forward_declaration.rb +22 -0
  214. data/test/conversions/test_interface.rb +30 -0
  215. data/test/conversions/test_module.rb +24 -0
  216. data/test/conversions/test_struct.rb +25 -0
  217. data/test/conversions/test_typedef.rb +22 -0
  218. data/test/conversions/test_union.rb +27 -0
  219. data/test/conversions/test_value_box_dcl.rb +22 -0
  220. data/test/helper.rb +23 -0
  221. data/test/simple/test_basictypes1.rb +87 -0
  222. data/test/simple/test_basictypes2.rb +18 -0
  223. data/test/simple/test_basictypes3.rb +32 -0
  224. data/test/simple/test_basictypes4.rb +25 -0
  225. data/test/simple/test_basictypes5.rb +17 -0
  226. data/test/simple/test_basictypes6.rb +19 -0
  227. data/test/simple/test_simple1.rb +40 -0
  228. data/test/simple/test_simple2.rb +48 -0
  229. data/test/simple/test_simple3.rb +20 -0
  230. data/test/test_array.rb +22 -0
  231. data/test/test_assets.rb +33 -0
  232. data/test/test_complex.rb +17 -0
  233. data/test/test_node.rb +16 -0
  234. data/test/test_parser.rb +17 -0
  235. data/test/test_scanner.rb +234 -0
  236. metadata +334 -0
@@ -0,0 +1,22 @@
1
+ require 'ruby2ruby'
2
+ require 'zomg/version'
3
+ require 'zomg/idl'
4
+
5
+ %w{
6
+ node
7
+ attribute
8
+ binary
9
+ constant
10
+ interface
11
+ interface_header
12
+ member
13
+ operation
14
+ parameter
15
+ typedef
16
+ union
17
+ }.each { |node_type|
18
+ require "zomg/idl/nodes/#{node_type}"
19
+ }
20
+ require 'zomg/idl/scanner'
21
+ require 'zomg/idl/lexer'
22
+ require 'zomg/idl/parser'
@@ -0,0 +1,14 @@
1
+ require 'zomg/idl/visitable'
2
+ require 'zomg/idl/visitors/sexp'
3
+ require 'zomg/idl/visitors/ruby_sexp'
4
+ require 'zomg/idl/visitors/duhr'
5
+
6
+ module ZOMG
7
+ module IDL
8
+ class << self
9
+ def parse(contents)
10
+ ZOMG::IDL::Parser.parse(contents)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Attribute < Node
5
+ alias :names :children
6
+ attr_accessor :type, :readonly
7
+ def initialize(type, names, readonly = false)
8
+ super(names)
9
+ @type = type
10
+ @readonly = readonly
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Binary < Node
5
+ attr_accessor :left
6
+ alias :right :children
7
+ def initialize(left, right)
8
+ @left = left
9
+ super(right)
10
+ end
11
+ end
12
+ %w{ MultiplyExpr PlusExpr ModulusExpr ShiftLeftExpr MinusExpr
13
+ DivideExpr OrExpr AmpersandExpr
14
+ }.each do |type|
15
+ const_set(type.to_sym, Class.new(Binary))
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Constant < Node
5
+ attr_accessor :type, :name
6
+ alias :value :children
7
+ def initialize(type, name, value)
8
+ @type = type
9
+ @name = name
10
+ super(value)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Interface < Node
5
+ attr_accessor :header
6
+ def initialize(header, body)
7
+ super(body)
8
+ @header = header
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class InterfaceHeader < Node
5
+ attr_accessor :abstract, :name
6
+ def initialize(abstract, name, inheritance_spec = [])
7
+ @abstract = abstract
8
+ @name = name
9
+ super(inheritance_spec)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Member < Node
5
+ attr_accessor :type
6
+ alias :declarators :children
7
+ def initialize(type, declarators)
8
+ @type = type
9
+ super(declarators)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,54 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Node
5
+ include Visitable
6
+ include Visitors
7
+
8
+ attr_accessor :children, :name
9
+ def initialize(children = [], options = {})
10
+ @children = children
11
+ options.each { |k,v| send(:"#{k}=", v) }
12
+ end
13
+
14
+ def to_i
15
+ children.to_i
16
+ end
17
+
18
+ def duhr
19
+ Duhr.new(self)
20
+ end
21
+
22
+ def to_sexp
23
+ Sexp.new.accept(self)
24
+ end
25
+
26
+ def to_ruby_sexp
27
+ RubySexp.new.accept(self)
28
+ end
29
+
30
+ def to_ruby(prefix = nil)
31
+ r2r = Ruby2Ruby.new
32
+ ruby_code = r2r.process(to_ruby_sexp)
33
+ if prefix
34
+ modules = prefix.split(/::/).map { |m| "module #{m}\n" }
35
+ ends = modules.map { |m| "end" }.join("\n")
36
+ "#{modules}#{ruby_code}\n#{ends}"
37
+ else
38
+ ruby_code
39
+ end
40
+ end
41
+ end
42
+ %w{ Boolean Char Double Float Long Octet Short UnsignedLong
43
+ UnsignedShort ElementSpec CaseLabel DefaultLabel IntegerLiteral
44
+ Sequence ArraySize Void In Out InOut CharacterLiteral
45
+ UnsignedLongLong LongLong Any ForwardDeclaration WChar
46
+ FloatingPointLiteral BooleanLiteral Context StringLiteral
47
+ WideStringLiteral WideCharacterLiteral UnaryMinus Object
48
+ UnaryPlus WString String Case Enum Exception ValueBoxDcl
49
+ Struct ScopedName Module ArrayDeclarator SimpleDeclarator
50
+ Specification
51
+ }.each { |type| const_set(type.to_sym, Class.new(Node)) }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Operation < Node
5
+ attr_accessor :attribute, :returns, :name, :raises, :context
6
+ def initialize(attribute, returns, name, params, raises, context)
7
+ super(params)
8
+ @attribute = attribute
9
+ @returns = returns
10
+ @name = name
11
+ @raises = raises
12
+ @context = context
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Parameter < Node
5
+ attr_accessor :attribute, :type, :declarator
6
+ def initialize(attribute, type, declarator)
7
+ @attribute = attribute
8
+ @type = type
9
+ @declarator = declarator
10
+ end
11
+
12
+ def name
13
+ declarator.name
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Typedef < Node
5
+ attr_accessor :type_spec
6
+ def initialize(type_spec, declarators)
7
+ @type_spec = type_spec
8
+ super(declarators)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Nodes
4
+ class Union < Node
5
+ attr_accessor :name, :switch_type
6
+ def initialize(name, switch_type, switch_body)
7
+ @name = name
8
+ @switch_type = switch_type
9
+ super(switch_body)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,54 @@
1
+ module ZOMG
2
+ module IDL
3
+ class Parser < Lexer
4
+ class << self
5
+ def parse_file(filename)
6
+ new.parse_file(filename)
7
+ end
8
+
9
+ def parse(string)
10
+ new.parse(string)
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ @scanner = Scanner.new
16
+ @yydebug = false
17
+ end
18
+
19
+ # Parse an IDL string
20
+ def parse(contents)
21
+ @scanner.scan_evaluate(contents)
22
+ do_parse
23
+ end
24
+
25
+ def parse_file(filename)
26
+ contents = File.read(filename)
27
+ if contents =~ /^\s*#/
28
+ dir = File.dirname(File.expand_path(filename))
29
+ contents = IO.popen("gcc -E -I #{dir} -I . -", 'w+') { |io|
30
+ io.write(contents)
31
+ io.close_write
32
+ io.read
33
+ }
34
+ end
35
+ parse(contents)
36
+ end
37
+
38
+ def next_token
39
+ token = @scanner.next_token
40
+ return [false, false] unless token
41
+ token = next_token() if token[0] == :T_COMMENT
42
+ token = next_token() if token[0] == :T_PREPROCESSOR
43
+ token = next_token() if token[0] == :T_PRAGMA
44
+ token
45
+ end
46
+
47
+ def on_error(error_token_id, error_value, value_stack)
48
+ puts token_to_str(error_token_id)
49
+ puts error_value
50
+ p value_stack.last
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Visitable
4
+ # Based off the visitor pattern from RubyGarden
5
+ def accept(visitor, &block)
6
+ klass = self.class
7
+ method_name = :"visit_#{klass.name.split(/::/).last}"
8
+ unless visitor.respond_to?(method_name)
9
+ raise "No visitor for #{self.class}"
10
+ end
11
+ visitor.send(method_name, self, &block)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Visitors
4
+ # Stupid path navigation
5
+ class Duhr
6
+ attr_accessor :tree
7
+ def initialize(tree)
8
+ @tree = tree
9
+ end
10
+
11
+ def to_sexp; @tree.to_sexp; end
12
+
13
+ def [](i)
14
+ Duhr.new(@tree[i])
15
+ end
16
+
17
+ def method_missing(sym, *args)
18
+ klass = ZOMG::IDL::Nodes.const_get(sym)
19
+ Duhr.new(tree.children.find_all { |child| child.is_a?(klass) })
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,204 @@
1
+ module ZOMG
2
+ module IDL
3
+ module Visitors
4
+ class RubySexp
5
+ def visit_Specification(o)
6
+ [:block] + o.children.map { |c| c.accept(self) }
7
+ end
8
+
9
+ def visit_Module(o)
10
+ [ :module,
11
+ classify(o.name),
12
+ [:scope, [:block] +
13
+ o.children.map { |c| c.accept(self) }.compact
14
+ ]
15
+ ]
16
+ end
17
+
18
+ def visit_Interface(o)
19
+ header = o.header.accept(self)
20
+ header = header ? [:block, header] : [:block]
21
+ o.children.each { |c|
22
+ list = c.accept(self)
23
+ if list
24
+ if list.first.is_a?(Symbol)
25
+ header << list
26
+ else
27
+ header += list
28
+ end
29
+ end
30
+ }
31
+ [ :module,
32
+ classify(o.header.name),
33
+ [:scope,
34
+ header
35
+ ]
36
+ ]
37
+ end
38
+
39
+ def visit_InterfaceHeader(o)
40
+ if o.children.length > 0
41
+ [:fcall, :include,
42
+ [:array] + o.children.map { |c|
43
+ [:const, classify(c.accept(self))]
44
+ } ]
45
+ else
46
+ nil
47
+ end
48
+ end
49
+
50
+ def visit_Exception(o)
51
+ attributes = o.children.length > 0 ?
52
+ [:scope, [:fcall,
53
+ :attr_accessor,
54
+ [:array] +
55
+ o.children.map { |c|
56
+ c.accept(self)
57
+ }.flatten.map { |att| [:lit, att] }
58
+ ]] : [:scope]
59
+
60
+ [:class, classify(o.name), [:const, :Exception], attributes]
61
+ end
62
+
63
+ def visit_Attribute(o)
64
+ attributes = []
65
+ o.children.each { |name|
66
+ name = name.accept(self)
67
+
68
+ # Reader
69
+ attributes <<
70
+ [:defn, :"get#{name.to_s.capitalize}",
71
+ [:scope, [:block, [:args],
72
+ [:fcall, :raise, [:array,
73
+ [:call, [:const, :NotImplementedError], :new]]
74
+ ]
75
+ ]]
76
+ ]
77
+ unless o.readonly
78
+ attributes <<
79
+ [:defn, :"set#{name.to_s.capitalize}",
80
+ [:scope, [:block, [:args, :_],
81
+ [:fcall, :raise, [:array,
82
+ [:call, [:const, :NotImplementedError], :new]]
83
+ ]
84
+ ]]
85
+ ]
86
+ end
87
+ }
88
+ attributes
89
+ end
90
+
91
+ def visit_ScopedName(o)
92
+ o.name.to_sym
93
+ end
94
+
95
+ def visit_Operation(o)
96
+ [ :defn,
97
+ o.name.to_sym,
98
+ [:scope,
99
+ [:block,
100
+ [:args] + o.children.map { |c| paramify(c.accept(self)) },
101
+ [:fcall, :raise, [:array,
102
+ [:call, [:const, :NotImplementedError], :new]]
103
+ ]
104
+ ]
105
+ ]
106
+ ]
107
+ end
108
+
109
+ def visit_Parameter(o)
110
+ o.declarator.accept(self)
111
+ end
112
+
113
+ def visit_Constant(o)
114
+ [:cdecl, o.name.upcase.to_sym, o.value.accept(self)]
115
+ end
116
+
117
+ def visit_Enum(o)
118
+ [ :cdecl,
119
+ o.name.to_sym,
120
+ [:hash] + o.children.inject([]) { |m,c|
121
+ m += [[:lit, c.to_sym], [:lit, c.to_sym]]
122
+ }
123
+ ]
124
+ end
125
+
126
+ def visit_IntegerLiteral(o)
127
+ [:lit, o.to_i]
128
+ end
129
+
130
+ def visit_ValueBoxDcl(o)
131
+ end
132
+
133
+ def visit_Struct(o)
134
+ [ :cdecl,
135
+ classify(o.name),
136
+ [ :call, [:const, :Struct],
137
+ :new,
138
+ [:array] + o.children.map { |c|
139
+ c.accept(self)
140
+ }.flatten.map { |lit| [:lit, lit] }
141
+ ]
142
+ ]
143
+ end
144
+
145
+ def visit_Union(o)
146
+ [ :cdecl,
147
+ o.name.to_sym,
148
+ [ :call, [:const, :Struct],
149
+ :new,
150
+ [:array] + o.children.map { |c|
151
+ val = c.accept(self)
152
+ val && [:lit, val]
153
+ }.compact
154
+ ]
155
+ ]
156
+ end
157
+
158
+ def visit_CaseLabel(o)
159
+ end
160
+
161
+ def visit_Case(o)
162
+ o.children.last.accept(self)
163
+ end
164
+
165
+ def visit_ElementSpec(o)
166
+ o.children[1].accept(self)
167
+ end
168
+
169
+ def visit_Member(o)
170
+ o.children.map { |c| c.accept(self) }
171
+ end
172
+
173
+ def visit_SimpleDeclarator(o)
174
+ o.name.to_sym
175
+ end
176
+
177
+ def visit_ArrayDeclarator(o)
178
+ o.name.to_sym
179
+ end
180
+
181
+ def visit_Typedef(o)
182
+ end
183
+
184
+ def visit_ForwardDeclaration(o)
185
+ end
186
+
187
+ def accept(target)
188
+ target.accept(self)
189
+ end
190
+
191
+ private
192
+ def classify(string)
193
+ s = string.to_s
194
+ :"#{s.slice(0,1).upcase}#{s[1..-1]}"
195
+ end
196
+
197
+ def paramify(string)
198
+ s = string.to_s
199
+ :"#{s.slice(0,1).downcase}#{s[1..-1]}"
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end