expressir 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/demo.rb +16 -0
  4. data/expressir.gemspec +1 -0
  5. data/generate-parser.sh +29 -0
  6. data/lib/expressir/express_exp/generated/ExpressBaseVisitor.rb +779 -0
  7. data/lib/expressir/express_exp/generated/ExpressLexer.rb +841 -0
  8. data/lib/expressir/express_exp/generated/ExpressParser.rb +12136 -0
  9. data/lib/expressir/express_exp/generated/ExpressVisitor.rb +394 -0
  10. data/lib/expressir/express_exp/parser.rb +25 -0
  11. data/lib/expressir/express_exp/visitor.rb +2103 -0
  12. data/lib/expressir/model/constant.rb +15 -0
  13. data/lib/expressir/model/derived.rb +15 -0
  14. data/lib/expressir/model/entity.rb +29 -0
  15. data/lib/expressir/model/explicit.rb +15 -0
  16. data/lib/expressir/model/expressions/aggregate_element.rb +15 -0
  17. data/lib/expressir/model/expressions/attribute_qualifier.rb +13 -0
  18. data/lib/expressir/model/expressions/entity_constructor.rb +15 -0
  19. data/lib/expressir/model/expressions/expression.rb +15 -0
  20. data/lib/expressir/model/expressions/function_call.rb +15 -0
  21. data/lib/expressir/model/expressions/group_qualifier.rb +13 -0
  22. data/lib/expressir/model/expressions/index_qualifier.rb +15 -0
  23. data/lib/expressir/model/expressions/interval.rb +21 -0
  24. data/lib/expressir/model/expressions/qualified_ref.rb +15 -0
  25. data/lib/expressir/model/expressions/query.rb +17 -0
  26. data/lib/expressir/model/expressions/unknown.rb +8 -0
  27. data/lib/expressir/model/function.rb +23 -0
  28. data/lib/expressir/model/inverse.rb +15 -0
  29. data/lib/expressir/model/local.rb +15 -0
  30. data/lib/expressir/model/operators/addition.rb +8 -0
  31. data/lib/expressir/model/operators/and.rb +8 -0
  32. data/lib/expressir/model/operators/andor.rb +8 -0
  33. data/lib/expressir/model/operators/combine.rb +8 -0
  34. data/lib/expressir/model/operators/equal.rb +8 -0
  35. data/lib/expressir/model/operators/exponentiation.rb +8 -0
  36. data/lib/expressir/model/operators/greater_than.rb +8 -0
  37. data/lib/expressir/model/operators/greater_than_or_equal.rb +8 -0
  38. data/lib/expressir/model/operators/in.rb +8 -0
  39. data/lib/expressir/model/operators/instance_equal.rb +8 -0
  40. data/lib/expressir/model/operators/instance_not_equal.rb +8 -0
  41. data/lib/expressir/model/operators/integer_division.rb +8 -0
  42. data/lib/expressir/model/operators/less_than.rb +8 -0
  43. data/lib/expressir/model/operators/less_than_or_equal.rb +8 -0
  44. data/lib/expressir/model/operators/like.rb +8 -0
  45. data/lib/expressir/model/operators/modulo.rb +8 -0
  46. data/lib/expressir/model/operators/multiplication.rb +8 -0
  47. data/lib/expressir/model/operators/not.rb +8 -0
  48. data/lib/expressir/model/operators/not_equal.rb +8 -0
  49. data/lib/expressir/model/operators/oneof.rb +8 -0
  50. data/lib/expressir/model/operators/or.rb +8 -0
  51. data/lib/expressir/model/operators/real_division.rb +8 -0
  52. data/lib/expressir/model/operators/subtraction.rb +8 -0
  53. data/lib/expressir/model/operators/unary_minus.rb +8 -0
  54. data/lib/expressir/model/operators/unary_plus.rb +8 -0
  55. data/lib/expressir/model/operators/xor.rb +8 -0
  56. data/lib/expressir/model/parameter.rb +15 -0
  57. data/lib/expressir/model/procedure.rb +21 -0
  58. data/lib/expressir/model/ref.rb +11 -0
  59. data/lib/expressir/model/reference.rb +13 -0
  60. data/lib/expressir/model/renamed_ref.rb +13 -0
  61. data/lib/expressir/model/repository.rb +11 -0
  62. data/lib/expressir/model/rule.rb +23 -0
  63. data/lib/expressir/model/schema.rb +41 -0
  64. data/lib/expressir/model/statements/alias.rb +17 -0
  65. data/lib/expressir/model/statements/assignment.rb +15 -0
  66. data/lib/expressir/model/statements/case.rb +17 -0
  67. data/lib/expressir/model/statements/case_action.rb +15 -0
  68. data/lib/expressir/model/statements/compound.rb +13 -0
  69. data/lib/expressir/model/statements/escape.rb +8 -0
  70. data/lib/expressir/model/statements/if.rb +17 -0
  71. data/lib/expressir/model/statements/null.rb +8 -0
  72. data/lib/expressir/model/statements/procedure_call.rb +15 -0
  73. data/lib/expressir/model/statements/repeat.rb +25 -0
  74. data/lib/expressir/model/statements/return.rb +13 -0
  75. data/lib/expressir/model/statements/skip.rb +8 -0
  76. data/lib/expressir/model/subtype_constraint.rb +19 -0
  77. data/lib/expressir/model/type.rb +15 -0
  78. data/lib/expressir/model/types/aggregate.rb +15 -0
  79. data/lib/expressir/model/types/array.rb +21 -0
  80. data/lib/expressir/model/types/bag.rb +17 -0
  81. data/lib/expressir/model/types/binary.rb +15 -0
  82. data/lib/expressir/model/types/boolean.rb +8 -0
  83. data/lib/expressir/model/types/enumeration.rb +19 -0
  84. data/lib/expressir/model/types/generic.rb +13 -0
  85. data/lib/expressir/model/types/generic_entity.rb +13 -0
  86. data/lib/expressir/model/types/integer.rb +8 -0
  87. data/lib/expressir/model/types/list.rb +19 -0
  88. data/lib/expressir/model/types/logical.rb +8 -0
  89. data/lib/expressir/model/types/number.rb +8 -0
  90. data/lib/expressir/model/types/real.rb +13 -0
  91. data/lib/expressir/model/types/select.rb +21 -0
  92. data/lib/expressir/model/types/set.rb +17 -0
  93. data/lib/expressir/model/types/string.rb +15 -0
  94. data/lib/expressir/model/unique.rb +13 -0
  95. data/lib/expressir/model/use.rb +13 -0
  96. data/lib/expressir/model/where.rb +13 -0
  97. data/lib/expressir/version.rb +1 -1
  98. data/original/examples/syntax/syntax.exp +295 -0
  99. data/spec/expressir/express_exp/ap233_spec.rb +22 -0
  100. data/spec/expressir/express_exp/syntax_spec.rb +2640 -0
  101. metadata +115 -5
@@ -0,0 +1,394 @@
1
+ # Generated from ../express-grammar/Express.g4 by ANTLR 4.7.2
2
+ require 'antlr4/runtime'
3
+
4
+ module Expressir
5
+ module ExpressExp
6
+ module Generated
7
+
8
+ class ExpressVisitor < Antlr4::Runtime::ParseTreeVisitor
9
+ def visitAttributeRef(ctx)
10
+ end
11
+ def visitConstantRef(ctx)
12
+ end
13
+ def visitEntityRef(ctx)
14
+ end
15
+ def visitEnumerationRef(ctx)
16
+ end
17
+ def visitFunctionRef(ctx)
18
+ end
19
+ def visitParameterRef(ctx)
20
+ end
21
+ def visitProcedureRef(ctx)
22
+ end
23
+ def visitRuleLabelRef(ctx)
24
+ end
25
+ def visitRuleRef(ctx)
26
+ end
27
+ def visitSchemaRef(ctx)
28
+ end
29
+ def visitSubtypeConstraintRef(ctx)
30
+ end
31
+ def visitTypeLabelRef(ctx)
32
+ end
33
+ def visitTypeRef(ctx)
34
+ end
35
+ def visitVariableRef(ctx)
36
+ end
37
+ def visitAbstractEntityDeclaration(ctx)
38
+ end
39
+ def visitAbstractSupertype(ctx)
40
+ end
41
+ def visitAbstractSupertypeDeclaration(ctx)
42
+ end
43
+ def visitActualParameterList(ctx)
44
+ end
45
+ def visitAddLikeOp(ctx)
46
+ end
47
+ def visitAggregateInitializer(ctx)
48
+ end
49
+ def visitAggregateSource(ctx)
50
+ end
51
+ def visitAggregateType(ctx)
52
+ end
53
+ def visitAggregationTypes(ctx)
54
+ end
55
+ def visitAlgorithmHead(ctx)
56
+ end
57
+ def visitAliasStmt(ctx)
58
+ end
59
+ def visitArrayType(ctx)
60
+ end
61
+ def visitAssignmentStmt(ctx)
62
+ end
63
+ def visitAttributeDecl(ctx)
64
+ end
65
+ def visitAttributeId(ctx)
66
+ end
67
+ def visitAttributeQualifier(ctx)
68
+ end
69
+ def visitBagType(ctx)
70
+ end
71
+ def visitBinaryType(ctx)
72
+ end
73
+ def visitBooleanType(ctx)
74
+ end
75
+ def visitBound1(ctx)
76
+ end
77
+ def visitBound2(ctx)
78
+ end
79
+ def visitBoundSpec(ctx)
80
+ end
81
+ def visitBuiltInConstant(ctx)
82
+ end
83
+ def visitBuiltInFunction(ctx)
84
+ end
85
+ def visitBuiltInProcedure(ctx)
86
+ end
87
+ def visitCaseAction(ctx)
88
+ end
89
+ def visitCaseLabel(ctx)
90
+ end
91
+ def visitCaseStmt(ctx)
92
+ end
93
+ def visitCompoundStmt(ctx)
94
+ end
95
+ def visitConcreteTypes(ctx)
96
+ end
97
+ def visitConstantBody(ctx)
98
+ end
99
+ def visitConstantDecl(ctx)
100
+ end
101
+ def visitConstantFactor(ctx)
102
+ end
103
+ def visitConstantId(ctx)
104
+ end
105
+ def visitConstructedTypes(ctx)
106
+ end
107
+ def visitDeclaration(ctx)
108
+ end
109
+ def visitDerivedAttr(ctx)
110
+ end
111
+ def visitDeriveClause(ctx)
112
+ end
113
+ def visitDomainRule(ctx)
114
+ end
115
+ def visitElement(ctx)
116
+ end
117
+ def visitEntityBody(ctx)
118
+ end
119
+ def visitEntityConstructor(ctx)
120
+ end
121
+ def visitEntityDecl(ctx)
122
+ end
123
+ def visitEntityHead(ctx)
124
+ end
125
+ def visitEntityId(ctx)
126
+ end
127
+ def visitEnumerationExtension(ctx)
128
+ end
129
+ def visitEnumerationId(ctx)
130
+ end
131
+ def visitEnumerationItems(ctx)
132
+ end
133
+ def visitEnumerationReference(ctx)
134
+ end
135
+ def visitEnumerationType(ctx)
136
+ end
137
+ def visitEscapeStmt(ctx)
138
+ end
139
+ def visitExplicitAttr(ctx)
140
+ end
141
+ def visitExpression(ctx)
142
+ end
143
+ def visitFactor(ctx)
144
+ end
145
+ def visitFormalParameter(ctx)
146
+ end
147
+ def visitFunctionCall(ctx)
148
+ end
149
+ def visitFunctionDecl(ctx)
150
+ end
151
+ def visitFunctionHead(ctx)
152
+ end
153
+ def visitFunctionId(ctx)
154
+ end
155
+ def visitGeneralizedTypes(ctx)
156
+ end
157
+ def visitGeneralAggregationTypes(ctx)
158
+ end
159
+ def visitGeneralArrayType(ctx)
160
+ end
161
+ def visitGeneralBagType(ctx)
162
+ end
163
+ def visitGeneralListType(ctx)
164
+ end
165
+ def visitGeneralRef(ctx)
166
+ end
167
+ def visitGeneralSetType(ctx)
168
+ end
169
+ def visitGenericEntityType(ctx)
170
+ end
171
+ def visitGenericType(ctx)
172
+ end
173
+ def visitGroupQualifier(ctx)
174
+ end
175
+ def visitIfStmt(ctx)
176
+ end
177
+ def visitIncrement(ctx)
178
+ end
179
+ def visitIncrementControl(ctx)
180
+ end
181
+ def visitIndex(ctx)
182
+ end
183
+ def visitIndex1(ctx)
184
+ end
185
+ def visitIndex2(ctx)
186
+ end
187
+ def visitIndexQualifier(ctx)
188
+ end
189
+ def visitInstantiableType(ctx)
190
+ end
191
+ def visitIntegerType(ctx)
192
+ end
193
+ def visitInterfaceSpecification(ctx)
194
+ end
195
+ def visitInterval(ctx)
196
+ end
197
+ def visitIntervalHigh(ctx)
198
+ end
199
+ def visitIntervalItem(ctx)
200
+ end
201
+ def visitIntervalLow(ctx)
202
+ end
203
+ def visitIntervalOp(ctx)
204
+ end
205
+ def visitInverseAttr(ctx)
206
+ end
207
+ def visitInverseClause(ctx)
208
+ end
209
+ def visitListType(ctx)
210
+ end
211
+ def visitLiteral(ctx)
212
+ end
213
+ def visitLocalDecl(ctx)
214
+ end
215
+ def visitLocalVariable(ctx)
216
+ end
217
+ def visitLogicalExpression(ctx)
218
+ end
219
+ def visitLogicalLiteral(ctx)
220
+ end
221
+ def visitLogicalType(ctx)
222
+ end
223
+ def visitMultiplicationLikeOp(ctx)
224
+ end
225
+ def visitNamedTypes(ctx)
226
+ end
227
+ def visitNamedTypeOrRename(ctx)
228
+ end
229
+ def visitNullStmt(ctx)
230
+ end
231
+ def visitNumberType(ctx)
232
+ end
233
+ def visitNumericExpression(ctx)
234
+ end
235
+ def visitOneOf(ctx)
236
+ end
237
+ def visitParameter(ctx)
238
+ end
239
+ def visitParameterId(ctx)
240
+ end
241
+ def visitParameterType(ctx)
242
+ end
243
+ def visitPopulation(ctx)
244
+ end
245
+ def visitPrecisionSpec(ctx)
246
+ end
247
+ def visitPrimary(ctx)
248
+ end
249
+ def visitProcedureCallStmt(ctx)
250
+ end
251
+ def visitProcedureDecl(ctx)
252
+ end
253
+ def visitProcedureHead(ctx)
254
+ end
255
+ def visitProcedureId(ctx)
256
+ end
257
+ def visitQualifiableFactor(ctx)
258
+ end
259
+ def visitQualifiedAttribute(ctx)
260
+ end
261
+ def visitQualifier(ctx)
262
+ end
263
+ def visitQueryExpression(ctx)
264
+ end
265
+ def visitRealType(ctx)
266
+ end
267
+ def visitRedeclaredAttribute(ctx)
268
+ end
269
+ def visitReferencedAttribute(ctx)
270
+ end
271
+ def visitReferenceClause(ctx)
272
+ end
273
+ def visitRelOp(ctx)
274
+ end
275
+ def visitRelOpExtended(ctx)
276
+ end
277
+ def visitRenameId(ctx)
278
+ end
279
+ def visitRepeatControl(ctx)
280
+ end
281
+ def visitRepeatStmt(ctx)
282
+ end
283
+ def visitRepetition(ctx)
284
+ end
285
+ def visitResourceOrRename(ctx)
286
+ end
287
+ def visitResourceRef(ctx)
288
+ end
289
+ def visitReturnStmt(ctx)
290
+ end
291
+ def visitRuleDecl(ctx)
292
+ end
293
+ def visitRuleHead(ctx)
294
+ end
295
+ def visitRuleId(ctx)
296
+ end
297
+ def visitRuleLabelId(ctx)
298
+ end
299
+ def visitSchemaBody(ctx)
300
+ end
301
+ def visitSchemaDecl(ctx)
302
+ end
303
+ def visitSchemaId(ctx)
304
+ end
305
+ def visitSchemaVersionId(ctx)
306
+ end
307
+ def visitSelector(ctx)
308
+ end
309
+ def visitSelectExtension(ctx)
310
+ end
311
+ def visitSelectList(ctx)
312
+ end
313
+ def visitSelectType(ctx)
314
+ end
315
+ def visitSetType(ctx)
316
+ end
317
+ def visitSimpleExpression(ctx)
318
+ end
319
+ def visitSimpleFactor(ctx)
320
+ end
321
+ def visitSimpleTypes(ctx)
322
+ end
323
+ def visitSkipStmt(ctx)
324
+ end
325
+ def visitStmt(ctx)
326
+ end
327
+ def visitStringLiteral(ctx)
328
+ end
329
+ def visitStringType(ctx)
330
+ end
331
+ def visitSubsuper(ctx)
332
+ end
333
+ def visitSubtypeConstraint(ctx)
334
+ end
335
+ def visitSubtypeConstraintBody(ctx)
336
+ end
337
+ def visitSubtypeConstraintDecl(ctx)
338
+ end
339
+ def visitSubtypeConstraintHead(ctx)
340
+ end
341
+ def visitSubtypeConstraintId(ctx)
342
+ end
343
+ def visitSubtypeDeclaration(ctx)
344
+ end
345
+ def visitSupertypeConstraint(ctx)
346
+ end
347
+ def visitSupertypeExpression(ctx)
348
+ end
349
+ def visitSupertypeFactor(ctx)
350
+ end
351
+ def visitSupertypeRule(ctx)
352
+ end
353
+ def visitSupertypeTerm(ctx)
354
+ end
355
+ def visitSyntax(ctx)
356
+ end
357
+ def visitTerm(ctx)
358
+ end
359
+ def visitTotalOver(ctx)
360
+ end
361
+ def visitTypeDecl(ctx)
362
+ end
363
+ def visitTypeId(ctx)
364
+ end
365
+ def visitTypeLabel(ctx)
366
+ end
367
+ def visitTypeLabelId(ctx)
368
+ end
369
+ def visitUnaryOp(ctx)
370
+ end
371
+ def visitUnderlyingType(ctx)
372
+ end
373
+ def visitUniqueClause(ctx)
374
+ end
375
+ def visitUniqueRule(ctx)
376
+ end
377
+ def visitUntilControl(ctx)
378
+ end
379
+ def visitUseClause(ctx)
380
+ end
381
+ def visitVariableId(ctx)
382
+ end
383
+ def visitWhereClause(ctx)
384
+ end
385
+ def visitWhileControl(ctx)
386
+ end
387
+ def visitWidth(ctx)
388
+ end
389
+ def visitWidthSpec(ctx)
390
+ end
391
+ end
392
+ end
393
+ end
394
+ end
@@ -0,0 +1,25 @@
1
+ require 'antlr4/runtime'
2
+ require 'expressir/express_exp/generated/ExpressVisitor'
3
+ require 'expressir/express_exp/generated/ExpressLexer'
4
+ require 'expressir/express_exp/generated/ExpressParser'
5
+ require 'expressir/express_exp/visitor'
6
+
7
+ module Expressir
8
+ module ExpressExp
9
+ class Parser
10
+ def self.from_exp(file)
11
+ input = File.read(file)
12
+
13
+ char_stream = Antlr4::Runtime::CharStreams.from_string(input, 'String')
14
+ lexer = Generated::ExpressLexer.new(char_stream)
15
+ token_stream = Antlr4::Runtime::CommonTokenStream.new(lexer)
16
+ parser = Generated::ExpressParser.new(token_stream)
17
+
18
+ parse_tree = parser.syntax()
19
+
20
+ visitor = Visitor.new()
21
+ visitor.visit(parse_tree)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,2103 @@
1
+ require 'expressir/express_exp/generated/ExpressBaseVisitor'
2
+ require 'expressir/model/constant'
3
+ require 'expressir/model/derived'
4
+ require 'expressir/model/entity'
5
+ require 'expressir/model/explicit'
6
+ require 'expressir/model/function'
7
+ require 'expressir/model/inverse'
8
+ require 'expressir/model/local'
9
+ require 'expressir/model/parameter'
10
+ require 'expressir/model/procedure'
11
+ require 'expressir/model/ref'
12
+ require 'expressir/model/reference'
13
+ require 'expressir/model/renamed_ref'
14
+ require 'expressir/model/repository'
15
+ require 'expressir/model/rule'
16
+ require 'expressir/model/schema'
17
+ require 'expressir/model/subtype_constraint'
18
+ require 'expressir/model/type'
19
+ require 'expressir/model/unique'
20
+ require 'expressir/model/use'
21
+ require 'expressir/model/where'
22
+ require 'expressir/model/expressions/aggregate_element'
23
+ require 'expressir/model/expressions/attribute_qualifier'
24
+ require 'expressir/model/expressions/entity_constructor'
25
+ require 'expressir/model/expressions/expression'
26
+ require 'expressir/model/expressions/function_call'
27
+ require 'expressir/model/expressions/group_qualifier'
28
+ require 'expressir/model/expressions/index_qualifier'
29
+ require 'expressir/model/expressions/interval'
30
+ require 'expressir/model/expressions/qualified_ref'
31
+ require 'expressir/model/expressions/query'
32
+ require 'expressir/model/expressions/unknown'
33
+ require 'expressir/model/operators/addition'
34
+ require 'expressir/model/operators/and'
35
+ require 'expressir/model/operators/andor'
36
+ require 'expressir/model/operators/combine'
37
+ require 'expressir/model/operators/equal'
38
+ require 'expressir/model/operators/exponentiation'
39
+ require 'expressir/model/operators/greater_than'
40
+ require 'expressir/model/operators/greater_than_or_equal'
41
+ require 'expressir/model/operators/in'
42
+ require 'expressir/model/operators/instance_equal'
43
+ require 'expressir/model/operators/instance_not_equal'
44
+ require 'expressir/model/operators/integer_division'
45
+ require 'expressir/model/operators/less_than'
46
+ require 'expressir/model/operators/less_than_or_equal'
47
+ require 'expressir/model/operators/like'
48
+ require 'expressir/model/operators/modulo'
49
+ require 'expressir/model/operators/multiplication'
50
+ require 'expressir/model/operators/not'
51
+ require 'expressir/model/operators/not_equal'
52
+ require 'expressir/model/operators/oneof'
53
+ require 'expressir/model/operators/or'
54
+ require 'expressir/model/operators/real_division'
55
+ require 'expressir/model/operators/subtraction'
56
+ require 'expressir/model/operators/unary_plus'
57
+ require 'expressir/model/operators/unary_minus'
58
+ require 'expressir/model/operators/xor'
59
+ require 'expressir/model/statements/alias'
60
+ require 'expressir/model/statements/assignment'
61
+ require 'expressir/model/statements/case'
62
+ require 'expressir/model/statements/case_action'
63
+ require 'expressir/model/statements/compound'
64
+ require 'expressir/model/statements/escape'
65
+ require 'expressir/model/statements/if'
66
+ require 'expressir/model/statements/null'
67
+ require 'expressir/model/statements/procedure_call'
68
+ require 'expressir/model/statements/repeat'
69
+ require 'expressir/model/statements/return'
70
+ require 'expressir/model/statements/skip'
71
+ require 'expressir/model/types/aggregate'
72
+ require 'expressir/model/types/array'
73
+ require 'expressir/model/types/bag'
74
+ require 'expressir/model/types/binary'
75
+ require 'expressir/model/types/boolean'
76
+ require 'expressir/model/types/enumeration'
77
+ require 'expressir/model/types/generic_entity'
78
+ require 'expressir/model/types/generic'
79
+ require 'expressir/model/types/integer'
80
+ require 'expressir/model/types/list'
81
+ require 'expressir/model/types/logical'
82
+ require 'expressir/model/types/number'
83
+ require 'expressir/model/types/real'
84
+ require 'expressir/model/types/set'
85
+ require 'expressir/model/types/select'
86
+ require 'expressir/model/types/string'
87
+
88
+ # issues:
89
+ #
90
+ # reference type is not recognized
91
+ # see note in A.1.5 Interpreted identifiers
92
+ # > It is expected that identifiers matching these syntax rules are known to an implementation.
93
+ # > How the implementation obtains this information is of no concern to the definition of the language. One
94
+ # > method of gaining this information is multi-pass parsing: the first pass collects the identifiers from their
95
+ # > declarations, so that subsequent passes are then able to distinguish a veriable_ref from a function_ref,
96
+ # > for example.
97
+ #
98
+ # - xxxRef - merged to Ref
99
+ # - entityConstructor, functionCall, procedureCall
100
+ #
101
+ # difference between generalized and instantiable types is not recognized
102
+ # see note in 8.6.2 Parameter data types
103
+ # > A syntactic construct such as ARRAY[1:3] OF REAL satisfies two syntactic productions —
104
+ # > aggregation_type and general_aggregation_type. It is considered to be instantiable no matter which
105
+ # > production it is required to satisfy in the syntax.
106
+
107
+ module Expressir
108
+ module ExpressExp
109
+ class Visitor < Generated::ExpressBaseVisitor
110
+ def visitAttributeRef(ctx)
111
+ id = visit(ctx.attributeId())
112
+
113
+ Model::Ref.new({
114
+ id: id
115
+ })
116
+ end
117
+
118
+ def visitConstantRef(ctx)
119
+ id = visit(ctx.constantId())
120
+
121
+ Model::Ref.new({
122
+ id: id
123
+ })
124
+ end
125
+
126
+ def visitEntityRef(ctx)
127
+ id = visit(ctx.entityId())
128
+
129
+ Model::Ref.new({
130
+ id: id
131
+ })
132
+ end
133
+
134
+ def visitEnumerationRef(ctx)
135
+ id = visit(ctx.enumerationId())
136
+
137
+ Model::Ref.new({
138
+ id: id
139
+ })
140
+ end
141
+
142
+ def visitFunctionRef(ctx)
143
+ id = visit(ctx.functionId())
144
+
145
+ Model::Ref.new({
146
+ id: id
147
+ })
148
+ end
149
+
150
+ def visitParameterRef(ctx)
151
+ id = visit(ctx.parameterId())
152
+
153
+ Model::Ref.new({
154
+ id: id
155
+ })
156
+ end
157
+
158
+ def visitProcedureRef(ctx)
159
+ id = visit(ctx.procedureId())
160
+
161
+ Model::Ref.new({
162
+ id: id
163
+ })
164
+ end
165
+
166
+ def visitRuleLabelRef(ctx)
167
+ id = visit(ctx.ruleLabelId())
168
+
169
+ Model::Ref.new({
170
+ id: id
171
+ })
172
+ end
173
+
174
+ def visitRuleRef(ctx)
175
+ id = visit(ctx.ruleId())
176
+
177
+ Model::Ref.new({
178
+ id: id
179
+ })
180
+ end
181
+
182
+ def visitSchemaRef(ctx)
183
+ id = visit(ctx.schemaId())
184
+
185
+ Model::Ref.new({
186
+ id: id
187
+ })
188
+ end
189
+
190
+ def visitSubtypeConstraintRef(ctx)
191
+ id = visit(ctx.subtypeConstraintId())
192
+
193
+ Model::Ref.new({
194
+ id: id
195
+ })
196
+ end
197
+
198
+ def visitTypeLabelRef(ctx)
199
+ id = visit(ctx.typeLabelId())
200
+
201
+ Model::Ref.new({
202
+ id: id
203
+ })
204
+ end
205
+
206
+ def visitTypeRef(ctx)
207
+ id = visit(ctx.typeId())
208
+
209
+ Model::Ref.new({
210
+ id: id
211
+ })
212
+ end
213
+
214
+ def visitVariableRef(ctx)
215
+ id = visit(ctx.variableId())
216
+
217
+ Model::Ref.new({
218
+ id: id
219
+ })
220
+ end
221
+
222
+ def visitAbstractEntityDeclaration(ctx)
223
+ raise 'Invalid state'
224
+ end
225
+
226
+ def visitAbstractSupertype(ctx)
227
+ raise 'Invalid state'
228
+ end
229
+
230
+ def visitAbstractSupertypeDeclaration(ctx)
231
+ raise 'Invalid state'
232
+ end
233
+
234
+ def visitActualParameterList(ctx)
235
+ raise 'Invalid state'
236
+ end
237
+
238
+ def visitAddLikeOp(ctx)
239
+ if ctx.text == '+'
240
+ Model::Operators::Addition.new()
241
+ elsif ctx.text == '-'
242
+ Model::Operators::Subtraction.new()
243
+ elsif ctx.OR()
244
+ Model::Operators::Or.new()
245
+ elsif ctx.XOR()
246
+ Model::Operators::Xor.new()
247
+ else
248
+ raise 'Invalid state'
249
+ end
250
+ end
251
+
252
+ def visitAggregateInitializer(ctx)
253
+ ctx.element().map{|ctx| visit(ctx)}
254
+ end
255
+
256
+ def visitAggregateSource(ctx)
257
+ raise 'Invalid state'
258
+ end
259
+
260
+ def visitAggregateType(ctx)
261
+ label = if ctx.typeLabel()
262
+ visit(ctx.typeLabel())
263
+ end
264
+ base_type = visit(ctx.parameterType())
265
+
266
+ Model::Types::Aggregate.new({
267
+ label: label,
268
+ base_type: base_type
269
+ })
270
+ end
271
+
272
+ def visitAggregationTypes(ctx)
273
+ if ctx.arrayType()
274
+ visit(ctx.arrayType())
275
+ elsif ctx.bagType()
276
+ visit(ctx.bagType())
277
+ elsif ctx.listType()
278
+ visit(ctx.listType())
279
+ elsif ctx.setType()
280
+ visit(ctx.setType())
281
+ else
282
+ raise 'Invalid state'
283
+ end
284
+ end
285
+
286
+ def visitAlgorithmHead(ctx)
287
+ raise 'Invalid state'
288
+ end
289
+
290
+ def visitAliasStmt(ctx)
291
+ id = visit(ctx.variableId())
292
+ expression = if ctx.qualifier().length > 0
293
+ ref = visit(ctx.generalRef())
294
+ qualifiers = ctx.qualifier().map{|ctx| visit(ctx)}
295
+
296
+ Model::Expressions::QualifiedRef.new({
297
+ ref: ref,
298
+ qualifiers: qualifiers
299
+ })
300
+ else
301
+ visit(ctx.generalRef())
302
+ end
303
+ statements = ctx.stmt().map{|ctx| visit(ctx)}
304
+
305
+ Model::Statements::Alias.new({
306
+ id: id,
307
+ expression: expression,
308
+ statements: statements
309
+ })
310
+ end
311
+
312
+ def visitArrayType(ctx)
313
+ bound1 = visit(ctx.boundSpec().bound1().numericExpression().simpleExpression())
314
+ bound2 = visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
315
+ optional = !!ctx.OPTIONAL()
316
+ unique = !!ctx.UNIQUE()
317
+ base_type = visit(ctx.instantiableType())
318
+
319
+ Model::Types::Array.new({
320
+ bound1: bound1,
321
+ bound2: bound2,
322
+ optional: optional,
323
+ unique: unique,
324
+ base_type: base_type
325
+ })
326
+ end
327
+
328
+ def visitAssignmentStmt(ctx)
329
+ expression = visit(ctx.expression())
330
+
331
+ ref = if ctx.qualifier().length > 0
332
+ ref = visit(ctx.generalRef())
333
+ qualifiers = ctx.qualifier().map{|ctx| visit(ctx)}
334
+
335
+ Model::Expressions::QualifiedRef.new({
336
+ ref: ref,
337
+ qualifiers: qualifiers
338
+ })
339
+ else
340
+ visit(ctx.generalRef())
341
+ end
342
+
343
+ Model::Statements::Assignment.new({
344
+ ref: ref,
345
+ expression: expression
346
+ })
347
+ end
348
+
349
+ def visitAttributeDecl(ctx)
350
+ if ctx.attributeId()
351
+ visit(ctx.attributeId())
352
+ elsif ctx.redeclaredAttribute()
353
+ visit(ctx.redeclaredAttribute())
354
+ else
355
+ raise 'Invalid state'
356
+ end
357
+ end
358
+
359
+ def visitAttributeId(ctx)
360
+ handleSimpleId(ctx.SimpleId())
361
+ end
362
+
363
+ def visitAttributeQualifier(ctx)
364
+ attribute = visit(ctx.attributeRef())
365
+
366
+ Model::Expressions::AttributeQualifier.new({
367
+ attribute: attribute
368
+ })
369
+ end
370
+
371
+ def visitBagType(ctx)
372
+ bound1, bound2 = if ctx.boundSpec()
373
+ [
374
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
375
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
376
+ ]
377
+ end
378
+ base_type = visit(ctx.instantiableType())
379
+
380
+ Model::Types::Bag.new({
381
+ bound1: bound1,
382
+ bound2: bound2,
383
+ base_type: base_type
384
+ })
385
+ end
386
+
387
+ def visitBinaryType(ctx)
388
+ width = if ctx.widthSpec()
389
+ visit(ctx.widthSpec().width().numericExpression().simpleExpression())
390
+ end
391
+ fixed = if ctx.widthSpec()
392
+ !!ctx.widthSpec().FIXED()
393
+ end
394
+
395
+ Model::Types::Binary.new({
396
+ width: width,
397
+ fixed: fixed
398
+ })
399
+ end
400
+
401
+ def visitBooleanType(ctx)
402
+ Model::Types::Boolean.new()
403
+ end
404
+
405
+ def visitBound1(ctx)
406
+ raise 'Invalid state'
407
+ end
408
+
409
+ def visitBound2(ctx)
410
+ raise 'Invalid state'
411
+ end
412
+
413
+ def visitBoundSpec(ctx)
414
+ raise 'Invalid state'
415
+ end
416
+
417
+ def visitBuiltInConstant(ctx)
418
+ id = ctx.text
419
+
420
+ Model::Ref.new({
421
+ id: id
422
+ })
423
+ end
424
+
425
+ def visitBuiltInFunction(ctx)
426
+ id = ctx.text
427
+
428
+ Model::Ref.new({
429
+ id: id
430
+ })
431
+ end
432
+
433
+ def visitBuiltInProcedure(ctx)
434
+ id = ctx.text
435
+
436
+ Model::Ref.new({
437
+ id: id
438
+ })
439
+ end
440
+
441
+ def visitCaseAction(ctx)
442
+ labels = ctx.caseLabel().map{|ctx| visit(ctx.expression())}
443
+ statement = visit(ctx.stmt())
444
+
445
+ labels.map do |label|
446
+ Model::Statements::CaseAction.new({
447
+ label: label,
448
+ statement: statement
449
+ })
450
+ end
451
+ end
452
+
453
+ def visitCaseLabel(ctx)
454
+ raise 'Invalid state'
455
+ end
456
+
457
+ def visitCaseStmt(ctx)
458
+ selector = visit(ctx.selector().expression())
459
+ actions = ctx.caseAction().map{|ctx| visit(ctx)}.flatten
460
+ otherwise_statement = if ctx.stmt()
461
+ visit(ctx.stmt())
462
+ end
463
+
464
+ Model::Statements::Case.new({
465
+ selector: selector,
466
+ actions: actions,
467
+ otherwise_statement: otherwise_statement
468
+ })
469
+ end
470
+
471
+ def visitCompoundStmt(ctx)
472
+ statements = ctx.stmt().map{|ctx| visit(ctx)}
473
+
474
+ Model::Statements::Compound.new({
475
+ statements: statements
476
+ })
477
+ end
478
+
479
+ def visitConcreteTypes(ctx)
480
+ if ctx.aggregationTypes()
481
+ visit(ctx.aggregationTypes())
482
+ elsif ctx.simpleTypes()
483
+ visit(ctx.simpleTypes())
484
+ elsif ctx.typeRef()
485
+ visit(ctx.typeRef())
486
+ else
487
+ raise 'Invalid state'
488
+ end
489
+ end
490
+
491
+ def visitConstantBody(ctx)
492
+ id = visit(ctx.constantId())
493
+ type = visit(ctx.instantiableType())
494
+ expression = visit(ctx.expression())
495
+
496
+ Model::Constant.new({
497
+ id: id,
498
+ type: type,
499
+ expression: expression
500
+ })
501
+ end
502
+
503
+ def visitConstantDecl(ctx)
504
+ raise 'Invalid state'
505
+ end
506
+
507
+ def visitConstantFactor(ctx)
508
+ if ctx.builtInConstant()
509
+ visit(ctx.builtInConstant())
510
+ elsif ctx.constantRef()
511
+ visit(ctx.constantRef())
512
+ else
513
+ raise 'Invalid state'
514
+ end
515
+ end
516
+
517
+ def visitConstantId(ctx)
518
+ handleSimpleId(ctx.SimpleId())
519
+ end
520
+
521
+ def visitConstructedTypes(ctx)
522
+ if ctx.enumerationType()
523
+ visit(ctx.enumerationType())
524
+ elsif ctx.selectType()
525
+ visit(ctx.selectType())
526
+ else
527
+ raise 'Invalid state'
528
+ end
529
+ end
530
+
531
+ def visitDeclaration(ctx)
532
+ if ctx.entityDecl()
533
+ visit(ctx.entityDecl())
534
+ elsif ctx.functionDecl()
535
+ visit(ctx.functionDecl())
536
+ elsif ctx.procedureDecl()
537
+ visit(ctx.procedureDecl())
538
+ elsif ctx.subtypeConstraintDecl()
539
+ visit(ctx.subtypeConstraintDecl())
540
+ elsif ctx.typeDecl()
541
+ visit(ctx.typeDecl())
542
+ else
543
+ raise 'Invalid state'
544
+ end
545
+ end
546
+
547
+ def visitDerivedAttr(ctx)
548
+ id = visit(ctx.attributeDecl())
549
+ type = visit(ctx.parameterType())
550
+ expression = visit(ctx.expression())
551
+
552
+ Model::Derived.new({
553
+ id: id,
554
+ type: type,
555
+ expression: expression
556
+ })
557
+ end
558
+
559
+ def visitDeriveClause(ctx)
560
+ raise 'Invalid state'
561
+ end
562
+
563
+ def visitDomainRule(ctx)
564
+ id = if ctx.ruleLabelId()
565
+ visit(ctx.ruleLabelId())
566
+ end
567
+ expression = visit(ctx.expression())
568
+
569
+ Model::Where.new({
570
+ id: id,
571
+ expression: expression
572
+ })
573
+ end
574
+
575
+ def visitElement(ctx)
576
+ if ctx.repetition()
577
+ expression = visit(ctx.expression())
578
+ repetition = visit(ctx.repetition().numericExpression().simpleExpression())
579
+
580
+ Model::Expressions::AggregateElement.new({
581
+ expression: expression,
582
+ repetition: repetition
583
+ })
584
+ else
585
+ visit(ctx.expression())
586
+ end
587
+ end
588
+
589
+ def visitEntityBody(ctx)
590
+ raise 'Invalid state'
591
+ end
592
+
593
+ def visitEntityConstructor(ctx)
594
+ entity = visit(ctx.entityRef())
595
+ parameters = ctx.expression().map{|ctx| visit(ctx)}
596
+
597
+ Model::Expressions::EntityConstructor.new({
598
+ entity: entity,
599
+ parameters: parameters
600
+ })
601
+ end
602
+
603
+ def visitEntityDecl(ctx)
604
+ id = visit(ctx.entityHead().entityId())
605
+ abstract = if ctx.entityHead().subsuper().supertypeConstraint()
606
+ !!ctx.entityHead().subsuper().supertypeConstraint().abstractEntityDeclaration()
607
+ end
608
+ abstract_supertype = if ctx.entityHead().subsuper().supertypeConstraint()
609
+ !!ctx.entityHead().subsuper().supertypeConstraint().abstractSupertypeDeclaration()
610
+ end
611
+ supertypes = if ctx.entityHead().subsuper().subtypeDeclaration()
612
+ ctx.entityHead().subsuper().subtypeDeclaration().entityRef().map{|ctx| visit(ctx)}
613
+ end
614
+ subtype_expression = if ctx.entityHead().subsuper().supertypeConstraint() && ctx.entityHead().subsuper().supertypeConstraint().abstractSupertypeDeclaration() && ctx.entityHead().subsuper().supertypeConstraint().abstractSupertypeDeclaration().subtypeConstraint()
615
+ visit(ctx.entityHead().subsuper().supertypeConstraint().abstractSupertypeDeclaration().subtypeConstraint().supertypeExpression())
616
+ elsif ctx.entityHead().subsuper().supertypeConstraint() && ctx.entityHead().subsuper().supertypeConstraint().supertypeRule()
617
+ visit(ctx.entityHead().subsuper().supertypeConstraint().supertypeRule().subtypeConstraint().supertypeExpression())
618
+ end
619
+ explicit = ctx.entityBody().explicitAttr().map{|ctx| visit(ctx)}.flatten
620
+ derived = if ctx.entityBody().deriveClause()
621
+ ctx.entityBody().deriveClause().derivedAttr().map{|ctx| visit(ctx)}
622
+ end
623
+ inverse = if ctx.entityBody().inverseClause()
624
+ ctx.entityBody().inverseClause().inverseAttr().map{|ctx| visit(ctx)}
625
+ end
626
+ unique = if ctx.entityBody().uniqueClause()
627
+ ctx.entityBody().uniqueClause().uniqueRule().map{|ctx| visit(ctx)}
628
+ end
629
+ where = if ctx.entityBody().whereClause()
630
+ ctx.entityBody().whereClause().domainRule().map{|ctx| visit(ctx)}
631
+ end
632
+
633
+ Model::Entity.new({
634
+ id: id,
635
+ abstract: abstract,
636
+ abstract_supertype: abstract_supertype,
637
+ supertypes: supertypes,
638
+ subtype_expression: subtype_expression,
639
+ explicit: explicit,
640
+ derived: derived,
641
+ inverse: inverse,
642
+ unique: unique,
643
+ where: where
644
+ })
645
+ end
646
+
647
+ def visitEntityHead(ctx)
648
+ raise 'Invalid state'
649
+ end
650
+
651
+ def visitEntityId(ctx)
652
+ handleSimpleId(ctx.SimpleId())
653
+ end
654
+
655
+ def visitEnumerationExtension(ctx)
656
+ raise 'Invalid state'
657
+ end
658
+
659
+ def visitEnumerationId(ctx)
660
+ handleSimpleId(ctx.SimpleId())
661
+ end
662
+
663
+ def visitEnumerationItems(ctx)
664
+ raise 'Invalid state'
665
+ end
666
+
667
+ def visitEnumerationReference(ctx)
668
+ if ctx.typeRef()
669
+ ref = visit(ctx.typeRef())
670
+ attribute = visit(ctx.enumerationRef())
671
+ attribute_qualifier = Model::Expressions::AttributeQualifier.new({
672
+ attribute: attribute
673
+ })
674
+ qualifiers = [attribute_qualifier]
675
+
676
+ Model::Expressions::QualifiedRef.new({
677
+ ref: ref,
678
+ qualifiers: qualifiers
679
+ })
680
+ else
681
+ visit(ctx.enumerationRef())
682
+ end
683
+ end
684
+
685
+ def visitEnumerationType(ctx)
686
+ extensible = !!ctx.EXTENSIBLE()
687
+ list = if ctx.enumerationItems()
688
+ ctx.enumerationItems().enumerationId().map{|ctx| visit(ctx)}
689
+ end
690
+ extension_type = if ctx.enumerationExtension()
691
+ visit(ctx.enumerationExtension().typeRef())
692
+ end
693
+ extension_list = if ctx.enumerationExtension() && ctx.enumerationExtension().enumerationItems()
694
+ ctx.enumerationExtension().enumerationItems().enumerationId().map{|ctx| visit(ctx)}
695
+ end
696
+
697
+ Model::Types::Enumeration.new({
698
+ extensible: extensible,
699
+ list: list,
700
+ extension_type: extension_type,
701
+ extension_list: extension_list
702
+ })
703
+ end
704
+
705
+ def visitEscapeStmt(ctx)
706
+ Model::Statements::Escape.new()
707
+ end
708
+
709
+ def visitExplicitAttr(ctx)
710
+ ids = ctx.attributeDecl().map{|ctx| visit(ctx)}
711
+ optional = !!ctx.OPTIONAL()
712
+ type = visit(ctx.parameterType())
713
+
714
+ ids.map do |id|
715
+ Model::Explicit.new({
716
+ id: id,
717
+ optional: optional,
718
+ type: type
719
+ })
720
+ end
721
+ end
722
+
723
+ def visitExpression(ctx)
724
+ if ctx.relOpExtended()
725
+ operator = visit(ctx.relOpExtended())
726
+ operands = ctx.simpleExpression().map{|ctx| visit(ctx)}
727
+
728
+ Model::Expressions::Expression.new({
729
+ operator: operator,
730
+ operands: operands
731
+ })
732
+ else
733
+ visit(ctx.simpleExpression()[0])
734
+ end
735
+ end
736
+
737
+ def visitFactor(ctx)
738
+ if ctx.child_at(1) && ctx.child_at(1).text == '**'
739
+ operator = Model::Operators::Exponentiation.new()
740
+ operands = ctx.simpleFactor().map{|ctx| visit(ctx)}
741
+
742
+ Model::Expressions::Expression.new({
743
+ operator: operator,
744
+ operands: operands
745
+ })
746
+ else
747
+ visit(ctx.simpleFactor()[0])
748
+ end
749
+ end
750
+
751
+ def visitFormalParameter(ctx)
752
+ ids = ctx.parameterId().map{|ctx| visit(ctx)}
753
+ type = visit(ctx.parameterType())
754
+
755
+ ids.map do |id|
756
+ Model::Parameter.new({
757
+ id: id,
758
+ type: type
759
+ })
760
+ end
761
+ end
762
+
763
+ def visitFunctionCall(ctx)
764
+ function = if ctx.builtInFunction()
765
+ visit(ctx.builtInFunction())
766
+ elsif ctx.functionRef()
767
+ visit(ctx.functionRef())
768
+ else
769
+ raise 'Invalid state'
770
+ end
771
+ parameters = if ctx.actualParameterList()
772
+ ctx.actualParameterList().parameter().map{|ctx| visit(ctx.expression)}
773
+ end
774
+
775
+ Model::Expressions::FunctionCall.new({
776
+ function: function,
777
+ parameters: parameters
778
+ })
779
+ end
780
+
781
+ def visitFunctionDecl(ctx)
782
+ id = visit(ctx.functionHead().functionId())
783
+ parameters = ctx.functionHead().formalParameter().map{|ctx| visit(ctx)}.flatten
784
+ return_type = visit(ctx.functionHead().parameterType())
785
+ declarations = ctx.algorithmHead().declaration().map{|ctx| visit(ctx)}
786
+ constants = if ctx.algorithmHead().constantDecl()
787
+ ctx.algorithmHead().constantDecl().constantBody().map{|ctx| visit(ctx)}
788
+ end
789
+ locals = if ctx.algorithmHead().localDecl()
790
+ ctx.algorithmHead().localDecl().localVariable().map{|ctx| visit(ctx)}.flatten
791
+ end
792
+ declarations = ctx.algorithmHead().declaration().map{|ctx| visit(ctx)}
793
+ statements = ctx.stmt().map{|ctx| visit(ctx)}
794
+
795
+ Model::Function.new({
796
+ id: id,
797
+ parameters: parameters,
798
+ return_type: return_type,
799
+ declarations: declarations,
800
+ constants: constants,
801
+ locals: locals,
802
+ statements: statements
803
+ })
804
+ end
805
+
806
+ def visitFunctionHead(ctx)
807
+ raise 'Invalid state'
808
+ end
809
+
810
+ def visitFunctionId(ctx)
811
+ handleSimpleId(ctx.SimpleId())
812
+ end
813
+
814
+ def visitGeneralizedTypes(ctx)
815
+ if ctx.aggregateType()
816
+ visit(ctx.aggregateType())
817
+ elsif ctx.generalAggregationTypes()
818
+ visit(ctx.generalAggregationTypes())
819
+ elsif ctx.genericEntityType()
820
+ visit(ctx.genericEntityType())
821
+ elsif ctx.genericType()
822
+ visit(ctx.genericType())
823
+ else
824
+ raise 'Invalid state'
825
+ end
826
+ end
827
+
828
+ def visitGeneralAggregationTypes(ctx)
829
+ if ctx.generalArrayType()
830
+ visit(ctx.generalArrayType())
831
+ elsif ctx.generalBagType()
832
+ visit(ctx.generalBagType())
833
+ elsif ctx.generalListType()
834
+ visit(ctx.generalListType())
835
+ elsif ctx.generalSetType()
836
+ visit(ctx.generalSetType())
837
+ else
838
+ raise 'Invalid state'
839
+ end
840
+ end
841
+
842
+ def visitGeneralArrayType(ctx)
843
+ bound1, bound2 = if ctx.boundSpec()
844
+ [
845
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
846
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
847
+ ]
848
+ end
849
+ optional = !!ctx.OPTIONAL()
850
+ unique = !!ctx.UNIQUE()
851
+ base_type = visit(ctx.parameterType())
852
+
853
+ Model::Types::Array.new({
854
+ bound1: bound1,
855
+ bound2: bound2,
856
+ optional: optional,
857
+ unique: unique,
858
+ base_type: base_type
859
+ })
860
+ end
861
+
862
+ def visitGeneralBagType(ctx)
863
+ bound1, bound2 = if ctx.boundSpec()
864
+ [
865
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
866
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
867
+ ]
868
+ end
869
+ base_type = visit(ctx.parameterType())
870
+
871
+ Model::Types::Bag.new({
872
+ bound1: bound1,
873
+ bound2: bound2,
874
+ base_type: base_type
875
+ })
876
+ end
877
+
878
+ def visitGeneralListType(ctx)
879
+ bound1, bound2 = if ctx.boundSpec()
880
+ [
881
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
882
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
883
+ ]
884
+ end
885
+ unique = !!ctx.UNIQUE()
886
+ base_type = visit(ctx.parameterType())
887
+
888
+ Model::Types::List.new({
889
+ bound1: bound1,
890
+ bound2: bound2,
891
+ unique: unique,
892
+ base_type: base_type
893
+ })
894
+ end
895
+
896
+ def visitGeneralRef(ctx)
897
+ if ctx.parameterRef()
898
+ visit(ctx.parameterRef())
899
+ elsif ctx.variableId()
900
+ visit(ctx.variableId())
901
+ else
902
+ raise 'Invalid state'
903
+ end
904
+ end
905
+
906
+ def visitGeneralSetType(ctx)
907
+ bound1, bound2 = if ctx.boundSpec()
908
+ [
909
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
910
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
911
+ ]
912
+ end
913
+ base_type = visit(ctx.parameterType())
914
+
915
+ Model::Types::Set.new({
916
+ bound1: bound1,
917
+ bound2: bound2,
918
+ base_type: base_type
919
+ })
920
+ end
921
+
922
+ def visitGenericEntityType(ctx)
923
+ label = if ctx.typeLabel()
924
+ visit(ctx.typeLabel())
925
+ end
926
+
927
+ Model::Types::GenericEntity.new({
928
+ label: label
929
+ })
930
+ end
931
+
932
+ def visitGenericType(ctx)
933
+ label = if ctx.typeLabel()
934
+ visit(ctx.typeLabel())
935
+ end
936
+
937
+ Model::Types::Generic.new({
938
+ label: label
939
+ })
940
+ end
941
+
942
+ def visitGroupQualifier(ctx)
943
+ entity = visit(ctx.entityRef())
944
+
945
+ Model::Expressions::GroupQualifier.new({
946
+ entity: entity
947
+ })
948
+ end
949
+
950
+ def visitIfStmt(ctx)
951
+ condition = visit(ctx.logicalExpression().expression())
952
+ else_index = if ctx.ELSE()
953
+ ctx.children.find_index{|x| x == ctx.ELSE()}
954
+ end
955
+ else_statement_index = if else_index
956
+ else_index - ctx.children.find_index{|x| x == ctx.stmt()[0]}
957
+ end
958
+ statements = if else_statement_index
959
+ ctx.stmt()[0..(else_statement_index - 1)].map{|ctx| visit(ctx)}
960
+ else
961
+ ctx.stmt().map{|ctx| visit(ctx)}
962
+ end
963
+ else_statements = if else_statement_index
964
+ ctx.stmt()[else_statement_index..(ctx.stmt().length - 1)].map{|ctx| visit(ctx)}
965
+ end
966
+
967
+ Model::Statements::If.new({
968
+ condition: condition,
969
+ statements: statements,
970
+ else_statements: else_statements
971
+ })
972
+ end
973
+
974
+ def visitIncrement(ctx)
975
+ raise 'Invalid state'
976
+ end
977
+
978
+ def visitIncrementControl(ctx)
979
+ raise 'Invalid state'
980
+ end
981
+
982
+ def visitIndex(ctx)
983
+ raise 'Invalid state'
984
+ end
985
+
986
+ def visitIndex1(ctx)
987
+ raise 'Invalid state'
988
+ end
989
+
990
+ def visitIndex2(ctx)
991
+ raise 'Invalid state'
992
+ end
993
+
994
+ def visitIndexQualifier(ctx)
995
+ index1 = visit(ctx.index1().index().numericExpression().simpleExpression())
996
+ index2 = if ctx.index2()
997
+ visit(ctx.index2().index().numericExpression().simpleExpression())
998
+ end
999
+
1000
+ Model::Expressions::IndexQualifier.new({
1001
+ index1: index1,
1002
+ index2: index2
1003
+ })
1004
+ end
1005
+
1006
+ def visitInstantiableType(ctx)
1007
+ if ctx.concreteTypes()
1008
+ visit(ctx.concreteTypes())
1009
+ elsif ctx.entityRef()
1010
+ visit(ctx.entityRef())
1011
+ else
1012
+ raise 'Invalid state'
1013
+ end
1014
+ end
1015
+
1016
+ def visitIntegerType(ctx)
1017
+ Model::Types::Integer.new()
1018
+ end
1019
+
1020
+ def visitInterfaceSpecification(ctx)
1021
+ if ctx.referenceClause()
1022
+ visit(ctx.referenceClause())
1023
+ elsif ctx.useClause()
1024
+ visit(ctx.useClause())
1025
+ else
1026
+ raise 'Invalid state'
1027
+ end
1028
+ end
1029
+
1030
+ def visitInterval(ctx)
1031
+ low = visit(ctx.intervalLow().simpleExpression())
1032
+ operator1 = visit(ctx.intervalOp()[0])
1033
+ item = visit(ctx.intervalItem().simpleExpression())
1034
+ operator2 = visit(ctx.intervalOp()[1])
1035
+ high = visit(ctx.intervalHigh().simpleExpression())
1036
+
1037
+ Model::Expressions::Interval.new({
1038
+ low: low,
1039
+ operator1: operator1,
1040
+ item: item,
1041
+ operator2: operator2,
1042
+ high: high
1043
+ })
1044
+ end
1045
+
1046
+ def visitIntervalHigh(ctx)
1047
+ raise 'Invalid state'
1048
+ end
1049
+
1050
+ def visitIntervalItem(ctx)
1051
+ raise 'Invalid state'
1052
+ end
1053
+
1054
+ def visitIntervalLow(ctx)
1055
+ raise 'Invalid state'
1056
+ end
1057
+
1058
+ def visitIntervalOp(ctx)
1059
+ if ctx.text == '<'
1060
+ Model::Operators::LessThan.new()
1061
+ elsif ctx.text == '<='
1062
+ Model::Operators::LessThanOrEqual.new()
1063
+ else
1064
+ raise 'Invalid state'
1065
+ end
1066
+ end
1067
+
1068
+ def visitInverseAttr(ctx)
1069
+ id = visit(ctx.attributeDecl())
1070
+ type = if ctx.SET()
1071
+ bound1, bound2 = if ctx.boundSpec()
1072
+ [
1073
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
1074
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
1075
+ ]
1076
+ end
1077
+ base_type = visit(ctx.entityRef()[0])
1078
+
1079
+ Model::Types::Set.new({
1080
+ bound1: bound1,
1081
+ bound2: bound2,
1082
+ base_type: base_type
1083
+ })
1084
+ elsif ctx.BAG()
1085
+ bound1, bound2 = if ctx.boundSpec()
1086
+ [
1087
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
1088
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
1089
+ ]
1090
+ end
1091
+ base_type = visit(ctx.entityRef()[0])
1092
+
1093
+ Model::Types::Bag.new({
1094
+ bound1: bound1,
1095
+ bound2: bound2,
1096
+ base_type: base_type
1097
+ })
1098
+ else
1099
+ visit(ctx.entityRef()[0])
1100
+ end
1101
+ attribute = if ctx.entityRef()[1]
1102
+ ref = visit(ctx.entityRef()[1])
1103
+ attribute = visit(ctx.attributeRef())
1104
+ attribute_qualifier = Model::Expressions::AttributeQualifier.new({
1105
+ attribute: attribute
1106
+ })
1107
+ qualifiers = [attribute_qualifier]
1108
+
1109
+ Model::Expressions::QualifiedRef.new({
1110
+ ref: ref,
1111
+ qualifiers: qualifiers
1112
+ })
1113
+ else
1114
+ visit(ctx.attributeRef())
1115
+ end
1116
+
1117
+ Model::Inverse.new({
1118
+ id: id,
1119
+ type: type,
1120
+ attribute: attribute
1121
+ })
1122
+ end
1123
+
1124
+ def visitInverseClause(ctx)
1125
+ raise 'Invalid state'
1126
+ end
1127
+
1128
+ def visitListType(ctx)
1129
+ bound1, bound2 = if ctx.boundSpec()
1130
+ [
1131
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
1132
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
1133
+ ]
1134
+ end
1135
+ unique = !!ctx.UNIQUE()
1136
+ base_type = visit(ctx.instantiableType())
1137
+
1138
+ Model::Types::List.new({
1139
+ bound1: bound1,
1140
+ bound2: bound2,
1141
+ unique: unique,
1142
+ base_type: base_type
1143
+ })
1144
+ end
1145
+
1146
+ def visitLiteral(ctx)
1147
+ if ctx.BinaryLiteral()
1148
+ handleBinaryLiteral(ctx.BinaryLiteral())
1149
+ elsif ctx.IntegerLiteral()
1150
+ handleIntegerLiteral(ctx.IntegerLiteral())
1151
+ elsif ctx.logicalLiteral()
1152
+ visit(ctx.logicalLiteral())
1153
+ elsif ctx.RealLiteral()
1154
+ handleRealLiteral(ctx.RealLiteral())
1155
+ elsif ctx.stringLiteral()
1156
+ visit(ctx.stringLiteral())
1157
+ else
1158
+ raise 'Invalid state'
1159
+ end
1160
+ end
1161
+
1162
+ def visitLocalDecl(ctx)
1163
+ raise 'Invalid state'
1164
+ end
1165
+
1166
+ def visitLocalVariable(ctx)
1167
+ ids = ctx.variableId().map{|ctx| visit(ctx)}
1168
+ type = visit(ctx.parameterType())
1169
+ expression = if ctx.expression()
1170
+ visit(ctx.expression())
1171
+ end
1172
+
1173
+ ids.map do |id|
1174
+ Model::Local.new({
1175
+ id: id,
1176
+ type: type,
1177
+ expression: expression
1178
+ })
1179
+ end
1180
+ end
1181
+
1182
+ def visitLogicalExpression(ctx)
1183
+ raise 'Invalid state'
1184
+ end
1185
+
1186
+ def visitLogicalLiteral(ctx)
1187
+ if ctx.TRUE()
1188
+ true
1189
+ elsif ctx.FALSE()
1190
+ false
1191
+ elsif ctx.UNKNOWN()
1192
+ Model::Expressions::Unknown.new()
1193
+ else
1194
+ raise 'Invalid state'
1195
+ end
1196
+ end
1197
+
1198
+ def visitLogicalType(ctx)
1199
+ Model::Types::Logical.new()
1200
+ end
1201
+
1202
+ def visitMultiplicationLikeOp(ctx)
1203
+ if ctx.text == '*'
1204
+ Model::Operators::Multiplication.new()
1205
+ elsif ctx.text == '/'
1206
+ Model::Operators::RealDivision.new()
1207
+ elsif ctx.DIV()
1208
+ Model::Operators::IntegerDivision.new()
1209
+ elsif ctx.MOD()
1210
+ Model::Operators::Modulo.new()
1211
+ elsif ctx.AND()
1212
+ Model::Operators::And.new()
1213
+ elsif ctx.text == '||'
1214
+ Model::Operators::Combine.new()
1215
+ else
1216
+ raise 'Invalid state'
1217
+ end
1218
+ end
1219
+
1220
+ def visitNamedTypes(ctx)
1221
+ if ctx.entityRef()
1222
+ visit(ctx.entityRef())
1223
+ elsif ctx.typeRef()
1224
+ visit(ctx.typeRef())
1225
+ else
1226
+ raise 'Invalid state'
1227
+ end
1228
+ end
1229
+
1230
+ def visitNamedTypeOrRename(ctx)
1231
+ if ctx.entityId() || ctx.typeId()
1232
+ ref = visit(ctx.namedTypes())
1233
+ id = if ctx.entityId()
1234
+ visit(ctx.entityId())
1235
+ elsif ctx.typeId()
1236
+ visit(ctx.typeId())
1237
+ end
1238
+
1239
+ Model::RenamedRef.new({
1240
+ ref: ref,
1241
+ id: id
1242
+ })
1243
+ else
1244
+ visit(ctx.namedTypes())
1245
+ end
1246
+ end
1247
+
1248
+ def visitNullStmt(ctx)
1249
+ Model::Statements::Null.new()
1250
+ end
1251
+
1252
+ def visitNumberType(ctx)
1253
+ Model::Types::Number.new()
1254
+ end
1255
+
1256
+ def visitNumericExpression(ctx)
1257
+ raise 'Invalid state'
1258
+ end
1259
+
1260
+ def visitOneOf(ctx)
1261
+ operator = Model::Operators::Oneof.new()
1262
+ operands = ctx.supertypeExpression().map{|ctx| visit(ctx)}
1263
+
1264
+ Model::Expressions::Expression.new({
1265
+ operator: operator,
1266
+ operands: operands
1267
+ })
1268
+ end
1269
+
1270
+ def visitParameter(ctx)
1271
+ raise 'Invalid state'
1272
+ end
1273
+
1274
+ def visitParameterId(ctx)
1275
+ handleSimpleId(ctx.SimpleId())
1276
+ end
1277
+
1278
+ def visitParameterType(ctx)
1279
+ if ctx.generalizedTypes()
1280
+ visit(ctx.generalizedTypes())
1281
+ elsif ctx.namedTypes()
1282
+ visit(ctx.namedTypes())
1283
+ elsif ctx.simpleTypes()
1284
+ visit(ctx.simpleTypes())
1285
+ else
1286
+ raise 'Invalid state'
1287
+ end
1288
+ end
1289
+
1290
+ def visitPopulation(ctx)
1291
+ raise 'Invalid state'
1292
+ end
1293
+
1294
+ def visitPrecisionSpec(ctx)
1295
+ raise 'Invalid state'
1296
+ end
1297
+
1298
+ def visitPrimary(ctx)
1299
+ if ctx.literal()
1300
+ visit(ctx.literal())
1301
+ elsif ctx.qualifiableFactor()
1302
+ visit(ctx.qualifiableFactor())
1303
+ else
1304
+ raise 'Invalid state'
1305
+ end
1306
+ end
1307
+
1308
+ def visitProcedureCallStmt(ctx)
1309
+ procedure = if ctx.builtInProcedure()
1310
+ visit(ctx.builtInProcedure())
1311
+ elsif ctx.procedureRef()
1312
+ visit(ctx.procedureRef())
1313
+ else
1314
+ raise 'Invalid state'
1315
+ end
1316
+ parameters = if ctx.actualParameterList()
1317
+ ctx.actualParameterList().parameter().map{|ctx| visit(ctx.expression)}
1318
+ end
1319
+
1320
+ Model::Statements::ProcedureCall.new({
1321
+ procedure: procedure,
1322
+ parameters: parameters
1323
+ })
1324
+ end
1325
+
1326
+ def visitProcedureDecl(ctx)
1327
+ id = visit(ctx.procedureHead().procedureId())
1328
+ parameters = ctx.procedureHead().formalParameter().map do |ctx|
1329
+ parameters = visit(ctx)
1330
+ parameters_index = ctx.parent.children.find_index{|x| x == ctx}
1331
+ var = ctx.parent.child_at(parameters_index - 1)
1332
+ if var.text == 'VAR'
1333
+ parameters.map do |parameter|
1334
+ Model::Parameter.new({
1335
+ id: parameter.id,
1336
+ type: parameter.type,
1337
+ var: true
1338
+ })
1339
+ end
1340
+ else
1341
+ parameters
1342
+ end
1343
+ end.flatten
1344
+ declarations = ctx.algorithmHead().declaration().map{|ctx| visit(ctx)}
1345
+ constants = if ctx.algorithmHead().constantDecl()
1346
+ ctx.algorithmHead().constantDecl().constantBody().map{|ctx| visit(ctx)}
1347
+ end
1348
+ locals = if ctx.algorithmHead().localDecl()
1349
+ ctx.algorithmHead().localDecl().localVariable().map{|ctx| visit(ctx)}.flatten
1350
+ end
1351
+ statements = ctx.stmt().map{|ctx| visit(ctx)}
1352
+
1353
+ Model::Procedure.new({
1354
+ id: id,
1355
+ parameters: parameters,
1356
+ declarations: declarations,
1357
+ constants: constants,
1358
+ locals: locals,
1359
+ statements: statements
1360
+ })
1361
+ end
1362
+
1363
+ def visitProcedureHead(ctx)
1364
+ raise 'Invalid state'
1365
+ end
1366
+
1367
+ def visitProcedureId(ctx)
1368
+ handleSimpleId(ctx.SimpleId())
1369
+ end
1370
+
1371
+ def visitQualifiableFactor(ctx)
1372
+ if ctx.attributeRef()
1373
+ visit(ctx.attributeRef())
1374
+ elsif ctx.constantFactor()
1375
+ visit(ctx.constantFactor())
1376
+ elsif ctx.functionCall()
1377
+ visit(ctx.functionCall())
1378
+ elsif ctx.generalRef()
1379
+ visit(ctx.generalRef())
1380
+ elsif ctx.population()
1381
+ visit(ctx.population().entityRef())
1382
+ else
1383
+ raise 'Invalid state'
1384
+ end
1385
+ end
1386
+
1387
+ def visitQualifiedAttribute(ctx)
1388
+ id = ctx.SELF().text
1389
+
1390
+ ref = Model::Ref.new({
1391
+ id: id
1392
+ })
1393
+
1394
+ group_qualifier = visit(ctx.groupQualifier())
1395
+ attribute_qualifier = visit(ctx.attributeQualifier())
1396
+ qualifiers = [group_qualifier, attribute_qualifier]
1397
+
1398
+ Model::Expressions::QualifiedRef.new({
1399
+ ref: ref,
1400
+ qualifiers: qualifiers
1401
+ })
1402
+ end
1403
+
1404
+ def visitQualifier(ctx)
1405
+ if ctx.attributeQualifier()
1406
+ visit(ctx.attributeQualifier())
1407
+ elsif ctx.groupQualifier()
1408
+ visit(ctx.groupQualifier())
1409
+ elsif ctx.indexQualifier()
1410
+ visit(ctx.indexQualifier())
1411
+ else
1412
+ raise 'Invalid state'
1413
+ end
1414
+ end
1415
+
1416
+ def visitQueryExpression(ctx)
1417
+ id = visit(ctx.variableId())
1418
+ source = visit(ctx.aggregateSource().simpleExpression())
1419
+ expression = visit(ctx.logicalExpression().expression())
1420
+
1421
+ Model::Expressions::Query.new({
1422
+ id: id,
1423
+ source: source,
1424
+ expression: expression
1425
+ })
1426
+ end
1427
+
1428
+ def visitRealType(ctx)
1429
+ precision = if ctx.precisionSpec()
1430
+ visit(ctx.precisionSpec().numericExpression().simpleExpression())
1431
+ end
1432
+
1433
+ Model::Types::Real.new({
1434
+ precision: precision
1435
+ })
1436
+ end
1437
+
1438
+ def visitRedeclaredAttribute(ctx)
1439
+ if ctx.attributeId()
1440
+ ref = visit(ctx.qualifiedAttribute())
1441
+ id = visit(ctx.attributeId())
1442
+
1443
+ Model::RenamedRef.new({
1444
+ ref: ref,
1445
+ id: id
1446
+ })
1447
+ else
1448
+ visit(ctx.qualifiedAttribute())
1449
+ end
1450
+ end
1451
+
1452
+ def visitReferencedAttribute(ctx)
1453
+ if ctx.attributeRef()
1454
+ visit(ctx.attributeRef())
1455
+ elsif ctx.qualifiedAttribute()
1456
+ visit(ctx.qualifiedAttribute())
1457
+ else
1458
+ raise 'Invalid state'
1459
+ end
1460
+ end
1461
+
1462
+ def visitReferenceClause(ctx)
1463
+ schema = visit(ctx.schemaRef())
1464
+ items = ctx.resourceOrRename().map{|ctx| visit(ctx)}
1465
+
1466
+ Model::Reference.new({
1467
+ schema: schema,
1468
+ items: items
1469
+ })
1470
+ end
1471
+
1472
+ def visitRelOp(ctx)
1473
+ if ctx.text == '<'
1474
+ Model::Operators::LessThan.new()
1475
+ elsif ctx.text == '>'
1476
+ Model::Operators::GreaterThan.new()
1477
+ elsif ctx.text == '<='
1478
+ Model::Operators::LessThanOrEqual.new()
1479
+ elsif ctx.text == '>='
1480
+ Model::Operators::GreaterThanOrEqual.new()
1481
+ elsif ctx.text == '<>'
1482
+ Model::Operators::NotEqual.new()
1483
+ elsif ctx.text == '='
1484
+ Model::Operators::Equal.new()
1485
+ elsif ctx.text == ':<>:'
1486
+ Model::Operators::InstanceNotEqual.new()
1487
+ elsif ctx.text == ':=:'
1488
+ Model::Operators::InstanceEqual.new()
1489
+ else
1490
+ raise 'Invalid state'
1491
+ end
1492
+ end
1493
+
1494
+ def visitRelOpExtended(ctx)
1495
+ if ctx.relOp()
1496
+ visit(ctx.relOp())
1497
+ elsif ctx.IN()
1498
+ Model::Operators::In.new()
1499
+ elsif ctx.LIKE()
1500
+ Model::Operators::Like.new()
1501
+ else
1502
+ raise 'Invalid state'
1503
+ end
1504
+ end
1505
+
1506
+ def visitRenameId(ctx)
1507
+ if ctx.constantId()
1508
+ visit(ctx.constantId())
1509
+ elsif ctx.entityId()
1510
+ visit(ctx.entityId())
1511
+ elsif ctx.functionId()
1512
+ visit(ctx.functionId())
1513
+ elsif ctx.procedureId()
1514
+ visit(ctx.procedureId())
1515
+ elsif ctx.typeId()
1516
+ visit(ctx.typeId())
1517
+ else
1518
+ raise 'Invalid state'
1519
+ end
1520
+ end
1521
+
1522
+ def visitRepeatControl(ctx)
1523
+ raise 'Invalid state'
1524
+ end
1525
+
1526
+ def visitRepeatStmt(ctx)
1527
+ variable = if ctx.repeatControl().incrementControl()
1528
+ visit(ctx.repeatControl().incrementControl().variableId())
1529
+ end
1530
+ bound1, bound2 = if ctx.repeatControl().incrementControl()
1531
+ [
1532
+ visit(ctx.repeatControl().incrementControl().bound1().numericExpression().simpleExpression()),
1533
+ visit(ctx.repeatControl().incrementControl().bound2().numericExpression().simpleExpression())
1534
+ ]
1535
+ end
1536
+ increment = if ctx.repeatControl().incrementControl() && ctx.repeatControl().incrementControl().increment()
1537
+ visit(ctx.repeatControl().incrementControl().increment().numericExpression().simpleExpression())
1538
+ end
1539
+ while_expression = if ctx.repeatControl().whileControl()
1540
+ visit(ctx.repeatControl().whileControl().logicalExpression().expression())
1541
+ end
1542
+ until_expression = if ctx.repeatControl().untilControl()
1543
+ visit(ctx.repeatControl().untilControl().logicalExpression().expression())
1544
+ end
1545
+ statements = ctx.stmt().map{|ctx| visit(ctx)}
1546
+
1547
+ Model::Statements::Repeat.new({
1548
+ variable: variable,
1549
+ bound1: bound1,
1550
+ bound2: bound2,
1551
+ increment: increment,
1552
+ while_expression: while_expression,
1553
+ until_expression: until_expression,
1554
+ statements: statements
1555
+ })
1556
+ end
1557
+
1558
+ def visitRepetition(ctx)
1559
+ raise 'Invalid state'
1560
+ end
1561
+
1562
+ def visitResourceOrRename(ctx)
1563
+ if ctx.renameId()
1564
+ ref = visit(ctx.resourceRef())
1565
+ id = visit(ctx.renameId())
1566
+
1567
+ Model::RenamedRef.new({
1568
+ ref: ref,
1569
+ id: id
1570
+ })
1571
+ else
1572
+ visit(ctx.resourceRef())
1573
+ end
1574
+ end
1575
+
1576
+ def visitResourceRef(ctx)
1577
+ if ctx.constantRef()
1578
+ visit(ctx.constantRef())
1579
+ elsif ctx.entityRef()
1580
+ visit(ctx.entityRef())
1581
+ elsif ctx.functionRef()
1582
+ visit(ctx.functionRef())
1583
+ elsif ctx.procedureRef()
1584
+ visit(ctx.procedureRef())
1585
+ elsif ctx.typeRef()
1586
+ visit(ctx.typeRef())
1587
+ else
1588
+ raise 'Invalid state'
1589
+ end
1590
+ end
1591
+
1592
+ def visitReturnStmt(ctx)
1593
+ expression = visit(ctx.expression())
1594
+
1595
+ Model::Statements::Return.new({
1596
+ expression: expression
1597
+ })
1598
+ end
1599
+
1600
+ def visitRuleDecl(ctx)
1601
+ id = visit(ctx.ruleHead().ruleId())
1602
+ applies_to = ctx.ruleHead().entityRef().map{|ctx| visit(ctx)}
1603
+ declarations = ctx.algorithmHead().declaration().map{|ctx| visit(ctx)}
1604
+ constants = if ctx.algorithmHead().constantDecl()
1605
+ ctx.algorithmHead().constantDecl().constantBody().map{|ctx| visit(ctx)}
1606
+ end
1607
+ locals = if ctx.algorithmHead().localDecl()
1608
+ ctx.algorithmHead().localDecl().localVariable().map{|ctx| visit(ctx)}.flatten
1609
+ end
1610
+ where = ctx.whereClause().domainRule().map{|ctx| visit(ctx)}
1611
+ statements = ctx.stmt().map{|ctx| visit(ctx)}
1612
+
1613
+ Model::Rule.new({
1614
+ id: id,
1615
+ applies_to: applies_to,
1616
+ declarations: declarations,
1617
+ constants: constants,
1618
+ locals: locals,
1619
+ where: where,
1620
+ statements: statements
1621
+ })
1622
+ end
1623
+
1624
+ def visitRuleHead(ctx)
1625
+ raise 'Invalid state'
1626
+ end
1627
+
1628
+ def visitRuleId(ctx)
1629
+ handleSimpleId(ctx.SimpleId())
1630
+ end
1631
+
1632
+ def visitRuleLabelId(ctx)
1633
+ handleSimpleId(ctx.SimpleId())
1634
+ end
1635
+
1636
+ def visitSchemaBody(ctx)
1637
+ raise 'Invalid state'
1638
+ end
1639
+
1640
+ def visitSchemaDecl(ctx)
1641
+ id = visit(ctx.schemaId())
1642
+ version = if ctx.schemaVersionId()
1643
+ visit(ctx.schemaVersionId().stringLiteral())
1644
+ end
1645
+ interfaces = ctx.schemaBody().interfaceSpecification().map{|ctx| visit(ctx)}
1646
+ constants = if ctx.schemaBody().constantDecl()
1647
+ ctx.schemaBody().constantDecl().constantBody().map{|ctx| visit(ctx)}
1648
+ end
1649
+ declarations = ctx.schemaBody().declaration().map{|ctx| visit(ctx)}
1650
+ rules = ctx.schemaBody().ruleDecl().map{|ctx| visit(ctx)}
1651
+
1652
+ Model::Schema.new({
1653
+ id: id,
1654
+ version: version,
1655
+ interfaces: interfaces,
1656
+ constants: constants,
1657
+ declarations: declarations,
1658
+ rules: rules
1659
+ })
1660
+ end
1661
+
1662
+ def visitSchemaId(ctx)
1663
+ handleSimpleId(ctx.SimpleId())
1664
+ end
1665
+
1666
+ def visitSchemaVersionId(ctx)
1667
+ raise 'Invalid state'
1668
+ end
1669
+
1670
+ def visitSelector(ctx)
1671
+ raise 'Invalid state'
1672
+ end
1673
+
1674
+ def visitSelectExtension(ctx)
1675
+ raise 'Invalid state'
1676
+ end
1677
+
1678
+ def visitSelectList(ctx)
1679
+ raise 'Invalid state'
1680
+ end
1681
+
1682
+ def visitSelectType(ctx)
1683
+ extensible = !!ctx.EXTENSIBLE()
1684
+ generic_entity = !!ctx.GENERIC_ENTITY()
1685
+ list = if ctx.selectList()
1686
+ ctx.selectList().namedTypes().map{|ctx| visit(ctx)}
1687
+ end
1688
+ extension_type = if ctx.selectExtension()
1689
+ visit(ctx.selectExtension().typeRef())
1690
+ end
1691
+ extension_list = if ctx.selectExtension() && ctx.selectExtension().selectList()
1692
+ ctx.selectExtension().selectList().namedTypes().map{|ctx| visit(ctx)}
1693
+ end
1694
+
1695
+ Model::Types::Select.new({
1696
+ extensible: extensible,
1697
+ generic_entity: generic_entity,
1698
+ list: list,
1699
+ extension_type: extension_type,
1700
+ extension_list: extension_list
1701
+ })
1702
+ end
1703
+
1704
+ def visitSetType(ctx)
1705
+ bound1, bound2 = if ctx.boundSpec()
1706
+ [
1707
+ visit(ctx.boundSpec().bound1().numericExpression().simpleExpression()),
1708
+ visit(ctx.boundSpec().bound2().numericExpression().simpleExpression())
1709
+ ]
1710
+ end
1711
+ base_type = visit(ctx.instantiableType())
1712
+
1713
+ Model::Types::Set.new({
1714
+ bound1: bound1,
1715
+ bound2: bound2,
1716
+ base_type: base_type
1717
+ })
1718
+ end
1719
+
1720
+ def visitSimpleExpression(ctx)
1721
+ if ctx.term().length >= 2
1722
+ operands = ctx.term().map{|ctx| visit(ctx)}
1723
+ operators = ctx.addLikeOp().map{|ctx| visit(ctx)}
1724
+
1725
+ handleBinaryExpression(operands, operators)
1726
+ else
1727
+ visit(ctx.term()[0])
1728
+ end
1729
+ end
1730
+
1731
+ def visitSimpleFactor(ctx)
1732
+ if ctx.aggregateInitializer()
1733
+ visit(ctx.aggregateInitializer())
1734
+ elsif ctx.entityConstructor()
1735
+ visit(ctx.entityConstructor())
1736
+ elsif ctx.enumerationReference()
1737
+ visit(ctx.enumerationReference())
1738
+ elsif ctx.interval()
1739
+ visit(ctx.interval())
1740
+ elsif ctx.queryExpression()
1741
+ visit(ctx.queryExpression())
1742
+ elsif !ctx.unaryOp() && ctx.expression()
1743
+ visit(ctx.expression())
1744
+ elsif !ctx.unaryOp() && ctx.primary()
1745
+ visit(ctx.primary())
1746
+ elsif ctx.unaryOp() && ctx.expression()
1747
+ operator = visit(ctx.unaryOp())
1748
+ operand = visit(ctx.expression())
1749
+
1750
+ Model::Expressions::Expression.new({
1751
+ operator: operator,
1752
+ operands: [operand]
1753
+ })
1754
+ elsif ctx.unaryOp() && ctx.primary()
1755
+ operator = visit(ctx.unaryOp())
1756
+ operand = visit(ctx.primary())
1757
+
1758
+ Model::Expressions::Expression.new({
1759
+ operator: operator,
1760
+ operands: [operand]
1761
+ })
1762
+ else
1763
+ raise 'Invalid state'
1764
+ end
1765
+ end
1766
+
1767
+ def visitSimpleTypes(ctx)
1768
+ if ctx.binaryType()
1769
+ visit(ctx.binaryType())
1770
+ elsif ctx.booleanType()
1771
+ visit(ctx.booleanType())
1772
+ elsif ctx.integerType()
1773
+ visit(ctx.integerType())
1774
+ elsif ctx.logicalType()
1775
+ visit(ctx.logicalType())
1776
+ elsif ctx.numberType()
1777
+ visit(ctx.numberType())
1778
+ elsif ctx.realType()
1779
+ visit(ctx.realType())
1780
+ elsif ctx.stringType()
1781
+ visit(ctx.stringType())
1782
+ else
1783
+ raise 'Invalid state'
1784
+ end
1785
+ end
1786
+
1787
+ def visitSkipStmt(ctx)
1788
+ Model::Statements::Skip.new()
1789
+ end
1790
+
1791
+ def visitStmt(ctx)
1792
+ if ctx.aliasStmt()
1793
+ visit(ctx.aliasStmt())
1794
+ elsif ctx.assignmentStmt()
1795
+ visit(ctx.assignmentStmt())
1796
+ elsif ctx.caseStmt()
1797
+ visit(ctx.caseStmt())
1798
+ elsif ctx.compoundStmt()
1799
+ visit(ctx.compoundStmt())
1800
+ elsif ctx.escapeStmt()
1801
+ visit(ctx.escapeStmt())
1802
+ elsif ctx.ifStmt()
1803
+ visit(ctx.ifStmt())
1804
+ elsif ctx.nullStmt()
1805
+ visit(ctx.nullStmt())
1806
+ elsif ctx.procedureCallStmt()
1807
+ visit(ctx.procedureCallStmt())
1808
+ elsif ctx.repeatStmt()
1809
+ visit(ctx.repeatStmt())
1810
+ elsif ctx.returnStmt()
1811
+ visit(ctx.returnStmt())
1812
+ elsif ctx.skipStmt()
1813
+ visit(ctx.skipStmt())
1814
+ else
1815
+ raise 'Invalid state'
1816
+ end
1817
+ end
1818
+
1819
+ def visitStringLiteral(ctx)
1820
+ if ctx.SimpleStringLiteral()
1821
+ handleSimpleStringLiteral(ctx.SimpleStringLiteral())
1822
+ elsif ctx.EncodedStringLiteral()
1823
+ handleEncodedStringLiteral(ctx.EncodedStringLiteral())
1824
+ else
1825
+ raise 'Invalid state'
1826
+ end
1827
+ end
1828
+
1829
+ def visitStringType(ctx)
1830
+ width = if ctx.widthSpec()
1831
+ visit(ctx.widthSpec().width().numericExpression().simpleExpression())
1832
+ end
1833
+ fixed = if ctx.widthSpec()
1834
+ !!ctx.widthSpec().FIXED()
1835
+ end
1836
+
1837
+ Model::Types::String.new({
1838
+ width: width,
1839
+ fixed: fixed
1840
+ })
1841
+ end
1842
+
1843
+ def visitSubsuper(ctx)
1844
+ raise 'Invalid state'
1845
+ end
1846
+
1847
+ def visitSubtypeConstraint(ctx)
1848
+ raise 'Invalid state'
1849
+ end
1850
+
1851
+ def visitSubtypeConstraintBody(ctx)
1852
+ raise 'Invalid state'
1853
+ end
1854
+
1855
+ def visitSubtypeConstraintDecl(ctx)
1856
+ id = visit(ctx.subtypeConstraintHead().subtypeConstraintId())
1857
+ applies_to = visit(ctx.subtypeConstraintHead().entityRef())
1858
+ abstract_supertype = !!ctx.subtypeConstraintBody().abstractSupertype()
1859
+ total_over = if ctx.subtypeConstraintBody().totalOver()
1860
+ ctx.subtypeConstraintBody().totalOver().entityRef().map{|ctx| visit(ctx)}
1861
+ end
1862
+ subtype_expression = if ctx.subtypeConstraintBody().supertypeExpression()
1863
+ visit(ctx.subtypeConstraintBody().supertypeExpression())
1864
+ end
1865
+
1866
+ Model::SubtypeConstraint.new({
1867
+ id: id,
1868
+ applies_to: applies_to,
1869
+ abstract_supertype: abstract_supertype,
1870
+ total_over: total_over,
1871
+ subtype_expression: subtype_expression
1872
+ })
1873
+ end
1874
+
1875
+ def visitSubtypeConstraintHead(ctx)
1876
+ raise 'Invalid state'
1877
+ end
1878
+
1879
+ def visitSubtypeConstraintId(ctx)
1880
+ handleSimpleId(ctx.SimpleId())
1881
+ end
1882
+
1883
+ def visitSubtypeDeclaration(ctx)
1884
+ raise 'Invalid state'
1885
+ end
1886
+
1887
+ def visitSupertypeConstraint(ctx)
1888
+ raise 'Invalid state'
1889
+ end
1890
+
1891
+ def visitSupertypeExpression(ctx)
1892
+ if ctx.supertypeFactor().length >= 2
1893
+ operands = ctx.supertypeFactor().map{|ctx| visit(ctx)}
1894
+ operators = ctx.ANDOR().map{|ctx| Model::Operators::Andor.new()}
1895
+
1896
+ handleBinaryExpression(operands, operators)
1897
+ else
1898
+ visit(ctx.supertypeFactor()[0])
1899
+ end
1900
+ end
1901
+
1902
+ def visitSupertypeFactor(ctx)
1903
+ if ctx.supertypeTerm().length >= 2
1904
+ operands = ctx.supertypeTerm().map{|ctx| visit(ctx)}
1905
+ operators = ctx.AND().map{|ctx| Model::Operators::And.new()}
1906
+
1907
+ handleBinaryExpression(operands, operators)
1908
+ else
1909
+ visit(ctx.supertypeTerm()[0])
1910
+ end
1911
+ end
1912
+
1913
+ def visitSupertypeRule(ctx)
1914
+ raise 'Invalid state'
1915
+ end
1916
+
1917
+ def visitSupertypeTerm(ctx)
1918
+ if ctx.entityRef()
1919
+ visit(ctx.entityRef())
1920
+ elsif ctx.oneOf()
1921
+ visit(ctx.oneOf())
1922
+ elsif ctx.supertypeExpression()
1923
+ visit(ctx.supertypeExpression())
1924
+ else
1925
+ raise 'Invalid state'
1926
+ end
1927
+ end
1928
+
1929
+ def visitSyntax(ctx)
1930
+ schemas = ctx.schemaDecl().map{|ctx| visit(ctx)}
1931
+
1932
+ Model::Repository.new({
1933
+ schemas: schemas
1934
+ })
1935
+ end
1936
+
1937
+ def visitTerm(ctx)
1938
+ if ctx.factor().length >= 2
1939
+ operands = ctx.factor().map{|ctx| visit(ctx)}
1940
+ operators = ctx.multiplicationLikeOp().map{|ctx| visit(ctx)}
1941
+
1942
+ handleBinaryExpression(operands, operators)
1943
+ else
1944
+ visit(ctx.factor()[0])
1945
+ end
1946
+ end
1947
+
1948
+ def visitTotalOver(ctx)
1949
+ raise 'Invalid state'
1950
+ end
1951
+
1952
+ def visitTypeDecl(ctx)
1953
+ id = visit(ctx.typeId())
1954
+ type = visit(ctx.underlyingType())
1955
+ where = if ctx.whereClause()
1956
+ ctx.whereClause().domainRule().map{|ctx| visit(ctx)}
1957
+ end
1958
+
1959
+ Model::Type.new({
1960
+ id: id,
1961
+ type: type,
1962
+ where: where
1963
+ })
1964
+ end
1965
+
1966
+ def visitTypeId(ctx)
1967
+ handleSimpleId(ctx.SimpleId())
1968
+ end
1969
+
1970
+ def visitTypeLabel(ctx)
1971
+ if ctx.typeLabelId()
1972
+ visit(ctx.typeLabelId())
1973
+ elsif ctx.typeLabelRef()
1974
+ visit(ctx.typeLabelRef())
1975
+ else
1976
+ raise 'Invalid state'
1977
+ end
1978
+ end
1979
+
1980
+ def visitTypeLabelId(ctx)
1981
+ handleSimpleId(ctx.SimpleId())
1982
+ end
1983
+
1984
+ def visitUnaryOp(ctx)
1985
+ if ctx.text == '+'
1986
+ Model::Operators::UnaryPlus.new()
1987
+ elsif ctx.text == '-'
1988
+ Model::Operators::UnaryMinus.new()
1989
+ elsif ctx.NOT()
1990
+ Model::Operators::Not.new()
1991
+ else
1992
+ raise 'Invalid state'
1993
+ end
1994
+ end
1995
+
1996
+ def visitUnderlyingType(ctx)
1997
+ if ctx.concreteTypes()
1998
+ visit(ctx.concreteTypes())
1999
+ elsif ctx.constructedTypes()
2000
+ visit(ctx.constructedTypes())
2001
+ else
2002
+ raise 'Invalid state'
2003
+ end
2004
+ end
2005
+
2006
+ def visitUniqueClause(ctx)
2007
+ raise 'Invalid state'
2008
+ end
2009
+
2010
+ def visitUniqueRule(ctx)
2011
+ id = if ctx.ruleLabelId()
2012
+ visit(ctx.ruleLabelId())
2013
+ end
2014
+ attributes = ctx.referencedAttribute().map{|ctx| visit(ctx)}
2015
+
2016
+ Model::Unique.new({
2017
+ id: id,
2018
+ attributes: attributes
2019
+ })
2020
+ end
2021
+
2022
+ def visitUntilControl(ctx)
2023
+ raise 'Invalid state'
2024
+ end
2025
+
2026
+ def visitUseClause(ctx)
2027
+ schema = visit(ctx.schemaRef())
2028
+ items = ctx.namedTypeOrRename().map{|ctx| visit(ctx)}
2029
+
2030
+ Model::Use.new({
2031
+ schema: schema,
2032
+ items: items
2033
+ })
2034
+ end
2035
+
2036
+ def visitVariableId(ctx)
2037
+ handleSimpleId(ctx.SimpleId())
2038
+ end
2039
+
2040
+ def visitWhereClause(ctx)
2041
+ raise 'Invalid state'
2042
+ end
2043
+
2044
+ def visitWhileControl(ctx)
2045
+ raise 'Invalid state'
2046
+ end
2047
+
2048
+ def visitWidth(ctx)
2049
+ raise 'Invalid state'
2050
+ end
2051
+
2052
+ def visitWidthSpec(ctx)
2053
+ raise 'Invalid state'
2054
+ end
2055
+
2056
+ def handleBinaryExpression(operands, operators)
2057
+ if operands.length != operators.length + 1
2058
+ raise 'Invalid state'
2059
+ end
2060
+
2061
+ expression = Model::Expressions::Expression.new({
2062
+ operator: operators[0],
2063
+ operands: [operands[0], operands[1]]
2064
+ })
2065
+ operators[1..(operators.length - 1)].each_with_index do |operator, i|
2066
+ expression = Model::Expressions::Expression.new({
2067
+ operator: operator,
2068
+ operands: [expression, operands[i + 2]]
2069
+ })
2070
+ end
2071
+ expression
2072
+ end
2073
+
2074
+ def handleBinaryLiteral(ctx)
2075
+ bin = ctx.text[1..(ctx.text.length - 1)]
2076
+ [bin].pack('B*')
2077
+ end
2078
+
2079
+ def handleIntegerLiteral(ctx)
2080
+ ctx.text.to_i
2081
+ end
2082
+
2083
+ def handleRealLiteral(ctx)
2084
+ ctx.text.to_f
2085
+ end
2086
+
2087
+ def handleSimpleId(ctx)
2088
+ ctx.text
2089
+ end
2090
+
2091
+ def handleSimpleStringLiteral(ctx)
2092
+ ctx.text[1..(ctx.text.length - 2)]
2093
+ end
2094
+
2095
+ def handleEncodedStringLiteral(ctx)
2096
+ hex = ctx.text[1..(ctx.text.length - 2)]
2097
+ str = [hex].pack('H*')
2098
+ str.force_encoding('UCS-4BE')
2099
+ str.encode('ASCII-8BIT')
2100
+ end
2101
+ end
2102
+ end
2103
+ end