lutaml / expressir

Ruby parser for the ISO EXPRESS language
3 stars 3 forks source link

Ability to include tail remarks tagged to the original position in parse tree and formatter #148

Open ronaldtse opened 6 months ago

ronaldtse commented 6 months ago

This is another issue similar to #64 .

In this case: action_schema.exp:

SCHEMA action_schema '{iso standard 10303 part(41) version(9) object(1) action_schema(1)}';
  REFERENCE FROM basic_attribute_schema (   -- ISO 10303-41
    description_attribute, 
    description_attribute_select,
    get_description_value, 
    get_id_value, 
    get_name_value,
    get_role, 
    id_attribute, 
    id_attribute_select,
    name_attribute,
    name_attribute_select,
    object_role,
    role_select,
    role_association);

  REFERENCE FROM support_resource_schema ( -- ISO 10303-41
    bag_to_set, 
    identifier, 
    label, 
    text);

Notice the lines like -- ISO 10303-41 are after the line's end. These are called tail remarks that start with --. Basically they are like # comments in Ruby.

The Expressir Parser and Formatter does not currently handle them. e.g.:

exp_file = "schemas/resources/action_schema/action_schema.exp"
repository = Expressir::Express::Parser.from_files([exp_file])
formatter  = Class.new(Expressir::Express::Formatter) do
    def format_remark(node, remark)
      [
        [
          '(*',
          '"',
          node.path || node.id,
          '"',
        ].join(''),
        remark,
        '*)'
      ].join("\n")
  end
end

puts formatter.format(repository)

Gives you the pretty printed version, but all "tail remarks" have been stripped off:

SCHEMA action_schema '{iso standard 10303 part(41) version(9) object(1) action_schema(1)}';

REFERENCE FROM basic_attribute_schema
  (description_attribute,
   description_attribute_select,
   get_description_value,
   get_id_value,
   get_name_value,
   get_role,
   id_attribute,
   id_attribute_select,
   name_attribute,
   name_attribute_select,
   object_role,
   role_select,
   role_association);
REFERENCE FROM support_resource_schema
  (bag_to_set,
   identifier,
   label,
   text);

So this task is to parse tail remarks (and put them at the right position) and also show them in Formatter.

There are 2 positions of tail remarks as follows:

Standalone line

SCHEMA mathematical_functions_schema;
-- This file constitutes document WG12 N921
-- Master document: ISO 10303-50:2001
-- EXPRESS last modified: 2001-09-07
FUNCTION assembly_root
  (item: product_definition) : BOOLEAN;
-- extraction of related assembly_component_relationships --
IF (SIZEOF(QUERY(pdr <* USEDIN (item, 
   'PRODUCT_DEFINITION_SCHEMA.PRODUCT_DEFINITION_RELATIONSHIP.' + 
   'RELATED_PRODUCT_DEFINITION') |
    'PRODUCT_STRUCTURE_SCHEMA.ASSEMBLY_COMPONENT_USAGE' IN
    TYPEOF(pdr)))
    = 0) THEN RETURN(TRUE);
ELSE RETURN (FALSE);
END_IF;
END_FUNCTION;
  FUNCTION item_in_context
    (item  : Representation_item;
     cntxt : Representation_context) : BOOLEAN;
    LOCAL
      y : BAG OF Representation_item;
    END_LOCAL;
    -- If there is one or more representation using both the item
    -- and cntxt return true.

Tail end remark

REFERENCE FROM assembly_constraint_schema; -- 10303-109
REFERENCE FROM geometry_schema; -- 10303-42
REFERENCE FROM kinematic_motion_representation_schema; -- 10303-105
REFERENCE FROM kinematic_structure_schema; -- 10303-105
TYPE general_part_terminal_associated_definition_select = EXTENSIBLE SELECT (
    Product_specification, -- added this member in order to align ARM select with DM PartTerminalElementOfSelect
    General_part_terminal, -- hierarchical
    Part_view_definition); -- terminus
END_TYPE;