Skip to content

Commit

Permalink
cs whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 4, 2022
1 parent fde01fa commit f37f671
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/Latte/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function addMacro(string $name, Macro $macro, int $flags = null)
} elseif ($flags && $this->flags[$name] !== $flags) {
throw new \LogicException("Incompatible flags for macro $name.");
}

$this->macros[$name][] = $macro;
return $this;
}
Expand Down Expand Up @@ -159,20 +160,23 @@ public function compile(array $tokens, string $className): string
)) {
$this->inHead = false;
}

$this->{"process$token->type"}($token);
}

while ($this->htmlNode) {
if (!empty($this->htmlNode->macroAttrs)) {
throw new CompileException('Missing ' . self::printEndTag($this->htmlNode));
}

$this->htmlNode = $this->htmlNode->parentNode;
}

while ($this->macroNode) {
if (~$this->flags[$this->macroNode->name] & Macro::AUTO_CLOSE) {
throw new CompileException('Missing ' . self::printEndTag($this->macroNode));
}

$this->closeMacro($this->macroNode->name);
}

Expand All @@ -188,14 +192,17 @@ public function compile(array $tokens, string $className): string
if ($prepare) {
$this->addMethod('prepare', "extract(\$this->params);?>$prepare<?php", '', 'void');
}

if ($this->contentType !== self::CONTENT_HTML) {
$this->addProperty('contentType', $this->contentType);
}

$members = [];

foreach ($this->properties as $name => $value) {
$members[] = "\tpublic $$name = " . PhpHelpers::dump($value, true) . ';';
}

foreach (array_filter($this->methods) as $name => $method) {
$members[] = "\n\tpublic function $name($method[arguments])"
. ($method['returns'] ? ': ' . $method['returns'] : '')
Expand Down Expand Up @@ -334,6 +341,7 @@ private function processText(Token $token): void
) {
$this->lastAttrValue = $token->text;
}

$this->output .= $this->escape($token->text);
}

Expand Down Expand Up @@ -361,11 +369,13 @@ private function processMacroTag(Token $token): void
&& ($t->type !== Token::HTML_ATTRIBUTE_BEGIN || $t->name !== Parser::N_PREFIX . $token->name));
$token->empty = $t ? !$t->closing : true;
}

$node = $this->openMacro($token->name, $token->value, $token->modifiers, $isRightmost);
if ($token->empty) {
if ($node->empty) {
throw new CompileException("Unexpected /} in tag {$token->text}");
}

$this->closeMacro($token->name, '', '', $isRightmost);
}
}
Expand All @@ -379,14 +389,18 @@ private function processHtmlTagBegin(Token $token): void
if (strcasecmp($this->htmlNode->name, $token->name) === 0) {
break;
}

if ($this->htmlNode->macroAttrs) {
throw new CompileException("Unexpected </$token->name>, expecting " . self::printEndTag($this->htmlNode));
}

$this->htmlNode = $this->htmlNode->parentNode;
}

if (!$this->htmlNode) {
$this->htmlNode = new HtmlNode($token->name);
}

$this->htmlNode->closing = true;
$this->htmlNode->endLine = $this->getLine();
$this->context = self::CONTEXT_HTML_TEXT;
Expand All @@ -402,6 +416,7 @@ private function processHtmlTagBegin(Token $token): void
$this->htmlNode->startLine = $this->getLine();
$this->context = self::CONTEXT_HTML_TAG;
}

$this->tagOffset = strlen($this->output);
$this->output .= $this->escape($token->text);
}
Expand Down Expand Up @@ -476,6 +491,7 @@ private function processHtmlAttributeBegin(Token $token): void
} elseif ($this->macroNode && $this->macroNode->htmlNode === $this->htmlNode) {
throw new CompileException("n:attributes must not appear inside macro; found $token->name inside {{$this->macroNode->name}}.");
}

$this->htmlNode->macroAttrs[$name] = $token->value;
return;
}
Expand Down Expand Up @@ -563,6 +579,7 @@ public function openMacro(
$this->output = &$node->content;
$this->output = '';
}

return $node;
}

Expand Down Expand Up @@ -616,6 +633,7 @@ public function closeMacro(
if ($node->prefix && $node->prefix !== MacroNode::PREFIX_TAG) {
$this->htmlNode->attrCode .= $node->attrCode;
}

$this->output = &$node->saved[0];
$this->writeCode((string) $node->openingCode, $node->replaced, $node->saved[1]);
$this->output .= $node->content;
Expand All @@ -632,6 +650,7 @@ private function writeCode(string $code, ?bool $isReplaced, ?bool $isRightmost,
if ($isReplaced === null) {
$isReplaced = preg_match('#<\?php.*\secho\s#As', $code);
}

if ($isLeftmost && !$isReplaced) {
$this->output = substr($this->output, 0, $leftOfs); // alone macro without output -> remove indentation
if (!$isClosing && substr($code, -2) !== '?>') {
Expand All @@ -641,6 +660,7 @@ private function writeCode(string $code, ?bool $isReplaced, ?bool $isRightmost,
$code .= "\n"; // double newline to avoid newline eating by PHP
}
}

$this->output .= $code;
}

Expand Down Expand Up @@ -673,6 +693,7 @@ public function writeAttrsMacro(string $html): void
}
});
}

unset($attrs[$attrName]);
}

Expand All @@ -687,7 +708,6 @@ public function writeAttrsMacro(string $html): void
});
}


foreach (array_reverse($this->macros) as $name => $foo) {
$attrName = MacroNode::PREFIX_TAG . "-$name";
if (!isset($attrs[$attrName])) {
Expand Down Expand Up @@ -722,6 +742,7 @@ public function writeAttrsMacro(string $html): void
}
});
}

unset($attrs[$name]);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Latte/Compiler/MacroTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function append($val, int $position = null)
is_array($val) ? [$val] : $this->parse($val)
);
}

return $this;
}

Expand All @@ -96,6 +97,7 @@ public function prepend($val)
if ($val != null) { // intentionally @
array_splice($this->tokens, 0, 0, is_array($val) ? [$val] : $this->parse($val));
}

return $this;
}

Expand Down Expand Up @@ -124,6 +126,7 @@ public function fetchWords(): array
&& (($dot = $this->nextValue('.')) || $this->isPrev('.'))) {
$words[0] .= $space . $dot . $this->joinUntil(',');
}

$this->nextToken(',');
$this->nextAll(self::T_WHITESPACE, self::T_COMMENT);
return $words === [''] ? [] : $words;
Expand Down
15 changes: 15 additions & 0 deletions src/Latte/Compiler/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,20 @@ public function parse(string $input): array
if ($this->{'context' . $this->context[0]}() === false) {
break;
}

while ($tokenCount < count($this->output)) {
$this->filter($this->output[$tokenCount++]);
}
}

if ($this->context[0] === self::CONTEXT_MACRO) {
throw new CompileException('Malformed macro');
}

if ($this->offset < strlen($input)) {
$this->addToken(Token::TEXT, substr($this->input, $this->offset));
}

return $this->output;
}

Expand Down Expand Up @@ -176,6 +179,7 @@ private function contextHtmlCData(): bool
if (empty($matches['tag'])) {
return $this->processMacro($matches);
}

// </tag
$token = $this->addToken(Token::HTML_TAG_BEGIN, $matches[0]);
$token->name = $this->lastHtmlTag;
Expand Down Expand Up @@ -219,6 +223,7 @@ private function contextHtmlTag(): bool
$this->setContext(self::CONTEXT_HTML_ATTRIBUTE, $matches['value']);
}
}

return true;

} else {
Expand All @@ -240,6 +245,7 @@ private function contextHtmlAttribute(): bool
if (empty($matches['quote'])) {
return $this->processMacro($matches);
}

// (attribute end) '"
$this->addToken(Token::HTML_ATTRIBUTE_END, $matches[0]);
$this->setContext(self::CONTEXT_HTML_TAG);
Expand All @@ -260,6 +266,7 @@ private function contextHtmlComment(): bool
if (empty($matches['htmlcomment'])) {
return $this->processMacro($matches);
}

// -->
$this->addToken(Token::HTML_TAG_END, $matches[0]);
$this->setContext(self::CONTEXT_HTML_TEXT);
Expand Down Expand Up @@ -320,6 +327,7 @@ private function processMacro(array $matches): bool
if (empty($matches['macro'])) {
return false;
}

// {macro} or {* *}
$this->setContext(self::CONTEXT_MACRO, [$this->context, $matches['macro']]);
return true;
Expand All @@ -336,17 +344,20 @@ private function match(string $re): array
if (preg_last_error()) {
throw new RegexpException(null, preg_last_error());
}

return [];
}

$value = substr($this->input, $this->offset, $matches[0][1] - $this->offset);
if ($value !== '') {
$this->addToken(Token::TEXT, $value);
}

$this->offset = $matches[0][1] + strlen($matches[0][0]);
foreach ($matches as $k => $v) {
$matches[$k] = $v[0];
}

return $matches;
}

Expand All @@ -363,6 +374,7 @@ public function setContentType(string $type)
} else {
$this->setContext(self::CONTEXT_NONE);
}

return $this;
}

Expand Down Expand Up @@ -425,11 +437,14 @@ public function parseMacroTag(string $tag): ?array
if (preg_last_error()) {
throw new RegexpException(null, preg_last_error());
}

return null;
}

if ($match['name'] === '') {
$match['name'] = $match['shortname'] ?: ($match['closing'] ? '' : '=');
}

return [$match['name'], trim($match['args']), $match['modifiers'], (bool) $match['empty'], (bool) $match['closing']];
}

Expand Down
10 changes: 9 additions & 1 deletion src/Latte/Compiler/PhpHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function reformatCode(string $source): string
} elseif (substr($next[1], -1) === "\n") {
$php .= "\n" . str_repeat("\t", $level);
}

$tokens->next();

} else {
Expand All @@ -56,16 +57,18 @@ public static function reformatCode(string $source): string
} else {
$php = rtrim($php, "\t");
}

$res .= $php . $token;
}

$php = '';
$lastChar = ';';
}

} elseif ($name === T_ELSE || $name === T_ELSEIF) {
if ($tokens[$n + 1] === ':' && $lastChar === '}') {
$php .= ';'; // semicolon needed in if(): ... if() ... else:
}

$lastChar = '';
$php .= $token;

Expand All @@ -80,6 +83,7 @@ public static function reformatCode(string $source): string
} elseif ($prev[0] === T_OPEN_TAG) {
$token = '';
}

$php .= $token;

} elseif ($name === T_OBJECT_OPERATOR) {
Expand All @@ -90,6 +94,7 @@ public static function reformatCode(string $source): string
if (in_array($name, [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true)) {
$level++;
}

$lastChar = '';
$php .= $token;
}
Expand All @@ -109,6 +114,7 @@ public static function reformatCode(string $source): string
$token .= "\n" . str_repeat("\t", $level); // indent last line
}
}

$lastChar = $token;
$php .= $token;
}
Expand All @@ -117,6 +123,7 @@ public static function reformatCode(string $source): string
if ($php) {
$res .= "<?php\n" . str_repeat("\t", $openLevel) . $php;
}

$res = str_replace(["\t\x08", "\x08"], '', $res);
return $res;
}
Expand All @@ -135,6 +142,7 @@ public static function dump($value, bool $multiline = false): string
? ($s === '' ? "\n" : '') . "\t" . ($indexed ? '' : self::dump($k) . ' => ') . self::dump($v) . ",\n"
: ($s === '' ? '' : ', ') . ($indexed ? '' : self::dump($k) . ' => ') . self::dump($v);
}

return '[' . $s . ']';
} elseif ($value === null) {
return 'null';
Expand Down
Loading

0 comments on commit f37f671

Please sign in to comment.