qrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +58 -0
  4. data/LICENCE.md +22 -0
  5. data/Manifest.txt +11 -0
  6. data/README.md +118 -0
  7. data/Rakefile +11 -0
  8. data/lib/qrb/Q/default.q +29 -0
  9. data/lib/qrb/data_type.rb +23 -0
  10. data/lib/qrb/errors.rb +23 -0
  11. data/lib/qrb/support/attribute.rb +53 -0
  12. data/lib/qrb/support/collection_type.rb +25 -0
  13. data/lib/qrb/support/dress_helper.rb +68 -0
  14. data/lib/qrb/support/heading.rb +57 -0
  15. data/lib/qrb/support/type_factory.rb +178 -0
  16. data/lib/qrb/support.rb +5 -0
  17. data/lib/qrb/syntax/ad_type.rb +25 -0
  18. data/lib/qrb/syntax/attribute.rb +11 -0
  19. data/lib/qrb/syntax/builtin_type.rb +13 -0
  20. data/lib/qrb/syntax/constraint_def.rb +11 -0
  21. data/lib/qrb/syntax/constraints.rb +18 -0
  22. data/lib/qrb/syntax/contract.rb +25 -0
  23. data/lib/qrb/syntax/definitions.rb +14 -0
  24. data/lib/qrb/syntax/expression.rb +12 -0
  25. data/lib/qrb/syntax/heading.rb +15 -0
  26. data/lib/qrb/syntax/lambda_expr.rb +11 -0
  27. data/lib/qrb/syntax/named_constraint.rb +11 -0
  28. data/lib/qrb/syntax/q.citrus +195 -0
  29. data/lib/qrb/syntax/relation_type.rb +11 -0
  30. data/lib/qrb/syntax/seq_type.rb +12 -0
  31. data/lib/qrb/syntax/set_type.rb +12 -0
  32. data/lib/qrb/syntax/sub_type.rb +13 -0
  33. data/lib/qrb/syntax/support.rb +13 -0
  34. data/lib/qrb/syntax/system.rb +15 -0
  35. data/lib/qrb/syntax/tuple_type.rb +11 -0
  36. data/lib/qrb/syntax/type_def.rb +14 -0
  37. data/lib/qrb/syntax/type_ref.rb +13 -0
  38. data/lib/qrb/syntax/union_type.rb +12 -0
  39. data/lib/qrb/syntax/unnamed_constraint.rb +11 -0
  40. data/lib/qrb/syntax.rb +42 -0
  41. data/lib/qrb/system.rb +63 -0
  42. data/lib/qrb/type/ad_type.rb +111 -0
  43. data/lib/qrb/type/builtin_type.rb +56 -0
  44. data/lib/qrb/type/relation_type.rb +81 -0
  45. data/lib/qrb/type/seq_type.rb +51 -0
  46. data/lib/qrb/type/set_type.rb +52 -0
  47. data/lib/qrb/type/sub_type.rb +94 -0
  48. data/lib/qrb/type/tuple_type.rb +99 -0
  49. data/lib/qrb/type/union_type.rb +78 -0
  50. data/lib/qrb/type.rb +63 -0
  51. data/lib/qrb/version.rb +14 -0
  52. data/lib/qrb.rb +63 -0
  53. data/qrb.gemspec +186 -0
  54. data/spec/acceptance/Q/test_default.rb +96 -0
  55. data/spec/acceptance/Q/test_parsing.rb +15 -0
  56. data/spec/acceptance/ad_type/test_in_q.rb +82 -0
  57. data/spec/acceptance/ad_type/test_in_ruby.rb +60 -0
  58. data/spec/spec_helper.rb +68 -0
  59. data/spec/unit/attribute/test_equality.rb +26 -0
  60. data/spec/unit/attribute/test_fetch_on.rb +50 -0
  61. data/spec/unit/attribute/test_initialize.rb +13 -0
  62. data/spec/unit/attribute/test_to_name.rb +10 -0
  63. data/spec/unit/heading/test_each.rb +28 -0
  64. data/spec/unit/heading/test_equality.rb +28 -0
  65. data/spec/unit/heading/test_initialize.rb +36 -0
  66. data/spec/unit/heading/test_size.rb +30 -0
  67. data/spec/unit/heading/test_to_name.rb +32 -0
  68. data/spec/unit/qrb/test_parse.rb +18 -0
  69. data/spec/unit/syntax/nodes/test_ad_type.rb +94 -0
  70. data/spec/unit/syntax/nodes/test_attribute.rb +25 -0
  71. data/spec/unit/syntax/nodes/test_builtin_type.rb +32 -0
  72. data/spec/unit/syntax/nodes/test_comment.rb +26 -0
  73. data/spec/unit/syntax/nodes/test_constraint_def.rb +27 -0
  74. data/spec/unit/syntax/nodes/test_constraints.rb +51 -0
  75. data/spec/unit/syntax/nodes/test_contract.rb +62 -0
  76. data/spec/unit/syntax/nodes/test_expression.rb +43 -0
  77. data/spec/unit/syntax/nodes/test_heading.rb +41 -0
  78. data/spec/unit/syntax/nodes/test_named_constraint.rb +31 -0
  79. data/spec/unit/syntax/nodes/test_relation_type.rb +41 -0
  80. data/spec/unit/syntax/nodes/test_seq_type.rb +24 -0
  81. data/spec/unit/syntax/nodes/test_set_type.rb +24 -0
  82. data/spec/unit/syntax/nodes/test_spacing.rb +25 -0
  83. data/spec/unit/syntax/nodes/test_sub_type.rb +52 -0
  84. data/spec/unit/syntax/nodes/test_tuple_type.rb +41 -0
  85. data/spec/unit/syntax/nodes/test_union_type.rb +23 -0
  86. data/spec/unit/syntax/nodes/test_unnamed_constraint.rb +31 -0
  87. data/spec/unit/syntax/test_compile_type.rb +22 -0
  88. data/spec/unit/system/test_add_type.rb +47 -0
  89. data/spec/unit/system/test_dsl.rb +30 -0
  90. data/spec/unit/system/test_dup.rb +30 -0
  91. data/spec/unit/system/test_fetch.rb +42 -0
  92. data/spec/unit/system/test_get_type.rb +30 -0
  93. data/spec/unit/system/test_initialize.rb +10 -0
  94. data/spec/unit/test_qrb.rb +15 -0
  95. data/spec/unit/type/ad_type/test_default_name.rb +15 -0
  96. data/spec/unit/type/ad_type/test_dress.rb +55 -0
  97. data/spec/unit/type/ad_type/test_include.rb +22 -0
  98. data/spec/unit/type/ad_type/test_initialize.rb +40 -0
  99. data/spec/unit/type/ad_type/test_name.rb +20 -0
  100. data/spec/unit/type/builtin_type/test_default_name.rb +12 -0
  101. data/spec/unit/type/builtin_type/test_dress.rb +33 -0
  102. data/spec/unit/type/builtin_type/test_equality.rb +26 -0
  103. data/spec/unit/type/builtin_type/test_include.rb +22 -0
  104. data/spec/unit/type/builtin_type/test_initialize.rb +12 -0
  105. data/spec/unit/type/builtin_type/test_name.rb +24 -0
  106. data/spec/unit/type/relation_type/test_default_name.rb +16 -0
  107. data/spec/unit/type/relation_type/test_dress.rb +164 -0
  108. data/spec/unit/type/relation_type/test_equality.rb +32 -0
  109. data/spec/unit/type/relation_type/test_include.rb +46 -0
  110. data/spec/unit/type/relation_type/test_initialize.rb +26 -0
  111. data/spec/unit/type/relation_type/test_name.rb +24 -0
  112. data/spec/unit/type/seq_type/test_default_name.rb +14 -0
  113. data/spec/unit/type/seq_type/test_dress.rb +49 -0
  114. data/spec/unit/type/seq_type/test_equality.rb +26 -0
  115. data/spec/unit/type/seq_type/test_include.rb +43 -0
  116. data/spec/unit/type/seq_type/test_initialize.rb +28 -0
  117. data/spec/unit/type/seq_type/test_name.rb +24 -0
  118. data/spec/unit/type/set_type/test_default_name.rb +14 -0
  119. data/spec/unit/type/set_type/test_dress.rb +66 -0
  120. data/spec/unit/type/set_type/test_equality.rb +26 -0
  121. data/spec/unit/type/set_type/test_include.rb +43 -0
  122. data/spec/unit/type/set_type/test_initialize.rb +28 -0
  123. data/spec/unit/type/set_type/test_name.rb +24 -0
  124. data/spec/unit/type/sub_type/test_default_name.rb +14 -0
  125. data/spec/unit/type/sub_type/test_dress.rb +75 -0
  126. data/spec/unit/type/sub_type/test_equality.rb +34 -0
  127. data/spec/unit/type/sub_type/test_include.rb +34 -0
  128. data/spec/unit/type/sub_type/test_initialize.rb +16 -0
  129. data/spec/unit/type/sub_type/test_name.rb +24 -0
  130. data/spec/unit/type/tuple_type/test_default_name.rb +14 -0
  131. data/spec/unit/type/tuple_type/test_dress.rb +112 -0
  132. data/spec/unit/type/tuple_type/test_equality.rb +32 -0
  133. data/spec/unit/type/tuple_type/test_include.rb +38 -0
  134. data/spec/unit/type/tuple_type/test_initialize.rb +30 -0
  135. data/spec/unit/type/tuple_type/test_name.rb +24 -0
  136. data/spec/unit/type/union_type/test_default_name.rb +12 -0
  137. data/spec/unit/type/union_type/test_dress.rb +43 -0
  138. data/spec/unit/type/union_type/test_equality.rb +30 -0
  139. data/spec/unit/type/union_type/test_include.rb +28 -0
  140. data/spec/unit/type/union_type/test_initialize.rb +24 -0
  141. data/spec/unit/type/union_type/test_name.rb +20 -0
  142. data/spec/unit/type_factory/dsl/test_adt.rb +54 -0
  143. data/spec/unit/type_factory/dsl/test_attribute.rb +37 -0
  144. data/spec/unit/type_factory/dsl/test_attributes.rb +41 -0
  145. data/spec/unit/type_factory/dsl/test_builtin.rb +45 -0
  146. data/spec/unit/type_factory/dsl/test_relation.rb +85 -0
  147. data/spec/unit/type_factory/dsl/test_seq.rb +57 -0
  148. data/spec/unit/type_factory/dsl/test_set.rb +57 -0
  149. data/spec/unit/type_factory/dsl/test_subtype.rb +91 -0
  150. data/spec/unit/type_factory/dsl/test_tuple.rb +73 -0
  151. data/spec/unit/type_factory/dsl/test_union.rb +81 -0
  152. data/spec/unit/type_factory/factory/test_builtin.rb +24 -0
  153. data/spec/unit/type_factory/factory/test_seq_type.rb +44 -0
  154. data/spec/unit/type_factory/factory/test_set_type.rb +44 -0
  155. data/spec/unit/type_factory/factory/test_sub_type.rb +53 -0
  156. data/spec/unit/type_factory/factory/test_tuple_type.rb +43 -0
  157. data/tasks/gem.rake +73 -0
  158. data/tasks/test.rake +31 -0
  159. metadata +344 -0
@@ -0,0 +1,5 @@
1
+ require_relative 'support/attribute'
2
+ require_relative 'support/heading'
3
+ require_relative 'support/dress_helper'
4
+ require_relative 'support/collection_type'
5
+ require_relative 'support/type_factory'
@@ -0,0 +1,25 @@
1
+ module Qrb
2
+ module Syntax
3
+ module AdType
4
+ include Support
5
+
6
+ def compile(factory)
7
+ name = builtin_type_name
8
+ clazz = name ? resolve_ruby_const(name.to_s) : nil
9
+ factory.adt(clazz, compile_contracts(factory, clazz))
10
+ end
11
+
12
+ def compile_contracts(factory, clazz)
13
+ contracts = {}
14
+ captures[:contract].each do |node|
15
+ contract = node.compile(factory, clazz)
16
+ contracts.merge!(contract) do |k,_,_|
17
+ raise Error, "Duplicate contract name `#{k}`"
18
+ end
19
+ end
20
+ contracts
21
+ end
22
+
23
+ end # module AdType
24
+ end # module Syntax
25
+ end # module Qrb
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Attribute
4
+
5
+ def compile(factory)
6
+ factory.attribute(attribute_name.to_sym, type.compile(factory))
7
+ end
8
+
9
+ end # module BuiltinType
10
+ end # module Syntax
11
+ end # module Qrb
@@ -0,0 +1,13 @@
1
+ module Qrb
2
+ module Syntax
3
+ module BuiltinType
4
+ include Support
5
+
6
+ def compile(factory)
7
+ clazz = resolve_ruby_const(builtin_type_name.to_s)
8
+ factory.builtin(clazz)
9
+ end
10
+
11
+ end # module BuiltinType
12
+ end # module Syntax
13
+ end # module Qrb
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module ConstraintDef
4
+
5
+ def compile(factory)
6
+ constraints.compile(var_name.to_s)
7
+ end
8
+
9
+ end # module ConstraintDef
10
+ end # module Syntax
11
+ end # module Qrb
@@ -0,0 +1,18 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Constraints
4
+
5
+ def compile(var_name)
6
+ constraints = {}
7
+ captures[:named_constraint].each do |node|
8
+ compiled = node.compile(var_name)
9
+ constraints.merge!(compiled) do |k,_,_|
10
+ raise Error, "Duplicate constraint name `#{k}`"
11
+ end
12
+ end
13
+ constraints
14
+ end
15
+
16
+ end # module Constraints
17
+ end # module Syntax
18
+ end # module Qrb
@@ -0,0 +1,25 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Contract
4
+
5
+ def compile(factory, clazz)
6
+ contract = [
7
+ type.compile(factory),
8
+ compile_upper(factory, clazz)
9
+ ]
10
+ { contract_name.to_sym => contract }
11
+ end
12
+
13
+ def compile_upper(factory, clazz)
14
+ if up
15
+ up.compile(factory)
16
+ elsif clazz
17
+ clazz.method(contract_name.to_sym)
18
+ else
19
+ Qrb::IDENTITY
20
+ end
21
+ end
22
+
23
+ end # module Contract
24
+ end # module Syntax
25
+ end # module Qrb
@@ -0,0 +1,14 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Definitions
4
+
5
+ def compile(system)
6
+ captures[:type_def].each do |node|
7
+ node.compile(system)
8
+ end
9
+ system
10
+ end
11
+
12
+ end # module Definitions
13
+ end # module Syntax
14
+ end # module Qrb
@@ -0,0 +1,12 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Expression
4
+
5
+ def compile(var_name)
6
+ expr = "->(#{var_name}){ #{self} }"
7
+ ::Kernel.eval(expr)
8
+ end
9
+
10
+ end # module Expression
11
+ end # module Syntax
12
+ end # module Qrb
@@ -0,0 +1,15 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Heading
4
+
5
+ def attributes(factory)
6
+ captures[:attribute].map{|x| x.compile(factory) }
7
+ end
8
+
9
+ def compile(factory)
10
+ Qrb::Heading.new(attributes(factory))
11
+ end
12
+
13
+ end # module Heading
14
+ end # module Syntax
15
+ end # module Qrb
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module LambdaExpr
4
+
5
+ def compile(factory)
6
+ expression.compile(var_name)
7
+ end
8
+
9
+ end # module LambdaExpr
10
+ end # module Syntax
11
+ end # module Qrb
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module NamedConstraint
4
+
5
+ def compile(var_name)
6
+ { constraint_name.to_sym => expression.compile(var_name) }
7
+ end
8
+
9
+ end # module NamedConstraint
10
+ end # module Syntax
11
+ end # module Qrb
@@ -0,0 +1,195 @@
1
+ grammar Qrb::Syntax::Parser
2
+
3
+ rule system
4
+ (spacing definitions spacing type? spacing eof)
5
+ <Qrb::Syntax::System>
6
+ end
7
+
8
+ rule definitions
9
+ (type_def (spacing type_def)*)?
10
+ <Qrb::Syntax::Definitions>
11
+ end
12
+
13
+ ###################################################################### Types
14
+ rule type_def
15
+ (type_name spacing '=' spacing type)
16
+ <Qrb::Syntax::TypeDef>
17
+ end
18
+
19
+ rule type
20
+ union_type
21
+ end
22
+
23
+ # union and sub types
24
+
25
+ rule union_type
26
+ (sub_type ('|' sub_type)+)
27
+ <Qrb::Syntax::UnionType>
28
+ | sub_type
29
+ end
30
+
31
+ rule sub_type
32
+ (rel_type constraint_def)
33
+ <Qrb::Syntax::SubType>
34
+ | rel_type
35
+ end
36
+
37
+ rule constraint_def
38
+ ('(' spacing var_name spacing '|' spacing constraints spacing ')')
39
+ <Qrb::Syntax::ConstraintDef>
40
+ end
41
+
42
+ rule constraints
43
+ (named_constraint (spacing ',' spacing named_constraint)*)
44
+ <Qrb::Syntax::Constraints>
45
+ | unnamed_constraint
46
+ end
47
+
48
+ rule named_constraint
49
+ (constraint_name ':' spacing expression)
50
+ <Qrb::Syntax::NamedConstraint>
51
+ end
52
+
53
+ rule unnamed_constraint
54
+ (spacing expression)
55
+ <Qrb::Syntax::UnnamedConstraint>
56
+ end
57
+
58
+ # relational types
59
+
60
+ rule rel_type
61
+ relation_type
62
+ | tuple_type
63
+ | collection_type
64
+ end
65
+
66
+ rule relation_type
67
+ ('{{' spacing heading spacing '}}')
68
+ <Qrb::Syntax::RelationType>
69
+ end
70
+
71
+ rule tuple_type
72
+ ('{' spacing heading spacing '}')
73
+ <Qrb::Syntax::TupleType>
74
+ end
75
+
76
+ rule heading
77
+ (attribute (',' spacing attribute)*)
78
+ <Qrb::Syntax::Heading>
79
+ | spacing
80
+ <Qrb::Syntax::Heading>
81
+ end
82
+
83
+ rule attribute
84
+ (attribute_name spacing ':' spacing type)
85
+ <Qrb::Syntax::Attribute>
86
+ end
87
+
88
+ # collection types
89
+
90
+ rule collection_type
91
+ set_type
92
+ | seq_type
93
+ | term_type
94
+ end
95
+
96
+ rule set_type
97
+ ('{' type '}')
98
+ <Qrb::Syntax::SetType>
99
+ end
100
+
101
+ rule seq_type
102
+ ('[' type ']')
103
+ <Qrb::Syntax::SeqType>
104
+ end
105
+
106
+ # terminal forms
107
+
108
+ rule term_type
109
+ ad_type
110
+ | builtin_type
111
+ | type_ref
112
+ end
113
+
114
+ # ad type
115
+
116
+ rule ad_type
117
+ (('.' builtin_type_name)? spacing contract (spacing ',' spacing contract)*)
118
+ <Qrb::Syntax::AdType>
119
+ end
120
+
121
+ rule contract
122
+ ('<' contract_name '>' spacing type (spacing ('\\' up:lambda_expr) spacing ('\\' down:lambda_expr))?)
123
+ <Qrb::Syntax::Contract>
124
+ end
125
+
126
+ # builtin and refs
127
+
128
+ rule builtin_type
129
+ ('.' builtin_type_name)
130
+ <Qrb::Syntax::BuiltinType>
131
+ end
132
+
133
+ rule type_ref
134
+ (type_name spacing)
135
+ <Qrb::Syntax::TypeRef>
136
+ end
137
+
138
+ # lambda and expressions
139
+
140
+ rule lambda_expr
141
+ ('(' spacing var_name spacing '|' spacing expression spacing ')')
142
+ <Qrb::Syntax::LambdaExpr>
143
+ end
144
+
145
+ rule expression
146
+ (
147
+ ('(' spacing expression spacing ')')
148
+ | (![\(,\)] .)+ expression?
149
+ )
150
+ <Qrb::Syntax::Expression>
151
+ end
152
+
153
+ # lexer
154
+
155
+ rule var_name
156
+ /[a-z]+/
157
+ end
158
+
159
+ rule contract_name
160
+ /[a-z][a-z0-9]*/
161
+ end
162
+
163
+ rule constraint_name
164
+ /[a-z][a-z_]*/
165
+ end
166
+
167
+ rule attribute_name
168
+ /[a-z][a-zA-Z0-9_]*/
169
+ end
170
+
171
+ rule type_name
172
+ /[A-Z][a-zA-Z]+/
173
+ end
174
+
175
+ rule builtin_type_name
176
+ /[a-zA-Z0-9:]/+
177
+ end
178
+
179
+ rule spacing
180
+ (spaces | comment)*
181
+ end
182
+
183
+ rule comment
184
+ '#' (![\n] .)* [\n]?
185
+ end
186
+
187
+ rule spaces
188
+ /[ \t\n]+/
189
+ end
190
+
191
+ rule eof
192
+ !.
193
+ end
194
+
195
+ end
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module RelationType
4
+
5
+ def compile(factory)
6
+ factory.relation(heading.compile(factory))
7
+ end
8
+
9
+ end # module RelationType
10
+ end # module Syntax
11
+ end # module Qrb
@@ -0,0 +1,12 @@
1
+ module Qrb
2
+ module Syntax
3
+ module SeqType
4
+
5
+ def compile(factory)
6
+ elm_type = type.compile(factory)
7
+ factory.seq(elm_type)
8
+ end
9
+
10
+ end # module SeqType
11
+ end # module Syntax
12
+ end # module Qrb
@@ -0,0 +1,12 @@
1
+ module Qrb
2
+ module Syntax
3
+ module SetType
4
+
5
+ def compile(factory)
6
+ elm_type = type.compile(factory)
7
+ factory.set(elm_type)
8
+ end
9
+
10
+ end # module SetType
11
+ end # module Syntax
12
+ end # module Qrb
@@ -0,0 +1,13 @@
1
+ module Qrb
2
+ module Syntax
3
+ module SubType
4
+
5
+ def compile(factory)
6
+ s = rel_type.compile(factory)
7
+ c = constraint_def.compile(factory)
8
+ factory.subtype(s, c)
9
+ end
10
+
11
+ end # module SubType
12
+ end # module Syntax
13
+ end # module Qrb
@@ -0,0 +1,13 @@
1
+ module Qrb
2
+ module Syntax
3
+ module Support
4
+
5
+ def resolve_ruby_const(name)
6
+ name.split('::').inject(::Kernel){|mod,const|
7
+ mod.const_get(const)
8
+ }
9
+ end
10
+
11
+ end # module Support
12
+ end # module Syntax
13
+ end # module Qrb
@@ -0,0 +1,15 @@
1
+ module Qrb
2
+ module Syntax
3
+ module System
4
+
5
+ def compile(system)
6
+ definitions.compile(system)
7
+ if type
8
+ system.main = type.compile(system)
9
+ end
10
+ system
11
+ end
12
+
13
+ end # module System
14
+ end # module Syntax
15
+ end # module Qrb
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module TupleType
4
+
5
+ def compile(factory)
6
+ factory.tuple(heading.compile(factory))
7
+ end
8
+
9
+ end # module TupleType
10
+ end # module Syntax
11
+ end # module Qrb
@@ -0,0 +1,14 @@
1
+ module Qrb
2
+ module Syntax
3
+ module TypeDef
4
+
5
+ def compile(system)
6
+ t = type.compile(system)
7
+ t.name = type_name.to_s
8
+ system.add_type(t)
9
+ t
10
+ end
11
+
12
+ end # module TypeDef
13
+ end # module Syntax
14
+ end # module Qrb
@@ -0,0 +1,13 @@
1
+ module Qrb
2
+ module Syntax
3
+ module TypeRef
4
+
5
+ def compile(factory)
6
+ factory.fetch(type_name.to_s) do |n|
7
+ raise Error, "Unknown type `#{n}`"
8
+ end
9
+ end
10
+
11
+ end # module TypeRef
12
+ end # module Syntax
13
+ end # module Qrb
@@ -0,0 +1,12 @@
1
+ module Qrb
2
+ module Syntax
3
+ module UnionType
4
+
5
+ def compile(factory)
6
+ cds = captures[:sub_type].map{|x| x.compile(factory) }
7
+ factory.union(cds)
8
+ end
9
+
10
+ end # module UnionType
11
+ end # module Syntax
12
+ end # module Qrb
@@ -0,0 +1,11 @@
1
+ module Qrb
2
+ module Syntax
3
+ module UnnamedConstraint
4
+
5
+ def compile(var_name)
6
+ { predicate: expression.compile(var_name) }
7
+ end
8
+
9
+ end # module UnnamedConstraint
10
+ end # module Syntax
11
+ end # module Qrb
data/lib/qrb/syntax.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'citrus'
2
+ require_relative 'syntax/support'
3
+ require_relative 'syntax/system'
4
+ require_relative 'syntax/definitions'
5
+ require_relative 'syntax/type_def'
6
+ require_relative 'syntax/expression'
7
+ require_relative 'syntax/attribute'
8
+ require_relative 'syntax/heading'
9
+ require_relative 'syntax/builtin_type'
10
+ require_relative 'syntax/sub_type'
11
+ require_relative 'syntax/constraint_def'
12
+ require_relative 'syntax/constraints'
13
+ require_relative 'syntax/named_constraint'
14
+ require_relative 'syntax/unnamed_constraint'
15
+ require_relative 'syntax/seq_type'
16
+ require_relative 'syntax/set_type'
17
+ require_relative 'syntax/tuple_type'
18
+ require_relative 'syntax/relation_type'
19
+ require_relative 'syntax/union_type'
20
+ require_relative 'syntax/type_ref'
21
+ require_relative 'syntax/ad_type'
22
+ require_relative 'syntax/contract'
23
+ require_relative 'syntax/lambda_expr'
24
+ module Qrb
25
+ module Syntax
26
+
27
+ Citrus.load File.expand_path('../syntax/q.citrus', __FILE__)
28
+
29
+ def self.parse(*args, &bl)
30
+ Parser.parse(*args, &bl)
31
+ end
32
+
33
+ def self.compile(source, system = Qrb::System.new)
34
+ Parser.parse(source, root: "system").compile(system)
35
+ end
36
+
37
+ def self.compile_type(str)
38
+ Parser.parse(str.strip, root: "type").compile(TypeFactory.new)
39
+ end
40
+
41
+ end
42
+ end
data/lib/qrb/system.rb ADDED
@@ -0,0 +1,63 @@
1
+ module Qrb
2
+ #
3
+ # A System is a collection of named Q types.
4
+ #
5
+ class System
6
+
7
+ def initialize(types = {}, main = nil)
8
+ @types = types
9
+ @main = main
10
+ end
11
+ attr_accessor :main
12
+
13
+ DSL_METHODS.each do |dsl_method|
14
+ define_method(dsl_method){|*args, &bl|
15
+ factory.public_send(dsl_method, *args, &bl)
16
+ }
17
+ end
18
+
19
+ def add_type(type)
20
+ unless type.is_a?(Type)
21
+ raise ArgumentError, "Qrb::Type expected, got `#{type}`"
22
+ end
23
+
24
+ if @types.has_key?(type.name)
25
+ raise Error, "Duplicate type name `#{type.name}`"
26
+ end
27
+
28
+ @types[type.name] = type
29
+ end
30
+
31
+ def get_type(name)
32
+ @types[name]
33
+ end
34
+ alias :[] :get_type
35
+
36
+ def fetch(name, &bl)
37
+ @types.fetch(name, &bl)
38
+ end
39
+
40
+ def factory
41
+ @factory ||= TypeFactory.new
42
+ end
43
+
44
+ def dress(*args, &bl)
45
+ raise Error, "No main type." unless main
46
+ main.dress(*args, &bl)
47
+ end
48
+
49
+ def parse(source)
50
+ require "qrb/syntax"
51
+ Syntax.compile(source, self.dup)
52
+ end
53
+
54
+ def inspect
55
+ @types.each_pair.map{|k,v| "#{k} = #{v}" }.join("\n")
56
+ end
57
+
58
+ def dup
59
+ System.new(@types.dup, @main)
60
+ end
61
+
62
+ end # class System
63
+ end # module Qrb