Class: Expressir::Express::PrettyFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/expressir/express/pretty_formatter.rb

Overview

Pretty formatter for EXPRESS schemas following ELF specification

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Attributes inherited from Formatter

#no_remarks

Instance Method Summary collapse

Methods inherited from Formatter

format, #format, #format_noop, format_registry, register_formatter

Methods included from Formatters::DeclarationsFormatter

#format_declarations_attribute, #format_declarations_constant, #format_declarations_informal_proposition_rule, #format_declarations_interface, #format_declarations_interface_item, #format_declarations_parameter, #format_declarations_schema_head, #format_declarations_schema_version, #format_declarations_subtype_constraint, #format_declarations_type, #format_declarations_unique_rule, #format_declarations_variable, #format_declarations_where_rule, included

Methods included from Formatters::DataTypesFormatter

#format_data_types_aggregate, #format_data_types_array, #format_data_types_bag, #format_data_types_binary, #format_data_types_boolean, #format_data_types_enumeration, #format_data_types_enumeration_item, #format_data_types_generic, #format_data_types_generic_entity, #format_data_types_integer, #format_data_types_list, #format_data_types_logical, #format_data_types_number, #format_data_types_real, #format_data_types_select, #format_data_types_set, #format_data_types_string, included

Methods included from Formatters::ExpressionsFormatter

#format_expressions_aggregate_initializer, #format_expressions_aggregate_initializer_item, #format_expressions_binary_expression, #format_expressions_entity_constructor, #format_expressions_function_call, #format_expressions_interval, #format_expressions_query_expression, #format_expressions_unary_expression, included

Methods included from Formatters::StatementsFormatter

#format_statements_alias, #format_statements_assignment, #format_statements_case, #format_statements_case_action, #format_statements_compound, #format_statements_escape, #format_statements_if, #format_statements_null, #format_statements_procedure_call, #format_statements_repeat, #format_statements_return, #format_statements_skip, included

Methods included from Formatters::SupertypeExpressionsFormatter

#format_supertype_expressions_binary_supertype_expression, #format_supertype_expressions_oneof_supertype_expression, included

Methods included from Formatters::ReferencesFormatter

#format_references_attribute_reference, #format_references_group_reference, #format_references_index_reference, #format_references_simple_reference, included

Methods included from Formatters::LiteralsFormatter

#format_literals_binary, #format_literals_integer, #format_literals_logical, #format_literals_real, #format_literals_string, included

Methods included from Formatters::RemarkFormatter

#format_end_scope_remark, #format_preamble_remarks, #format_remark, #format_remarks, #format_untagged_remark

Methods included from Formatters::RemarkItemFormatter

#format_remark_item, included

Constructor Details

#initialize(options = {}) ⇒ PrettyFormatter

Initialize PrettyFormatter with options

Parameters:

  • options (Hash) (defaults to: {})

    Formatting options

Options Hash (options):

  • :indent (Integer)

    Number of spaces per indentation level (default: 4)

  • :line_length (Integer, nil)

    Maximum line length (default: nil)

  • :insert_newline (Boolean)

    Insert newlines for readability (default: true)

  • :insert_space (Boolean)

    Insert spaces for readability (default: true)

  • :provenance (Boolean)

    Include provenance information (default: true)

  • :provenance_name (String)

    Tool name for provenance (default: “Expressir”)

  • :provenance_version (String)

    Tool version for provenance (default: Expressir::VERSION)

  • :no_remarks (Boolean)

    Suppress remarks in output (default: false)



72
73
74
75
# File 'lib/expressir/express/pretty_formatter.rb', line 72

def initialize(options = {})
  @config = Configuration.new(options)
  super(no_remarks: options.fetch(:no_remarks, false))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



60
61
62
# File 'lib/expressir/express/pretty_formatter.rb', line 60

def config
  @config
end

Instance Method Details

#format_constant_block(constants) ⇒ String

Format a block of constants with aligned colons and assignments

Parameters:

Returns:

  • (String)

    Formatted constants with alignment



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/expressir/express/pretty_formatter.rb', line 355

def format_constant_block(constants)
  return "" if constants.nil? || constants.empty?

  # Calculate alignment positions
  max_name_len = constants.map { |c| c.id.length }.max
  type_strings = constants.map { |c| format(c.type) }
  max_type_len = type_strings.map(&:length).max

  # Format each constant with alignment
  formatted = constants.zip(type_strings).map do |constant, type_str|
    name_padding = " " * (max_name_len - constant.id.length)
    type_padding = " " * (max_type_len - type_str.length)

    "#{constant.id}#{name_padding} : #{type_str}#{type_padding} := #{format(constant.expression)};"
  end

  formatted.join("\n")
end

#format_declarations_entity(node) ⇒ String

Override to preserve tail remarks on attributes

Parameters:

Returns:

  • (String)

    Formatted entity



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/expressir/express/pretty_formatter.rb', line 114

def format_declarations_entity(node)
  if node.attributes.nil?
    explicit_attributes = []
    derived_attributes = []
    inverse_attributes = []
  else
    explicit_attributes = node.attributes.select do |x|
      x.kind == Model::Declarations::Attribute::EXPLICIT
    end
    derived_attributes = node.attributes.select do |x|
      x.kind == Model::Declarations::Attribute::DERIVED
    end
    inverse_attributes = node.attributes.select do |x|
      x.kind == Model::Declarations::Attribute::INVERSE
    end
  end

  indent_str = " " * @config.indent
  preamble = format_preamble_remarks(node, indent_str)

  [
    [
      "ENTITY",
      " ",
      node.id,
      *if node.abstract && !node.supertype_expression
         [
           "\n",
           indent([
             "ABSTRACT",
             " ",
             "SUPERTYPE",
           ].join),
         ].join
       end,
      *if node.abstract && node.supertype_expression
         [
           "\n",
           indent([
             "ABSTRACT",
             " ",
             "SUPERTYPE",
             " ",
             "OF",
             " ",
             "(",
             format(node.supertype_expression),
             ")",
           ].join),
         ].join
       end,
      *if !node.abstract && node.supertype_expression
         [
           "\n",
           indent([
             "SUPERTYPE",
             " ",
             "OF",
             " ",
             "(",
             format(node.supertype_expression),
             ")",
           ].join),
         ].join
       end,
      *if node.subtype_of&.length&.positive?
         [
           "\n",
           indent([
             "SUBTYPE",
             " ",
             "OF",
             " ",
             "(",
             node.subtype_of.map { |x| format(x) }.join(", "),
             ")",
           ].join),
         ].join
       end,
      ";",
    ].join,
    *if preamble && !preamble.empty?
       preamble.rstrip
     end,
    *if explicit_attributes&.length&.positive?
       indent(explicit_attributes.map { |x| format(x) }.join("\n"))
     end,
    *if derived_attributes&.length&.positive?
       [
         "DERIVE",
         indent(derived_attributes.map { |x| format(x) }.join("\n")),
       ]
     end,
    *if inverse_attributes&.length&.positive?
       [
         "INVERSE",
         indent(inverse_attributes.map { |x| format(x) }.join("\n")),
       ]
     end,
    *if node.unique_rules&.length&.positive?
       [
         "UNIQUE",
         indent(node.unique_rules.map { |x| format(x) }.join("\n")),
       ]
     end,
    *if node.where_rules&.length&.positive?
       [
         "WHERE",
         indent(node.where_rules.map { |x| format(x) }.join("\n")),
       ]
     end,
    [
      "END_ENTITY",
      ";",
      format_end_scope_remark(node),
    ].join,
  ].join("\n")
end

#format_declarations_function(node) ⇒ Object

Override format_declarations_function to use aligned constants



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/expressir/express/pretty_formatter.rb', line 435

def format_declarations_function(node)
  [
    [
      "FUNCTION",
      " ",
      node.id,
      *if node.parameters&.length&.positive?
         parameter_indent = " " * "FUNCTION #{node.id}(".length
         [
           "(",
           node.parameters.map do |x|
             format(x)
           end.join(";\n#{parameter_indent}"),
           ")",
         ].join
       end,
      " ",
      ":",
      " ",
      format(node.return_type),
      ";",
    ].join,
    *format_scope_body(node),
    format_scope_footer("END_FUNCTION", node),
  ].join("\n")
end

#format_declarations_procedure(node) ⇒ Object

Override format_declarations_procedure to use aligned constants



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/expressir/express/pretty_formatter.rb', line 463

def format_declarations_procedure(node)
  [
    [
      "PROCEDURE",
      " ",
      node.id,
      *if node.parameters&.length&.positive?
         parameter_indent = " " * "PROCEDURE #{node.id}(".length
         [
           "(",
           node.parameters.map do |x|
             format(x)
           end.join(";\n#{parameter_indent}"),
           ")",
         ].join
       end,
      ";",
    ].join,
    *format_scope_body(node),
    format_scope_footer("END_PROCEDURE", node),
  ].join("\n")
end

#format_declarations_rule(node) ⇒ Object

Override format_declarations_rule to use aligned constants



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/expressir/express/pretty_formatter.rb', line 487

def format_declarations_rule(node)
  [
    [
      "RULE",
      " ",
      node.id,
      " ",
      "FOR",
      " ",
      "(",
      Array(node.applies_to).map { |x| format(x) }.join(", "),
      ")",
      ";",
    ].join,
    *format_scope_body(node),
    *if node.where_rules&.length&.positive?
       [
         "WHERE",
         indent(node.where_rules.map { |x| format(x) }.join("\n")),
       ]
     end,
    format_scope_footer("END_RULE", node),
  ].join("\n")
end

#format_declarations_schema(node) ⇒ String

Override format_declarations_schema to use aligned constants

Parameters:

Returns:

  • (String)

    Formatted schema



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/expressir/express/pretty_formatter.rb', line 377

def format_declarations_schema(node)
  indent_str = " " * @config.indent
  preamble = format_preamble_remarks(node, indent_str)
  schema_declarations = [
    *if node.constants&.length&.positive?
       [
         "CONSTANT",
         indent(format_constant_block(node.constants)),
         [
           "END_CONSTANT",
           ";",
         ].join,
       ].join("\n")
     end,
    *if node.types&.length&.positive?
       node.types.map { |x| format(x) }
     end,
    *if node.entities&.length&.positive?
       node.entities.map { |x| format(x) }
     end,
    *if node.subtype_constraints&.length&.positive?
       node.subtype_constraints.map { |x| format(x) }
     end,
    *if node.functions&.length&.positive?
       node.functions.map { |x| format(x) }
     end,
    *if node.rules&.length&.positive?
       node.rules.map { |x| format(x) }
     end,
    *if node.procedures&.length&.positive?
       node.procedures.map { |x| format(x) }
     end,
  ]

  [
    format_declarations_schema_head(node),
    *if preamble && !preamble.empty?
       [
         "",
         preamble.rstrip,
       ]
     end,
    *if schema_declarations&.length&.positive?
       [
         "",
         schema_declarations.join("\n\n"),
         "",
       ]
     end,
    [
      "END_SCHEMA",
      ";",
      format_end_scope_remark(node),
    ].join,
  ].join("\n")
end

#format_exp_file(node) ⇒ String

Format ExpFile with file-level preamble

Parameters:

Returns:



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/expressir/express/pretty_formatter.rb', line 332

def format_exp_file(node)
  result = []

  # Add file-level preamble if present
  if node.untagged_remarks && !node.untagged_remarks.empty?
    result.concat(format_preamble(node.untagged_remarks))
  end

  # Add provenance (only for first file or single file)
  provenance = format_provenance
  result << provenance if provenance
  result << "" if provenance

  # Add schemas
  schemas = node.schemas&.map { |x| format(x) }&.join("\n\n")
  result << schemas if schemas

  result.empty? ? "" : "#{result.join("\n")}\n"
end

#format_inline_tail_remark(node) ⇒ String

Format inline tail remark

Parameters:

Returns:

  • (String)

    Tail remark or empty string



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/expressir/express/pretty_formatter.rb', line 90

def format_inline_tail_remark(node)
  return "" if @no_remarks
  return "" unless node.is_a?(Model::ModelElement)
  return "" if node.untagged_remarks.nil? || node.untagged_remarks.empty?

  # Get first untagged remark
  remark = node.untagged_remarks.first
  return "" if remark.nil?

  return "" unless remark.is_a?(Model::RemarkInfo)

  text = remark.text
  return "" if text.nil? || text.empty?

  # Include tag if present
  formatted_text = remark.tagged? ? "\"#{remark.tag}\" #{text}" : text

  # Use format from RemarkInfo
  remark.tail? ? " -- #{formatted_text}" : " (* #{formatted_text} *)"
end

#format_preamble(source_remarks) ⇒ Array<String>

Format preamble remarks before SCHEMA

Parameters:

  • source_remarks (Array<String>)

    Source remarks

Returns:

  • (Array<String>)

    Formatted preamble lines



236
237
238
239
240
241
242
243
244
# File 'lib/expressir/express/pretty_formatter.rb', line 236

def format_preamble(source_remarks)
  return [] if source_remarks.nil? || source_remarks.empty?

  preamble = source_remarks.map do |remark|
    format_preamble_remark(remark)
  end
  preamble << "" # Blank line after preamble
  preamble
end

#format_preamble_remark(remark, _indent_str = "") ⇒ String

Format a single preamble remark

Parameters:

  • remark (RemarkInfo)

    RemarkInfo object

  • indent_str (String)

    Indentation to use (optional)

Returns:

  • (String)

    Formatted remark



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/expressir/express/pretty_formatter.rb', line 250

def format_preamble_remark(remark, _indent_str = "")
  return "" unless remark.is_a?(Model::RemarkInfo)

  text = remark.text
  return "" if text.nil? || text.empty?

  # Include tag if present
  text = "\"#{remark.tag}\" #{text}" if remark.tagged?

  # Use format from RemarkInfo
  if remark.tail?
    "-- #{text}"
  elsif text.include?("\n")
    # Embedded remark - always use embedded format for preamble
    ["(*", text,
     "*)"].join("\n")
  else
    "(* #{text} *)"
  end
end

#format_provenanceString?

Generate provenance remark

Returns:

  • (String, nil)

    Provenance comment or nil if disabled



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/expressir/express/pretty_formatter.rb', line 273

def format_provenance
  return nil unless @config.provenance_enabled

  params = []
  params << "indent: #{@config.indent}"
  params << "line_length: #{@config.line_length}" if @config.line_length

  [
    "(*",
    "  Generated by: #{@config.provenance_name} version #{@config.provenance_version}",
    "  Format parameters: #{params.join(', ')}",
    "*)",
  ].join("\n")
end

#format_repository(node) ⇒ String

Override to add preamble and provenance

Parameters:

Returns:

  • (String)

    Formatted repository



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/expressir/express/pretty_formatter.rb', line 291

def format_repository(node)
  result = []

  # Format files if present
  node.files&.each do |exp_file|
    file_output = format_exp_file(exp_file)
    result << file_output if file_output && !file_output.empty?
  end

  # Handle schemas directly added to repository (not via files)
  direct_schemas = node.schemas.select do |s|
    # Schema is direct if it's not in any file
    node.files.nil? || node.files.none? { |f| f.schemas&.include?(s) }
  end

  if direct_schemas.any?
    # Add preamble if repository has untagged remarks
    if node.untagged_remarks && !node.untagged_remarks.empty?
      result.concat(format_preamble(node.untagged_remarks))
    end

    # Add provenance
    provenance = format_provenance
    result << provenance if provenance
    result << "" if provenance

    # Add schemas
    schemas_output = direct_schemas.map { |x| format(x) }.join("\n\n")
    result << schemas_output if schemas_output
  elsif result.empty?
    # Empty repository - just add provenance
    provenance = format_provenance
    result << provenance if provenance
  end

  result.any? ? "#{result.join("\n")}\n" : ""
end

#format_scope_body(node) ⇒ Object

Shared scope body for function, procedure, and rule declarations. These declaration types share the same internal structure: types, entities, subtype_constraints, functions, procedures, constants, variables, statements.



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/expressir/express/pretty_formatter.rb', line 516

def format_scope_body(node)
  [
    *if node.types&.length&.positive?
       indent(node.types.map { |x| format(x) }.join("\n"))
     end,
    *if node.entities&.length&.positive?
       indent(node.entities.map { |x| format(x) }.join("\n"))
     end,
    *if node.subtype_constraints&.length&.positive?
       indent(node.subtype_constraints.map { |x| format(x) }.join("\n"))
     end,
    *if node.functions&.length&.positive?
       indent(node.functions.map { |x| format(x) }.join("\n"))
     end,
    *if node.procedures&.length&.positive?
       indent(node.procedures.map { |x| format(x) }.join("\n"))
     end,
    *if node.constants&.length&.positive?
       indent([
         "CONSTANT",
         indent(format_constant_block(node.constants)),
         [
           "END_CONSTANT",
           ";",
         ].join,
       ].join("\n"))
     end,
    *if node.variables&.length&.positive?
       indent([
         "LOCAL",
         indent(node.variables.map { |x| format(x) }.join("\n")),
         [
           "END_LOCAL",
           ";",
         ].join,
       ].join("\n"))
     end,
    *if node.statements&.length&.positive?
       indent(node.statements.map { |x| format(x) }.join("\n"))
     end,
  ]
end


559
560
561
562
563
564
565
# File 'lib/expressir/express/pretty_formatter.rb', line 559

def format_scope_footer(keyword, node)
  [
    keyword,
    ";",
    format_end_scope_remark(node),
  ].join
end

#indent(str) ⇒ String

Override indent to use configured width

Parameters:

  • str (String)

    String to indent

Returns:

  • (String)

    Indented string



80
81
82
83
84
85
# File 'lib/expressir/express/pretty_formatter.rb', line 80

def indent(str)
  return if str.nil?

  indent_str = " " * @config.indent
  str.split("\n").map { |x| "#{indent_str}#{x}" }.join("\n")
end