Module: Expressir::Express::Formatters::DeclarationsFormatter
- Included in:
- Expressir::Express::Formatter
- Defined in:
- lib/expressir/express/formatters/declarations_formatter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #format_declarations_attribute(node) ⇒ Object
- #format_declarations_constant(node) ⇒ Object
- #format_declarations_entity(node) ⇒ Object
- #format_declarations_function(node) ⇒ Object
- #format_declarations_informal_proposition_rule(node) ⇒ Object
- #format_declarations_interface(node) ⇒ Object
- #format_declarations_interface_item(node) ⇒ Object
- #format_declarations_parameter(node) ⇒ Object
- #format_declarations_procedure(node) ⇒ Object
- #format_declarations_rule(node) ⇒ Object
- #format_declarations_schema(node) ⇒ Object
- #format_declarations_schema_head(node) ⇒ Object
- #format_declarations_schema_version(node) ⇒ Object
- #format_declarations_subtype_constraint(node) ⇒ Object
- #format_declarations_type(node) ⇒ Object
- #format_declarations_unique_rule(node) ⇒ Object
- #format_declarations_variable(node) ⇒ Object
- #format_declarations_where_rule(node) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 5 def self.included(base) base.register_formatter Model::Declarations::Attribute, :format_declarations_attribute base.register_formatter Model::Declarations::Constant, :format_declarations_constant base.register_formatter Model::Declarations::Entity, :format_declarations_entity base.register_formatter Model::Declarations::Function, :format_declarations_function base.register_formatter Model::Declarations::Interface, :format_declarations_interface base.register_formatter Model::Declarations::InterfaceItem, :format_declarations_interface_item base.register_formatter Model::Declarations::Parameter, :format_declarations_parameter base.register_formatter Model::Declarations::Procedure, :format_declarations_procedure base.register_formatter Model::Declarations::Rule, :format_declarations_rule base.register_formatter Model::Declarations::Schema, :format_declarations_schema base.register_formatter Model::Declarations::SchemaVersion, :format_declarations_schema_version base.register_formatter Model::Declarations::SubtypeConstraint, :format_declarations_subtype_constraint base.register_formatter Model::Declarations::Type, :format_declarations_type base.register_formatter Model::Declarations::UniqueRule, :format_declarations_unique_rule base.register_formatter Model::Declarations::Variable, :format_declarations_variable base.register_formatter Model::Declarations::WhereRule, :format_declarations_where_rule base.register_formatter Model::Declarations::InformalPropositionRule, :format_declarations_informal_proposition_rule base.register_formatter Model::Declarations::DerivedAttribute, :format_declarations_attribute base.register_formatter Model::Declarations::InverseAttribute, :format_declarations_attribute end |
Instance Method Details
#format_declarations_attribute(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 46 def format_declarations_attribute(node) [ *if node.supertype_attribute [ format(node.supertype_attribute), " ", ].join end, *if node.supertype_attribute && node.id != node.supertype_attribute.attribute.id [ "RENAMED", " ", node.id, " ", ].join end, *if node.id && !node.supertype_attribute [ node.id, " ", ].join end, ":", *if node.optional [ " ", "OPTIONAL", ].join end, " ", format(node.type), *if node.kind == Model::Declarations::Attribute::DERIVED [ " ", ":=", " ", format(node.expression), ].join elsif node.kind == Model::Declarations::Attribute::INVERSE [ " ", "FOR", " ", format(node.expression), ].join end, ";", format_inline_tail_remark(node), ].join end |
#format_declarations_constant(node) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 97 def format_declarations_constant(node) [ node.id, " ", ":", " ", format(node.type), " ", ":=", " ", format(node.expression), ";", ].join end |
#format_declarations_entity(node) ⇒ Object
112 113 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 232 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 112 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_const = self.class.const_get(:INDENT) # Only show preamble remarks if entity has explicit attributes preamble = if explicit_attributes&.length&.positive? format_preamble_remarks(node, indent(indent_const)) end [ [ "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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 234 def format_declarations_function(node) indent_char = self.class.const_get(:INDENT_CHAR) [ [ "FUNCTION", " ", node.id, *if node.parameters&.length&.positive? parameter_indent = indent_char * "FUNCTION #{node.id}(".length [ "(", node.parameters.map do |x| format(x) end.join(";\n#{parameter_indent}"), ")", ].join end, " ", ":", " ", format(node.return_type), ";", ].join, *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(node.constants.map { |x| format(x) }.join("\n")), [ "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_FUNCTION", ";", format_end_scope_remark(node), ].join, ].join("\n") end |
#format_declarations_informal_proposition_rule(node) ⇒ Object
716 717 718 719 720 721 722 723 724 725 726 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 716 def format_declarations_informal_proposition_rule(node) [ *if node.id [ node.id, ":", " ", ].join end, ].join end |
#format_declarations_interface(node) ⇒ Object
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/formatters/declarations_formatter.rb', line 303 def format_declarations_interface(node) indent_char = self.class.const_get(:INDENT_CHAR) [ case node.kind when Model::Declarations::Interface::USE then "USE" when Model::Declarations::Interface::REFERENCE then "REFERENCE" end, " ", "FROM", " ", format(node.schema), *if node.items&.length&.positive? item_indent = indent_char * "(".length [ "\n", indent([ "(", node.items.map { |x| format(x) }.join(",\n#{item_indent}"), ")", ].join), ].join end, ";", ].join end |
#format_declarations_interface_item(node) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 329 def format_declarations_interface_item(node) [ format(node.ref), *if node.id [ " ", "AS", " ", node.id, ] end, ].join end |
#format_declarations_parameter(node) ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 343 def format_declarations_parameter(node) [ *if node.var [ "VAR", " ", ].join end, node.id, " ", ":", " ", format(node.type), ].join end |
#format_declarations_procedure(node) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 359 def format_declarations_procedure(node) indent_char = self.class.const_get(:INDENT_CHAR) [ [ "PROCEDURE", " ", node.id, *if node.parameters&.length&.positive? parameter_indent = indent_char * "PROCEDURE #{node.id}(".length [ "(", node.parameters.map do |x| format(x) end.join(";\n#{parameter_indent}"), ")", ].join end, ";", ].join, *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(node.constants.map { |x| format(x) }.join("\n")), [ "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_PROCEDURE", ";", format_end_scope_remark(node), ].join, ].join("\n") end |
#format_declarations_rule(node) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 424 def format_declarations_rule(node) # Filter out statements that only exist to hold remarks in rules # (ALIAS/REPEAT with only Null sub-statements, query assignments, or Null statements) formatted_statements = [] if node.statements&.length&.positive? formatted_statements = node.statements.reject do |stmt| # Filter ALIAS/REPEAT with only Null statements if stmt.is_a?(Model::Statements::Alias) || stmt.is_a?(Model::Statements::Repeat) stmt.statements&.all?(Model::Statements::Null) # Filter query assignments (assignments with QueryExpression that exist only for remarks) elsif stmt.is_a?(Model::Statements::Assignment) && stmt.expression.is_a?(Model::Expressions::QueryExpression) true # Filter Null statements elsif stmt.is_a?(Model::Statements::Null) true else false end end end [ [ "RULE", " ", node.id, " ", "FOR", " ", "(", Array(node.applies_to).map { |x| format(x) }.join(", "), ")", ";", ].join, *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(node.constants.map { |x| format(x) }.join("\n")), [ "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 formatted_statements.length.positive? indent(formatted_statements.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_RULE", ";", format_end_scope_remark(node), ].join, ].join("\n") end |
#format_declarations_schema(node) ⇒ Object
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 534 def format_declarations_schema(node) indent_const = self.class.const_get(:INDENT) preamble = format_preamble_remarks(node, indent(indent_const)) schema_declarations = [ *if node.constants&.length&.positive? [ "CONSTANT", indent(node.constants.map { |x| format(x) }.join("\n")), [ "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", ";", ].join, ].join("\n") end |
#format_declarations_schema_head(node) ⇒ Object
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 511 def format_declarations_schema_head(node) [ [ "SCHEMA", " ", node.id, *if node.version [ " ", format(node.version), ].join end, ";", ].join, *if node.interfaces&.length&.positive? [ "", node.interfaces.map { |x| format(x) }.join("\n"), ] end, ].join("\n") end |
#format_declarations_schema_version(node) ⇒ Object
592 593 594 595 596 597 598 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 592 def format_declarations_schema_version(node) [ "'", node.value, "'", ].join end |
#format_declarations_subtype_constraint(node) ⇒ Object
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 600 def format_declarations_subtype_constraint(node) [ [ "SUBTYPE_CONSTRAINT", " ", node.id, " ", "FOR", " ", format(node.applies_to), ";", ].join, *if node.abstract indent([ "ABSTRACT", " ", "SUPERTYPE", ";", ].join) end, *if node.total_over&.length&.positive? indent([ "TOTAL_OVER", "(", node.total_over.map { |x| format(x) }.join(", "), ")", ";", ].join) end, *if node.supertype_expression indent([ format(node.supertype_expression), ";", ].join) end, [ "END_SUBTYPE_CONSTRAINT", ";", format_end_scope_remark(node), ].join, ].join("\n") end |
#format_declarations_type(node) ⇒ Object
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 643 def format_declarations_type(node) [ [ "TYPE", " ", node.id, " ", "=", " ", format(node.), ";", ].join, *if node.where_rules&.length&.positive? [ "WHERE", indent(node.where_rules.map { |x| format(x) }.join("\n")), ] end, [ "END_TYPE", ";", format_end_scope_remark(node), ].join, ].join("\n") end |
#format_declarations_unique_rule(node) ⇒ Object
669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 669 def format_declarations_unique_rule(node) [ *if node.id [ node.id, ":", " ", ].join end, Array(node.attributes).map { |x| format(x) }.join(", "), ";", ].join end |
#format_declarations_variable(node) ⇒ Object
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 683 def format_declarations_variable(node) [ node.id, " ", ":", " ", format(node.type), *if node.expression [ " ", ":=", " ", format(node.expression), ].join end, ";", ].join end |
#format_declarations_where_rule(node) ⇒ Object
702 703 704 705 706 707 708 709 710 711 712 713 714 |
# File 'lib/expressir/express/formatters/declarations_formatter.rb', line 702 def format_declarations_where_rule(node) [ *if node.id [ node.id, ":", " ", ].join end, format(node.expression), ";", ].join end |