- First commit of first draft

This commit is contained in:
mcndave committed 2019-08-21 16:11:39 -04:00
commit 66c7b349d1
36 files changed
+820

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
<?php declare(strict_types=1);
namespace Picea;
class Picea
{
public Compiler\Context $context;
public function __construct(?Context $context = null)
{
$this->context = $context ?? new Compiler\BaseContext();
}
public function compileSource(string $source) : array
{
return [
'context' => $context = clone $this->context,
'source' => $this->instanciateCompiler($source)->compile($context)
];
}
public function compileFile(string $filePath) : array
{
if (! file_exists($filePath) ) {
throw new \InvalidArgumentException("File `$filePath` cannot be found or it is a permission problem");
}
if ( false === $fileContent = file_get_contents($filePath) ) {
throw new \ErrorException("Given file could not be opened `$filePath`. This could indicate a permission misconfiguration on your file or folder");
}
return [
'context' => $context = clone $this->context,
'source' => $this->instanciateCompiler($fileContent)->compile($context)
];
}
public function buildFromSource(string $source) : array
{
$tmpFolder = sys_get_temp_dir();
$builder = $this->instanciateBuilder();
$compiledSource = $this->compileSource($source);
list($namespace, $className, $compiledSource) = $builder->build($compiledSource['context'], $compiledSource['source']) ;
$path = "$tmpFolder/$className.php";
file_put_contents($path, $compiledSource);
return [
'path' => $path,
'namespace' => $namespace,
'className' => $className,
];
}
public function buildFromPath(string $path) : string
{
}
public function instanciateCompiler(string $sourceCode) : Compiler
{
$compiler = new Compiler();
$compiler->loadSourceCode($sourceCode);
$this->registerSyntax($compiler)
->registerControlStructure($compiler)
->registerExtension($compiler);
return $compiler;
}
public function instanciateBuilder() : Builder
{
$builder = new Builder(dirname(__FILE__) . "/Builder/ClassTemplate.php");
return $builder;
}
protected function registerSyntax(Compiler $compiler) : self
{
$compiler->registerSyntax( new \Picea\Syntax\PhpTagToken() );
$compiler->registerSyntax( new \Picea\Syntax\CommentToken() );
$compiler->registerSyntax( new \Picea\Syntax\EchoRawToken() );
$compiler->registerSyntax( new \Picea\Syntax\EchoSafeToken() );
return $this;
}
protected function registerControlStructure(Compiler $compiler) : self
{
$compiler->registerControlStructure(new \Picea\ControlStructure\NamespaceToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\UseToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\IfToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ElseToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ElseIfToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\EndIfToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ForeachToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ForToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\OrToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\EndforeachToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\EndforToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\SwitchToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\CaseToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\DefaultToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\BreakToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\EndCaseToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\EndSwitchToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\EndSectionToken());
return $this;
}
protected function registerExtension(Compiler $compiler) : self
{
$compiler->registerExtension(new \Picea\Extension\JsonExtension());
return $this;
}
}