qrb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +58 -0
- data/LICENCE.md +22 -0
- data/Manifest.txt +11 -0
- data/README.md +118 -0
- data/Rakefile +11 -0
- data/lib/qrb/Q/default.q +29 -0
- data/lib/qrb/data_type.rb +23 -0
- data/lib/qrb/errors.rb +23 -0
- data/lib/qrb/support/attribute.rb +53 -0
- data/lib/qrb/support/collection_type.rb +25 -0
- data/lib/qrb/support/dress_helper.rb +68 -0
- data/lib/qrb/support/heading.rb +57 -0
- data/lib/qrb/support/type_factory.rb +178 -0
- data/lib/qrb/support.rb +5 -0
- data/lib/qrb/syntax/ad_type.rb +25 -0
- data/lib/qrb/syntax/attribute.rb +11 -0
- data/lib/qrb/syntax/builtin_type.rb +13 -0
- data/lib/qrb/syntax/constraint_def.rb +11 -0
- data/lib/qrb/syntax/constraints.rb +18 -0
- data/lib/qrb/syntax/contract.rb +25 -0
- data/lib/qrb/syntax/definitions.rb +14 -0
- data/lib/qrb/syntax/expression.rb +12 -0
- data/lib/qrb/syntax/heading.rb +15 -0
- data/lib/qrb/syntax/lambda_expr.rb +11 -0
- data/lib/qrb/syntax/named_constraint.rb +11 -0
- data/lib/qrb/syntax/q.citrus +195 -0
- data/lib/qrb/syntax/relation_type.rb +11 -0
- data/lib/qrb/syntax/seq_type.rb +12 -0
- data/lib/qrb/syntax/set_type.rb +12 -0
- data/lib/qrb/syntax/sub_type.rb +13 -0
- data/lib/qrb/syntax/support.rb +13 -0
- data/lib/qrb/syntax/system.rb +15 -0
- data/lib/qrb/syntax/tuple_type.rb +11 -0
- data/lib/qrb/syntax/type_def.rb +14 -0
- data/lib/qrb/syntax/type_ref.rb +13 -0
- data/lib/qrb/syntax/union_type.rb +12 -0
- data/lib/qrb/syntax/unnamed_constraint.rb +11 -0
- data/lib/qrb/syntax.rb +42 -0
- data/lib/qrb/system.rb +63 -0
- data/lib/qrb/type/ad_type.rb +111 -0
- data/lib/qrb/type/builtin_type.rb +56 -0
- data/lib/qrb/type/relation_type.rb +81 -0
- data/lib/qrb/type/seq_type.rb +51 -0
- data/lib/qrb/type/set_type.rb +52 -0
- data/lib/qrb/type/sub_type.rb +94 -0
- data/lib/qrb/type/tuple_type.rb +99 -0
- data/lib/qrb/type/union_type.rb +78 -0
- data/lib/qrb/type.rb +63 -0
- data/lib/qrb/version.rb +14 -0
- data/lib/qrb.rb +63 -0
- data/qrb.gemspec +186 -0
- data/spec/acceptance/Q/test_default.rb +96 -0
- data/spec/acceptance/Q/test_parsing.rb +15 -0
- data/spec/acceptance/ad_type/test_in_q.rb +82 -0
- data/spec/acceptance/ad_type/test_in_ruby.rb +60 -0
- data/spec/spec_helper.rb +68 -0
- data/spec/unit/attribute/test_equality.rb +26 -0
- data/spec/unit/attribute/test_fetch_on.rb +50 -0
- data/spec/unit/attribute/test_initialize.rb +13 -0
- data/spec/unit/attribute/test_to_name.rb +10 -0
- data/spec/unit/heading/test_each.rb +28 -0
- data/spec/unit/heading/test_equality.rb +28 -0
- data/spec/unit/heading/test_initialize.rb +36 -0
- data/spec/unit/heading/test_size.rb +30 -0
- data/spec/unit/heading/test_to_name.rb +32 -0
- data/spec/unit/qrb/test_parse.rb +18 -0
- data/spec/unit/syntax/nodes/test_ad_type.rb +94 -0
- data/spec/unit/syntax/nodes/test_attribute.rb +25 -0
- data/spec/unit/syntax/nodes/test_builtin_type.rb +32 -0
- data/spec/unit/syntax/nodes/test_comment.rb +26 -0
- data/spec/unit/syntax/nodes/test_constraint_def.rb +27 -0
- data/spec/unit/syntax/nodes/test_constraints.rb +51 -0
- data/spec/unit/syntax/nodes/test_contract.rb +62 -0
- data/spec/unit/syntax/nodes/test_expression.rb +43 -0
- data/spec/unit/syntax/nodes/test_heading.rb +41 -0
- data/spec/unit/syntax/nodes/test_named_constraint.rb +31 -0
- data/spec/unit/syntax/nodes/test_relation_type.rb +41 -0
- data/spec/unit/syntax/nodes/test_seq_type.rb +24 -0
- data/spec/unit/syntax/nodes/test_set_type.rb +24 -0
- data/spec/unit/syntax/nodes/test_spacing.rb +25 -0
- data/spec/unit/syntax/nodes/test_sub_type.rb +52 -0
- data/spec/unit/syntax/nodes/test_tuple_type.rb +41 -0
- data/spec/unit/syntax/nodes/test_union_type.rb +23 -0
- data/spec/unit/syntax/nodes/test_unnamed_constraint.rb +31 -0
- data/spec/unit/syntax/test_compile_type.rb +22 -0
- data/spec/unit/system/test_add_type.rb +47 -0
- data/spec/unit/system/test_dsl.rb +30 -0
- data/spec/unit/system/test_dup.rb +30 -0
- data/spec/unit/system/test_fetch.rb +42 -0
- data/spec/unit/system/test_get_type.rb +30 -0
- data/spec/unit/system/test_initialize.rb +10 -0
- data/spec/unit/test_qrb.rb +15 -0
- data/spec/unit/type/ad_type/test_default_name.rb +15 -0
- data/spec/unit/type/ad_type/test_dress.rb +55 -0
- data/spec/unit/type/ad_type/test_include.rb +22 -0
- data/spec/unit/type/ad_type/test_initialize.rb +40 -0
- data/spec/unit/type/ad_type/test_name.rb +20 -0
- data/spec/unit/type/builtin_type/test_default_name.rb +12 -0
- data/spec/unit/type/builtin_type/test_dress.rb +33 -0
- data/spec/unit/type/builtin_type/test_equality.rb +26 -0
- data/spec/unit/type/builtin_type/test_include.rb +22 -0
- data/spec/unit/type/builtin_type/test_initialize.rb +12 -0
- data/spec/unit/type/builtin_type/test_name.rb +24 -0
- data/spec/unit/type/relation_type/test_default_name.rb +16 -0
- data/spec/unit/type/relation_type/test_dress.rb +164 -0
- data/spec/unit/type/relation_type/test_equality.rb +32 -0
- data/spec/unit/type/relation_type/test_include.rb +46 -0
- data/spec/unit/type/relation_type/test_initialize.rb +26 -0
- data/spec/unit/type/relation_type/test_name.rb +24 -0
- data/spec/unit/type/seq_type/test_default_name.rb +14 -0
- data/spec/unit/type/seq_type/test_dress.rb +49 -0
- data/spec/unit/type/seq_type/test_equality.rb +26 -0
- data/spec/unit/type/seq_type/test_include.rb +43 -0
- data/spec/unit/type/seq_type/test_initialize.rb +28 -0
- data/spec/unit/type/seq_type/test_name.rb +24 -0
- data/spec/unit/type/set_type/test_default_name.rb +14 -0
- data/spec/unit/type/set_type/test_dress.rb +66 -0
- data/spec/unit/type/set_type/test_equality.rb +26 -0
- data/spec/unit/type/set_type/test_include.rb +43 -0
- data/spec/unit/type/set_type/test_initialize.rb +28 -0
- data/spec/unit/type/set_type/test_name.rb +24 -0
- data/spec/unit/type/sub_type/test_default_name.rb +14 -0
- data/spec/unit/type/sub_type/test_dress.rb +75 -0
- data/spec/unit/type/sub_type/test_equality.rb +34 -0
- data/spec/unit/type/sub_type/test_include.rb +34 -0
- data/spec/unit/type/sub_type/test_initialize.rb +16 -0
- data/spec/unit/type/sub_type/test_name.rb +24 -0
- data/spec/unit/type/tuple_type/test_default_name.rb +14 -0
- data/spec/unit/type/tuple_type/test_dress.rb +112 -0
- data/spec/unit/type/tuple_type/test_equality.rb +32 -0
- data/spec/unit/type/tuple_type/test_include.rb +38 -0
- data/spec/unit/type/tuple_type/test_initialize.rb +30 -0
- data/spec/unit/type/tuple_type/test_name.rb +24 -0
- data/spec/unit/type/union_type/test_default_name.rb +12 -0
- data/spec/unit/type/union_type/test_dress.rb +43 -0
- data/spec/unit/type/union_type/test_equality.rb +30 -0
- data/spec/unit/type/union_type/test_include.rb +28 -0
- data/spec/unit/type/union_type/test_initialize.rb +24 -0
- data/spec/unit/type/union_type/test_name.rb +20 -0
- data/spec/unit/type_factory/dsl/test_adt.rb +54 -0
- data/spec/unit/type_factory/dsl/test_attribute.rb +37 -0
- data/spec/unit/type_factory/dsl/test_attributes.rb +41 -0
- data/spec/unit/type_factory/dsl/test_builtin.rb +45 -0
- data/spec/unit/type_factory/dsl/test_relation.rb +85 -0
- data/spec/unit/type_factory/dsl/test_seq.rb +57 -0
- data/spec/unit/type_factory/dsl/test_set.rb +57 -0
- data/spec/unit/type_factory/dsl/test_subtype.rb +91 -0
- data/spec/unit/type_factory/dsl/test_tuple.rb +73 -0
- data/spec/unit/type_factory/dsl/test_union.rb +81 -0
- data/spec/unit/type_factory/factory/test_builtin.rb +24 -0
- data/spec/unit/type_factory/factory/test_seq_type.rb +44 -0
- data/spec/unit/type_factory/factory/test_set_type.rb +44 -0
- data/spec/unit/type_factory/factory/test_sub_type.rb +53 -0
- data/spec/unit/type_factory/factory/test_tuple_type.rb +43 -0
- data/tasks/gem.rake +73 -0
- data/tasks/test.rake +31 -0
- metadata +344 -0
data/lib/qrb/support.rb
ADDED
|
@@ -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,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,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,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
|
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
|