Open gnugat opened 8 years ago
Here's a first draft:
%placeholder%
or alternatively :placeholder
placeholder?
, if it is falsey then replace it with an empty string (and remove the next whitespace?)So for example:
<?php
namespace_statement?
use_statements?
abstractness? finalness? class :name extend? inherit?
{
constant_statements?
property_statements?
method_statements?
}
In the above example, we have some whitespace issues:
Second draft without alternative syntax, and optional placeholders use a similar syntax to the required ones:
%placeholder%
?optional placeholder?
PhpSpec uses templates without optional placeholders, so it should work perfectly:
public static function %methodName%(%arguments%)
{
%returnVar% = new %className%(%constructorArguments%);
// TODO: write logic here
return %returnVar%;
}
Pretty Printer should rely on templates that mirror PHP Parser's nodes:
<?php
namesapce %name%;
?Stmt_Uses?
?Stmt_Interfaces?
?Stmt_Classes?
?Stmt_Traits?
With deeper templates:
interface %name% ?extends?
{
?Stmt_Constants?
?Stmt_Functions?
}
But once again optional placeholders are problematic regarding surrounding whitespaces when they shouldn't print anything:
Stmt_Uses
, then we expect it to be removed with its two new lines charactersextends
, then we expect the previous space to be removedStmt_Constants
, then we expect its indentation to be removed as well as its two new lines charactersThird draft adding up a collection placeholder:
+collection placeholder+
Collection placeholders should received an array:
So for example the Stmt_Interface
template would look like:
interface %name% ?extends?
{
+Stmt_Constant+
+Stmt_Function+
}
And Stmt_Constant
:
const %name% = %value%;
If there are no Stmt_Constant
, interface should look like:
interface FindLatestLocation
{
public function find() : array;
}
If there's one constant:
interface FindLatestLocation
{
const MY_CONSTANT = 42;
public function find() : array;
}
If there's more than one constants:
interface FindLatestLocation
{
const MY_CONSTANT = 42;
public function find() : array;
}
Simpla Templating Language
We need a simple templating language that allows us to describe how the code generated from the AST should look like.
Why not Twig?
PHP-Printer will define an interface for the Templating Engine, allowing us to implement an adapter for any existing third party Templating Engine. In Memio, we've done the same thing and provided by default a Twig implementation, since it is the most popular Templating Engine. However this choice made us miss the opportunity to work with Puli as they were reluctant to pull Twig dependency in all projects that'd use it, and it made phpspec core template incompatible, which is a shame since spec-gen is the main use case for Memio.
Also as it happens, Twig isn't as fantastic with generating PHP code as it is with generating HTML...
Requirements
℅parameter%
with value associated toparameter
key)Of course the tricky part is on how to make the syntax "succinct" especially in regards to the following challenges:
Usage examples
Here's an example of generated code we'd like to be able to handle: