expressir 1.3.0.pre.1-aarch64-linux-gnu

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +7 -0
  2. data/.cross_rubies +20 -0
  3. data/.github/workflows/rake.yml +312 -0
  4. data/.github/workflows/release.yml +124 -0
  5. data/.gitignore +23 -0
  6. data/.gitmodules +6 -0
  7. data/.hound.yml +3 -0
  8. data/.rspec +2 -0
  9. data/.rubocop.yml +18 -0
  10. data/.yardopts +11 -0
  11. data/Gemfile +4 -0
  12. data/README.adoc +155 -0
  13. data/Rakefile +17 -0
  14. data/bin/console +11 -0
  15. data/bin/rspec +29 -0
  16. data/bin/setup +8 -0
  17. data/docs/development.md +90 -0
  18. data/exe/expressir +22 -0
  19. data/exe/format +18 -0
  20. data/exe/format-test +81 -0
  21. data/exe/generate-parser +51 -0
  22. data/expressir.gemspec +48 -0
  23. data/lib/expressir/cli/ui.rb +36 -0
  24. data/lib/expressir/cli.rb +21 -0
  25. data/lib/expressir/config.rb +23 -0
  26. data/lib/expressir/express/2.7/express_parser.so +0 -0
  27. data/lib/expressir/express/3.0/express_parser.so +0 -0
  28. data/lib/expressir/express/3.1/express_parser.so +0 -0
  29. data/lib/expressir/express/3.2/express_parser.so +0 -0
  30. data/lib/expressir/express/cache.rb +51 -0
  31. data/lib/expressir/express/extension.rb +30 -0
  32. data/lib/expressir/express/formatter.rb +1608 -0
  33. data/lib/expressir/express/hyperlink_formatter.rb +36 -0
  34. data/lib/expressir/express/model_visitor.rb +24 -0
  35. data/lib/expressir/express/parser.rb +79 -0
  36. data/lib/expressir/express/resolve_references_model_visitor.rb +31 -0
  37. data/lib/expressir/express/schema_head_formatter.rb +23 -0
  38. data/lib/expressir/express/visitor.rb +2581 -0
  39. data/lib/expressir/model/cache.rb +17 -0
  40. data/lib/expressir/model/data_type.rb +9 -0
  41. data/lib/expressir/model/data_types/aggregate.rb +31 -0
  42. data/lib/expressir/model/data_types/array.rb +31 -0
  43. data/lib/expressir/model/data_types/bag.rb +25 -0
  44. data/lib/expressir/model/data_types/binary.rb +22 -0
  45. data/lib/expressir/model/data_types/boolean.rb +10 -0
  46. data/lib/expressir/model/data_types/enumeration.rb +25 -0
  47. data/lib/expressir/model/data_types/enumeration_item.rb +26 -0
  48. data/lib/expressir/model/data_types/generic.rb +26 -0
  49. data/lib/expressir/model/data_types/generic_entity.rb +26 -0
  50. data/lib/expressir/model/data_types/integer.rb +10 -0
  51. data/lib/expressir/model/data_types/list.rb +28 -0
  52. data/lib/expressir/model/data_types/logical.rb +10 -0
  53. data/lib/expressir/model/data_types/number.rb +10 -0
  54. data/lib/expressir/model/data_types/real.rb +19 -0
  55. data/lib/expressir/model/data_types/select.rb +28 -0
  56. data/lib/expressir/model/data_types/set.rb +25 -0
  57. data/lib/expressir/model/data_types/string.rb +22 -0
  58. data/lib/expressir/model/declaration.rb +9 -0
  59. data/lib/expressir/model/declarations/attribute.rb +47 -0
  60. data/lib/expressir/model/declarations/constant.rb +34 -0
  61. data/lib/expressir/model/declarations/entity.rb +53 -0
  62. data/lib/expressir/model/declarations/function.rb +67 -0
  63. data/lib/expressir/model/declarations/interface.rb +28 -0
  64. data/lib/expressir/model/declarations/interface_item.rb +23 -0
  65. data/lib/expressir/model/declarations/interfaced_item.rb +37 -0
  66. data/lib/expressir/model/declarations/parameter.rb +34 -0
  67. data/lib/expressir/model/declarations/procedure.rb +64 -0
  68. data/lib/expressir/model/declarations/remark_item.rb +21 -0
  69. data/lib/expressir/model/declarations/rule.rb +71 -0
  70. data/lib/expressir/model/declarations/schema.rb +117 -0
  71. data/lib/expressir/model/declarations/schema_version.rb +22 -0
  72. data/lib/expressir/model/declarations/schema_version_item.rb +22 -0
  73. data/lib/expressir/model/declarations/subtype_constraint.rb +40 -0
  74. data/lib/expressir/model/declarations/type.rb +45 -0
  75. data/lib/expressir/model/declarations/unique_rule.rb +31 -0
  76. data/lib/expressir/model/declarations/variable.rb +34 -0
  77. data/lib/expressir/model/declarations/where_rule.rb +31 -0
  78. data/lib/expressir/model/expression.rb +9 -0
  79. data/lib/expressir/model/expressions/aggregate_initializer.rb +19 -0
  80. data/lib/expressir/model/expressions/aggregate_initializer_item.rb +22 -0
  81. data/lib/expressir/model/expressions/binary_expression.rb +53 -0
  82. data/lib/expressir/model/expressions/entity_constructor.rb +22 -0
  83. data/lib/expressir/model/expressions/function_call.rb +22 -0
  84. data/lib/expressir/model/expressions/interval.rb +34 -0
  85. data/lib/expressir/model/expressions/query_expression.rb +35 -0
  86. data/lib/expressir/model/expressions/unary_expression.rb +27 -0
  87. data/lib/expressir/model/identifier.rb +34 -0
  88. data/lib/expressir/model/literal.rb +9 -0
  89. data/lib/expressir/model/literals/binary.rb +19 -0
  90. data/lib/expressir/model/literals/integer.rb +19 -0
  91. data/lib/expressir/model/literals/logical.rb +23 -0
  92. data/lib/expressir/model/literals/real.rb +19 -0
  93. data/lib/expressir/model/literals/string.rb +22 -0
  94. data/lib/expressir/model/model_element.rb +208 -0
  95. data/lib/expressir/model/reference.rb +9 -0
  96. data/lib/expressir/model/references/attribute_reference.rb +22 -0
  97. data/lib/expressir/model/references/group_reference.rb +22 -0
  98. data/lib/expressir/model/references/index_reference.rb +27 -0
  99. data/lib/expressir/model/references/simple_reference.rb +24 -0
  100. data/lib/expressir/model/repository.rb +23 -0
  101. data/lib/expressir/model/statement.rb +9 -0
  102. data/lib/expressir/model/statements/alias.rb +35 -0
  103. data/lib/expressir/model/statements/assignment.rb +22 -0
  104. data/lib/expressir/model/statements/case.rb +25 -0
  105. data/lib/expressir/model/statements/case_action.rb +22 -0
  106. data/lib/expressir/model/statements/compound.rb +19 -0
  107. data/lib/expressir/model/statements/escape.rb +10 -0
  108. data/lib/expressir/model/statements/if.rb +25 -0
  109. data/lib/expressir/model/statements/null.rb +10 -0
  110. data/lib/expressir/model/statements/procedure_call.rb +22 -0
  111. data/lib/expressir/model/statements/repeat.rb +47 -0
  112. data/lib/expressir/model/statements/return.rb +19 -0
  113. data/lib/expressir/model/statements/skip.rb +10 -0
  114. data/lib/expressir/model/supertype_expression.rb +9 -0
  115. data/lib/expressir/model/supertype_expressions/binary_supertype_expression.rb +29 -0
  116. data/lib/expressir/model/supertype_expressions/oneof_supertype_expression.rb +19 -0
  117. data/lib/expressir/model.rb +79 -0
  118. data/lib/expressir/version.rb +3 -0
  119. data/lib/expressir.rb +24 -0
  120. data/rakelib/antlr4-native.rake +161 -0
  121. data/rakelib/cross-ruby.rake +383 -0
  122. data/spec/acceptance/version_spec.rb +27 -0
  123. data/spec/expressir/express/cache_spec.rb +89 -0
  124. data/spec/expressir/express/formatter_spec.rb +173 -0
  125. data/spec/expressir/express/parser_spec.rb +141 -0
  126. data/spec/expressir/model/model_element_spec.rb +318 -0
  127. data/spec/spec_helper.rb +24 -0
  128. data/spec/support/console_helper.rb +29 -0
  129. data/spec/syntax/multiple.exp +23 -0
  130. data/spec/syntax/multiple.yaml +198 -0
  131. data/spec/syntax/multiple_formatted.exp +71 -0
  132. data/spec/syntax/multiple_hyperlink_formatted.exp +71 -0
  133. data/spec/syntax/multiple_schema_head_hyperlink_formatted.exp +13 -0
  134. data/spec/syntax/remark.exp +193 -0
  135. data/spec/syntax/remark.yaml +471 -0
  136. data/spec/syntax/remark_formatted.exp +228 -0
  137. data/spec/syntax/single.exp +4 -0
  138. data/spec/syntax/single.yaml +18 -0
  139. data/spec/syntax/single_formatted.exp +10 -0
  140. data/spec/syntax/single_formatted.yaml +36 -0
  141. data/spec/syntax/syntax.exp +333 -0
  142. data/spec/syntax/syntax.yaml +3509 -0
  143. data/spec/syntax/syntax_formatted.exp +902 -0
  144. data/spec/syntax/syntax_hyperlink_formatted.exp +902 -0
  145. data/spec/syntax/syntax_schema_head_formatted.exp +18 -0
  146. metadata +391 -0
@@ -0,0 +1,2581 @@
1
+ require_relative 'extension'
2
+ require "expressir/model"
3
+ require "set"
4
+
5
+ # reference type is not recognized
6
+ # see note in A.1.5 Interpreted identifiers
7
+ # > It is expected that identifiers matching these syntax rules are known to an implementation.
8
+ # > How the implementation obtains this information is of no concern to the definition of the language. One
9
+ # > method of gaining this information is multi-pass parsing: the first pass collects the identifiers from their
10
+ # > declarations, so that subsequent passes are then able to distinguish a veriable_ref from a function_ref,
11
+ # > for example.
12
+ # - such multi-pass parsing is not implemented yet
13
+ # - xxxRef - merged to SimpleReference
14
+ # - entityConstructor, functionCall - merged to FunctionCall
15
+ #
16
+ # difference between generalized and instantiable types is not recognized
17
+ # see note in 8.6.2 Parameter data types
18
+ # > A syntactic construct such as ARRAY[1:3] OF REAL satisfies two syntactic productions —
19
+ # > aggregation_type and general_aggregation_type. It is considered to be instantiable no matter which
20
+ # > production it is required to satisfy in the syntax.
21
+ #
22
+ # static shorthands are unwrapped
23
+ # - entity attributes, function/procedure parameters, local variables
24
+ #
25
+ # all access to ctx members must happen before calling other visitor code
26
+ # - prevents segfault in ANTLR4 C++ runtime, not sure why they are caused
27
+ # - e.g. see visit_schema_decl
28
+
29
+ module Expressir
30
+ module Express
31
+ class Visitor < ::ExpressParser::Visitor
32
+ REMARK_CHANNEL = 2
33
+
34
+ private_constant :REMARK_CHANNEL
35
+
36
+ # @param [Array<::ExpressParser::Token>] tokens
37
+ # @param [Boolean] include_source attach original source code to model elements
38
+ def initialize(tokens, include_source: nil)
39
+ @tokens = tokens
40
+ @include_source = include_source
41
+
42
+ @attached_remark_tokens = ::Set.new
43
+
44
+ super()
45
+ end
46
+
47
+ def visit(ctx)
48
+ node = super(ctx)
49
+ if @include_source
50
+ attach_source(ctx, node)
51
+ end
52
+ attach_remarks(ctx, node)
53
+ node
54
+ end
55
+
56
+ private
57
+
58
+ def visit_if(ctx, default = nil)
59
+ if ctx
60
+ visit(ctx)
61
+ else
62
+ default
63
+ end
64
+ end
65
+
66
+ def visit_if_map(ctx)
67
+ if ctx
68
+ ctx.map{|ctx2| visit(ctx2)}
69
+ else
70
+ []
71
+ end
72
+ end
73
+
74
+ def visit_if_map_flatten(ctx)
75
+ if ctx
76
+ ctx.map{|ctx2| visit(ctx2)}.flatten
77
+ else
78
+ []
79
+ end
80
+ end
81
+
82
+ def get_tokens_source(tokens)
83
+ if tokens.last.text == '<EOF>'
84
+ tokens.pop
85
+ end
86
+
87
+ tokens.map{|x| x.text}.join('').force_encoding('UTF-8')
88
+ end
89
+
90
+ def get_tokens(ctx)
91
+ start_index, stop_index = if ctx.is_a? ::ExpressParser::SyntaxContext
92
+ [0, @tokens.size - 1]
93
+ else
94
+ [ctx.start.token_index, ctx.stop.token_index]
95
+ end
96
+
97
+ @tokens[start_index..stop_index]
98
+ end
99
+
100
+ def attach_source(ctx, node)
101
+ if node.class.method_defined? :source
102
+ tokens = get_tokens(ctx)
103
+ node.source = get_tokens_source(tokens)
104
+ end
105
+ end
106
+
107
+ def node_find(node, path)
108
+ if node.is_a?(Enumerable)
109
+ target_node = node.find { |item| item.find(path) }
110
+ else
111
+ target_node = node.find(path)
112
+ end
113
+ target_node
114
+ end
115
+
116
+ def find_remark_target(node, path)
117
+ target_node = node_find(node, path)
118
+
119
+ return target_node if target_node
120
+
121
+ # check if path can create implicit remark item
122
+ # see https://github.com/lutaml/expressir/issues/78
123
+ rest, _, current_path = path.rpartition(".") # get last path part
124
+ _, _, current_path = current_path.rpartition(":") # ignore prefix
125
+ parent_node = node_find(node, rest)
126
+ if parent_node and parent_node.class.method_defined? :remark_items
127
+ remark_item = Model::Declarations::RemarkItem.new(
128
+ id: current_path
129
+ )
130
+ remark_item.parent = parent_node
131
+
132
+ # check if path can create implicit informal proposition
133
+ # see https://github.com/lutaml/expressir/issues/50
134
+ if parent_node.class.method_defined? :informal_propositions and current_path.match(/^IP\d+$/)
135
+ parent_node.informal_propositions << remark_item
136
+ else
137
+ parent_node.remark_items << remark_item
138
+ end
139
+ parent_node.reset_children_by_id
140
+
141
+ remark_item
142
+ end
143
+ end
144
+
145
+ def attach_remarks(ctx, node)
146
+ @remark_tokens = get_tokens(ctx)
147
+ @remark_tokens = @remark_tokens.select{ |x| x.channel == REMARK_CHANNEL
148
+ }
149
+
150
+ # skip already attached remarks
151
+ @remark_tokens = @remark_tokens.select{|x| !@attached_remark_tokens.include?(x)}
152
+
153
+ # parse remarks, find remark targets
154
+ tagged_remark_tokens = @remark_tokens.map do |remark_token|
155
+ _, remark_tag, remark_text = if remark_token.text.start_with?('--')
156
+ remark_token.text.match(/^--"([^"]*)"(.*)$/).to_a
157
+ else
158
+ remark_token.text.match(/^\(\*"([^"]*)"(.*)\*\)$/m).to_a
159
+ end
160
+
161
+ if remark_tag
162
+ remark_target = find_remark_target(node, remark_tag)
163
+ end
164
+ if remark_text
165
+ remark_text = remark_text.strip.force_encoding('UTF-8')
166
+ end
167
+
168
+ [remark_token, remark_target, remark_text]
169
+ end.select{|x| x[1]}
170
+
171
+ tagged_remark_tokens.each do |remark_token, remark_target, remark_text|
172
+ # attach remark
173
+ remark_target.remarks ||= []
174
+ remark_target.remarks << remark_text
175
+
176
+ # mark remark as attached, so that it is not attached again at higher nesting level
177
+ @attached_remark_tokens << remark_token
178
+ end
179
+ end
180
+
181
+ def visit_attribute_ref(ctx)
182
+ ctx__attribute_id = ctx.attribute_id
183
+
184
+ id = visit_if(ctx__attribute_id)
185
+
186
+ Model::References::SimpleReference.new(
187
+ id: id
188
+ )
189
+ end
190
+
191
+ def visit_constant_ref(ctx)
192
+ ctx__constant_id = ctx.constant_id
193
+
194
+ id = visit_if(ctx__constant_id)
195
+
196
+ Model::References::SimpleReference.new(
197
+ id: id
198
+ )
199
+ end
200
+
201
+ def visit_entity_ref(ctx)
202
+ ctx__entity_id = ctx.entity_id
203
+
204
+ id = visit_if(ctx__entity_id)
205
+
206
+ Model::References::SimpleReference.new(
207
+ id: id
208
+ )
209
+ end
210
+
211
+ def visit_enumeration_ref(ctx)
212
+ ctx__enumeration_id = ctx.enumeration_id
213
+
214
+ id = visit_if(ctx__enumeration_id)
215
+
216
+ Model::References::SimpleReference.new(
217
+ id: id
218
+ )
219
+ end
220
+
221
+ def visit_function_ref(ctx)
222
+ ctx__function_id = ctx.function_id
223
+
224
+ id = visit_if(ctx__function_id)
225
+
226
+ Model::References::SimpleReference.new(
227
+ id: id
228
+ )
229
+ end
230
+
231
+ def visit_parameter_ref(ctx)
232
+ ctx__parameter_id = ctx.parameter_id
233
+
234
+ id = visit_if(ctx__parameter_id)
235
+
236
+ Model::References::SimpleReference.new(
237
+ id: id
238
+ )
239
+ end
240
+
241
+ def visit_procedure_ref(ctx)
242
+ ctx__procedure_id = ctx.procedure_id
243
+
244
+ id = visit_if(ctx__procedure_id)
245
+
246
+ Model::References::SimpleReference.new(
247
+ id: id
248
+ )
249
+ end
250
+
251
+ def visit_rule_label_ref(ctx)
252
+ ctx__rule_label_id = ctx.rule_label_id
253
+
254
+ id = visit_if(ctx__rule_label_id)
255
+
256
+ Model::References::SimpleReference.new(
257
+ id: id
258
+ )
259
+ end
260
+
261
+ def visit_rule_ref(ctx)
262
+ ctx__rule_id = ctx.rule_id
263
+
264
+ id = visit_if(ctx__rule_id)
265
+
266
+ Model::References::SimpleReference.new(
267
+ id: id
268
+ )
269
+ end
270
+
271
+ def visit_schema_ref(ctx)
272
+ ctx__schema_id = ctx.schema_id
273
+
274
+ id = visit_if(ctx__schema_id)
275
+
276
+ Model::References::SimpleReference.new(
277
+ id: id
278
+ )
279
+ end
280
+
281
+ def visit_subtype_constraint_ref(ctx)
282
+ ctx__subtype_constraint_id = ctx.subtype_constraint_id
283
+
284
+ id = visit_if(ctx__subtype_constraint_id)
285
+
286
+ Model::References::SimpleReference.new(
287
+ id: id
288
+ )
289
+ end
290
+
291
+ def visit_type_label_ref(ctx)
292
+ ctx__type_label_id = ctx.type_label_id
293
+
294
+ id = visit_if(ctx__type_label_id)
295
+
296
+ Model::References::SimpleReference.new(
297
+ id: id
298
+ )
299
+ end
300
+
301
+ def visit_type_ref(ctx)
302
+ ctx__type_id = ctx.type_id
303
+
304
+ id = visit_if(ctx__type_id)
305
+
306
+ Model::References::SimpleReference.new(
307
+ id: id
308
+ )
309
+ end
310
+
311
+ def visit_variable_ref(ctx)
312
+ ctx__variable_id = ctx.variable_id
313
+
314
+ id = visit_if(ctx__variable_id)
315
+
316
+ Model::References::SimpleReference.new(
317
+ id: id
318
+ )
319
+ end
320
+
321
+ def visit_abstract_entity_declaration(ctx)
322
+ raise 'Invalid state'
323
+ end
324
+
325
+ def visit_abstract_supertype(ctx)
326
+ raise 'Invalid state'
327
+ end
328
+
329
+ def visit_abstract_supertype_declaration(ctx)
330
+ ctx__subtype_constraint = ctx.subtype_constraint
331
+
332
+ visit_if(ctx__subtype_constraint)
333
+ end
334
+
335
+ def visit_actual_parameter_list(ctx)
336
+ ctx__parameter = ctx.parameter
337
+
338
+ visit_if_map(ctx__parameter)
339
+ end
340
+
341
+ def visit_add_like_op(ctx)
342
+ ctx__text = ctx.text
343
+ ctx__ADDITION = ctx__text == '+'
344
+ ctx__SUBTRACTION = ctx__text == '-'
345
+ ctx__OR = ctx.OR
346
+ ctx__XOR = ctx.XOR
347
+
348
+ if ctx__ADDITION
349
+ Model::Expressions::BinaryExpression::ADDITION
350
+ elsif ctx__SUBTRACTION
351
+ Model::Expressions::BinaryExpression::SUBTRACTION
352
+ elsif ctx__OR
353
+ Model::Expressions::BinaryExpression::OR
354
+ elsif ctx__XOR
355
+ Model::Expressions::BinaryExpression::XOR
356
+ else
357
+ raise 'Invalid state'
358
+ end
359
+ end
360
+
361
+ def visit_aggregate_initializer(ctx)
362
+ ctx__element = ctx.element
363
+
364
+ items = visit_if_map(ctx__element)
365
+
366
+ Model::Expressions::AggregateInitializer.new(
367
+ items: items
368
+ )
369
+ end
370
+
371
+ def visit_aggregate_source(ctx)
372
+ ctx__simple_expression = ctx.simple_expression
373
+
374
+ visit_if(ctx__simple_expression)
375
+ end
376
+
377
+ def visit_aggregate_type(ctx)
378
+ ctx__type_label = ctx.type_label
379
+ ctx__parameter_type = ctx.parameter_type
380
+
381
+ id = visit_if(ctx__type_label)
382
+ base_type = visit_if(ctx__parameter_type)
383
+
384
+ Model::DataTypes::Aggregate.new(
385
+ id: id,
386
+ base_type: base_type
387
+ )
388
+ end
389
+
390
+ def visit_aggregation_types(ctx)
391
+ ctx__array_type = ctx.array_type
392
+ ctx__bag_type = ctx.bag_type
393
+ ctx__list_type = ctx.list_type
394
+ ctx__set_type = ctx.set_type
395
+
396
+ visit_if(ctx__array_type || ctx__bag_type || ctx__list_type || ctx__set_type)
397
+ end
398
+
399
+ def visit_algorithm_head(ctx)
400
+ raise 'Invalid state'
401
+ end
402
+
403
+ def visit_alias_stmt(ctx)
404
+ ctx__variable_id = ctx.variable_id
405
+ ctx__general_ref = ctx.general_ref
406
+ ctx__qualifier = ctx.qualifier
407
+ ctx__stmt = ctx.stmt
408
+
409
+ id = visit_if(ctx__variable_id)
410
+ expression = handle_qualified_ref(visit_if(ctx__general_ref), ctx__qualifier)
411
+ statements = visit_if_map(ctx__stmt)
412
+
413
+ Model::Statements::Alias.new(
414
+ id: id,
415
+ expression: expression,
416
+ statements: statements
417
+ )
418
+ end
419
+
420
+ def visit_array_type(ctx)
421
+ ctx__bound_spec = ctx.bound_spec
422
+ ctx__OPTIONAL = ctx.OPTIONAL
423
+ ctx__UNIQUE = ctx.UNIQUE
424
+ ctx__instantiable_type = ctx.instantiable_type
425
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
426
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
427
+
428
+ bound1 = visit_if(ctx__bound_spec__bound1)
429
+ bound2 = visit_if(ctx__bound_spec__bound2)
430
+ optional = ctx__OPTIONAL && true
431
+ unique = ctx__UNIQUE && true
432
+ base_type = visit_if(ctx__instantiable_type)
433
+
434
+ Model::DataTypes::Array.new(
435
+ bound1: bound1,
436
+ bound2: bound2,
437
+ optional: optional,
438
+ unique: unique,
439
+ base_type: base_type
440
+ )
441
+ end
442
+
443
+ def visit_assignment_stmt(ctx)
444
+ ctx__general_ref = ctx.general_ref
445
+ ctx__qualifier = ctx.qualifier
446
+ ctx__expression = ctx.expression
447
+
448
+ ref = handle_qualified_ref(visit_if(ctx__general_ref), ctx__qualifier)
449
+ expression = visit_if(ctx__expression)
450
+
451
+ Model::Statements::Assignment.new(
452
+ ref: ref,
453
+ expression: expression
454
+ )
455
+ end
456
+
457
+ def visit_attribute_decl(ctx)
458
+ ctx__attribute_id = ctx.attribute_id
459
+ ctx__redeclared_attribute = ctx.redeclared_attribute
460
+ ctx__redeclared_attribute__qualified_attribute = ctx__redeclared_attribute&.qualified_attribute
461
+ ctx__redeclared_attribute__attribute_id = ctx__redeclared_attribute&.attribute_id
462
+
463
+ id = visit_if(ctx__attribute_id || ctx__redeclared_attribute__attribute_id)
464
+ supertype_attribute = visit_if(ctx__redeclared_attribute__qualified_attribute)
465
+
466
+ Model::Declarations::Attribute.new(
467
+ id: id,
468
+ supertype_attribute: supertype_attribute
469
+ )
470
+ end
471
+
472
+ def visit_attribute_id(ctx)
473
+ ctx__SimpleId = ctx.SimpleId
474
+
475
+ handle_simple_id(ctx__SimpleId)
476
+ end
477
+
478
+ def visit_attribute_qualifier(ctx)
479
+ ctx__attribute_ref = ctx.attribute_ref
480
+
481
+ attribute = visit_if(ctx__attribute_ref)
482
+
483
+ Model::References::AttributeReference.new(
484
+ attribute: attribute
485
+ )
486
+ end
487
+
488
+ def visit_attribute_reference(ctx)
489
+ raise 'Invalid state'
490
+ end
491
+
492
+ def visit_bag_type(ctx)
493
+ ctx__bound_spec = ctx.bound_spec
494
+ ctx__instantiable_type = ctx.instantiable_type
495
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
496
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
497
+
498
+ bound1 = visit_if(ctx__bound_spec__bound1)
499
+ bound2 = visit_if(ctx__bound_spec__bound2)
500
+ base_type = visit_if(ctx__instantiable_type)
501
+
502
+ Model::DataTypes::Bag.new(
503
+ bound1: bound1,
504
+ bound2: bound2,
505
+ base_type: base_type
506
+ )
507
+ end
508
+
509
+ def visit_binary_type(ctx)
510
+ ctx__width_spec = ctx.width_spec
511
+ ctx__width_spec__width = ctx__width_spec&.width
512
+ ctx__width_spec__FIXED = ctx__width_spec&.FIXED
513
+
514
+ width = visit_if(ctx__width_spec__width)
515
+ fixed = ctx__width_spec__FIXED && true
516
+
517
+ Model::DataTypes::Binary.new(
518
+ width: width,
519
+ fixed: fixed
520
+ )
521
+ end
522
+
523
+ def visit_boolean_type(ctx)
524
+ Model::DataTypes::Boolean.new
525
+ end
526
+
527
+ def visit_bound1(ctx)
528
+ ctx__numeric_expression = ctx.numeric_expression
529
+
530
+ visit_if(ctx__numeric_expression)
531
+ end
532
+
533
+ def visit_bound2(ctx)
534
+ ctx__numeric_expression = ctx.numeric_expression
535
+
536
+ visit_if(ctx__numeric_expression)
537
+ end
538
+
539
+ def visit_bound_spec(ctx)
540
+ raise 'Invalid state'
541
+ end
542
+
543
+ def visit_built_in_constant(ctx)
544
+ ctx__text = ctx.text
545
+
546
+ id = ctx__text
547
+
548
+ Model::References::SimpleReference.new(
549
+ id: id
550
+ )
551
+ end
552
+
553
+ def visit_built_in_function(ctx)
554
+ ctx__text = ctx.text
555
+
556
+ id = ctx__text
557
+
558
+ Model::References::SimpleReference.new(
559
+ id: id
560
+ )
561
+ end
562
+
563
+ def visit_built_in_procedure(ctx)
564
+ ctx__text = ctx.text
565
+
566
+ id = ctx__text
567
+
568
+ Model::References::SimpleReference.new(
569
+ id: id
570
+ )
571
+ end
572
+
573
+ def visit_case_action(ctx)
574
+ ctx__case_label = ctx.case_label
575
+ ctx__stmt = ctx.stmt
576
+
577
+ labels = visit_if_map(ctx__case_label)
578
+ statement = visit_if(ctx__stmt)
579
+
580
+ Model::Statements::CaseAction.new(
581
+ labels: labels,
582
+ statement: statement
583
+ )
584
+ end
585
+
586
+ def visit_case_label(ctx)
587
+ ctx__expression = ctx.expression
588
+
589
+ visit_if(ctx__expression)
590
+ end
591
+
592
+ def visit_case_stmt(ctx)
593
+ ctx__selector = ctx.selector
594
+ ctx__case_action = ctx.case_action
595
+ ctx__stmt = ctx.stmt
596
+ ctx__selector__expression = ctx__selector&.expression
597
+
598
+ expression = visit_if(ctx__selector__expression)
599
+ actions = visit_if_map_flatten(ctx__case_action)
600
+ otherwise_statement = visit_if(ctx__stmt)
601
+
602
+ Model::Statements::Case.new(
603
+ expression: expression,
604
+ actions: actions,
605
+ otherwise_statement: otherwise_statement
606
+ )
607
+ end
608
+
609
+ def visit_compound_stmt(ctx)
610
+ ctx__stmt = ctx.stmt
611
+
612
+ statements = visit_if_map(ctx__stmt)
613
+
614
+ Model::Statements::Compound.new(
615
+ statements: statements
616
+ )
617
+ end
618
+
619
+ def visit_concrete_types(ctx)
620
+ ctx__aggregation_types = ctx.aggregation_types
621
+ ctx__simple_types = ctx.simple_types
622
+ ctx__type_ref = ctx.type_ref
623
+
624
+ visit_if(ctx__aggregation_types || ctx__simple_types || ctx__type_ref)
625
+ end
626
+
627
+ def visit_constant_body(ctx)
628
+ ctx__constant_id = ctx.constant_id
629
+ ctx__instantiable_type = ctx.instantiable_type
630
+ ctx__expression = ctx.expression
631
+
632
+ id = visit_if(ctx__constant_id)
633
+ type = visit_if(ctx__instantiable_type)
634
+ expression = visit_if(ctx__expression)
635
+
636
+ Model::Declarations::Constant.new(
637
+ id: id,
638
+ type: type,
639
+ expression: expression
640
+ )
641
+ end
642
+
643
+ def visit_constant_decl(ctx)
644
+ ctx__constant_body = ctx.constant_body
645
+
646
+ visit_if_map(ctx__constant_body)
647
+ end
648
+
649
+ def visit_constant_factor(ctx)
650
+ ctx__built_in_constant = ctx.built_in_constant
651
+ ctx__constant_ref = ctx.constant_ref
652
+
653
+ visit_if(ctx__built_in_constant || ctx__constant_ref)
654
+ end
655
+
656
+ def visit_constant_id(ctx)
657
+ ctx__SimpleId = ctx.SimpleId
658
+
659
+ handle_simple_id(ctx__SimpleId)
660
+ end
661
+
662
+ def visit_constructed_types(ctx)
663
+ ctx__enumeration_type = ctx.enumeration_type
664
+ ctx__select_type = ctx.select_type
665
+
666
+ visit_if(ctx__enumeration_type || ctx__select_type)
667
+ end
668
+
669
+ def visit_declaration(ctx)
670
+ ctx__entity_decl = ctx.entity_decl
671
+ ctx__function_decl = ctx.function_decl
672
+ ctx__procedure_decl = ctx.procedure_decl
673
+ ctx__subtype_constraint_decl = ctx.subtype_constraint_decl
674
+ ctx__type_decl = ctx.type_decl
675
+
676
+ visit_if(ctx__entity_decl || ctx__function_decl || ctx__procedure_decl || ctx__subtype_constraint_decl || ctx__type_decl)
677
+ end
678
+
679
+ def visit_derived_attr(ctx)
680
+ ctx__attribute_decl = ctx.attribute_decl
681
+ ctx__parameter_type = ctx.parameter_type
682
+ ctx__expression = ctx.expression
683
+
684
+ attribute = visit_if(ctx__attribute_decl)
685
+ type = visit_if(ctx__parameter_type)
686
+ expression = visit_if(ctx__expression)
687
+
688
+ Model::Declarations::Attribute.new(
689
+ id: attribute.id, # reuse
690
+ kind: Model::Declarations::Attribute::DERIVED,
691
+ supertype_attribute: attribute.supertype_attribute, # reuse
692
+ type: type,
693
+ expression: expression
694
+ )
695
+ end
696
+
697
+ def visit_derive_clause(ctx)
698
+ ctx__derived_attr = ctx.derived_attr
699
+
700
+ visit_if_map(ctx__derived_attr)
701
+ end
702
+
703
+ def visit_domain_rule(ctx)
704
+ ctx__rule_label_id = ctx.rule_label_id
705
+ ctx__expression = ctx.expression
706
+
707
+ id = visit_if(ctx__rule_label_id)
708
+ expression = visit_if(ctx__expression)
709
+
710
+ Model::Declarations::WhereRule.new(
711
+ id: id,
712
+ expression: expression
713
+ )
714
+ end
715
+
716
+ def visit_element(ctx)
717
+ ctx__expression = ctx.expression
718
+ ctx__repetition = ctx.repetition
719
+
720
+ if ctx__repetition
721
+ expression = visit_if(ctx__expression)
722
+ repetition = visit_if(ctx__repetition)
723
+
724
+ Model::Expressions::AggregateInitializerItem.new(
725
+ expression: expression,
726
+ repetition: repetition
727
+ )
728
+ else
729
+ visit_if(ctx__expression)
730
+ end
731
+ end
732
+
733
+ def visit_entity_body(ctx)
734
+ raise 'Invalid state'
735
+ end
736
+
737
+ def visit_entity_constructor(ctx)
738
+ ctx__entity_ref = ctx.entity_ref
739
+ ctx__expression = ctx.expression
740
+
741
+ entity = visit_if(ctx__entity_ref)
742
+ parameters = visit_if_map(ctx__expression)
743
+
744
+ # Model::Expressions::EntityConstructor.new(
745
+ # entity: entity,
746
+ # parameters: parameters
747
+ # )
748
+ Model::Expressions::FunctionCall.new(
749
+ function: entity,
750
+ parameters: parameters
751
+ )
752
+ end
753
+
754
+ def visit_entity_decl(ctx)
755
+ ctx__entity_head = ctx.entity_head
756
+ ctx__entity_body = ctx.entity_body
757
+ ctx__entity_head__entity_id = ctx__entity_head&.entity_id
758
+ ctx__entity_head__subsuper = ctx__entity_head&.subsuper
759
+ ctx__entity_head__subsuper__supertype_constraint = ctx__entity_head__subsuper&.supertype_constraint
760
+ ctx__entity_head__subsuper__supertype_constraint__abstract_entity_declaration = ctx__entity_head__subsuper__supertype_constraint&.abstract_entity_declaration
761
+ ctx__entity_head__subsuper__supertype_constraint__abstract_supertype_declaration = ctx__entity_head__subsuper__supertype_constraint&.abstract_supertype_declaration
762
+ ctx__entity_head__subsuper__supertype_constraint__supertype_rule = ctx__entity_head__subsuper__supertype_constraint&.supertype_rule
763
+ ctx__entity_head__subsuper__subtype_declaration = ctx__entity_head__subsuper&.subtype_declaration
764
+ ctx__entity_body__explicit_attr = ctx__entity_body&.explicit_attr
765
+ ctx__entity_body__derive_clause = ctx__entity_body&.derive_clause
766
+ ctx__entity_body__inverse_clause = ctx__entity_body&.inverse_clause
767
+ ctx__entity_body__unique_clause = ctx__entity_body&.unique_clause
768
+ ctx__entity_body__where_clause = ctx__entity_body&.where_clause
769
+
770
+ id = visit_if(ctx__entity_head__entity_id)
771
+ abstract = (ctx__entity_head__subsuper__supertype_constraint__abstract_entity_declaration || ctx__entity_head__subsuper__supertype_constraint__abstract_supertype_declaration) && true
772
+ supertype_expression = visit_if(ctx__entity_head__subsuper__supertype_constraint__abstract_supertype_declaration || ctx__entity_head__subsuper__supertype_constraint__supertype_rule)
773
+ subtype_of = visit_if(ctx__entity_head__subsuper__subtype_declaration, [])
774
+ attributes = [
775
+ *visit_if_map_flatten(ctx__entity_body__explicit_attr),
776
+ *visit_if(ctx__entity_body__derive_clause),
777
+ *visit_if(ctx__entity_body__inverse_clause)
778
+ ]
779
+ unique_rules = visit_if(ctx__entity_body__unique_clause, [])
780
+ where_rules = visit_if(ctx__entity_body__where_clause, [])
781
+
782
+ Model::Declarations::Entity.new(
783
+ id: id,
784
+ abstract: abstract,
785
+ supertype_expression: supertype_expression,
786
+ subtype_of: subtype_of,
787
+ attributes: attributes,
788
+ unique_rules: unique_rules,
789
+ where_rules: where_rules
790
+ )
791
+ end
792
+
793
+ def visit_entity_head(ctx)
794
+ raise 'Invalid state'
795
+ end
796
+
797
+ def visit_entity_id(ctx)
798
+ ctx__SimpleId = ctx.SimpleId
799
+
800
+ handle_simple_id(ctx__SimpleId)
801
+ end
802
+
803
+ def visit_enumeration_extension(ctx)
804
+ raise 'Invalid state'
805
+ end
806
+
807
+ def visit_enumeration_id(ctx)
808
+ ctx__SimpleId = ctx.SimpleId
809
+
810
+ handle_simple_id(ctx__SimpleId)
811
+ end
812
+
813
+ def visit_enumeration_items(ctx)
814
+ ctx__enumeration_item = ctx.enumeration_item
815
+
816
+ visit_if_map(ctx__enumeration_item)
817
+ end
818
+
819
+ def visit_enumeration_item(ctx)
820
+ ctx__enumeration_id = ctx.enumeration_id
821
+
822
+ id = visit_if(ctx__enumeration_id)
823
+
824
+ Model::DataTypes::EnumerationItem.new(
825
+ id: id
826
+ )
827
+ end
828
+
829
+ def visit_enumeration_reference(ctx)
830
+ ctx__type_ref = ctx.type_ref
831
+ ctx__enumeration_ref = ctx.enumeration_ref
832
+
833
+ if ctx__type_ref
834
+ ref = visit_if(ctx__type_ref)
835
+ attribute = visit_if(ctx__enumeration_ref)
836
+
837
+ Model::References::AttributeReference.new(
838
+ ref: ref,
839
+ attribute: attribute
840
+ )
841
+ else
842
+ visit_if(ctx__enumeration_ref)
843
+ end
844
+ end
845
+
846
+ def visit_enumeration_type(ctx)
847
+ ctx__EXTENSIBLE = ctx.EXTENSIBLE
848
+ ctx__enumeration_items = ctx.enumeration_items
849
+ ctx__enumeration_extension = ctx.enumeration_extension
850
+ ctx__enumeration_extension__type_ref = ctx__enumeration_extension&.type_ref
851
+ ctx__enumeration_extension__enumeration_items = ctx__enumeration_extension&.enumeration_items
852
+
853
+ extensible = ctx__EXTENSIBLE && true
854
+ based_on = visit_if(ctx__enumeration_extension__type_ref)
855
+ items = visit_if(ctx__enumeration_items || ctx__enumeration_extension__enumeration_items, [])
856
+
857
+ Model::DataTypes::Enumeration.new(
858
+ extensible: extensible,
859
+ based_on: based_on,
860
+ items: items
861
+ )
862
+ end
863
+
864
+ def visit_escape_stmt(ctx)
865
+ Model::Statements::Escape.new
866
+ end
867
+
868
+ def visit_explicit_attr(ctx)
869
+ ctx__attribute_decl = ctx.attribute_decl
870
+ ctx__OPTIONAL = ctx.OPTIONAL
871
+ ctx__parameter_type = ctx.parameter_type
872
+
873
+ attributes = visit_if_map(ctx__attribute_decl)
874
+ optional = ctx__OPTIONAL && true
875
+ type = visit_if(ctx__parameter_type)
876
+
877
+ attributes.map do |attribute|
878
+ Model::Declarations::Attribute.new(
879
+ id: attribute.id, # reuse
880
+ kind: Model::Declarations::Attribute::EXPLICIT,
881
+ supertype_attribute: attribute.supertype_attribute, # reuse
882
+ optional: optional,
883
+ type: type
884
+ )
885
+ end
886
+ end
887
+
888
+ def visit_expression(ctx)
889
+ ctx__simple_expression = ctx.simple_expression
890
+ ctx__rel_op_extended = ctx.rel_op_extended
891
+
892
+ if ctx__simple_expression.length == 2
893
+ operator = visit_if(ctx__rel_op_extended)
894
+ operand1 = visit_if(ctx__simple_expression[0])
895
+ operand2 = visit_if(ctx__simple_expression[1])
896
+
897
+ Model::Expressions::BinaryExpression.new(
898
+ operator: operator,
899
+ operand1: operand1,
900
+ operand2: operand2
901
+ )
902
+ else
903
+ visit_if(ctx__simple_expression[0])
904
+ end
905
+ end
906
+
907
+ def visit_factor(ctx)
908
+ ctx__simple_factor = ctx.simple_factor
909
+
910
+ if ctx__simple_factor.length == 2
911
+ operator = Model::Expressions::BinaryExpression::EXPONENTIATION
912
+ operand1 = visit(ctx__simple_factor[0])
913
+ operand2 = visit(ctx__simple_factor[1])
914
+
915
+ Model::Expressions::BinaryExpression.new(
916
+ operator: operator,
917
+ operand1: operand1,
918
+ operand2: operand2
919
+ )
920
+ elsif ctx__simple_factor.length == 1
921
+ visit_if(ctx__simple_factor[0])
922
+ else
923
+ raise 'Invalid state'
924
+ end
925
+ end
926
+
927
+ def visit_formal_parameter(ctx)
928
+ ctx__parameter_id = ctx.parameter_id
929
+ ctx__parameter_type = ctx.parameter_type
930
+
931
+ ids = visit_if_map(ctx__parameter_id)
932
+ type = visit_if(ctx__parameter_type)
933
+
934
+ ids.map do |id|
935
+ Model::Declarations::Parameter.new(
936
+ id: id,
937
+ type: type
938
+ )
939
+ end
940
+ end
941
+
942
+ def visit_function_call(ctx)
943
+ ctx__built_in_function = ctx.built_in_function
944
+ ctx__function_ref = ctx.function_ref
945
+ ctx__actual_parameter_list = ctx.actual_parameter_list
946
+
947
+ function = visit_if(ctx__built_in_function || ctx__function_ref)
948
+ parameters = visit_if(ctx__actual_parameter_list, [])
949
+
950
+ Model::Expressions::FunctionCall.new(
951
+ function: function,
952
+ parameters: parameters
953
+ )
954
+ end
955
+
956
+ def visit_function_decl(ctx)
957
+ ctx__function_head = ctx.function_head
958
+ ctx__algorithm_head = ctx.algorithm_head
959
+ ctx__stmt = ctx.stmt
960
+ ctx__function_head__function_id = ctx__function_head&.function_id
961
+ ctx__function_head__formal_parameter = ctx__function_head&.formal_parameter
962
+ ctx__function_head__parameter_type = ctx__function_head&.parameter_type
963
+ ctx__algorithm_head__declaration = ctx__algorithm_head&.declaration
964
+ ctx__algorithm_head__constant_decl = ctx__algorithm_head&.constant_decl
965
+ ctx__algorithm_head__local_decl = ctx__algorithm_head&.local_decl
966
+
967
+ id = visit_if(ctx__function_head__function_id)
968
+ parameters = visit_if_map_flatten(ctx__function_head__formal_parameter)
969
+ return_type = visit_if(ctx__function_head__parameter_type)
970
+ declarations = visit_if_map(ctx__algorithm_head__declaration)
971
+ types = declarations.select{|x| x.is_a? Model::Declarations::Type}
972
+ entities = declarations.select{|x| x.is_a? Model::Declarations::Entity}
973
+ subtype_constraints = declarations.select{|x| x.is_a? Model::Declarations::SubtypeConstraint}
974
+ functions = declarations.select{|x| x.is_a? Model::Declarations::Function}
975
+ procedures = declarations.select{|x| x.is_a? Model::Declarations::Procedure}
976
+ constants = visit_if(ctx__algorithm_head__constant_decl, [])
977
+ variables = visit_if(ctx__algorithm_head__local_decl, [])
978
+ statements = visit_if_map(ctx__stmt)
979
+
980
+ Model::Declarations::Function.new(
981
+ id: id,
982
+ parameters: parameters,
983
+ return_type: return_type,
984
+ types: types,
985
+ entities: entities,
986
+ subtype_constraints: subtype_constraints,
987
+ functions: functions,
988
+ procedures: procedures,
989
+ constants: constants,
990
+ variables: variables,
991
+ statements: statements
992
+ )
993
+ end
994
+
995
+ def visit_function_head(ctx)
996
+ raise 'Invalid state'
997
+ end
998
+
999
+ def visit_function_id(ctx)
1000
+ ctx__SimpleId = ctx.SimpleId
1001
+
1002
+ handle_simple_id(ctx__SimpleId)
1003
+ end
1004
+
1005
+ def visit_generalized_types(ctx)
1006
+ ctx__aggregate_type = ctx.aggregate_type
1007
+ ctx__general_aggregation_types = ctx.general_aggregation_types
1008
+ ctx__generic_entity_type = ctx.generic_entity_type
1009
+ ctx__generic_type = ctx.generic_type
1010
+
1011
+ visit_if(ctx__aggregate_type || ctx__general_aggregation_types || ctx__generic_entity_type || ctx__generic_type)
1012
+ end
1013
+
1014
+ def visit_general_aggregation_types(ctx)
1015
+ ctx__general_array_type = ctx.general_array_type
1016
+ ctx__general_bag_type = ctx.general_bag_type
1017
+ ctx__general_list_type = ctx.general_list_type
1018
+ ctx__general_set_type = ctx.general_set_type
1019
+
1020
+ visit_if(ctx__general_array_type || ctx__general_bag_type || ctx__general_list_type || ctx__general_set_type)
1021
+ end
1022
+
1023
+ def visit_general_array_type(ctx)
1024
+ ctx__bound_spec = ctx.bound_spec
1025
+ ctx__OPTIONAL = ctx.OPTIONAL
1026
+ ctx__UNIQUE = ctx.UNIQUE
1027
+ ctx__parameter_type = ctx.parameter_type
1028
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
1029
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
1030
+
1031
+ bound1 = visit_if(ctx__bound_spec__bound1)
1032
+ bound2 = visit_if(ctx__bound_spec__bound2)
1033
+ optional = ctx__OPTIONAL && true
1034
+ unique = ctx__UNIQUE && true
1035
+ base_type = visit_if(ctx__parameter_type)
1036
+
1037
+ Model::DataTypes::Array.new(
1038
+ bound1: bound1,
1039
+ bound2: bound2,
1040
+ optional: optional,
1041
+ unique: unique,
1042
+ base_type: base_type
1043
+ )
1044
+ end
1045
+
1046
+ def visit_general_bag_type(ctx)
1047
+ ctx__bound_spec = ctx.bound_spec
1048
+ ctx__parameter_type = ctx.parameter_type
1049
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
1050
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
1051
+
1052
+ bound1 = visit_if(ctx__bound_spec__bound1)
1053
+ bound2 = visit_if(ctx__bound_spec__bound2)
1054
+ base_type = visit_if(ctx__parameter_type)
1055
+
1056
+ Model::DataTypes::Bag.new(
1057
+ bound1: bound1,
1058
+ bound2: bound2,
1059
+ base_type: base_type
1060
+ )
1061
+ end
1062
+
1063
+ def visit_general_list_type(ctx)
1064
+ ctx__bound_spec = ctx.bound_spec
1065
+ ctx__UNIQUE = ctx.UNIQUE
1066
+ ctx__parameter_type = ctx.parameter_type
1067
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
1068
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
1069
+
1070
+ bound1 = visit_if(ctx__bound_spec__bound1)
1071
+ bound2 = visit_if(ctx__bound_spec__bound2)
1072
+ unique = ctx__UNIQUE && true
1073
+ base_type = visit_if(ctx__parameter_type)
1074
+
1075
+ Model::DataTypes::List.new(
1076
+ bound1: bound1,
1077
+ bound2: bound2,
1078
+ unique: unique,
1079
+ base_type: base_type
1080
+ )
1081
+ end
1082
+
1083
+ def visit_general_ref(ctx)
1084
+ ctx__parameter_ref = ctx.parameter_ref
1085
+ ctx__variable_id = ctx.variable_id
1086
+
1087
+ visit_if(ctx__parameter_ref || ctx__variable_id)
1088
+ end
1089
+
1090
+ def visit_general_set_type(ctx)
1091
+ ctx__bound_spec = ctx.bound_spec
1092
+ ctx__parameter_type = ctx.parameter_type
1093
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
1094
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
1095
+
1096
+ bound1 = visit_if(ctx__bound_spec__bound1)
1097
+ bound2 = visit_if(ctx__bound_spec__bound2)
1098
+ base_type = visit_if(ctx__parameter_type)
1099
+
1100
+ Model::DataTypes::Set.new(
1101
+ bound1: bound1,
1102
+ bound2: bound2,
1103
+ base_type: base_type
1104
+ )
1105
+ end
1106
+
1107
+ def visit_generic_entity_type(ctx)
1108
+ ctx__type_label = ctx.type_label
1109
+
1110
+ id = visit_if(ctx__type_label)
1111
+
1112
+ Model::DataTypes::GenericEntity.new(
1113
+ id: id
1114
+ )
1115
+ end
1116
+
1117
+ def visit_generic_type(ctx)
1118
+ ctx__type_label = ctx.type_label
1119
+
1120
+ id = visit_if(ctx__type_label)
1121
+
1122
+ Model::DataTypes::Generic.new(
1123
+ id: id
1124
+ )
1125
+ end
1126
+
1127
+ def visit_group_qualifier(ctx)
1128
+ ctx__entity_ref = ctx.entity_ref
1129
+
1130
+ entity = visit_if(ctx__entity_ref)
1131
+
1132
+ Model::References::GroupReference.new(
1133
+ entity: entity
1134
+ )
1135
+ end
1136
+
1137
+ def visit_group_reference(ctx)
1138
+ raise 'Invalid state'
1139
+ end
1140
+
1141
+ def visit_if_stmt(ctx)
1142
+ ctx__logical_expression = ctx.logical_expression
1143
+ ctx__if_stmt_statements = ctx.if_stmt_statements
1144
+ ctx__if_stmt_else_statements = ctx.if_stmt_else_statements
1145
+
1146
+ expression = visit_if(ctx__logical_expression)
1147
+ statements = visit_if(ctx__if_stmt_statements, [])
1148
+ else_statements = visit_if(ctx__if_stmt_else_statements, [])
1149
+
1150
+ Model::Statements::If.new(
1151
+ expression: expression,
1152
+ statements: statements,
1153
+ else_statements: else_statements
1154
+ )
1155
+ end
1156
+
1157
+ def visit_if_stmt_statements(ctx)
1158
+ ctx__stmt = ctx.stmt
1159
+
1160
+ visit_if_map(ctx__stmt)
1161
+ end
1162
+
1163
+ def visit_if_stmt_else_statements(ctx)
1164
+ ctx__stmt = ctx.stmt
1165
+
1166
+ visit_if_map(ctx__stmt)
1167
+ end
1168
+
1169
+ def visit_increment(ctx)
1170
+ ctx__numeric_expression = ctx.numeric_expression
1171
+
1172
+ visit_if(ctx__numeric_expression)
1173
+ end
1174
+
1175
+ def visit_increment_control(ctx)
1176
+ raise 'Invalid state'
1177
+ end
1178
+
1179
+ def visit_index(ctx)
1180
+ ctx__numeric_expression = ctx.numeric_expression
1181
+
1182
+ visit_if(ctx__numeric_expression)
1183
+ end
1184
+
1185
+ def visit_index1(ctx)
1186
+ ctx__index = ctx.index
1187
+
1188
+ visit_if(ctx__index)
1189
+ end
1190
+
1191
+ def visit_index2(ctx)
1192
+ ctx__index = ctx.index
1193
+
1194
+ visit_if(ctx__index)
1195
+ end
1196
+
1197
+ def visit_index_qualifier(ctx)
1198
+ ctx__index1 = ctx.index1
1199
+ ctx__index2 = ctx.index2
1200
+
1201
+ index1 = visit_if(ctx__index1)
1202
+ index2 = visit_if(ctx__index2)
1203
+
1204
+ Model::References::IndexReference.new(
1205
+ index1: index1,
1206
+ index2: index2
1207
+ )
1208
+ end
1209
+
1210
+ def visit_index_reference(ctx)
1211
+ raise 'Invalid state'
1212
+ end
1213
+
1214
+ def visit_instantiable_type(ctx)
1215
+ ctx__concrete_types = ctx.concrete_types
1216
+ ctx__entity_ref = ctx.entity_ref
1217
+
1218
+ visit_if(ctx__concrete_types || ctx__entity_ref)
1219
+ end
1220
+
1221
+ def visit_integer_type(ctx)
1222
+ Model::DataTypes::Integer.new
1223
+ end
1224
+
1225
+ def visit_interface_specification(ctx)
1226
+ ctx__reference_clause = ctx.reference_clause
1227
+ ctx__use_clause = ctx.use_clause
1228
+
1229
+ visit_if(ctx__reference_clause || ctx__use_clause)
1230
+ end
1231
+
1232
+ def visit_interval(ctx)
1233
+ ctx__interval_low = ctx.interval_low
1234
+ ctx__interval_op = ctx.interval_op
1235
+ ctx__interval_item = ctx.interval_item
1236
+ ctx__interval_high = ctx.interval_high
1237
+
1238
+ low = visit_if(ctx__interval_low)
1239
+ operator1 = visit_if(ctx__interval_op[0])
1240
+ item = visit_if(ctx__interval_item)
1241
+ operator2 = visit_if(ctx__interval_op[1])
1242
+ high = visit_if(ctx__interval_high)
1243
+
1244
+ Model::Expressions::Interval.new(
1245
+ low: low,
1246
+ operator1: operator1,
1247
+ item: item,
1248
+ operator2: operator2,
1249
+ high: high
1250
+ )
1251
+ end
1252
+
1253
+ def visit_interval_high(ctx)
1254
+ ctx__simple_expression = ctx.simple_expression
1255
+
1256
+ visit_if(ctx__simple_expression)
1257
+ end
1258
+
1259
+ def visit_interval_item(ctx)
1260
+ ctx__simple_expression = ctx.simple_expression
1261
+
1262
+ visit_if(ctx__simple_expression)
1263
+ end
1264
+
1265
+ def visit_interval_low(ctx)
1266
+ ctx__simple_expression = ctx.simple_expression
1267
+
1268
+ visit_if(ctx__simple_expression)
1269
+ end
1270
+
1271
+ def visit_interval_op(ctx)
1272
+ ctx__text = ctx.text
1273
+ ctx__LESS_THAN = ctx__text == '<'
1274
+ ctx__LESS_THAN_OR_EQUAL = ctx__text == '<='
1275
+
1276
+ if ctx__LESS_THAN
1277
+ Model::Expressions::Interval::LESS_THAN
1278
+ elsif ctx__LESS_THAN_OR_EQUAL
1279
+ Model::Expressions::Interval::LESS_THAN_OR_EQUAL
1280
+ else
1281
+ raise 'Invalid state'
1282
+ end
1283
+ end
1284
+
1285
+ def visit_inverse_attr(ctx)
1286
+ ctx__attribute_decl = ctx.attribute_decl
1287
+ ctx__inverse_attr_type = ctx.inverse_attr_type
1288
+ ctx__entity_ref = ctx.entity_ref
1289
+ ctx__attribute_ref = ctx.attribute_ref
1290
+
1291
+ attribute = visit_if(ctx__attribute_decl)
1292
+ type = visit_if(ctx__inverse_attr_type)
1293
+ expression = if ctx__entity_ref
1294
+ ref = visit(ctx__entity_ref)
1295
+ attribute_ref = visit(ctx__attribute_ref)
1296
+
1297
+ Model::References::AttributeReference.new(
1298
+ ref: ref,
1299
+ attribute: attribute_ref
1300
+ )
1301
+ else
1302
+ visit(ctx__attribute_ref)
1303
+ end
1304
+
1305
+ Model::Declarations::Attribute.new(
1306
+ id: attribute.id, # reuse
1307
+ kind: Model::Declarations::Attribute::INVERSE,
1308
+ supertype_attribute: attribute.supertype_attribute, # reuse
1309
+ type: type,
1310
+ expression: expression
1311
+ )
1312
+ end
1313
+
1314
+ def visit_inverse_attr_type(ctx)
1315
+ ctx__SET = ctx.SET
1316
+ ctx__BAG = ctx.BAG
1317
+ ctx__bound_spec = ctx.bound_spec
1318
+ ctx__entity_ref = ctx.entity_ref
1319
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
1320
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
1321
+
1322
+ if ctx__SET
1323
+ bound1 = visit_if(ctx__bound_spec__bound1)
1324
+ bound2 = visit_if(ctx__bound_spec__bound2)
1325
+ base_type = visit_if(ctx__entity_ref)
1326
+
1327
+ Model::DataTypes::Set.new(
1328
+ bound1: bound1,
1329
+ bound2: bound2,
1330
+ base_type: base_type
1331
+ )
1332
+ elsif ctx__BAG
1333
+ bound1 = visit_if(ctx__bound_spec__bound1)
1334
+ bound2 = visit_if(ctx__bound_spec__bound2)
1335
+ base_type = visit_if(ctx__entity_ref)
1336
+
1337
+ Model::DataTypes::Bag.new(
1338
+ bound1: bound1,
1339
+ bound2: bound2,
1340
+ base_type: base_type
1341
+ )
1342
+ else
1343
+ visit_if(ctx__entity_ref)
1344
+ end
1345
+ end
1346
+
1347
+ def visit_inverse_clause(ctx)
1348
+ ctx__inverse_attr = ctx.inverse_attr
1349
+
1350
+ visit_if_map(ctx__inverse_attr)
1351
+ end
1352
+
1353
+ def visit_list_type(ctx)
1354
+ ctx__bound_spec = ctx.bound_spec
1355
+ ctx__UNIQUE = ctx.UNIQUE
1356
+ ctx__instantiable_type = ctx.instantiable_type
1357
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
1358
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
1359
+
1360
+ bound1 = visit_if(ctx__bound_spec__bound1)
1361
+ bound2 = visit_if(ctx__bound_spec__bound2)
1362
+ unique = ctx__UNIQUE && true
1363
+ base_type = visit_if(ctx__instantiable_type)
1364
+
1365
+ Model::DataTypes::List.new(
1366
+ bound1: bound1,
1367
+ bound2: bound2,
1368
+ unique: unique,
1369
+ base_type: base_type
1370
+ )
1371
+ end
1372
+
1373
+ def visit_literal(ctx)
1374
+ ctx__BinaryLiteral = ctx.BinaryLiteral
1375
+ ctx__IntegerLiteral = ctx.IntegerLiteral
1376
+ ctx__logical_literal = ctx.logical_literal
1377
+ ctx__RealLiteral = ctx.RealLiteral
1378
+ ctx__string_literal = ctx.string_literal
1379
+
1380
+ if ctx__BinaryLiteral
1381
+ handle_binary_literal(ctx__BinaryLiteral)
1382
+ elsif ctx__IntegerLiteral
1383
+ handle_integer_literal(ctx__IntegerLiteral)
1384
+ elsif ctx__logical_literal
1385
+ visit(ctx__logical_literal)
1386
+ elsif ctx__RealLiteral
1387
+ handle_real_literal(ctx__RealLiteral)
1388
+ elsif ctx__string_literal
1389
+ visit(ctx__string_literal)
1390
+ else
1391
+ raise 'Invalid state'
1392
+ end
1393
+ end
1394
+
1395
+ def visit_local_decl(ctx)
1396
+ ctx__local_variable = ctx.local_variable
1397
+
1398
+ visit_if_map_flatten(ctx__local_variable)
1399
+ end
1400
+
1401
+ def visit_local_variable(ctx)
1402
+ ctx__variable_id = ctx.variable_id
1403
+ ctx__parameter_type = ctx.parameter_type
1404
+ ctx__expression = ctx.expression
1405
+
1406
+ ids = visit_if_map(ctx__variable_id)
1407
+ type = visit_if(ctx__parameter_type)
1408
+ expression = visit_if(ctx__expression)
1409
+
1410
+ ids.map do |id|
1411
+ Model::Declarations::Variable.new(
1412
+ id: id,
1413
+ type: type,
1414
+ expression: expression
1415
+ )
1416
+ end
1417
+ end
1418
+
1419
+ def visit_logical_expression(ctx)
1420
+ ctx__expression = ctx.expression
1421
+
1422
+ visit_if(ctx__expression)
1423
+ end
1424
+
1425
+ def visit_logical_literal(ctx)
1426
+ ctx__TRUE = ctx.TRUE
1427
+ ctx__FALSE = ctx.FALSE
1428
+ ctx__UNKNOWN = ctx.UNKNOWN
1429
+
1430
+ value = if ctx__TRUE
1431
+ Model::Literals::Logical::TRUE
1432
+ elsif ctx__FALSE
1433
+ Model::Literals::Logical::FALSE
1434
+ elsif ctx__UNKNOWN
1435
+ Model::Literals::Logical::UNKNOWN
1436
+ else
1437
+ raise 'Invalid state'
1438
+ end
1439
+
1440
+ Model::Literals::Logical.new(
1441
+ value: value
1442
+ )
1443
+ end
1444
+
1445
+ def visit_logical_type(ctx)
1446
+ Model::DataTypes::Logical.new
1447
+ end
1448
+
1449
+ def visit_multiplication_like_op(ctx)
1450
+ ctx__text = ctx.text
1451
+ ctx__MULTIPLICATION = ctx__text == '*'
1452
+ ctx__REAL_DIVISION = ctx__text == '/'
1453
+ ctx__INTEGER_DIVISION = ctx.DIV
1454
+ ctx__MODULO = ctx.MOD
1455
+ ctx__AND = ctx.AND
1456
+ ctx__COMBINE = ctx__text == '||'
1457
+
1458
+ if ctx__MULTIPLICATION
1459
+ Model::Expressions::BinaryExpression::MULTIPLICATION
1460
+ elsif ctx__REAL_DIVISION
1461
+ Model::Expressions::BinaryExpression::REAL_DIVISION
1462
+ elsif ctx__INTEGER_DIVISION
1463
+ Model::Expressions::BinaryExpression::INTEGER_DIVISION
1464
+ elsif ctx__MODULO
1465
+ Model::Expressions::BinaryExpression::MODULO
1466
+ elsif ctx__AND
1467
+ Model::Expressions::BinaryExpression::AND
1468
+ elsif ctx__COMBINE
1469
+ Model::Expressions::BinaryExpression::COMBINE
1470
+ else
1471
+ raise 'Invalid state'
1472
+ end
1473
+ end
1474
+
1475
+ def visit_named_types(ctx)
1476
+ ctx__entity_ref = ctx.entity_ref
1477
+ ctx__type_ref = ctx.type_ref
1478
+
1479
+ visit_if(ctx__entity_ref || ctx__type_ref)
1480
+ end
1481
+
1482
+ def visit_named_type_or_rename(ctx)
1483
+ ctx__named_types = ctx.named_types
1484
+ ctx__entity_id = ctx.entity_id
1485
+ ctx__type_id = ctx.type_id
1486
+
1487
+ ref = visit_if(ctx__named_types)
1488
+ id = visit_if(ctx__entity_id || ctx__type_id)
1489
+
1490
+ Model::Declarations::InterfaceItem.new(
1491
+ ref: ref,
1492
+ id: id
1493
+ )
1494
+ end
1495
+
1496
+ def visit_null_stmt(ctx)
1497
+ Model::Statements::Null.new
1498
+ end
1499
+
1500
+ def visit_number_type(ctx)
1501
+ Model::DataTypes::Number.new
1502
+ end
1503
+
1504
+ def visit_numeric_expression(ctx)
1505
+ ctx__simple_expression = ctx.simple_expression
1506
+
1507
+ visit_if(ctx__simple_expression)
1508
+ end
1509
+
1510
+ def visit_one_of(ctx)
1511
+ ctx__supertype_expression = ctx.supertype_expression
1512
+
1513
+ operands = visit_if_map(ctx__supertype_expression)
1514
+
1515
+ Model::SupertypeExpressions::OneofSupertypeExpression.new(
1516
+ operands: operands
1517
+ )
1518
+ end
1519
+
1520
+ def visit_parameter(ctx)
1521
+ ctx__expression = ctx.expression
1522
+
1523
+ visit_if(ctx__expression)
1524
+ end
1525
+
1526
+ def visit_parameter_id(ctx)
1527
+ ctx__SimpleId = ctx.SimpleId
1528
+
1529
+ handle_simple_id(ctx__SimpleId)
1530
+ end
1531
+
1532
+ def visit_parameter_type(ctx)
1533
+ ctx__generalized_types = ctx.generalized_types
1534
+ ctx__named_types = ctx.named_types
1535
+ ctx__simple_types = ctx.simple_types
1536
+
1537
+ visit_if(ctx__generalized_types || ctx__named_types || ctx__simple_types)
1538
+ end
1539
+
1540
+ def visit_population(ctx)
1541
+ ctx__entity_ref = ctx.entity_ref
1542
+
1543
+ visit_if(ctx__entity_ref)
1544
+ end
1545
+
1546
+ def visit_precision_spec(ctx)
1547
+ ctx__numeric_expression = ctx.numeric_expression
1548
+
1549
+ visit_if(ctx__numeric_expression)
1550
+ end
1551
+
1552
+ def visit_primary(ctx)
1553
+ ctx__literal = ctx.literal
1554
+ ctx__qualifiable_factor = ctx.qualifiable_factor
1555
+ ctx__qualifier = ctx.qualifier
1556
+
1557
+ if ctx__literal
1558
+ visit(ctx__literal)
1559
+ elsif ctx__qualifiable_factor
1560
+ handle_qualified_ref(visit(ctx__qualifiable_factor), ctx__qualifier)
1561
+ else
1562
+ raise 'Invalid state'
1563
+ end
1564
+ end
1565
+
1566
+ def visit_procedure_call_stmt(ctx)
1567
+ ctx__built_in_procedure = ctx.built_in_procedure
1568
+ ctx__procedure_ref = ctx.procedure_ref
1569
+ ctx__actual_parameter_list = ctx.actual_parameter_list
1570
+
1571
+ procedure = visit_if(ctx__built_in_procedure || ctx__procedure_ref)
1572
+ parameters = visit_if(ctx__actual_parameter_list, [])
1573
+
1574
+ Model::Statements::ProcedureCall.new(
1575
+ procedure: procedure,
1576
+ parameters: parameters
1577
+ )
1578
+ end
1579
+
1580
+ def visit_procedure_decl(ctx)
1581
+ ctx__procedure_head = ctx.procedure_head
1582
+ ctx__algorithm_head = ctx.algorithm_head
1583
+ ctx__stmt = ctx.stmt
1584
+ ctx__procedure_head__procedure_id = ctx__procedure_head&.procedure_id
1585
+ ctx__procedure_head__procedure_head_parameter = ctx__procedure_head&.procedure_head_parameter
1586
+ ctx__algorithm_head__declaration = ctx__algorithm_head&.declaration
1587
+ ctx__algorithm_head__constant_decl = ctx__algorithm_head&.constant_decl
1588
+ ctx__algorithm_head__local_decl = ctx__algorithm_head&.local_decl
1589
+
1590
+ id = visit_if(ctx__procedure_head__procedure_id)
1591
+ parameters = visit_if_map_flatten(ctx__procedure_head__procedure_head_parameter)
1592
+ declarations = visit_if_map(ctx__algorithm_head__declaration)
1593
+ types = declarations.select{|x| x.is_a? Model::Declarations::Type}
1594
+ entities = declarations.select{|x| x.is_a? Model::Declarations::Entity}
1595
+ subtype_constraints = declarations.select{|x| x.is_a? Model::Declarations::SubtypeConstraint}
1596
+ functions = declarations.select{|x| x.is_a? Model::Declarations::Function}
1597
+ procedures = declarations.select{|x| x.is_a? Model::Declarations::Procedure}
1598
+ constants = visit_if(ctx__algorithm_head__constant_decl, [])
1599
+ variables = visit_if(ctx__algorithm_head__local_decl, [])
1600
+ statements = visit_if_map(ctx__stmt)
1601
+
1602
+ Model::Declarations::Procedure.new(
1603
+ id: id,
1604
+ parameters: parameters,
1605
+ types: types,
1606
+ entities: entities,
1607
+ subtype_constraints: subtype_constraints,
1608
+ functions: functions,
1609
+ procedures: procedures,
1610
+ constants: constants,
1611
+ variables: variables,
1612
+ statements: statements
1613
+ )
1614
+ end
1615
+
1616
+ def visit_procedure_head(ctx)
1617
+ raise 'Invalid state'
1618
+ end
1619
+
1620
+ def visit_procedure_head_parameter(ctx)
1621
+ ctx__formal_parameter = ctx.formal_parameter
1622
+ ctx__VAR = ctx.VAR
1623
+
1624
+ parameters = visit(ctx__formal_parameter)
1625
+
1626
+ if ctx.VAR
1627
+ parameters.map do |parameter|
1628
+ Model::Declarations::Parameter.new(
1629
+ id: parameter.id,
1630
+ var: true,
1631
+ type: parameter.type
1632
+ )
1633
+ end
1634
+ else
1635
+ parameters
1636
+ end
1637
+ end
1638
+
1639
+ def visit_procedure_id(ctx)
1640
+ ctx__SimpleId = ctx.SimpleId
1641
+
1642
+ handle_simple_id(ctx__SimpleId)
1643
+ end
1644
+
1645
+ def visit_qualifiable_factor(ctx)
1646
+ ctx__attribute_ref = ctx.attribute_ref
1647
+ ctx__constant_factor = ctx.constant_factor
1648
+ ctx__function_call = ctx.function_call
1649
+ ctx__general_ref = ctx.general_ref
1650
+ ctx__population = ctx.population
1651
+
1652
+ visit_if(ctx__attribute_ref || ctx__constant_factor || ctx__function_call || ctx__general_ref || ctx__population)
1653
+ end
1654
+
1655
+ def visit_qualified_attribute(ctx)
1656
+ ctx__group_qualifier = ctx.group_qualifier
1657
+ ctx__attribute_qualifier = ctx.attribute_qualifier
1658
+
1659
+ id = 'SELF'
1660
+ group_reference = visit_if(ctx__group_qualifier)
1661
+ attribute_reference = visit_if(ctx__attribute_qualifier)
1662
+
1663
+ Model::References::AttributeReference.new(
1664
+ ref: Model::References::GroupReference.new(
1665
+ ref: Model::References::SimpleReference.new(
1666
+ id: id
1667
+ ),
1668
+ entity: group_reference.entity # reuse
1669
+ ),
1670
+ attribute: attribute_reference.attribute # reuse
1671
+ )
1672
+ end
1673
+
1674
+ def visit_qualifier(ctx)
1675
+ ctx__attribute_qualifier = ctx.attribute_qualifier
1676
+ ctx__group_qualifier = ctx.group_qualifier
1677
+ ctx__index_qualifier = ctx.index_qualifier
1678
+
1679
+ visit_if(ctx__attribute_qualifier || ctx__group_qualifier || ctx__index_qualifier)
1680
+ end
1681
+
1682
+ def visit_query_expression(ctx)
1683
+ ctx__variable_id = ctx.variable_id
1684
+ ctx__aggregate_source = ctx.aggregate_source
1685
+ ctx__logical_expression = ctx.logical_expression
1686
+
1687
+ id = visit_if(ctx__variable_id)
1688
+ aggregate_source = visit_if(ctx__aggregate_source)
1689
+ expression = visit_if(ctx__logical_expression)
1690
+
1691
+ Model::Expressions::QueryExpression.new(
1692
+ id: id,
1693
+ aggregate_source: aggregate_source,
1694
+ expression: expression
1695
+ )
1696
+ end
1697
+
1698
+ def visit_real_type(ctx)
1699
+ ctx__precision_spec = ctx.precision_spec
1700
+
1701
+ precision = visit_if(ctx__precision_spec)
1702
+
1703
+ Model::DataTypes::Real.new(
1704
+ precision: precision
1705
+ )
1706
+ end
1707
+
1708
+ def visit_redeclared_attribute(ctx)
1709
+ raise 'Invalid state'
1710
+ end
1711
+
1712
+ def visit_referenced_attribute(ctx)
1713
+ ctx__attribute_ref = ctx.attribute_ref
1714
+ ctx__qualified_attribute = ctx.qualified_attribute
1715
+
1716
+ visit_if(ctx__attribute_ref || ctx__qualified_attribute)
1717
+ end
1718
+
1719
+ def visit_reference_clause(ctx)
1720
+ ctx__schema_ref = ctx.schema_ref
1721
+ ctx__resource_or_rename = ctx.resource_or_rename
1722
+
1723
+ schema = visit_if(ctx__schema_ref)
1724
+ items = visit_if_map(ctx__resource_or_rename)
1725
+
1726
+ Model::Declarations::Interface.new(
1727
+ kind: Model::Declarations::Interface::REFERENCE,
1728
+ schema: schema,
1729
+ items: items
1730
+ )
1731
+ end
1732
+
1733
+ def visit_rel_op(ctx)
1734
+ ctx__text = ctx.text
1735
+ ctx__LESS_THAN = ctx__text == '<'
1736
+ ctx__GREATER_THAN = ctx__text == '>'
1737
+ ctx__LESS_THAN_OR_EQUAL = ctx__text == '<='
1738
+ ctx__GREATER_THAN_OR_EQUAL = ctx__text == '>='
1739
+ ctx__NOT_EQUAL = ctx__text == '<>'
1740
+ ctx__EQUAL = ctx__text == '='
1741
+ ctx__INSTANCE_NOT_EQUAL = ctx__text == ':<>:'
1742
+ ctx__INSTANCE_EQUAL = ctx__text == ':=:'
1743
+
1744
+ if ctx__LESS_THAN
1745
+ Model::Expressions::BinaryExpression::LESS_THAN
1746
+ elsif ctx__GREATER_THAN
1747
+ Model::Expressions::BinaryExpression::GREATER_THAN
1748
+ elsif ctx__LESS_THAN_OR_EQUAL
1749
+ Model::Expressions::BinaryExpression::LESS_THAN_OR_EQUAL
1750
+ elsif ctx__GREATER_THAN_OR_EQUAL
1751
+ Model::Expressions::BinaryExpression::GREATER_THAN_OR_EQUAL
1752
+ elsif ctx__NOT_EQUAL
1753
+ Model::Expressions::BinaryExpression::NOT_EQUAL
1754
+ elsif ctx__EQUAL
1755
+ Model::Expressions::BinaryExpression::EQUAL
1756
+ elsif ctx__INSTANCE_NOT_EQUAL
1757
+ Model::Expressions::BinaryExpression::INSTANCE_NOT_EQUAL
1758
+ elsif ctx__INSTANCE_EQUAL
1759
+ Model::Expressions::BinaryExpression::INSTANCE_EQUAL
1760
+ else
1761
+ raise 'Invalid state'
1762
+ end
1763
+ end
1764
+
1765
+ def visit_rel_op_extended(ctx)
1766
+ ctx__rel_op = ctx.rel_op
1767
+ ctx__IN = ctx.IN
1768
+ ctx__LIKE = ctx.LIKE
1769
+
1770
+ if ctx__rel_op
1771
+ visit(ctx__rel_op)
1772
+ elsif ctx__IN
1773
+ Model::Expressions::BinaryExpression::IN
1774
+ elsif ctx__LIKE
1775
+ Model::Expressions::BinaryExpression::LIKE
1776
+ else
1777
+ raise 'Invalid state'
1778
+ end
1779
+ end
1780
+
1781
+ def visit_rename_id(ctx)
1782
+ ctx__constant_id = ctx.constant_id
1783
+ ctx__entity_id = ctx.entity_id
1784
+ ctx__function_id = ctx.function_id
1785
+ ctx__procedure_id = ctx.procedure_id
1786
+ ctx__type_id = ctx.type_id
1787
+
1788
+ visit_if(ctx__constant_id || ctx__entity_id || ctx__function_id || ctx__procedure_id || ctx__type_id)
1789
+ end
1790
+
1791
+ def visit_repeat_control(ctx)
1792
+ raise 'Invalid state'
1793
+ end
1794
+
1795
+ def visit_repeat_stmt(ctx)
1796
+ ctx__repeat_control = ctx.repeat_control
1797
+ ctx__stmt = ctx.stmt
1798
+ ctx__repeat_control__increment_control = ctx__repeat_control&.increment_control
1799
+ ctx__repeat_control__increment_control__variable_id = ctx__repeat_control__increment_control&.variable_id
1800
+ ctx__repeat_control__increment_control__bound1 = ctx__repeat_control__increment_control&.bound1
1801
+ ctx__repeat_control__increment_control__bound2 = ctx__repeat_control__increment_control&.bound2
1802
+ ctx__repeat_control__increment_control__increment = ctx__repeat_control__increment_control&.increment
1803
+ ctx__repeat_control__while_control = ctx__repeat_control&.while_control
1804
+ ctx__repeat_control__until_control = ctx__repeat_control&.until_control
1805
+
1806
+ id = visit_if(ctx__repeat_control__increment_control__variable_id)
1807
+ bound1 = visit_if(ctx__repeat_control__increment_control__bound1)
1808
+ bound2 = visit_if(ctx__repeat_control__increment_control__bound2)
1809
+ increment = visit_if(ctx__repeat_control__increment_control__increment)
1810
+ while_expression = visit_if(ctx__repeat_control__while_control)
1811
+ until_expression = visit_if(ctx__repeat_control__until_control)
1812
+ statements = visit_if_map(ctx__stmt)
1813
+
1814
+ Model::Statements::Repeat.new(
1815
+ id: id,
1816
+ bound1: bound1,
1817
+ bound2: bound2,
1818
+ increment: increment,
1819
+ while_expression: while_expression,
1820
+ until_expression: until_expression,
1821
+ statements: statements
1822
+ )
1823
+ end
1824
+
1825
+ def visit_repetition(ctx)
1826
+ ctx__numeric_expression = ctx.numeric_expression
1827
+
1828
+ visit_if(ctx__numeric_expression)
1829
+ end
1830
+
1831
+ def visit_resource_or_rename(ctx)
1832
+ ctx__resource_ref = ctx.resource_ref
1833
+ ctx__rename_id = ctx.rename_id
1834
+
1835
+ ref = visit_if(ctx__resource_ref)
1836
+ id = visit_if(ctx__rename_id)
1837
+
1838
+ Model::Declarations::InterfaceItem.new(
1839
+ ref: ref,
1840
+ id: id
1841
+ )
1842
+ end
1843
+
1844
+ def visit_resource_ref(ctx)
1845
+ ctx__constant_ref = ctx.constant_ref
1846
+ ctx__entity_ref = ctx.entity_ref
1847
+ ctx__function_ref = ctx.function_ref
1848
+ ctx__procedure_ref = ctx.procedure_ref
1849
+ ctx__type_ref = ctx.type_ref
1850
+
1851
+ visit_if(ctx__constant_ref || ctx__entity_ref || ctx__function_ref || ctx__procedure_ref || ctx__type_ref)
1852
+ end
1853
+
1854
+ def visit_return_stmt(ctx)
1855
+ ctx__expression = ctx.expression
1856
+
1857
+ expression = visit_if(ctx__expression)
1858
+
1859
+ Model::Statements::Return.new(
1860
+ expression: expression
1861
+ )
1862
+ end
1863
+
1864
+ def visit_rule_decl(ctx)
1865
+ ctx__rule_head = ctx.rule_head
1866
+ ctx__algorithm_head = ctx.algorithm_head
1867
+ ctx__stmt = ctx.stmt
1868
+ ctx__where_clause = ctx.where_clause
1869
+ ctx__rule_head__rule_id = ctx__rule_head&.rule_id
1870
+ ctx__rule_head__entity_ref = ctx__rule_head&.entity_ref
1871
+ ctx__algorithm_head__declaration = ctx__algorithm_head&.declaration
1872
+ ctx__algorithm_head__constant_decl = ctx__algorithm_head&.constant_decl
1873
+ ctx__algorithm_head__local_decl = ctx__algorithm_head&.local_decl
1874
+
1875
+ id = visit_if(ctx__rule_head__rule_id)
1876
+ applies_to = visit_if_map(ctx__rule_head__entity_ref)
1877
+ declarations = visit_if_map(ctx__algorithm_head__declaration)
1878
+ types = declarations.select{|x| x.is_a? Model::Declarations::Type}
1879
+ entities = declarations.select{|x| x.is_a? Model::Declarations::Entity}
1880
+ subtype_constraints = declarations.select{|x| x.is_a? Model::Declarations::SubtypeConstraint}
1881
+ functions = declarations.select{|x| x.is_a? Model::Declarations::Function}
1882
+ procedures = declarations.select{|x| x.is_a? Model::Declarations::Procedure}
1883
+ constants = visit_if(ctx__algorithm_head__constant_decl, [])
1884
+ variables = visit_if(ctx__algorithm_head__local_decl, [])
1885
+ statements = visit_if_map(ctx__stmt)
1886
+ where_rules = visit_if(ctx__where_clause, [])
1887
+
1888
+ Model::Declarations::Rule.new(
1889
+ id: id,
1890
+ applies_to: applies_to,
1891
+ types: types,
1892
+ entities: entities,
1893
+ subtype_constraints: subtype_constraints,
1894
+ functions: functions,
1895
+ procedures: procedures,
1896
+ constants: constants,
1897
+ variables: variables,
1898
+ statements: statements,
1899
+ where_rules: where_rules
1900
+ )
1901
+ end
1902
+
1903
+ def visit_rule_head(ctx)
1904
+ raise 'Invalid state'
1905
+ end
1906
+
1907
+ def visit_rule_id(ctx)
1908
+ ctx__SimpleId = ctx.SimpleId
1909
+
1910
+ handle_simple_id(ctx__SimpleId)
1911
+ end
1912
+
1913
+ def visit_rule_label_id(ctx)
1914
+ ctx__SimpleId = ctx.SimpleId
1915
+
1916
+ handle_simple_id(ctx__SimpleId)
1917
+ end
1918
+
1919
+ def visit_schema_body(ctx)
1920
+ raise 'Invalid state'
1921
+ end
1922
+
1923
+ def visit_schema_body_declaration(ctx)
1924
+ ctx__declaration = ctx.declaration
1925
+ ctx__rule_decl = ctx.rule_decl
1926
+
1927
+ visit_if(ctx__declaration || ctx__rule_decl)
1928
+ end
1929
+
1930
+ def visit_schema_decl(ctx)
1931
+ ctx__schema_id = ctx.schema_id
1932
+ ctx__schema_version_id = ctx.schema_version_id
1933
+ ctx__schema_body = ctx.schema_body
1934
+ ctx__schema_body__interface_specification = ctx__schema_body&.interface_specification
1935
+ ctx__schema_body__constant_decl = ctx__schema_body&.constant_decl
1936
+ ctx__schema_body__schema_body_declaration = ctx__schema_body&.schema_body_declaration
1937
+
1938
+ id = visit_if(ctx__schema_id)
1939
+ version = visit_if(ctx__schema_version_id)
1940
+ interfaces = visit_if_map(ctx__schema_body__interface_specification)
1941
+ constants = visit_if(ctx__schema_body__constant_decl, [])
1942
+ declarations = visit_if_map(ctx__schema_body__schema_body_declaration)
1943
+ types = declarations.select{|x| x.is_a? Model::Declarations::Type}
1944
+ entities = declarations.select{|x| x.is_a? Model::Declarations::Entity}
1945
+ subtype_constraints = declarations.select{|x| x.is_a? Model::Declarations::SubtypeConstraint}
1946
+ functions = declarations.select{|x| x.is_a? Model::Declarations::Function}
1947
+ rules = declarations.select{|x| x.is_a? Model::Declarations::Rule}
1948
+ procedures = declarations.select{|x| x.is_a? Model::Declarations::Procedure}
1949
+
1950
+ Model::Declarations::Schema.new(
1951
+ id: id,
1952
+ version: version,
1953
+ interfaces: interfaces,
1954
+ constants: constants,
1955
+ types: types,
1956
+ entities: entities,
1957
+ subtype_constraints: subtype_constraints,
1958
+ functions: functions,
1959
+ rules: rules,
1960
+ procedures: procedures
1961
+ )
1962
+ end
1963
+
1964
+ def visit_schema_id(ctx)
1965
+ ctx__SimpleId = ctx.SimpleId
1966
+
1967
+ handle_simple_id(ctx__SimpleId)
1968
+ end
1969
+
1970
+ def visit_schema_version_id(ctx)
1971
+ ctx__string_literal = ctx.string_literal
1972
+
1973
+ value = visit_if(ctx__string_literal)
1974
+ value = value.value
1975
+
1976
+ items = if value.start_with?('{') and value.end_with?('}')
1977
+ parts = value.sub(/^\{/, '').sub(/\}$/, '').split(' ')
1978
+ parts.map do |part|
1979
+ if match = part.match(/^(.+)\((\d+)\)$/)
1980
+ Model::Declarations::SchemaVersionItem.new(
1981
+ name: match[1],
1982
+ value: match[2]
1983
+ )
1984
+ elsif part.match(/^\d+$/)
1985
+ Model::Declarations::SchemaVersionItem.new(
1986
+ value: part
1987
+ )
1988
+ else
1989
+ Model::Declarations::SchemaVersionItem.new(
1990
+ name: part
1991
+ )
1992
+ end
1993
+ end
1994
+ end
1995
+
1996
+ Model::Declarations::SchemaVersion.new(
1997
+ value: value,
1998
+ items: items
1999
+ )
2000
+ end
2001
+
2002
+ def visit_selector(ctx)
2003
+ raise 'Invalid state'
2004
+ end
2005
+
2006
+ def visit_select_extension(ctx)
2007
+ ctx__named_types = ctx.named_types
2008
+
2009
+ visit_if_map(ctx__named_types)
2010
+ end
2011
+
2012
+ def visit_select_list(ctx)
2013
+ ctx__named_types = ctx.named_types
2014
+
2015
+ visit_if_map(ctx__named_types)
2016
+ end
2017
+
2018
+ def visit_select_type(ctx)
2019
+ ctx__EXTENSIBLE = ctx.EXTENSIBLE
2020
+ ctx__GENERIC_ENTITY = ctx.GENERIC_ENTITY
2021
+ ctx__select_list = ctx.select_list
2022
+ ctx__select_extension = ctx.select_extension
2023
+ ctx__select_extension__type_ref = ctx.select_extension&.type_ref
2024
+ ctx__select_extension__select_list = ctx__select_extension&.select_list
2025
+
2026
+ extensible = ctx__EXTENSIBLE && true
2027
+ generic_entity = ctx__GENERIC_ENTITY && true
2028
+ based_on = visit_if(ctx__select_extension__type_ref)
2029
+ items = visit_if(ctx__select_list || ctx__select_extension__select_list, [])
2030
+
2031
+ Model::DataTypes::Select.new(
2032
+ extensible: extensible,
2033
+ generic_entity: generic_entity,
2034
+ based_on: based_on,
2035
+ items: items
2036
+ )
2037
+ end
2038
+
2039
+ def visit_set_type(ctx)
2040
+ ctx__bound_spec = ctx.bound_spec
2041
+ ctx__instantiable_type = ctx.instantiable_type
2042
+ ctx__bound_spec__bound1 = ctx__bound_spec&.bound1
2043
+ ctx__bound_spec__bound2 = ctx__bound_spec&.bound2
2044
+
2045
+ bound1 = visit_if(ctx__bound_spec__bound1)
2046
+ bound2 = visit_if(ctx__bound_spec__bound2)
2047
+ base_type = visit_if(ctx__instantiable_type)
2048
+
2049
+ Model::DataTypes::Set.new(
2050
+ bound1: bound1,
2051
+ bound2: bound2,
2052
+ base_type: base_type
2053
+ )
2054
+ end
2055
+
2056
+ def visit_simple_expression(ctx)
2057
+ ctx__term = ctx.term
2058
+ ctx__add_like_op = ctx.add_like_op
2059
+
2060
+ if ctx__term
2061
+ if ctx__term.length >= 2
2062
+ if ctx__add_like_op and ctx__add_like_op.length == ctx__term.length - 1
2063
+ operands = ctx__term.map(&self.method(:visit))
2064
+ operators = ctx__add_like_op.map(&self.method(:visit))
2065
+
2066
+ handle_binary_expression(operands, operators)
2067
+ else
2068
+ raise 'Invalid state'
2069
+ end
2070
+ elsif ctx__term.length == 1
2071
+ visit(ctx__term[0])
2072
+ else
2073
+ raise 'Invalid state'
2074
+ end
2075
+ end
2076
+ end
2077
+
2078
+ def visit_simple_factor(ctx)
2079
+ ctx__aggregate_initializer = ctx.aggregate_initializer
2080
+ ctx__entity_constructor = ctx.entity_constructor
2081
+ ctx__enumeration_reference = ctx.enumeration_reference
2082
+ ctx__interval = ctx.interval
2083
+ ctx__query_expression = ctx.query_expression
2084
+ ctx__simple_factor_expression = ctx.simple_factor_expression
2085
+ ctx__simple_factor_unary_expression = ctx.simple_factor_unary_expression
2086
+
2087
+ visit_if(ctx__aggregate_initializer || ctx__entity_constructor || ctx__enumeration_reference || ctx__interval || ctx__query_expression || ctx__simple_factor_expression || ctx__simple_factor_unary_expression)
2088
+ end
2089
+
2090
+ def visit_simple_factor_expression(ctx)
2091
+ ctx__expression = ctx.expression
2092
+ ctx__primary = ctx.primary
2093
+
2094
+ visit_if(ctx__expression || ctx__primary)
2095
+ end
2096
+
2097
+ def visit_simple_factor_unary_expression(ctx)
2098
+ ctx__unary_op = ctx.unary_op
2099
+ ctx__simple_factor_expression = ctx.simple_factor_expression
2100
+
2101
+ operator = visit_if(ctx__unary_op)
2102
+ operand = visit_if(ctx__simple_factor_expression)
2103
+
2104
+ Model::Expressions::UnaryExpression.new(
2105
+ operator: operator,
2106
+ operand: operand
2107
+ )
2108
+ end
2109
+
2110
+ def visit_simple_types(ctx)
2111
+ ctx__binary_type = ctx.binary_type
2112
+ ctx__boolean_type = ctx.boolean_type
2113
+ ctx__integer_type = ctx.integer_type
2114
+ ctx__logical_type = ctx.logical_type
2115
+ ctx__number_type = ctx.number_type
2116
+ ctx__real_type = ctx.real_type
2117
+ ctx__string_type = ctx.string_type
2118
+
2119
+ visit_if(ctx__binary_type || ctx__boolean_type || ctx__integer_type || ctx__logical_type || ctx__number_type || ctx__real_type || ctx__string_type)
2120
+ end
2121
+
2122
+ def visit_skip_stmt(ctx)
2123
+ Model::Statements::Skip.new
2124
+ end
2125
+
2126
+ def visit_stmt(ctx)
2127
+ ctx__alias_stmt = ctx.alias_stmt
2128
+ ctx__assignment_stmt = ctx.assignment_stmt
2129
+ ctx__case_stmt = ctx.case_stmt
2130
+ ctx__compound_stmt = ctx.compound_stmt
2131
+ ctx__escape_stmt = ctx.escape_stmt
2132
+ ctx__if_stmt = ctx.if_stmt
2133
+ ctx__null_stmt = ctx.null_stmt
2134
+ ctx__procedure_call_stmt = ctx.procedure_call_stmt
2135
+ ctx__repeat_stmt = ctx.repeat_stmt
2136
+ ctx__return_stmt = ctx.return_stmt
2137
+ ctx__skip_stmt = ctx.skip_stmt
2138
+
2139
+ visit_if(ctx__alias_stmt || ctx__assignment_stmt || ctx__case_stmt || ctx__compound_stmt || ctx__escape_stmt || ctx__if_stmt || ctx__null_stmt || ctx__procedure_call_stmt || ctx__repeat_stmt || ctx__return_stmt || ctx__skip_stmt)
2140
+ end
2141
+
2142
+ def visit_string_literal(ctx)
2143
+ ctx__SimpleStringLiteral = ctx.SimpleStringLiteral
2144
+ ctx__EncodedStringLiteral = ctx.EncodedStringLiteral
2145
+
2146
+ if ctx__SimpleStringLiteral
2147
+ handle_simple_string_literal(ctx__SimpleStringLiteral)
2148
+ elsif ctx__EncodedStringLiteral
2149
+ handle_encoded_string_literal(ctx__EncodedStringLiteral)
2150
+ else
2151
+ raise 'Invalid state'
2152
+ end
2153
+ end
2154
+
2155
+ def visit_string_type(ctx)
2156
+ ctx__width_spec = ctx.width_spec
2157
+ ctx__width_spec__width = ctx__width_spec&.width
2158
+ ctx__width_spec__FIXED = ctx__width_spec&.FIXED
2159
+
2160
+ width = visit_if(ctx__width_spec__width)
2161
+ fixed = ctx__width_spec__FIXED && true
2162
+
2163
+ Model::DataTypes::String.new(
2164
+ width: width,
2165
+ fixed: fixed
2166
+ )
2167
+ end
2168
+
2169
+ def visit_subsuper(ctx)
2170
+ raise 'Invalid state'
2171
+ end
2172
+
2173
+ def visit_subtype_constraint(ctx)
2174
+ ctx__supertype_expression = ctx.supertype_expression
2175
+
2176
+ visit_if(ctx__supertype_expression)
2177
+ end
2178
+
2179
+ def visit_subtype_constraint_body(ctx)
2180
+ raise 'Invalid state'
2181
+ end
2182
+
2183
+ def visit_subtype_constraint_decl(ctx)
2184
+ ctx__subtype_constraint_head = ctx.subtype_constraint_head
2185
+ ctx__subtype_constraint_body = ctx.subtype_constraint_body
2186
+ ctx__subtype_constraint_head__subtype_constraint_id = ctx__subtype_constraint_head&.subtype_constraint_id
2187
+ ctx__subtype_constraint_head__entity_ref = ctx__subtype_constraint_head&.entity_ref
2188
+ ctx__subtype_constraint_body__abstract_supertype = ctx__subtype_constraint_body&.abstract_supertype
2189
+ ctx__subtype_constraint_body__total_over = ctx__subtype_constraint_body&.total_over
2190
+ ctx__subtype_constraint_body__supertype_expression = ctx__subtype_constraint_body&.supertype_expression
2191
+
2192
+ id = visit_if(ctx__subtype_constraint_head__subtype_constraint_id)
2193
+ applies_to = visit_if(ctx__subtype_constraint_head__entity_ref)
2194
+ abstract = ctx__subtype_constraint_body__abstract_supertype && true
2195
+ total_over = visit_if(ctx__subtype_constraint_body__total_over, [])
2196
+ supertype_expression = visit_if(ctx__subtype_constraint_body__supertype_expression)
2197
+
2198
+ Model::Declarations::SubtypeConstraint.new(
2199
+ id: id,
2200
+ applies_to: applies_to,
2201
+ abstract: abstract,
2202
+ total_over: total_over,
2203
+ supertype_expression: supertype_expression
2204
+ )
2205
+ end
2206
+
2207
+ def visit_subtype_constraint_head(ctx)
2208
+ raise 'Invalid state'
2209
+ end
2210
+
2211
+ def visit_subtype_constraint_id(ctx)
2212
+ ctx__SimpleId = ctx.SimpleId
2213
+
2214
+ handle_simple_id(ctx__SimpleId)
2215
+ end
2216
+
2217
+ def visit_subtype_declaration(ctx)
2218
+ ctx__entity_ref = ctx.entity_ref
2219
+
2220
+ visit_if_map(ctx__entity_ref)
2221
+ end
2222
+
2223
+ def visit_supertype_constraint(ctx)
2224
+ raise 'Invalid state'
2225
+ end
2226
+
2227
+ def visit_supertype_expression(ctx)
2228
+ ctx__supertype_factor = ctx.supertype_factor
2229
+ ctx__ANDOR = ctx.ANDOR
2230
+
2231
+ if ctx__supertype_factor
2232
+ if ctx__supertype_factor.length >= 2
2233
+ if ctx__ANDOR and ctx__ANDOR.length == ctx__supertype_factor.length - 1
2234
+ operands = ctx__supertype_factor.map(&self.method(:visit))
2235
+ operators = ctx__ANDOR.map{Model::SupertypeExpressions::BinarySupertypeExpression::ANDOR}
2236
+
2237
+ handle_binary_supertype_expression(operands, operators)
2238
+ else
2239
+ raise 'Invalid state'
2240
+ end
2241
+ elsif ctx__supertype_factor.length == 1
2242
+ visit(ctx__supertype_factor[0])
2243
+ else
2244
+ raise 'Invalid state'
2245
+ end
2246
+ end
2247
+ end
2248
+
2249
+ def visit_supertype_factor(ctx)
2250
+ ctx__supertype_term = ctx.supertype_term
2251
+ ctx__AND = ctx.AND
2252
+
2253
+ if ctx__supertype_term
2254
+ if ctx__supertype_term.length >= 2
2255
+ if ctx__AND and ctx__AND.length == ctx__supertype_term.length - 1
2256
+ operands = ctx__supertype_term.map(&self.method(:visit))
2257
+ operators = ctx__AND.map{Model::SupertypeExpressions::BinarySupertypeExpression::AND}
2258
+
2259
+ handle_binary_supertype_expression(operands, operators)
2260
+ else
2261
+ raise 'Invalid state'
2262
+ end
2263
+ elsif ctx__supertype_term.length == 1
2264
+ visit(ctx__supertype_term[0])
2265
+ else
2266
+ raise 'Invalid state'
2267
+ end
2268
+ end
2269
+ end
2270
+
2271
+ def visit_supertype_rule(ctx)
2272
+ ctx__subtype_constraint = ctx.subtype_constraint
2273
+
2274
+ visit_if(ctx__subtype_constraint)
2275
+ end
2276
+
2277
+ def visit_supertype_term(ctx)
2278
+ ctx__entity_ref = ctx.entity_ref
2279
+ ctx__one_of = ctx.one_of
2280
+ ctx__supertype_expression = ctx.supertype_expression
2281
+
2282
+ visit_if(ctx__entity_ref || ctx__one_of || ctx__supertype_expression)
2283
+ end
2284
+
2285
+ def visit_syntax(ctx)
2286
+ ctx__schema_decl = ctx.schema_decl
2287
+
2288
+ schemas = visit_if_map(ctx__schema_decl)
2289
+
2290
+ Model::Repository.new(
2291
+ schemas: schemas
2292
+ )
2293
+ end
2294
+
2295
+ def visit_term(ctx)
2296
+ ctx__factor = ctx.factor
2297
+ ctx__multiplication_like_op = ctx.multiplication_like_op
2298
+
2299
+ if ctx__factor
2300
+ if ctx__factor.length >= 2
2301
+ if ctx__multiplication_like_op and ctx__multiplication_like_op.length == ctx__factor.length - 1
2302
+ operands = ctx__factor.map(&self.method(:visit))
2303
+ operators = ctx__multiplication_like_op.map(&self.method(:visit))
2304
+
2305
+ handle_binary_expression(operands, operators)
2306
+ else
2307
+ raise 'Invalid state'
2308
+ end
2309
+ elsif ctx__factor.length == 1
2310
+ visit(ctx__factor[0])
2311
+ else
2312
+ raise 'Invalid state'
2313
+ end
2314
+ end
2315
+ end
2316
+
2317
+ def visit_total_over(ctx)
2318
+ ctx__entity_ref = ctx.entity_ref
2319
+
2320
+ visit_if_map(ctx__entity_ref)
2321
+ end
2322
+
2323
+ def visit_type_decl(ctx)
2324
+ ctx__type_id = ctx.type_id
2325
+ ctx__underlying_type = ctx.underlying_type
2326
+ ctx__where_clause = ctx.where_clause
2327
+
2328
+ id = visit_if(ctx__type_id)
2329
+ underlying_type = visit_if(ctx__underlying_type)
2330
+ where_rules = visit_if(ctx__where_clause, [])
2331
+
2332
+ Model::Declarations::Type.new(
2333
+ id: id,
2334
+ underlying_type: underlying_type,
2335
+ where_rules: where_rules
2336
+ )
2337
+ end
2338
+
2339
+ def visit_type_id(ctx)
2340
+ ctx__SimpleId = ctx.SimpleId
2341
+
2342
+ handle_simple_id(ctx__SimpleId)
2343
+ end
2344
+
2345
+ def visit_type_label(ctx)
2346
+ ctx__type_label_id = ctx.type_label_id
2347
+ ctx__type_label_ref = ctx.type_label_ref
2348
+
2349
+ visit_if(ctx__type_label_id || ctx__type_label_ref)
2350
+ end
2351
+
2352
+ def visit_type_label_id(ctx)
2353
+ ctx__SimpleId = ctx.SimpleId
2354
+
2355
+ handle_simple_id(ctx__SimpleId)
2356
+ end
2357
+
2358
+ def visit_unary_op(ctx)
2359
+ ctx__text = ctx.text
2360
+ ctx__PLUS = ctx__text == '+'
2361
+ ctx__MINUS = ctx__text == '-'
2362
+ ctx__NOT = ctx.NOT
2363
+
2364
+ if ctx__PLUS
2365
+ Model::Expressions::UnaryExpression::PLUS
2366
+ elsif ctx__MINUS
2367
+ Model::Expressions::UnaryExpression::MINUS
2368
+ elsif ctx__NOT
2369
+ Model::Expressions::UnaryExpression::NOT
2370
+ else
2371
+ raise 'Invalid state'
2372
+ end
2373
+ end
2374
+
2375
+ def visit_underlying_type(ctx)
2376
+ ctx__concrete_types = ctx.concrete_types
2377
+ ctx__constructed_types = ctx.constructed_types
2378
+
2379
+ visit_if(ctx__concrete_types || ctx__constructed_types)
2380
+ end
2381
+
2382
+ def visit_unique_clause(ctx)
2383
+ ctx__unique_rule = ctx.unique_rule
2384
+
2385
+ visit_if_map(ctx__unique_rule)
2386
+ end
2387
+
2388
+ def visit_unique_rule(ctx)
2389
+ ctx__rule_label_id = ctx.rule_label_id
2390
+ ctx__referenced_attribute = ctx.referenced_attribute
2391
+
2392
+ id = visit_if(ctx__rule_label_id)
2393
+ attributes = visit_if_map(ctx__referenced_attribute)
2394
+
2395
+ Model::Declarations::UniqueRule.new(
2396
+ id: id,
2397
+ attributes: attributes
2398
+ )
2399
+ end
2400
+
2401
+ def visit_until_control(ctx)
2402
+ ctx__logical_expression = ctx.logical_expression
2403
+
2404
+ visit_if(ctx__logical_expression)
2405
+ end
2406
+
2407
+ def visit_use_clause(ctx)
2408
+ ctx__schema_ref = ctx.schema_ref
2409
+ ctx__named_type_or_rename = ctx.named_type_or_rename
2410
+
2411
+ schema = visit_if(ctx__schema_ref)
2412
+ items = visit_if_map(ctx__named_type_or_rename)
2413
+
2414
+ Model::Declarations::Interface.new(
2415
+ kind: Model::Declarations::Interface::USE,
2416
+ schema: schema,
2417
+ items: items
2418
+ )
2419
+ end
2420
+
2421
+ def visit_variable_id(ctx)
2422
+ ctx__SimpleId = ctx.SimpleId
2423
+
2424
+ handle_simple_id(ctx__SimpleId)
2425
+ end
2426
+
2427
+ def visit_where_clause(ctx)
2428
+ ctx__domain_rule = ctx.domain_rule
2429
+
2430
+ visit_if_map(ctx__domain_rule)
2431
+ end
2432
+
2433
+ def visit_while_control(ctx)
2434
+ ctx__logical_expression = ctx.logical_expression
2435
+
2436
+ visit_if(ctx__logical_expression)
2437
+ end
2438
+
2439
+ def visit_width(ctx)
2440
+ ctx__numeric_expression = ctx.numeric_expression
2441
+
2442
+ visit_if(ctx__numeric_expression)
2443
+ end
2444
+
2445
+ def visit_width_spec(ctx)
2446
+ raise 'Invalid state'
2447
+ end
2448
+
2449
+ def handle_binary_expression(operands, operators)
2450
+ if operands.length != operators.length + 1
2451
+ raise 'Invalid state'
2452
+ end
2453
+
2454
+ expression = Model::Expressions::BinaryExpression.new(
2455
+ operator: operators[0],
2456
+ operand1: operands[0],
2457
+ operand2: operands[1]
2458
+ )
2459
+ operators[1..(operators.length - 1)].each_with_index do |operator, i|
2460
+ expression = Model::Expressions::BinaryExpression.new(
2461
+ operator: operator,
2462
+ operand1: expression,
2463
+ operand2: operands[i + 2]
2464
+ )
2465
+ end
2466
+ expression
2467
+ end
2468
+
2469
+ def handle_binary_supertype_expression(operands, operators)
2470
+ if operands.length != operators.length + 1
2471
+ raise 'Invalid state'
2472
+ end
2473
+
2474
+ expression = Model::SupertypeExpressions::BinarySupertypeExpression.new(
2475
+ operator: operators[0],
2476
+ operand1: operands[0],
2477
+ operand2: operands[1]
2478
+ )
2479
+ operators[1..(operators.length - 1)].each_with_index do |operator, i|
2480
+ expression = Model::SupertypeExpressions::BinarySupertypeExpression.new(
2481
+ operator: operator,
2482
+ operand1: expression,
2483
+ operand2: operands[i + 2]
2484
+ )
2485
+ end
2486
+ expression
2487
+ end
2488
+
2489
+ def handle_qualified_ref(ref, qualifiers)
2490
+ qualifiers.reduce(ref) do |ref, ctx|
2491
+ ctx__attribute_qualifier = ctx.attribute_qualifier
2492
+ ctx__group_qualifier = ctx.group_qualifier
2493
+ ctx__index_qualifier = ctx.index_qualifier
2494
+
2495
+ if ctx__attribute_qualifier
2496
+ attribute_reference = visit_if(ctx__attribute_qualifier)
2497
+
2498
+ Model::References::AttributeReference.new(
2499
+ ref: ref,
2500
+ attribute: attribute_reference.attribute
2501
+ )
2502
+ elsif ctx__group_qualifier
2503
+ group_reference = visit_if(ctx__group_qualifier)
2504
+
2505
+ Model::References::GroupReference.new(
2506
+ ref: ref,
2507
+ entity: group_reference.entity
2508
+ )
2509
+ elsif ctx__index_qualifier
2510
+ index_reference = visit_if(ctx__index_qualifier)
2511
+
2512
+ Model::References::IndexReference.new(
2513
+ ref: ref,
2514
+ index1: index_reference.index1,
2515
+ index2: index_reference.index2
2516
+ )
2517
+ else
2518
+ raise 'Invalid state'
2519
+ end
2520
+ end
2521
+ end
2522
+
2523
+ def handle_binary_literal(ctx)
2524
+ ctx__text = ctx.text
2525
+
2526
+ value = ctx__text[1..(ctx__text.length - 1)]
2527
+
2528
+ Model::Literals::Binary.new(
2529
+ value: value
2530
+ )
2531
+ end
2532
+
2533
+ def handle_integer_literal(ctx)
2534
+ ctx__text = ctx.text
2535
+
2536
+ value = ctx__text
2537
+
2538
+ Model::Literals::Integer.new(
2539
+ value: value
2540
+ )
2541
+ end
2542
+
2543
+ def handle_real_literal(ctx)
2544
+ ctx__text = ctx.text
2545
+
2546
+ value = ctx__text
2547
+
2548
+ Model::Literals::Real.new(
2549
+ value: value
2550
+ )
2551
+ end
2552
+
2553
+ def handle_simple_id(ctx)
2554
+ ctx__text = ctx.text
2555
+
2556
+ ctx__text
2557
+ end
2558
+
2559
+ def handle_simple_string_literal(ctx)
2560
+ ctx__text = ctx.text
2561
+
2562
+ value = ctx__text[1..(ctx__text.length - 2)].force_encoding('UTF-8')
2563
+
2564
+ Model::Literals::String.new(
2565
+ value: value
2566
+ )
2567
+ end
2568
+
2569
+ def handle_encoded_string_literal(ctx)
2570
+ ctx__text = ctx.text
2571
+
2572
+ value = ctx__text[1..(ctx__text.length - 2)].force_encoding('UTF-8')
2573
+
2574
+ Model::Literals::String.new(
2575
+ value: value,
2576
+ encoded: true
2577
+ )
2578
+ end
2579
+ end
2580
+ end
2581
+ end