expressir 1.3.0-aarch64-linux-gnu

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.cross_rubies +20 -0
  3. data/.github/workflows/rake.yml +313 -0
  4. data/.github/workflows/release.yml +181 -0
  5. data/.gitignore +23 -0
  6. data/.gitmodules +6 -0
  7. data/.hound.yml +3 -0
  8. data/.rspec +2 -0
  9. data/.rubocop.yml +18 -0
  10. data/.yardopts +11 -0
  11. data/Gemfile +4 -0
  12. data/README.adoc +155 -0
  13. data/Rakefile +17 -0
  14. data/bin/console +11 -0
  15. data/bin/rspec +29 -0
  16. data/bin/setup +8 -0
  17. data/docs/development.md +90 -0
  18. data/exe/expressir +22 -0
  19. data/exe/format +18 -0
  20. data/exe/format-test +81 -0
  21. data/exe/generate-parser +51 -0
  22. data/expressir.gemspec +48 -0
  23. data/lib/expressir/cli/ui.rb +36 -0
  24. data/lib/expressir/cli.rb +21 -0
  25. data/lib/expressir/config.rb +23 -0
  26. data/lib/expressir/express/2.7/express_parser.so +0 -0
  27. data/lib/expressir/express/3.0/express_parser.so +0 -0
  28. data/lib/expressir/express/3.1/express_parser.so +0 -0
  29. data/lib/expressir/express/3.2/express_parser.so +0 -0
  30. data/lib/expressir/express/cache.rb +51 -0
  31. data/lib/expressir/express/formatter.rb +1608 -0
  32. data/lib/expressir/express/hyperlink_formatter.rb +36 -0
  33. data/lib/expressir/express/model_visitor.rb +24 -0
  34. data/lib/expressir/express/parser.rb +84 -0
  35. data/lib/expressir/express/resolve_references_model_visitor.rb +31 -0
  36. data/lib/expressir/express/schema_head_formatter.rb +23 -0
  37. data/lib/expressir/express/visitor.rb +2588 -0
  38. data/lib/expressir/model/cache.rb +17 -0
  39. data/lib/expressir/model/data_type.rb +9 -0
  40. data/lib/expressir/model/data_types/aggregate.rb +31 -0
  41. data/lib/expressir/model/data_types/array.rb +31 -0
  42. data/lib/expressir/model/data_types/bag.rb +25 -0
  43. data/lib/expressir/model/data_types/binary.rb +22 -0
  44. data/lib/expressir/model/data_types/boolean.rb +10 -0
  45. data/lib/expressir/model/data_types/enumeration.rb +25 -0
  46. data/lib/expressir/model/data_types/enumeration_item.rb +26 -0
  47. data/lib/expressir/model/data_types/generic.rb +26 -0
  48. data/lib/expressir/model/data_types/generic_entity.rb +26 -0
  49. data/lib/expressir/model/data_types/integer.rb +10 -0
  50. data/lib/expressir/model/data_types/list.rb +28 -0
  51. data/lib/expressir/model/data_types/logical.rb +10 -0
  52. data/lib/expressir/model/data_types/number.rb +10 -0
  53. data/lib/expressir/model/data_types/real.rb +19 -0
  54. data/lib/expressir/model/data_types/select.rb +28 -0
  55. data/lib/expressir/model/data_types/set.rb +25 -0
  56. data/lib/expressir/model/data_types/string.rb +22 -0
  57. data/lib/expressir/model/declaration.rb +9 -0
  58. data/lib/expressir/model/declarations/attribute.rb +47 -0
  59. data/lib/expressir/model/declarations/constant.rb +34 -0
  60. data/lib/expressir/model/declarations/entity.rb +53 -0
  61. data/lib/expressir/model/declarations/function.rb +67 -0
  62. data/lib/expressir/model/declarations/interface.rb +28 -0
  63. data/lib/expressir/model/declarations/interface_item.rb +23 -0
  64. data/lib/expressir/model/declarations/interfaced_item.rb +37 -0
  65. data/lib/expressir/model/declarations/parameter.rb +34 -0
  66. data/lib/expressir/model/declarations/procedure.rb +64 -0
  67. data/lib/expressir/model/declarations/remark_item.rb +21 -0
  68. data/lib/expressir/model/declarations/rule.rb +71 -0
  69. data/lib/expressir/model/declarations/schema.rb +117 -0
  70. data/lib/expressir/model/declarations/schema_version.rb +22 -0
  71. data/lib/expressir/model/declarations/schema_version_item.rb +22 -0
  72. data/lib/expressir/model/declarations/subtype_constraint.rb +40 -0
  73. data/lib/expressir/model/declarations/type.rb +45 -0
  74. data/lib/expressir/model/declarations/unique_rule.rb +31 -0
  75. data/lib/expressir/model/declarations/variable.rb +34 -0
  76. data/lib/expressir/model/declarations/where_rule.rb +31 -0
  77. data/lib/expressir/model/expression.rb +9 -0
  78. data/lib/expressir/model/expressions/aggregate_initializer.rb +19 -0
  79. data/lib/expressir/model/expressions/aggregate_initializer_item.rb +22 -0
  80. data/lib/expressir/model/expressions/binary_expression.rb +53 -0
  81. data/lib/expressir/model/expressions/entity_constructor.rb +22 -0
  82. data/lib/expressir/model/expressions/function_call.rb +22 -0
  83. data/lib/expressir/model/expressions/interval.rb +34 -0
  84. data/lib/expressir/model/expressions/query_expression.rb +35 -0
  85. data/lib/expressir/model/expressions/unary_expression.rb +27 -0
  86. data/lib/expressir/model/identifier.rb +34 -0
  87. data/lib/expressir/model/literal.rb +9 -0
  88. data/lib/expressir/model/literals/binary.rb +19 -0
  89. data/lib/expressir/model/literals/integer.rb +19 -0
  90. data/lib/expressir/model/literals/logical.rb +23 -0
  91. data/lib/expressir/model/literals/real.rb +19 -0
  92. data/lib/expressir/model/literals/string.rb +22 -0
  93. data/lib/expressir/model/model_element.rb +208 -0
  94. data/lib/expressir/model/reference.rb +9 -0
  95. data/lib/expressir/model/references/attribute_reference.rb +22 -0
  96. data/lib/expressir/model/references/group_reference.rb +22 -0
  97. data/lib/expressir/model/references/index_reference.rb +27 -0
  98. data/lib/expressir/model/references/simple_reference.rb +24 -0
  99. data/lib/expressir/model/repository.rb +23 -0
  100. data/lib/expressir/model/statement.rb +9 -0
  101. data/lib/expressir/model/statements/alias.rb +35 -0
  102. data/lib/expressir/model/statements/assignment.rb +22 -0
  103. data/lib/expressir/model/statements/case.rb +25 -0
  104. data/lib/expressir/model/statements/case_action.rb +22 -0
  105. data/lib/expressir/model/statements/compound.rb +19 -0
  106. data/lib/expressir/model/statements/escape.rb +10 -0
  107. data/lib/expressir/model/statements/if.rb +25 -0
  108. data/lib/expressir/model/statements/null.rb +10 -0
  109. data/lib/expressir/model/statements/procedure_call.rb +22 -0
  110. data/lib/expressir/model/statements/repeat.rb +47 -0
  111. data/lib/expressir/model/statements/return.rb +19 -0
  112. data/lib/expressir/model/statements/skip.rb +10 -0
  113. data/lib/expressir/model/supertype_expression.rb +9 -0
  114. data/lib/expressir/model/supertype_expressions/binary_supertype_expression.rb +29 -0
  115. data/lib/expressir/model/supertype_expressions/oneof_supertype_expression.rb +19 -0
  116. data/lib/expressir/model.rb +79 -0
  117. data/lib/expressir/version.rb +3 -0
  118. data/lib/expressir.rb +24 -0
  119. data/rakelib/antlr4-native.rake +161 -0
  120. data/rakelib/cross-ruby.rake +384 -0
  121. data/spec/acceptance/version_spec.rb +27 -0
  122. data/spec/expressir/express/cache_spec.rb +89 -0
  123. data/spec/expressir/express/formatter_spec.rb +173 -0
  124. data/spec/expressir/express/parser_spec.rb +141 -0
  125. data/spec/expressir/model/model_element_spec.rb +318 -0
  126. data/spec/spec_helper.rb +24 -0
  127. data/spec/support/console_helper.rb +29 -0
  128. data/spec/syntax/multiple.exp +23 -0
  129. data/spec/syntax/multiple.yaml +198 -0
  130. data/spec/syntax/multiple_formatted.exp +71 -0
  131. data/spec/syntax/multiple_hyperlink_formatted.exp +71 -0
  132. data/spec/syntax/multiple_schema_head_hyperlink_formatted.exp +13 -0
  133. data/spec/syntax/remark.exp +193 -0
  134. data/spec/syntax/remark.yaml +471 -0
  135. data/spec/syntax/remark_formatted.exp +228 -0
  136. data/spec/syntax/single.exp +4 -0
  137. data/spec/syntax/single.yaml +18 -0
  138. data/spec/syntax/single_formatted.exp +10 -0
  139. data/spec/syntax/single_formatted.yaml +36 -0
  140. data/spec/syntax/syntax.exp +333 -0
  141. data/spec/syntax/syntax.yaml +3509 -0
  142. data/spec/syntax/syntax_formatted.exp +902 -0
  143. data/spec/syntax/syntax_hyperlink_formatted.exp +902 -0
  144. data/spec/syntax/syntax_schema_head_formatted.exp +18 -0
  145. metadata +390 -0
@@ -0,0 +1,1608 @@
1
+ require 'expressir/model'
2
+
3
+ module Expressir
4
+ module Express
5
+ class Formatter
6
+ INDENT_CHAR = ' '
7
+ INDENT_WIDTH = 2
8
+ INDENT = INDENT_CHAR * INDENT_WIDTH
9
+ OPERATOR_PRECEDENCE = {
10
+ Model::Expressions::BinaryExpression::EXPONENTIATION => 1,
11
+ Model::Expressions::BinaryExpression::MULTIPLICATION => 2,
12
+ Model::Expressions::BinaryExpression::REAL_DIVISION => 2,
13
+ Model::Expressions::BinaryExpression::INTEGER_DIVISION => 2,
14
+ Model::Expressions::BinaryExpression::MODULO => 2,
15
+ Model::Expressions::BinaryExpression::AND => 2,
16
+ Model::Expressions::BinaryExpression::COMBINE => 2,
17
+ Model::Expressions::BinaryExpression::SUBTRACTION => 3,
18
+ Model::Expressions::BinaryExpression::ADDITION => 3,
19
+ Model::Expressions::BinaryExpression::OR => 3,
20
+ Model::Expressions::BinaryExpression::XOR => 3,
21
+ Model::Expressions::BinaryExpression::EQUAL => 4,
22
+ Model::Expressions::BinaryExpression::NOT_EQUAL => 4,
23
+ Model::Expressions::BinaryExpression::LESS_THAN_OR_EQUAL => 4,
24
+ Model::Expressions::BinaryExpression::GREATER_THAN_OR_EQUAL => 4,
25
+ Model::Expressions::BinaryExpression::LESS_THAN => 4,
26
+ Model::Expressions::BinaryExpression::GREATER_THAN => 4,
27
+ Model::Expressions::BinaryExpression::INSTANCE_EQUAL => 4,
28
+ Model::Expressions::BinaryExpression::INSTANCE_NOT_EQUAL => 4,
29
+ Model::Expressions::BinaryExpression::IN => 4,
30
+ Model::Expressions::BinaryExpression::LIKE => 4,
31
+ }
32
+ SUPERTYPE_OPERATOR_PRECEDENCE = {
33
+ Model::SupertypeExpressions::BinarySupertypeExpression::AND => 1,
34
+ Model::SupertypeExpressions::BinarySupertypeExpression::ANDOR => 2,
35
+ }
36
+
37
+ private_constant :INDENT_CHAR
38
+ private_constant :INDENT_WIDTH
39
+ private_constant :INDENT
40
+ private_constant :OPERATOR_PRECEDENCE
41
+ private_constant :SUPERTYPE_OPERATOR_PRECEDENCE
42
+
43
+ # Formats Express model into an Express code
44
+ # @param [Model::ModelElement] node
45
+ # @return [String]
46
+ def self.format(node)
47
+ formatter = self.new
48
+ formatter.format(node)
49
+ end
50
+
51
+ # Formats Express model into an Express code
52
+ # @param [Model::ModelElement] node
53
+ # @return [String]
54
+ def format(node)
55
+ if node.is_a? Model::Repository
56
+ format_repository(node)
57
+ elsif node.is_a? Model::Declarations::Attribute
58
+ format_declarations_attribute(node)
59
+ elsif node.is_a? Model::Declarations::Constant
60
+ format_declarations_constant(node)
61
+ elsif node.is_a? Model::Declarations::Entity
62
+ format_declarations_entity(node)
63
+ elsif node.is_a? Model::Declarations::Function
64
+ format_declarations_function(node)
65
+ elsif node.is_a? Model::Declarations::Interface
66
+ format_declarations_interface(node)
67
+ elsif node.is_a? Model::Declarations::InterfaceItem
68
+ format_declarations_interface_item(node)
69
+ elsif node.is_a? Model::Declarations::Parameter
70
+ format_declarations_parameter(node)
71
+ elsif node.is_a? Model::Declarations::Procedure
72
+ format_declarations_procedure(node)
73
+ elsif node.is_a? Model::Declarations::Rule
74
+ format_declarations_rule(node)
75
+ elsif node.is_a? Model::Declarations::Schema
76
+ format_declarations_schema(node)
77
+ elsif node.is_a? Model::Declarations::SchemaVersion
78
+ format_declarations_schema_version(node)
79
+ elsif node.is_a? Model::Declarations::SubtypeConstraint
80
+ format_declarations_subtype_constraint(node)
81
+ elsif node.is_a? Model::Declarations::Type
82
+ format_declarations_type(node)
83
+ elsif node.is_a? Model::Declarations::UniqueRule
84
+ format_declarations_unique_rule(node)
85
+ elsif node.is_a? Model::Declarations::Variable
86
+ format_declarations_variable(node)
87
+ elsif node.is_a? Model::Declarations::WhereRule
88
+ format_declarations_where_rule(node)
89
+ elsif node.is_a? Model::DataTypes::Aggregate
90
+ format_data_types_aggregate(node)
91
+ elsif node.is_a? Model::DataTypes::Array
92
+ format_data_types_array(node)
93
+ elsif node.is_a? Model::DataTypes::Bag
94
+ format_data_types_bag(node)
95
+ elsif node.is_a? Model::DataTypes::Binary
96
+ format_data_types_binary(node)
97
+ elsif node.is_a? Model::DataTypes::Boolean
98
+ format_data_types_boolean(node)
99
+ elsif node.is_a? Model::DataTypes::Enumeration
100
+ format_data_types_enumeration(node)
101
+ elsif node.is_a? Model::DataTypes::EnumerationItem
102
+ format_data_types_enumeration_item(node)
103
+ elsif node.is_a? Model::DataTypes::GenericEntity
104
+ format_data_types_generic_entity(node)
105
+ elsif node.is_a? Model::DataTypes::Generic
106
+ format_data_types_generic(node)
107
+ elsif node.is_a? Model::DataTypes::Integer
108
+ format_data_types_integer(node)
109
+ elsif node.is_a? Model::DataTypes::List
110
+ format_data_types_list(node)
111
+ elsif node.is_a? Model::DataTypes::Logical
112
+ format_data_types_logical(node)
113
+ elsif node.is_a? Model::DataTypes::Number
114
+ format_data_types_number(node)
115
+ elsif node.is_a? Model::DataTypes::Real
116
+ format_data_types_real(node)
117
+ elsif node.is_a? Model::DataTypes::Select
118
+ format_data_types_select(node)
119
+ elsif node.is_a? Model::DataTypes::Set
120
+ format_data_types_set(node)
121
+ elsif node.is_a? Model::DataTypes::String
122
+ format_data_types_string(node)
123
+ elsif node.is_a? Model::Expressions::AggregateInitializer
124
+ format_expressions_aggregate_initializer(node)
125
+ elsif node.is_a? Model::Expressions::AggregateInitializerItem
126
+ format_expressions_aggregate_initializer_item(node)
127
+ elsif node.is_a? Model::Expressions::BinaryExpression
128
+ format_expressions_binary_expression(node)
129
+ elsif node.is_a? Model::Expressions::EntityConstructor
130
+ format_expressions_entity_constructor(node)
131
+ elsif node.is_a? Model::Expressions::FunctionCall
132
+ format_expressions_function_call(node)
133
+ elsif node.is_a? Model::Expressions::Interval
134
+ format_expressions_interval(node)
135
+ elsif node.is_a? Model::Expressions::QueryExpression
136
+ format_expressions_query_expression(node)
137
+ elsif node.is_a? Model::Expressions::UnaryExpression
138
+ format_expressions_unary_expression(node)
139
+ elsif node.is_a? Model::Literals::Binary
140
+ format_literals_binary(node)
141
+ elsif node.is_a? Model::Literals::Integer
142
+ format_literals_integer(node)
143
+ elsif node.is_a? Model::Literals::Logical
144
+ format_literals_logical(node)
145
+ elsif node.is_a? Model::Literals::Real
146
+ format_literals_real(node)
147
+ elsif node.is_a? Model::Literals::String
148
+ format_literals_string(node)
149
+ elsif node.is_a? Model::References::AttributeReference
150
+ format_references_attribute_reference(node)
151
+ elsif node.is_a? Model::References::GroupReference
152
+ format_references_group_reference(node)
153
+ elsif node.is_a? Model::References::IndexReference
154
+ format_references_index_reference(node)
155
+ elsif node.is_a? Model::References::SimpleReference
156
+ format_references_simple_reference(node)
157
+ elsif node.is_a? Model::Statements::Alias
158
+ format_statements_alias(node)
159
+ elsif node.is_a? Model::Statements::Assignment
160
+ format_statements_assignment(node)
161
+ elsif node.is_a? Model::Statements::Case
162
+ format_statements_case(node)
163
+ elsif node.is_a? Model::Statements::CaseAction
164
+ format_statements_case_action(node)
165
+ elsif node.is_a? Model::Statements::Compound
166
+ format_statements_compound(node)
167
+ elsif node.is_a? Model::Statements::Escape
168
+ format_statements_escape(node)
169
+ elsif node.is_a? Model::Statements::If
170
+ format_statements_if(node)
171
+ elsif node.is_a? Model::Statements::Null
172
+ format_statements_null(node)
173
+ elsif node.is_a? Model::Statements::ProcedureCall
174
+ format_statements_procedure_call(node)
175
+ elsif node.is_a? Model::Statements::Repeat
176
+ format_statements_repeat(node)
177
+ elsif node.is_a? Model::Statements::Return
178
+ format_statements_return(node)
179
+ elsif node.is_a? Model::Statements::Skip
180
+ format_statements_skip(node)
181
+ elsif node.is_a? Model::SupertypeExpressions::BinarySupertypeExpression
182
+ format_supertype_expressions_binary_supertype_expression(node)
183
+ elsif node.is_a? Model::SupertypeExpressions::OneofSupertypeExpression
184
+ format_supertype_expressions_oneof_supertype_expression(node)
185
+ else
186
+ STDERR.puts "#{node.class.name} format not implemented"
187
+ ''
188
+ end
189
+ end
190
+
191
+ private
192
+
193
+ def format_repository(node)
194
+ node.schemas.map{|x| format(x)}.join("\n\n")
195
+ end
196
+
197
+ def format_declarations_attribute(node)
198
+ [
199
+ *if node.supertype_attribute
200
+ [
201
+ format(node.supertype_attribute),
202
+ ' ',
203
+ ].join('')
204
+ end,
205
+ *if node.supertype_attribute and node.id
206
+ [
207
+ 'RENAMED',
208
+ ' '
209
+ ].join('')
210
+ end,
211
+ *if node.id
212
+ [
213
+ node.id,
214
+ ' '
215
+ ].join('')
216
+ end,
217
+ ':',
218
+ *if node.optional
219
+ [
220
+ ' ',
221
+ 'OPTIONAL'
222
+ ].join('')
223
+ end,
224
+ ' ',
225
+ format(node.type),
226
+ *if node.kind == Model::Declarations::Attribute::DERIVED
227
+ [
228
+ ' ',
229
+ ':=',
230
+ ' ',
231
+ format(node.expression)
232
+ ].join('')
233
+ elsif node.kind == Model::Declarations::Attribute::INVERSE
234
+ [
235
+ ' ',
236
+ 'FOR',
237
+ ' ',
238
+ format(node.expression)
239
+ ].join('')
240
+ end,
241
+ ';',
242
+ ].join('')
243
+ end
244
+
245
+ def format_declarations_constant(node)
246
+ [
247
+ node.id,
248
+ ' ',
249
+ ':',
250
+ ' ',
251
+ format(node.type),
252
+ ' ',
253
+ ':=',
254
+ ' ',
255
+ format(node.expression),
256
+ ';'
257
+ ].join('')
258
+ end
259
+
260
+ def format_declarations_entity(node)
261
+ explicit_attributes = node.attributes.select{|x| x.kind == Model::Declarations::Attribute::EXPLICIT}
262
+ derived_attributes = node.attributes.select{|x| x.kind == Model::Declarations::Attribute::DERIVED}
263
+ inverse_attributes = node.attributes.select{|x| x.kind == Model::Declarations::Attribute::INVERSE}
264
+
265
+ [
266
+ [
267
+ 'ENTITY',
268
+ ' ',
269
+ node.id,
270
+ *if node.abstract and !node.supertype_expression
271
+ [
272
+ "\n",
273
+ indent([
274
+ 'ABSTRACT',
275
+ ' ',
276
+ 'SUPERTYPE'
277
+ ].join(''))
278
+ ].join('')
279
+ end,
280
+ *if node.abstract and node.supertype_expression
281
+ [
282
+ "\n",
283
+ indent([
284
+ 'ABSTRACT',
285
+ ' ',
286
+ 'SUPERTYPE',
287
+ ' ',
288
+ 'OF',
289
+ ' ',
290
+ '(',
291
+ format(node.supertype_expression),
292
+ ')'
293
+ ].join(''))
294
+ ].join('')
295
+ end,
296
+ *if !node.abstract and node.supertype_expression
297
+ [
298
+ "\n",
299
+ indent([
300
+ 'SUPERTYPE',
301
+ ' ',
302
+ 'OF',
303
+ ' ',
304
+ '(',
305
+ format(node.supertype_expression),
306
+ ')'
307
+ ].join(''))
308
+ ].join('')
309
+ end,
310
+ *if node.subtype_of.length > 0
311
+ [
312
+ "\n",
313
+ indent([
314
+ 'SUBTYPE',
315
+ ' ',
316
+ 'OF',
317
+ ' ',
318
+ '(',
319
+ node.subtype_of.map{|x| format(x)}.join(', '),
320
+ ')'
321
+ ].join(''))
322
+ ].join('')
323
+ end,
324
+ ';'
325
+ ].join(''),
326
+ *if explicit_attributes and explicit_attributes.length > 0
327
+ indent(explicit_attributes.map{|x| format(x)}.join("\n"))
328
+ end,
329
+ *if derived_attributes and derived_attributes.length > 0
330
+ [
331
+ 'DERIVE',
332
+ indent(derived_attributes.map{|x| format(x)}.join("\n")),
333
+ ]
334
+ end,
335
+ *if inverse_attributes and inverse_attributes.length > 0
336
+ [
337
+ 'INVERSE',
338
+ indent(inverse_attributes.map{|x| format(x)}.join("\n")),
339
+ ]
340
+ end,
341
+ *if node.unique_rules.length > 0
342
+ [
343
+ 'UNIQUE',
344
+ indent(node.unique_rules.map{|x| format(x)}.join("\n"))
345
+ ]
346
+ end,
347
+ *if node.where_rules.length > 0
348
+ [
349
+ 'WHERE',
350
+ indent(node.where_rules.map{|x| format(x)}.join("\n")),
351
+ ]
352
+ end,
353
+ [
354
+ 'END_ENTITY',
355
+ ';'
356
+ ].join('')
357
+ ].join("\n")
358
+ end
359
+
360
+ def format_declarations_function(node)
361
+ [
362
+ [
363
+ 'FUNCTION',
364
+ ' ',
365
+ node.id,
366
+ *if node.parameters.length > 0
367
+ parameter_indent = INDENT_CHAR * "FUNCTION #{node.id}(".length
368
+ [
369
+ '(',
370
+ node.parameters.map{|x| format(x)}.join(";\n#{parameter_indent}"),
371
+ ')'
372
+ ].join('')
373
+ end,
374
+ ' ',
375
+ ':',
376
+ ' ',
377
+ format(node.return_type),
378
+ ';'
379
+ ].join(''),
380
+ *if node.types.length > 0
381
+ indent(node.types.map{|x| format(x)}.join("\n"))
382
+ end,
383
+ *if node.entities.length > 0
384
+ indent(node.entities.map{|x| format(x)}.join("\n"))
385
+ end,
386
+ *if node.subtype_constraints.length > 0
387
+ indent(node.subtype_constraints.map{|x| format(x)}.join("\n"))
388
+ end,
389
+ *if node.functions.length > 0
390
+ indent(node.functions.map{|x| format(x)}.join("\n"))
391
+ end,
392
+ *if node.procedures.length > 0
393
+ indent(node.procedures.map{|x| format(x)}.join("\n"))
394
+ end,
395
+ *if node.constants.length > 0
396
+ indent([
397
+ 'CONSTANT',
398
+ indent(node.constants.map{|x| format(x)}.join("\n")),
399
+ [
400
+ 'END_CONSTANT',
401
+ ';'
402
+ ].join('')
403
+ ].join("\n"))
404
+ end,
405
+ *if node.variables.length > 0
406
+ indent([
407
+ 'LOCAL',
408
+ indent(node.variables.map{|x| format(x)}.join("\n")),
409
+ [
410
+ 'END_LOCAL',
411
+ ';'
412
+ ].join('')
413
+ ].join("\n"))
414
+ end,
415
+ *if node.statements.length > 0
416
+ indent(node.statements.map{|x| format(x)}.join("\n"))
417
+ end,
418
+ [
419
+ 'END_FUNCTION',
420
+ ';'
421
+ ].join('')
422
+ ].join("\n")
423
+ end
424
+
425
+ def format_declarations_interface(node)
426
+ [
427
+ case node.kind
428
+ when Model::Declarations::Interface::USE then 'USE'
429
+ when Model::Declarations::Interface::REFERENCE then 'REFERENCE'
430
+ end,
431
+ ' ',
432
+ 'FROM',
433
+ ' ',
434
+ format(node.schema),
435
+ *if node.items.length > 0
436
+ item_indent = INDENT_CHAR * '('.length
437
+ [
438
+ "\n",
439
+ indent([
440
+ '(',
441
+ node.items.map{|x| format(x)}.join(",\n#{item_indent}"),
442
+ ')'
443
+ ].join(''))
444
+ ].join('')
445
+ end,
446
+ ';',
447
+ ].join('')
448
+ end
449
+
450
+ def format_declarations_interface_item(node)
451
+ [
452
+ format(node.ref),
453
+ *if node.id
454
+ [
455
+ ' ',
456
+ 'AS',
457
+ ' ',
458
+ node.id
459
+ ]
460
+ end
461
+ ].join('')
462
+ end
463
+
464
+ def format_declarations_parameter(node)
465
+ [
466
+ *if node.var
467
+ [
468
+ 'VAR',
469
+ ' '
470
+ ].join('')
471
+ end,
472
+ node.id,
473
+ ' ',
474
+ ':',
475
+ ' ',
476
+ format(node.type)
477
+ ].join('')
478
+ end
479
+
480
+ def format_declarations_procedure(node)
481
+ [
482
+ [
483
+ 'PROCEDURE',
484
+ ' ',
485
+ node.id,
486
+ *if node.parameters.length > 0
487
+ parameter_indent = INDENT_CHAR * "PROCEDURE #{node.id}(".length
488
+ [
489
+ '(',
490
+ node.parameters.map{|x| format(x)}.join(";\n#{parameter_indent}"),
491
+ ')'
492
+ ].join('')
493
+ end,
494
+ ';'
495
+ ].join(''),
496
+ *if node.types.length > 0
497
+ indent(node.types.map{|x| format(x)}.join("\n"))
498
+ end,
499
+ *if node.entities.length > 0
500
+ indent(node.entities.map{|x| format(x)}.join("\n"))
501
+ end,
502
+ *if node.subtype_constraints.length > 0
503
+ indent(node.subtype_constraints.map{|x| format(x)}.join("\n"))
504
+ end,
505
+ *if node.functions.length > 0
506
+ indent(node.functions.map{|x| format(x)}.join("\n"))
507
+ end,
508
+ *if node.procedures.length > 0
509
+ indent(node.procedures.map{|x| format(x)}.join("\n"))
510
+ end,
511
+ *if node.constants.length > 0
512
+ indent([
513
+ 'CONSTANT',
514
+ indent(node.constants.map{|x| format(x)}.join("\n")),
515
+ [
516
+ 'END_CONSTANT',
517
+ ';'
518
+ ].join('')
519
+ ].join("\n"))
520
+ end,
521
+ *if node.variables.length > 0
522
+ indent([
523
+ 'LOCAL',
524
+ indent(node.variables.map{|x| format(x)}.join("\n")),
525
+ [
526
+ 'END_LOCAL',
527
+ ';'
528
+ ].join('')
529
+ ].join("\n"))
530
+ end,
531
+ *if node.statements.length > 0
532
+ indent(node.statements.map{|x| format(x)}.join("\n"))
533
+ end,
534
+ [
535
+ 'END_PROCEDURE',
536
+ ';'
537
+ ].join('')
538
+ ].join("\n")
539
+ end
540
+
541
+ def format_declarations_rule(node)
542
+ [
543
+ [
544
+ 'RULE',
545
+ ' ',
546
+ node.id,
547
+ ' ',
548
+ 'FOR',
549
+ ' ',
550
+ '(',
551
+ node.applies_to.map{|x| format(x)}.join(', '),
552
+ ')',
553
+ ';'
554
+ ].join(''),
555
+ *if node.types.length > 0
556
+ indent(node.types.map{|x| format(x)}.join("\n"))
557
+ end,
558
+ *if node.entities.length > 0
559
+ indent(node.entities.map{|x| format(x)}.join("\n"))
560
+ end,
561
+ *if node.subtype_constraints.length > 0
562
+ indent(node.subtype_constraints.map{|x| format(x)}.join("\n"))
563
+ end,
564
+ *if node.functions.length > 0
565
+ indent(node.functions.map{|x| format(x)}.join("\n"))
566
+ end,
567
+ *if node.procedures.length > 0
568
+ indent(node.procedures.map{|x| format(x)}.join("\n"))
569
+ end,
570
+ *if node.constants.length > 0
571
+ indent([
572
+ 'CONSTANT',
573
+ indent(node.constants.map{|x| format(x)}.join("\n")),
574
+ [
575
+ 'END_CONSTANT',
576
+ ';'
577
+ ].join('')
578
+ ].join("\n"))
579
+ end,
580
+ *if node.variables.length > 0
581
+ indent([
582
+ 'LOCAL',
583
+ indent(node.variables.map{|x| format(x)}.join("\n")),
584
+ [
585
+ 'END_LOCAL',
586
+ ';'
587
+ ].join('')
588
+ ].join("\n"))
589
+ end,
590
+ *if node.statements.length > 0
591
+ indent(node.statements.map{|x| format(x)}.join("\n"))
592
+ end,
593
+ *if node.where_rules.length > 0
594
+ [
595
+ 'WHERE',
596
+ indent(node.where_rules.map{|x| format(x)}.join("\n"))
597
+ ]
598
+ end,
599
+ [
600
+ 'END_RULE',
601
+ ';'
602
+ ].join('')
603
+ ].join("\n")
604
+ end
605
+
606
+ def format_declarations_schema_head(node)
607
+ [
608
+ [
609
+ 'SCHEMA',
610
+ ' ',
611
+ node.id,
612
+ *if node.version
613
+ [
614
+ ' ',
615
+ format(node.version),
616
+ ].join('')
617
+ end,
618
+ ';'
619
+ ].join(''),
620
+ *if node.interfaces.length > 0
621
+ [
622
+ '',
623
+ node.interfaces.map{|x| format(x)}.join("\n")
624
+ ]
625
+ end
626
+ ].join("\n")
627
+ end
628
+
629
+ def format_declarations_schema(node)
630
+ schema_declarations = [
631
+ *if node.constants.length > 0
632
+ [
633
+ 'CONSTANT',
634
+ indent(node.constants.map{|x| format(x)}.join("\n")),
635
+ [
636
+ 'END_CONSTANT',
637
+ ';'
638
+ ].join('')
639
+ ].join("\n")
640
+ end,
641
+ *if node.types.length > 0
642
+ node.types.map{|x| format(x)}
643
+ end,
644
+ *if node.entities.length > 0
645
+ node.entities.map{|x| format(x)}
646
+ end,
647
+ *if node.subtype_constraints.length > 0
648
+ node.subtype_constraints.map{|x| format(x)}
649
+ end,
650
+ *if node.functions.length > 0
651
+ node.functions.map{|x| format(x)}
652
+ end,
653
+ *if node.rules.length > 0
654
+ node.rules.map{|x| format(x)}
655
+ end,
656
+ *if node.procedures and node.procedures.length > 0
657
+ node.procedures.map{|x| format(x)}
658
+ end
659
+ ]
660
+
661
+ [
662
+ format_declarations_schema_head(node),
663
+ *if schema_declarations.length > 0
664
+ [
665
+ '',
666
+ schema_declarations.join("\n\n"),
667
+ ''
668
+ ]
669
+ end,
670
+ [
671
+ 'END_SCHEMA',
672
+ ';'
673
+ ].join(''),
674
+ *format_scope_remarks(node)
675
+ ].join("\n")
676
+ end
677
+
678
+ def format_declarations_schema_version(node)
679
+ [
680
+ "'",
681
+ node.value,
682
+ "'"
683
+ ].join('')
684
+ end
685
+
686
+ def format_declarations_subtype_constraint(node)
687
+ [
688
+ [
689
+ 'SUBTYPE_CONSTRAINT',
690
+ ' ',
691
+ node.id,
692
+ ' ',
693
+ 'FOR',
694
+ ' ',
695
+ format(node.applies_to),
696
+ ';'
697
+ ].join(''),
698
+ *if node.abstract
699
+ indent([
700
+ 'ABSTRACT',
701
+ ' ',
702
+ 'SUPERTYPE',
703
+ ';'
704
+ ].join(''))
705
+ end,
706
+ *if node.total_over.length > 0
707
+ indent([
708
+ 'TOTAL_OVER',
709
+ '(',
710
+ node.total_over.map{|x| format(x)}.join(', '),
711
+ ')',
712
+ ';'
713
+ ].join(''))
714
+ end,
715
+ *if node.supertype_expression
716
+ indent([
717
+ format(node.supertype_expression),
718
+ ';'
719
+ ].join(''))
720
+ end,
721
+ [
722
+ 'END_SUBTYPE_CONSTRAINT',
723
+ ';'
724
+ ].join('')
725
+ ].join("\n")
726
+ end
727
+
728
+ def format_declarations_type(node)
729
+ [
730
+ [
731
+ 'TYPE',
732
+ ' ',
733
+ node.id,
734
+ ' ',
735
+ '=',
736
+ ' ',
737
+ format(node.underlying_type),
738
+ ';',
739
+ ].join(''),
740
+ *if node.where_rules.length > 0
741
+ [
742
+ 'WHERE',
743
+ indent(node.where_rules.map{|x| format(x)}.join("\n"))
744
+ ]
745
+ end,
746
+ [
747
+ 'END_TYPE',
748
+ ';'
749
+ ].join('')
750
+ ].join("\n")
751
+ end
752
+
753
+ def format_declarations_unique_rule(node)
754
+ [
755
+ *if node.id
756
+ [
757
+ node.id,
758
+ ':',
759
+ ' '
760
+ ].join('')
761
+ end,
762
+ node.attributes.map{|x| format(x)}.join(', '),
763
+ ';'
764
+ ].join('')
765
+ end
766
+
767
+ def format_declarations_variable(node)
768
+ [
769
+ node.id,
770
+ ' ',
771
+ ':',
772
+ ' ',
773
+ format(node.type),
774
+ *if node.expression
775
+ [
776
+ ' ',
777
+ ':=',
778
+ ' ',
779
+ format(node.expression),
780
+ ].join('')
781
+ end,
782
+ ';'
783
+ ].join('')
784
+ end
785
+
786
+ def format_declarations_where_rule(node)
787
+ [
788
+ *if node.id
789
+ [
790
+ node.id,
791
+ ':',
792
+ ' '
793
+ ].join('')
794
+ end,
795
+ format(node.expression),
796
+ ';'
797
+ ].join('')
798
+ end
799
+
800
+ def format_expressions_aggregate_initializer(node)
801
+ [
802
+ '[',
803
+ node.items.map{|x| format(x)}.join(', '),
804
+ ']'
805
+ ].join('')
806
+ end
807
+
808
+ def format_expressions_aggregate_initializer_item(node)
809
+ [
810
+ format(node.expression),
811
+ ':',
812
+ format(node.repetition)
813
+ ].join('')
814
+ end
815
+
816
+ def format_expressions_binary_expression(node)
817
+ [
818
+ *if node.operand1.is_a? Model::Expressions::BinaryExpression and OPERATOR_PRECEDENCE[node.operand1.operator] > OPERATOR_PRECEDENCE[node.operator]
819
+ '('
820
+ end,
821
+ format(node.operand1),
822
+ *if node.operand1.is_a? Model::Expressions::BinaryExpression and OPERATOR_PRECEDENCE[node.operand1.operator] > OPERATOR_PRECEDENCE[node.operator]
823
+ ')'
824
+ end,
825
+ ' ',
826
+ case node.operator
827
+ when Model::Expressions::BinaryExpression::ADDITION then '+'
828
+ when Model::Expressions::BinaryExpression::AND then 'AND'
829
+ when Model::Expressions::BinaryExpression::COMBINE then '||'
830
+ when Model::Expressions::BinaryExpression::EQUAL then '='
831
+ when Model::Expressions::BinaryExpression::EXPONENTIATION then '**'
832
+ when Model::Expressions::BinaryExpression::GREATER_THAN then '>'
833
+ when Model::Expressions::BinaryExpression::GREATER_THAN_OR_EQUAL then '>='
834
+ when Model::Expressions::BinaryExpression::IN then 'IN'
835
+ when Model::Expressions::BinaryExpression::INSTANCE_EQUAL then ':=:'
836
+ when Model::Expressions::BinaryExpression::INSTANCE_NOT_EQUAL then ':<>:'
837
+ when Model::Expressions::BinaryExpression::INTEGER_DIVISION then 'DIV'
838
+ when Model::Expressions::BinaryExpression::LESS_THAN then '<'
839
+ when Model::Expressions::BinaryExpression::LESS_THAN_OR_EQUAL then '<='
840
+ when Model::Expressions::BinaryExpression::LIKE then 'LIKE'
841
+ when Model::Expressions::BinaryExpression::MODULO then 'MOD'
842
+ when Model::Expressions::BinaryExpression::MULTIPLICATION then '*'
843
+ when Model::Expressions::BinaryExpression::NOT_EQUAL then '<>'
844
+ when Model::Expressions::BinaryExpression::OR then 'OR'
845
+ when Model::Expressions::BinaryExpression::REAL_DIVISION then '/'
846
+ when Model::Expressions::BinaryExpression::SUBTRACTION then '-'
847
+ when Model::Expressions::BinaryExpression::XOR then 'XOR'
848
+ end,
849
+ ' ',
850
+ *if node.operand2.is_a? Model::Expressions::BinaryExpression and OPERATOR_PRECEDENCE[node.operand2.operator] > OPERATOR_PRECEDENCE[node.operator]
851
+ '('
852
+ end,
853
+ format(node.operand2),
854
+ *if node.operand2.is_a? Model::Expressions::BinaryExpression and OPERATOR_PRECEDENCE[node.operand2.operator] > OPERATOR_PRECEDENCE[node.operator]
855
+ ')'
856
+ end,
857
+ ].join('')
858
+ end
859
+
860
+ def format_expressions_entity_constructor(node)
861
+ [
862
+ format(node.entity),
863
+ '(',
864
+ node.parameters.map{|x| format(x)}.join(', '),
865
+ ')'
866
+ ].join('')
867
+ end
868
+
869
+ def format_expressions_function_call(node)
870
+ [
871
+ format(node.function),
872
+ '(',
873
+ node.parameters.map{|x| format(x)}.join(', '),
874
+ ')'
875
+ ].join('')
876
+ end
877
+
878
+ def format_expressions_interval(node)
879
+ [
880
+ '{',
881
+ format(node.low),
882
+ ' ',
883
+ case node.operator1
884
+ when Model::Expressions::Interval::LESS_THAN then '<'
885
+ when Model::Expressions::Interval::LESS_THAN_OR_EQUAL then '<='
886
+ end,
887
+ ' ',
888
+ format(node.item),
889
+ ' ',
890
+ case node.operator2
891
+ when Model::Expressions::Interval::LESS_THAN then '<'
892
+ when Model::Expressions::Interval::LESS_THAN_OR_EQUAL then '<='
893
+ end,
894
+ ' ',
895
+ format(node.high),
896
+ '}'
897
+ ].join('')
898
+ end
899
+
900
+ def format_expressions_query_expression(node)
901
+ [
902
+ 'QUERY',
903
+ '(',
904
+ node.id,
905
+ ' ',
906
+ '<*',
907
+ ' ',
908
+ format(node.aggregate_source),
909
+ ' ',
910
+ '|',
911
+ ' ',
912
+ format(node.expression),
913
+ *format_remarks(node).instance_eval{|x| x.length > 0 ? ["\n", *x, "\n"] : x},
914
+ ')'
915
+ ].join('')
916
+ end
917
+
918
+ def format_expressions_unary_expression(node)
919
+ [
920
+ case node.operator
921
+ when Model::Expressions::UnaryExpression::MINUS then '-'
922
+ when Model::Expressions::UnaryExpression::NOT then 'NOT'
923
+ when Model::Expressions::UnaryExpression::PLUS then '+'
924
+ end,
925
+ if node.operator == Model::Expressions::UnaryExpression::NOT
926
+ ' '
927
+ end,
928
+ *if node.operand.is_a? Model::Expressions::BinaryExpression
929
+ '('
930
+ end,
931
+ format(node.operand),
932
+ *if node.operand.is_a? Model::Expressions::BinaryExpression
933
+ ')'
934
+ end
935
+ ].join('')
936
+ end
937
+
938
+ def format_literals_binary(node)
939
+ [
940
+ '%',
941
+ node.value
942
+ ].join('')
943
+ end
944
+
945
+ def format_literals_integer(node)
946
+ node.value
947
+ end
948
+
949
+ def format_literals_logical(node)
950
+ case node.value
951
+ when Model::Literals::Logical::TRUE then 'TRUE'
952
+ when Model::Literals::Logical::FALSE then 'FALSE'
953
+ when Model::Literals::Logical::UNKNOWN then 'UNKNOWN'
954
+ end
955
+ end
956
+
957
+ def format_literals_real(node)
958
+ node.value
959
+ end
960
+
961
+ def format_literals_string(node)
962
+ if node.encoded
963
+ [
964
+ "\"",
965
+ node.value,
966
+ "\""
967
+ ].join('')
968
+ else
969
+ [
970
+ "'",
971
+ node.value,
972
+ "'"
973
+ ].join('')
974
+ end
975
+ end
976
+
977
+ def format_references_attribute_reference(node)
978
+ [
979
+ format(node.ref),
980
+ '.',
981
+ format(node.attribute)
982
+ ].join('')
983
+ end
984
+
985
+ def format_references_group_reference(node)
986
+ [
987
+ format(node.ref),
988
+ '\\',
989
+ format(node.entity)
990
+ ].join('')
991
+ end
992
+
993
+ def format_references_index_reference(node)
994
+ [
995
+ format(node.ref),
996
+ '[',
997
+ format(node.index1),
998
+ *if node.index2
999
+ [
1000
+ ':',
1001
+ format(node.index2)
1002
+ ].join('')
1003
+ end,
1004
+ ']'
1005
+ ].join('')
1006
+ end
1007
+
1008
+ def format_references_simple_reference(node)
1009
+ node.id
1010
+ end
1011
+
1012
+ def format_statements_alias(node)
1013
+ [
1014
+ [
1015
+ 'ALIAS',
1016
+ ' ',
1017
+ node.id,
1018
+ ' ',
1019
+ 'FOR',
1020
+ ' ',
1021
+ format(node.expression),
1022
+ ';'
1023
+ ].join(''),
1024
+ *if node.statements.length > 0
1025
+ indent(node.statements.map{|x| format(x)}.join("\n"))
1026
+ end,
1027
+ *format_remarks(node),
1028
+ [
1029
+ 'END_ALIAS',
1030
+ ';'
1031
+ ].join('')
1032
+ ].join("\n")
1033
+ end
1034
+
1035
+ def format_statements_assignment(node)
1036
+ [
1037
+ format(node.ref),
1038
+ ' ',
1039
+ ':=',
1040
+ ' ',
1041
+ format(node.expression),
1042
+ ';'
1043
+ ].join('')
1044
+ end
1045
+
1046
+ def format_statements_procedure_call(node)
1047
+ [
1048
+ format(node.procedure),
1049
+ *if node.parameters.length > 0
1050
+ [
1051
+ '(',
1052
+ node.parameters.map{|x| format(x)}.join(', '),
1053
+ ')'
1054
+ ].join('')
1055
+ end,
1056
+ ';'
1057
+ ].join('')
1058
+ end
1059
+
1060
+ def format_statements_case(node)
1061
+ [
1062
+ [
1063
+ 'CASE',
1064
+ ' ',
1065
+ format(node.expression),
1066
+ ' ',
1067
+ 'OF'
1068
+ ].join(''),
1069
+ *if node.actions.length > 0
1070
+ node.actions.map{|x| format(x)}
1071
+ end,
1072
+ *if node.otherwise_statement
1073
+ [
1074
+ [
1075
+ 'OTHERWISE',
1076
+ ' ',
1077
+ ':'
1078
+ ].join(''),
1079
+ indent(format(node.otherwise_statement))
1080
+ ]
1081
+ end,
1082
+ [
1083
+ 'END_CASE',
1084
+ ';'
1085
+ ].join('')
1086
+ ].join("\n")
1087
+ end
1088
+
1089
+ def format_statements_case_action(node)
1090
+ [
1091
+ [
1092
+ node.labels.map{|x| format(x)}.join(', '),
1093
+ ' ',
1094
+ ':'
1095
+ ].join(''),
1096
+ indent(format(node.statement))
1097
+ ].join("\n")
1098
+ end
1099
+
1100
+ def format_statements_compound(node)
1101
+ [
1102
+ 'BEGIN',
1103
+ *if node.statements.length > 0
1104
+ indent(node.statements.map{|x| format(x)}.join("\n"))
1105
+ end,
1106
+ [
1107
+ 'END',
1108
+ ';'
1109
+ ].join('')
1110
+ ].join("\n")
1111
+ end
1112
+
1113
+ def format_statements_escape(node)
1114
+ [
1115
+ 'ESCAPE',
1116
+ ';'
1117
+ ].join('')
1118
+ end
1119
+
1120
+ def format_statements_if(node)
1121
+ [
1122
+ [
1123
+ 'IF',
1124
+ ' ',
1125
+ format(node.expression),
1126
+ ' ',
1127
+ 'THEN'
1128
+ ].join(''),
1129
+ *if node.statements.length > 0
1130
+ indent(node.statements.map{|x| format(x)}.join("\n"))
1131
+ end,
1132
+ *if node.else_statements.length > 0
1133
+ [
1134
+ 'ELSE',
1135
+ indent(node.else_statements.map{|x| format(x)}.join("\n")),
1136
+ ].join("\n")
1137
+ end,
1138
+ [
1139
+ 'END_IF',
1140
+ ';'
1141
+ ].join('')
1142
+ ].join("\n")
1143
+ end
1144
+
1145
+ def format_statements_null(node)
1146
+ ';'
1147
+ end
1148
+
1149
+ def format_statements_repeat(node)
1150
+ [
1151
+ [
1152
+ 'REPEAT',
1153
+ *if node.id
1154
+ [
1155
+ ' ',
1156
+ node.id,
1157
+ ' ',
1158
+ ':=',
1159
+ ' ',
1160
+ format(node.bound1),
1161
+ ' ',
1162
+ 'TO',
1163
+ ' ',
1164
+ format(node.bound2),
1165
+ *if node.increment
1166
+ [
1167
+ ' ',
1168
+ 'BY',
1169
+ ' ',
1170
+ format(node.increment)
1171
+ ].join('')
1172
+ end
1173
+ ].join('')
1174
+ end,
1175
+ *if node.while_expression
1176
+ [
1177
+ ' ',
1178
+ 'WHILE',
1179
+ ' ',
1180
+ format(node.while_expression)
1181
+ ].join('')
1182
+ end,
1183
+ *if node.until_expression
1184
+ [
1185
+ ' ',
1186
+ 'UNTIL',
1187
+ ' ',
1188
+ format(node.until_expression)
1189
+ ].join('')
1190
+ end,
1191
+ ';'
1192
+ ].join(''),
1193
+ *if node.statements.length > 0
1194
+ indent(node.statements.map{|x| format(x)}.join("\n"))
1195
+ end,
1196
+ *format_remarks(node),
1197
+ [
1198
+ 'END_REPEAT',
1199
+ ';'
1200
+ ].join('')
1201
+ ].join("\n")
1202
+ end
1203
+
1204
+ def format_statements_return(node)
1205
+ [
1206
+ 'RETURN',
1207
+ *if node.expression
1208
+ [
1209
+ ' ',
1210
+ '(',
1211
+ format(node.expression),
1212
+ ')'
1213
+ ].join('')
1214
+ end,
1215
+ ';'
1216
+ ].join('')
1217
+ end
1218
+
1219
+ def format_statements_skip(node)
1220
+ [
1221
+ 'SKIP',
1222
+ ';'
1223
+ ].join('')
1224
+ end
1225
+
1226
+ def format_supertype_expressions_binary_supertype_expression(node)
1227
+ [
1228
+ *if node.operand1.is_a? Model::SupertypeExpressions::BinarySupertypeExpression and SUPERTYPE_OPERATOR_PRECEDENCE[node.operand1.operator] > SUPERTYPE_OPERATOR_PRECEDENCE[node.operator]
1229
+ '('
1230
+ end,
1231
+ format(node.operand1),
1232
+ *if node.operand1.is_a? Model::SupertypeExpressions::BinarySupertypeExpression and SUPERTYPE_OPERATOR_PRECEDENCE[node.operand1.operator] > SUPERTYPE_OPERATOR_PRECEDENCE[node.operator]
1233
+ ')'
1234
+ end,
1235
+ ' ',
1236
+ case node.operator
1237
+ when Model::SupertypeExpressions::BinarySupertypeExpression::AND then 'AND'
1238
+ when Model::SupertypeExpressions::BinarySupertypeExpression::ANDOR then 'ANDOR'
1239
+ end,
1240
+ ' ',
1241
+ *if node.operand2.is_a? Model::SupertypeExpressions::BinarySupertypeExpression and SUPERTYPE_OPERATOR_PRECEDENCE[node.operand2.operator] > SUPERTYPE_OPERATOR_PRECEDENCE[node.operator]
1242
+ '('
1243
+ end,
1244
+ format(node.operand2),
1245
+ *if node.operand2.is_a? Model::SupertypeExpressions::BinarySupertypeExpression and SUPERTYPE_OPERATOR_PRECEDENCE[node.operand2.operator] > SUPERTYPE_OPERATOR_PRECEDENCE[node.operator]
1246
+ ')'
1247
+ end,
1248
+ ].join('')
1249
+ end
1250
+
1251
+ def format_supertype_expressions_oneof_supertype_expression(node)
1252
+ [
1253
+ 'ONEOF',
1254
+ '(',
1255
+ node.operands.map{|x| format(x)}.join(', '),
1256
+ ')'
1257
+ ].join('')
1258
+ end
1259
+
1260
+ def format_data_types_aggregate(node)
1261
+ 'AGGREGATE'
1262
+ end
1263
+
1264
+ def format_data_types_array(node)
1265
+ [
1266
+ 'ARRAY',
1267
+ *if node.bound1 and node.bound2
1268
+ [
1269
+ ' ',
1270
+ '[',
1271
+ format(node.bound1),
1272
+ ':',
1273
+ format(node.bound2),
1274
+ ']',
1275
+ ].join('')
1276
+ end,
1277
+ ' ',
1278
+ 'OF',
1279
+ *if node.optional
1280
+ [
1281
+ ' ',
1282
+ 'OPTIONAL'
1283
+ ].join('')
1284
+ end,
1285
+ *if node.unique
1286
+ [
1287
+ ' ',
1288
+ 'UNIQUE'
1289
+ ].join('')
1290
+ end,
1291
+ ' ',
1292
+ format(node.base_type)
1293
+ ].join('')
1294
+ end
1295
+
1296
+ def format_data_types_bag(node)
1297
+ [
1298
+ 'BAG',
1299
+ *if node.bound1 and node.bound2
1300
+ [
1301
+ ' ',
1302
+ '[',
1303
+ format(node.bound1),
1304
+ ':',
1305
+ format(node.bound2),
1306
+ ']',
1307
+ ].join('')
1308
+ end,
1309
+ ' ',
1310
+ 'OF',
1311
+ ' ',
1312
+ format(node.base_type)
1313
+ ].join('')
1314
+ end
1315
+
1316
+ def format_data_types_binary(node)
1317
+ [
1318
+ 'BINARY',
1319
+ *if node.width
1320
+ [
1321
+ ' ',
1322
+ '(',
1323
+ format(node.width),
1324
+ ')'
1325
+ ].join('')
1326
+ end,
1327
+ *if node.fixed
1328
+ [
1329
+ ' ',
1330
+ 'FIXED'
1331
+ ].join('')
1332
+ end
1333
+ ].join('')
1334
+ end
1335
+
1336
+ def format_data_types_boolean(node)
1337
+ 'BOOLEAN'
1338
+ end
1339
+
1340
+ def format_data_types_enumeration(node)
1341
+ [
1342
+ *if node.extensible
1343
+ [
1344
+ 'EXTENSIBLE',
1345
+ ' '
1346
+ ].join('')
1347
+ end,
1348
+ 'ENUMERATION',
1349
+ *if node.based_on
1350
+ [
1351
+ ' ',
1352
+ 'BASED_ON',
1353
+ ' ',
1354
+ format(node.based_on),
1355
+ *if node.items.length > 0
1356
+ item_indent = INDENT_CHAR * '('.length
1357
+ [
1358
+ ' ',
1359
+ 'WITH',
1360
+ "\n",
1361
+ indent([
1362
+ '(',
1363
+ node.items.map{|x| format(x)}.join(",\n#{item_indent}"),
1364
+ ')'
1365
+ ].join(''))
1366
+ ].join('')
1367
+ end
1368
+ ].join('')
1369
+ else
1370
+ [
1371
+ *if node.items.length > 0
1372
+ item_indent = INDENT_CHAR * '('.length
1373
+ [
1374
+ ' ',
1375
+ 'OF',
1376
+ "\n",
1377
+ indent([
1378
+ '(',
1379
+ node.items.map{|x| format(x)}.join(",\n#{item_indent}"),
1380
+ ')'
1381
+ ].join(''))
1382
+ ].join('')
1383
+ end
1384
+ ].join('')
1385
+ end
1386
+ ].join('')
1387
+ end
1388
+
1389
+ def format_data_types_enumeration_item(node)
1390
+ node.id
1391
+ end
1392
+
1393
+ def format_data_types_generic_entity(node)
1394
+ [
1395
+ 'GENERIC_ENTITY',
1396
+ *if node.id
1397
+ [
1398
+ ':',
1399
+ node.id
1400
+ ]
1401
+ end
1402
+ ].join('')
1403
+ end
1404
+
1405
+ def format_data_types_generic(node)
1406
+ [
1407
+ 'GENERIC',
1408
+ *if node.id
1409
+ [
1410
+ ':',
1411
+ node.id
1412
+ ]
1413
+ end
1414
+ ].join('')
1415
+ end
1416
+
1417
+ def format_data_types_integer(node)
1418
+ 'INTEGER'
1419
+ end
1420
+
1421
+ def format_data_types_list(node)
1422
+ [
1423
+ 'LIST',
1424
+ *if node.bound1 and node.bound2
1425
+ [
1426
+ ' ',
1427
+ '[',
1428
+ format(node.bound1),
1429
+ ':',
1430
+ format(node.bound2),
1431
+ ']',
1432
+ ].join('')
1433
+ end,
1434
+ ' ',
1435
+ 'OF',
1436
+ *if node.unique
1437
+ [
1438
+ ' ',
1439
+ 'UNIQUE'
1440
+ ].join('')
1441
+ end,
1442
+ ' ',
1443
+ format(node.base_type)
1444
+ ].join('')
1445
+ end
1446
+
1447
+ def format_data_types_logical(node)
1448
+ 'LOGICAL'
1449
+ end
1450
+
1451
+ def format_data_types_number(node)
1452
+ 'NUMBER'
1453
+ end
1454
+
1455
+ def format_data_types_real(node)
1456
+ [
1457
+ 'REAL',
1458
+ *if node.precision
1459
+ [
1460
+ ' ',
1461
+ '(',
1462
+ format(node.precision),
1463
+ ')'
1464
+ ].join('')
1465
+ end
1466
+ ].join('')
1467
+ end
1468
+
1469
+ def format_data_types_select(node)
1470
+ [
1471
+ *if node.extensible
1472
+ [
1473
+ 'EXTENSIBLE',
1474
+ ' '
1475
+ ].join('')
1476
+ end,
1477
+ *if node.generic_entity
1478
+ [
1479
+ 'GENERIC_ENTITY',
1480
+ ' '
1481
+ ].join('')
1482
+ end,
1483
+ 'SELECT',
1484
+ *if node.based_on
1485
+ [
1486
+ ' ',
1487
+ 'BASED_ON',
1488
+ ' ',
1489
+ format(node.based_on),
1490
+ *if node.items.length > 0
1491
+ item_indent = INDENT_CHAR * '('.length
1492
+ [
1493
+ ' ',
1494
+ 'WITH',
1495
+ "\n",
1496
+ indent([
1497
+ '(',
1498
+ node.items.map{|x| format(x)}.join(",\n#{item_indent}"),
1499
+ ')'
1500
+ ].join(''))
1501
+ ].join('')
1502
+ end
1503
+ ].join('')
1504
+ else
1505
+ [
1506
+ *if node.items.length > 0
1507
+ item_indent = INDENT_CHAR * '('.length
1508
+ [
1509
+ "\n",
1510
+ indent([
1511
+ '(',
1512
+ node.items.map{|x| format(x)}.join(",\n#{item_indent}"),
1513
+ ')'
1514
+ ].join(''))
1515
+ ].join('')
1516
+ end
1517
+ ].join('')
1518
+ end
1519
+ ].join('')
1520
+ end
1521
+
1522
+ def format_data_types_set(node)
1523
+ [
1524
+ 'SET',
1525
+ *if node.bound1 and node.bound2
1526
+ [
1527
+ ' ',
1528
+ '[',
1529
+ format(node.bound1),
1530
+ ':',
1531
+ format(node.bound2),
1532
+ ']',
1533
+ ].join('')
1534
+ end,
1535
+ ' ',
1536
+ 'OF',
1537
+ ' ',
1538
+ format(node.base_type)
1539
+ ].join('')
1540
+ end
1541
+
1542
+ def format_data_types_string(node)
1543
+ [
1544
+ 'STRING',
1545
+ *if node.width
1546
+ [
1547
+ ' ',
1548
+ '(',
1549
+ format(node.width),
1550
+ ')'
1551
+ ].join('')
1552
+ end,
1553
+ *if node.fixed
1554
+ [
1555
+ ' ',
1556
+ 'FIXED'
1557
+ ].join('')
1558
+ end
1559
+ ].join('')
1560
+ end
1561
+
1562
+ def indent(str)
1563
+ str.split("\n").map{|x| "#{INDENT}#{x}"}.join("\n")
1564
+ end
1565
+
1566
+ def format_remark(node, remark)
1567
+ if remark.include?("\n")
1568
+ [
1569
+ [
1570
+ '(*',
1571
+ '"',
1572
+ node.path || node.id,
1573
+ '"',
1574
+ ].join(''),
1575
+ remark,
1576
+ '*)'
1577
+ ].join("\n")
1578
+ else
1579
+ [
1580
+ '--',
1581
+ '"',
1582
+ node.path || node.id,
1583
+ '"',
1584
+ ' ',
1585
+ remark
1586
+ ].join('')
1587
+ end
1588
+ end
1589
+
1590
+ def format_remarks(node)
1591
+ if node.class.method_defined? :remarks
1592
+ node.remarks.map do |remark|
1593
+ format_remark(node, remark)
1594
+ end
1595
+ else
1596
+ []
1597
+ end
1598
+ end
1599
+
1600
+ def format_scope_remarks(node)
1601
+ [
1602
+ *format_remarks(node),
1603
+ *node.children.select{|x| !x.is_a? Model::DataTypes::EnumerationItem or node.is_a? Model::Declarations::Type}.flat_map{|x| format_scope_remarks(x)}
1604
+ ]
1605
+ end
1606
+ end
1607
+ end
1608
+ end