expressir 1.2.5-x64-mingw-ucrt

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