RE: [SMARTY-DEV] Smarty compiler
| From: | Lee Coltrane | Date: | Wed, 06 Nov 2002 21:35:03 +0000 |
| Subject: | RE: [SMARTY-DEV] Smarty compiler | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-327@lists.php.net to get a copy of this message | ||
> Since the grammar in smarty is very simple, you can write a basic
> token-based parser in PHP that does what we want, and doesn't _limit_ us
> with what regexps can do. I don't care about the speed at all when
> compiling, but I think we are limited by the way Smarty works now.
+1 for this concept, if we're going with a "real" compiler, let's go this
route...
It accomplishes the stated goals, without introducing the unneeded
complexity of a context-free grammar, a lexical analyzer, and parser
generator.
It also discourages making the smarty grammar (and thus the feature set) too
complex in the future - which, again, I also discourage.
I dare say, something like this would significantly clean up the smarty
compiler code, and allow for much more full-featured compiler plugins. In
addition to setting the stage for introducing simple mathematical operations
into smarty - which I'm also in favor of.
One more thing that pops to mind...if we used this approach, and separated
the tokenizer/parser from the compiler[/optimizer] then we could,
effectively make the entire grammar and syntax of smarty "pluggable".
For example:
------------
Smarty->display('my.tpl');
1) Smarty determines that a compile is needed, so it loads my.tpl,
2) and passes it to SmartyParser, which tokenizes it, and returns the
token tree.
3) Now Smarty dispatches the tree to SmartyCompiler, which returns php
code.
4) Now smarty can write that code out to the compile directory, and call
it.
...If we made it possible to swap out the implementation of
SmartyParser...then imagine the possibilities... If designed correctly,
this would provide a TON of flexibility for people with existing templates
in some other format, and for people who need to seriously enhance the
grammar for some reason...
-Lee
-----Original Message-----
From: Victor [mailto:vboivie@home.se]
Sent: Wednesday, November 06, 2002 3:52 PM
To: smarty-dev@lists.php.net
Subject: Re: [SMARTY-DEV] Smarty compiler
> The overhead of regular expressions isn't such a big issue, it only does
> this at compile time. I see many drawbacks with a compiled solution,
> some already mentioned.
Maybe I've misunderstood it all, but who says you have to write a C-version
of smarty to do this?
Since the grammar in smarty is very simple, you can write a basic
token-based parser in PHP that does what we want, and doesn't _limit_ us
with what regexps can do. I don't care about the speed at all when
compiling, but I think we are limited by the way Smarty works now. Let's say
that you want to add the ability to use:
{$myvar + 5}
in a template. Right now you have to rewrite 5 complex regular expressions
to even allow simple math to not be completely cut off in this expression.
With a real compiler it wouldn't be that difficult at all to add this
feature (theoretically).
> One other would be the ability for everyone to
> contribute plugins/patches, etc. I'm sure there are a lot more php-savvy
> users than there are C-savvy, and we'd be cutting our own throats by
> limiting who can contribute.
Plugins would still be allowed in my way.
I have swept together a smarty-compatible (no, not at all ;) parser written
in PHP that generates a tree from a given source file. I guess I don't have
to tell you that it's far easier to use the tree to generate good PHP-code
compared to now.
Look at http://p22.ryd.student.liu.se/~yweb/pc/
I didn't write it, but one of my codevelopers.
// Victor
(PS, this is somewhat what the code looks like
$components["text"]["container"] = false;
$components["b"]["container"] = true;
$components["link"]["container"] = false;
$components["counter"]["container"] = false;
$data = "{{ Hi, this is \"a string\" and this is {b}something in bold. Not
that {this tag} doesn't get parsed.{/b}. This is a {link
url=\"http://www.google.com\"
title=\"link\"/} escaped in XML syntax (do we
want that? - it's possible anyway!) and this is a {counter cheat=100}
counter";
$parser = new Parser($components);
$tree = $parser->parse($data);
print_r($tree);
)
--
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php