AdaCore / templates-parser

AWS templates engine.
Other
7 stars 17 forks source link

@@IF@@ sections add a newline #3

Closed maxime-esa closed 6 years ago

maxime-esa commented 6 years ago

When using @@IF@@ sections, newlines are added unconditionally. For example I have a template file named signature.tmplt containing:

@@IF@@ $_EXIST:Params_@
(@_Params_@);
@@ELSE@@
;
@@END_IF@@

Say I render it in a string that I want to use in another template with the tagname @_Signature_@ which is like this:

procedure Hello @_Signature_@ -- This is a comment.

This will not be possible because @_Signature_@ will contain a newline (at the beginning or at the end). The workaround I found was to use a @_REPLACE_ALL((\n)/):Signature_@ filter but that makes the template unnecessarily complex - in addition to many other filters I use. (example line 32 of my template here: https://gitrepos.estec.esa.int/taste/aadl-parser/blob/master/templates/skeletons/ada/header.tmplt )

I would suggest to remove the newlines by default (since they are always easy to add by the user of the result of the IF section).

Thanks!

TurboGit commented 6 years ago

I think that you are looking for the @@INLINE@@ statement, see: http://docs.adacore.com/aws-docs/templates_parser/template_statements.html#inline-statement

Let me know if this works on your side. Thanks.

maxime-esa commented 6 years ago

Thanks for your quick answer. That does not seem to work however. When the inline is in a separate template file, it seems to still add a newline at the end when processing this template.

TurboGit commented 6 years ago

Not sure, can you show a small reproducer? How @Signature@ is constructed? What do you mean by another template? Is that included with a @@INCLUDE@@? Thanks.

maxime-esa commented 6 years ago

Here is a simple application that reproduces it.

File signature.tmplt:

@@INLINE@@
@@IF@@ @_EXIST:Parameters_@
@_CAPITALIZE:Name_@ (@_Parameters_@)
@@ELSE@@
@_CAPITALIZE:Name_@
@@END_IF@@
@@END_INLINE@@

File header.tmplt:

@@INLINE@@
@@TABLE@@
   procedure @_Provided_Interfaces_@ is
      This is an example: @_Provided_Interfaces_@ THIS SHOULD BE ON THE SAME LINE
@@END_TABLE@@
@@END_INLINE@@

File parser.adb:

with Templates_Parser; use Templates_Parser;
with Text_IO;          use Text_IO;

procedure Parser is
    Params : Tag;
    Set    : Translate_Set;
begin
    Params := Params & "Param1" & "Param2";
    Set    := +Assoc ("Parameters", Params) & Assoc ("Name", "Name1");
    declare
        Result1 : constant String := Parse ("signature.tmplt", Set);
        Interfaces : Tag;
        Set2 : Translate_Set;
    begin
        Interfaces := Interfaces & Result1;
        Set2 := +Assoc ("Provided_Interfaces", Interfaces);
        Put_Line (Parse ("header.tmplt", Set2));
    end;
end Parser;

When I run it:

$ ./parser 
procedure Name1 (Param1, Param2)
 isThis is an example: Name1 (Param1, Param2)
 THIS SHOULD BE ON THE SAME LINE

Do you see any obvious mistake (or misuse of the inline) ? Thanks!

TurboGit commented 6 years ago

I see, but signature.tmplt contains indeed a single line and a line does terminate with a new-line char. So at the end Resuls1 is a string with an ending new-line. That's not possible to change that, it is the semantic.

The solution above it to remove the new-line from the Result1 string before adding it into the Interfaces vector tag.

maxime-esa commented 6 years ago

Then perhaps it would be useful to add a default filter named "STRIP", that removes any spaces/newlines at the begining and end of a string (similar to Python's str.strip())? That would make cleaner templates. I will add it as a custom filter in my code, in the meantime. Thanks again for your answers.

TurboGit commented 6 years ago

Agreed, TRIM only remove spaces, STRIP may be added to remove spaces and all non printable characters.

TurboGit commented 6 years ago

A TRIM filter is now available.

maxime-esa commented 6 years ago

You mean STRIP :-) Thanks!

TurboGit commented 6 years ago

Right :)