[Templates] TT2 syntax checker or lint?

Matthew Pressly mpressly@claborn.net
Wed, 06 Mar 2002 14:53:16 -0600


At 03:22 PM 3/6/2002 -0500, you wrote:
>>This is an *incredibly* hard thing to do.
>
>True.
>
>The original idea of having a "ttlint" is a really cool one though.  I=
 wonder if there's a way to just re-purpose the parser somehow, or if this=
 would need to be a separate one.  It could be much slower and simpler than=
 the real TT parser, if it's just looking for stuff like unclosed IF tags.
>
>- Perrin


I could see reporting all errors being difficult, but I think adding=
 balanced tag checking would not be too difficult. =20

I have not looked at all how the parsing is implemented for TT2, but I think=
 even a separate perl script that reads the templates and checks for and=
 reports unbalanced tags would not be too difficult.  This is the case that=
 I seem to spend the most time debugging.  Even if the template parser was=
 not of any help in writing a ttlint, I was thinking it might be possible to=
 just convert the template into some form of overly simplistic perl=
 (basically perl for the control flow and print statements or strings for=
 everything else), then just run it through perl -w -c and use perl's built=
 in error checking.  Use strict would probably have to be turned off to=
 avoid having to predeclare everything.  Comments could be used to associate=
 line numbers in perl with line numbers in TT2.  I realize that TT2's parser=
 in effect already does this and much more, but in some sense, the ttlint=
 might need to be a little looser so that it could process the whole file=
 into perl without quitting, and it could afford to be looser because it is=
 doing only a static validation.  Some things would be fatal to this type of=
 lint, like [% without %], but that could be checked and reported explicitly=
 first in a preprocessing step. Maybe I am oversimplifying the problem?

I.e. something like:
[% FOREACH item =3D list %]
   <input name=3D"[% item.name %]" ...>
[% END %]

could be turned into something like:
# line 1
foreach my $item (@list) {
  print q[<input name=3D"[% item.name %]">];
#  line 3
}

so that if the [% END %] was missing, the } would be missing in perl.


Matthew Pressly