[Templates] TT2 syntax checker or lint?

Mark Mills mark@xodiax.com
Wed, 6 Mar 2002 16:04:35 -0500


You are oversimplifying the problem.

I don't know if you have noticed it but perl can't tell where you failed =
to nest {} braces right.

All the block tags in TT end with [% END %] and they all allow =
nesting...=20

[% BLOCK a %]blah
?? blah
[% FOREACH a=3Db %]blah
?? blah
[% FOREACH c=3Dd %]blah
?? blah
[% END %] blah
?? blah
[% END % ] blah
??=20

Which ?? is the missing END tag?  It is IMPOSSIBLE to tell from either =
context or syntax in a programmatic way. Only something that understands =
the situational code logic can even hope to spot it. All lint can do is =
tell you it is unbalanced. If it finds an extra closing it can complain =
at the point it finds it, but it can't be sure about one missing till it =
hits the end of file...

--mark

> -----Original Message-----
> From: Matthew Pressly [mailto:mpressly@claborn.net]
> Sent: Wednesday, March 06, 2002 3:53 PM
> To: Perrin Harkins
> Cc: templates@template-toolkit.org
> Subject: Re: [Templates] TT2 syntax checker or lint?
>=20
>=20
> 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=20
> though.  I wonder if there's a way to just re-purpose the=20
> parser somehow, or if this would need to be a separate one. =20
> It could be much slower and simpler than the real TT parser,=20
> if it's just looking for stuff like unclosed IF tags.
> >
> >- Perrin
>=20
>=20
> I could see reporting all errors being difficult, but I think=20
> adding balanced tag checking would not be too difficult. =20
>=20
> I have not looked at all how the parsing is implemented for=20
> TT2, but I think even a separate perl script that reads the=20
> templates and checks for and reports unbalanced tags would=20
> not be too difficult.  This is the case that I seem to spend=20
> the most time debugging.  Even if the template parser was not=20
> of any help in writing a ttlint, I was thinking it might be=20
> possible to just convert the template into some form of=20
> overly simplistic perl (basically perl for the control flow=20
> and print statements or strings for everything else), then=20
> just run it through perl -w -c and use perl's built in error=20
> checking.  Use strict would probably have to be turned off to=20
> avoid having to predeclare everything.  Comments could be=20
> used to associate line numbers in perl with line numbers in=20
> TT2.  I realize that TT2's parser in effect already does this=20
> and much more, but in some sense, the ttlint might need to be=20
> a little looser so that it could process the whole file into=20
> perl without quitting, and it could afford to be looser=20
> because it is doing only a static validation.  Some things=20
> would be fatal to this type of lint, like [% without %], but=20
> that could be checked and reported explicitly first in a=20
> preprocessing step. Maybe I am oversimplifying the problem?
>=20
> I.e. something like:
> [% FOREACH item =3D list %]
>    <input name=3D"[% item.name %]" ...>
> [% END %]
>=20
> could be turned into something like:
> # line 1
> foreach my $item (@list) {
>   print q[<input name=3D"[% item.name %]">];
> #  line 3
> }
>=20
> so that if the [% END %] was missing, the } would be missing in perl.
>=20
>=20
> Matthew Pressly
>=20
>=20
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://www.template-toolkit.org/mailman/listinfo/templates
>=20