xslate / p5-Text-Xslate

Scalable template engine for Perl5
https://metacpan.org/release/Text-Xslate
Other
121 stars 47 forks source link

Comment line couldn't be parsed as it's expected #209

Closed bayashi closed 2 years ago

bayashi commented 2 years ago

Fine case.

perl -Iextlib/lib/perl5 -e 'use Text::Xslate; my $tx = Text::Xslate->new(syntax => "Kolon"); print $tx->render_string(":# foo bar");'

Error case

$ perl -Iextlib/lib/perl5 -e 'use Text::Xslate; my $tx = Text::Xslate->new(syntax => "Kolon"); print $tx->render_string(":# foo; bar");'
Text::Xslate: Undefined symbol 'bar' at -e line 1.
 (<string>:1) at -e line 1.
----------------------------------------------------------------------------
:# foo; bar
----------------------------------------------------------------------------

Really corner case though. And I may wrong something...

syohex commented 2 years ago

Could you tell us what result you expect ?

The specification says

Comments start from # to a new line or semicolon.

So ":# foo; bar" means # foo; is comment, other part is not comment and bar is handled as a function.

% xslate -e ':# foo bar' --debug=dump=token
[operator => ;] #1

% xslate -e ':# foo; bar' --debug=dump=token
[operator => ;] #1
[name => bar] #1
[operator => ;] #1
Text::Xslate: Undefined symbol 'bar' at /home/syohei/.plenv/versions/5.34.1/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/Text/Xslate/Runner.pm line 306.
bayashi commented 2 years ago

@syohex Thank you for replying. My expectaion is that it should be assumed whole comment line is # to end of line, i.e "\n", even if there is ;. I didn't think comment would be end at ;. It would be a SPEC, but not helpful to comment out JavaScript code.

It's okay as in-line comment <: # this is comment; $baz # $baz is rendered :>

I prefer to not render $baz as comment line, especially comment out JavaScript code. : # this is comment; $baz

syohex commented 2 years ago

Thanks I see what you mean. However Text::Xslate is no longer maintained actively so I'm not sure that we can apply such a breaking change.

diff --git a/lib/Text/Xslate/Parser.pm b/lib/Text/Xslate/Parser.pm
index d79c540b..e5fea18a 100644
--- a/lib/Text/Xslate/Parser.pm
+++ b/lib/Text/Xslate/Parser.pm
@@ -345,7 +345,9 @@ sub split :method {
         elsif($lex_line_code
                 && (@tokens == 0 || $tokens[-1][1] =~ /\n\z/xms)
                 && s/$lex_line_code//xms) {
-            push @tokens, [ code => $1 ];
+            my $tmp = $1;
+            $tmp =~ s{#.*$}{};
+            push @tokens, [ code => $tmp ];
         }
         elsif(s/$lex_tag_start//xms) {
             $in_tag = 1;
diff --git a/t/030_kolon/019_explicit_interpolate.t b/t/030_kolon/019_explicit_interpolate.t
index 158a2aec..3144ceac 100644
--- a/t/030_kolon/019_explicit_interpolate.t
+++ b/t/030_kolon/019_explicit_interpolate.t
@@ -29,6 +29,7 @@ my @data = (
         :>bar}, "foobar"],
     [q{foo<: # this is a comment
         $lang :>bar}, "fooXslatebar"],
+    [q{:# foo ; this is also a comment}, ""],
 );

 foreach my $pair(@data) {
bayashi commented 2 years ago

This is certainly breaking change😺 It's great to me though. Thank you so much.