Add pragmas to compiler, tokenizer.

This commit is contained in:
Justin Hileman
2012-10-01 14:37:39 -07:00
parent 6a462c6792
commit f73df2af68
2 changed files with 28 additions and 0 deletions
+6
View File
@@ -22,6 +22,7 @@ class Mustache_Compiler
private $indentNextLine;
private $customEscape;
private $charset;
private $pragmas;
/**
* Compile a Mustache token parse tree into PHP source code.
@@ -36,6 +37,7 @@ class Mustache_Compiler
*/
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8')
{
$this->pragmas = array();
$this->sections = array();
$this->source = $source;
$this->indentNextLine = true;
@@ -61,6 +63,10 @@ class Mustache_Compiler
$level++;
foreach ($tree as $node) {
switch ($node[Mustache_Tokenizer::TYPE]) {
case Mustache_Tokenizer::T_PRAGMA:
$this->pragmas[$node[Mustache_Tokenizer::NAME]] = true;
break;
case Mustache_Tokenizer::T_SECTION:
$code .= $this->section(
$node[Mustache_Tokenizer::NODES],
+22
View File
@@ -34,6 +34,7 @@ class Mustache_Tokenizer
const T_UNESCAPED = '{';
const T_UNESCAPED_2 = '&';
const T_TEXT = '_t';
const T_PRAGMA = '%';
// Valid token types
private static $tagTypes = array(
@@ -47,6 +48,7 @@ class Mustache_Tokenizer
self::T_ESCAPED => true,
self::T_UNESCAPED => true,
self::T_UNESCAPED_2 => true,
self::T_PRAGMA => true,
);
// Interpolated tags
@@ -67,6 +69,7 @@ class Mustache_Tokenizer
const NODES = 'nodes';
const VALUE = 'value';
private $pragmas;
private $state;
private $tagType;
private $tag;
@@ -126,6 +129,9 @@ class Mustache_Tokenizer
if ($this->tagType === self::T_DELIM_CHANGE) {
$i = $this->changeDelimiters($text, $i);
$this->state = self::IN_TEXT;
} elseif ($this->tagType === self::T_PRAGMA) {
$i = $this->addPragma($text, $i);
$this->state = self::IN_TEXT;
} else {
if ($tag !== null) {
$i++;
@@ -168,6 +174,13 @@ class Mustache_Tokenizer
$this->filterLine(true);
foreach ($this->pragmas as $pragma) {
array_unshift($this->tokens, array(
self::TYPE => self::T_PRAGMA,
self::NAME => $pragma,
));
}
return $this->tokens;
}
@@ -185,6 +198,7 @@ class Mustache_Tokenizer
$this->lineStart = 0;
$this->otag = '{{';
$this->ctag = '}}';
$this->pragmas = array();
}
/**
@@ -270,6 +284,14 @@ class Mustache_Tokenizer
return $closeIndex + strlen($close) - 1;
}
private function addPragma($text, $index)
{
$end = strpos($text, $this->ctag, $index);
$this->pragmas[] = trim(substr($text, $index + 2, $end - $index - 2));
return $end + strlen($this->ctag) - 1;
}
/**
* Test whether it's time to change tags.
*