expressir 1.3.3-x86_64-linux-musl

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