@vituum/vite-plugin-latte 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +39 -0
  2. package/handler.js +9 -0
  3. package/index.js +138 -0
  4. package/index.php +173 -0
  5. package/latte/AssetFilter.php +11 -0
  6. package/latte/FetchFunction.php +28 -0
  7. package/latte/IconFilter.php +14 -0
  8. package/latte/JsonTag.php +64 -0
  9. package/latte/PlaceholderFunction.php +11 -0
  10. package/latte/RandomColorFunction.php +14 -0
  11. package/latte/TelFilter.php +22 -0
  12. package/package.json +35 -0
  13. package/vendor/autoload.php +7 -0
  14. package/vendor/bin/latte-lint +107 -0
  15. package/vendor/composer/ClassLoader.php +572 -0
  16. package/vendor/composer/InstalledVersions.php +350 -0
  17. package/vendor/composer/LICENSE +21 -0
  18. package/vendor/composer/autoload_classmap.php +183 -0
  19. package/vendor/composer/autoload_namespaces.php +9 -0
  20. package/vendor/composer/autoload_psr4.php +9 -0
  21. package/vendor/composer/autoload_real.php +57 -0
  22. package/vendor/composer/autoload_static.php +193 -0
  23. package/vendor/composer/installed.json +93 -0
  24. package/vendor/composer/installed.php +32 -0
  25. package/vendor/composer/platform_check.php +26 -0
  26. package/vendor/latte/latte/bin/latte-lint +29 -0
  27. package/vendor/latte/latte/composer.json +53 -0
  28. package/vendor/latte/latte/license.md +60 -0
  29. package/vendor/latte/latte/ncs.php +14 -0
  30. package/vendor/latte/latte/ncs.xml +9 -0
  31. package/vendor/latte/latte/readme.md +21 -0
  32. package/vendor/latte/latte/src/Bridges/Tracy/BlueScreenPanel.php +114 -0
  33. package/vendor/latte/latte/src/Bridges/Tracy/LattePanel.php +99 -0
  34. package/vendor/latte/latte/src/Bridges/Tracy/templates/LattePanel.panel.phtml +98 -0
  35. package/vendor/latte/latte/src/Bridges/Tracy/templates/LattePanel.tab.phtml +15 -0
  36. package/vendor/latte/latte/src/Latte/Compiler/Block.php +44 -0
  37. package/vendor/latte/latte/src/Latte/Compiler/Escaper.php +244 -0
  38. package/vendor/latte/latte/src/Latte/Compiler/ExpressionBuilder.php +128 -0
  39. package/vendor/latte/latte/src/Latte/Compiler/Node.php +28 -0
  40. package/vendor/latte/latte/src/Latte/Compiler/NodeHelpers.php +132 -0
  41. package/vendor/latte/latte/src/Latte/Compiler/NodeTraverser.php +77 -0
  42. package/vendor/latte/latte/src/Latte/Compiler/Nodes/AreaNode.php +17 -0
  43. package/vendor/latte/latte/src/Latte/Compiler/Nodes/AuxiliaryNode.php +27 -0
  44. package/vendor/latte/latte/src/Latte/Compiler/Nodes/FragmentNode.php +59 -0
  45. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Html/AttributeNode.php +48 -0
  46. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Html/BogusTagNode.php +46 -0
  47. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Html/CommentNode.php +39 -0
  48. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Html/ElementNode.php +129 -0
  49. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Html/QuotedValue.php +53 -0
  50. package/vendor/latte/latte/src/Latte/Compiler/Nodes/NopNode.php +21 -0
  51. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ArgumentNode.php +45 -0
  52. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ComplexTypeNode.php +17 -0
  53. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ArrayAccessNode.php +41 -0
  54. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ArrayItemNode.php +52 -0
  55. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ArrayNode.php +94 -0
  56. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/AssignNode.php +39 -0
  57. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/AssignOpNode.php +45 -0
  58. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/BinaryOpNode.php +65 -0
  59. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/CastNode.php +43 -0
  60. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ClassConstantFetchNode.php +42 -0
  61. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/CloneNode.php +36 -0
  62. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ClosureNode.php +73 -0
  63. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ClosureUseNode.php +37 -0
  64. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ConstantFetchNode.php +37 -0
  65. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/EmptyNode.php +36 -0
  66. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/ErrorSuppressNode.php +36 -0
  67. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/FilterCallNode.php +39 -0
  68. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/FunctionCallNode.php +45 -0
  69. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/FunctionCallableNode.php +39 -0
  70. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/InRangeNode.php +42 -0
  71. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/InstanceofNode.php +40 -0
  72. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/IssetNode.php +40 -0
  73. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/MatchNode.php +49 -0
  74. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/MethodCallNode.php +50 -0
  75. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/MethodCallableNode.php +42 -0
  76. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/NewNode.php +45 -0
  77. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/NotNode.php +36 -0
  78. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/PostOpNode.php +43 -0
  79. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/PreOpNode.php +43 -0
  80. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/PropertyFetchNode.php +42 -0
  81. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/StaticCallNode.php +55 -0
  82. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/StaticCallableNode.php +47 -0
  83. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/StaticPropertyFetchNode.php +42 -0
  84. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/TernaryNode.php +50 -0
  85. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/UnaryOpNode.php +45 -0
  86. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/VariableNode.php +40 -0
  87. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ExpressionNode.php +17 -0
  88. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/FilterNode.php +67 -0
  89. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/IdentifierNode.php +36 -0
  90. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/IntersectionTypeNode.php +39 -0
  91. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/MatchArmNode.php +45 -0
  92. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ModifierNode.php +107 -0
  93. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/NameNode.php +89 -0
  94. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/NullableTypeNode.php +35 -0
  95. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ParameterNode.php +54 -0
  96. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/BooleanNode.php +30 -0
  97. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/EncapsedStringNode.php +80 -0
  98. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/EncapsedStringPartNode.php +30 -0
  99. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/FloatNode.php +63 -0
  100. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/IntegerNode.php +70 -0
  101. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/NullNode.php +29 -0
  102. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Scalar/StringNode.php +40 -0
  103. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ScalarNode.php +15 -0
  104. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php +29 -0
  105. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/UnionTypeNode.php +39 -0
  106. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/VarLikeIdentifierNode.php +21 -0
  107. package/vendor/latte/latte/src/Latte/Compiler/Nodes/StatementNode.php +15 -0
  108. package/vendor/latte/latte/src/Latte/Compiler/Nodes/TemplateNode.php +34 -0
  109. package/vendor/latte/latte/src/Latte/Compiler/Nodes/TextNode.php +37 -0
  110. package/vendor/latte/latte/src/Latte/Compiler/PhpHelpers.php +227 -0
  111. package/vendor/latte/latte/src/Latte/Compiler/Position.php +43 -0
  112. package/vendor/latte/latte/src/Latte/Compiler/PrintContext.php +334 -0
  113. package/vendor/latte/latte/src/Latte/Compiler/Tag.php +117 -0
  114. package/vendor/latte/latte/src/Latte/Compiler/TagLexer.php +399 -0
  115. package/vendor/latte/latte/src/Latte/Compiler/TagParser.php +388 -0
  116. package/vendor/latte/latte/src/Latte/Compiler/TagParserData.php +580 -0
  117. package/vendor/latte/latte/src/Latte/Compiler/TemplateGenerator.php +206 -0
  118. package/vendor/latte/latte/src/Latte/Compiler/TemplateLexer.php +391 -0
  119. package/vendor/latte/latte/src/Latte/Compiler/TemplateParser.php +448 -0
  120. package/vendor/latte/latte/src/Latte/Compiler/TemplateParserHtml.php +506 -0
  121. package/vendor/latte/latte/src/Latte/Compiler/Token.php +259 -0
  122. package/vendor/latte/latte/src/Latte/Compiler/TokenStream.php +151 -0
  123. package/vendor/latte/latte/src/Latte/ContentType.php +22 -0
  124. package/vendor/latte/latte/src/Latte/Engine.php +591 -0
  125. package/vendor/latte/latte/src/Latte/Essential/Blueprint.php +160 -0
  126. package/vendor/latte/latte/src/Latte/Essential/CachingIterator.php +232 -0
  127. package/vendor/latte/latte/src/Latte/Essential/CoreExtension.php +232 -0
  128. package/vendor/latte/latte/src/Latte/Essential/Filters.php +591 -0
  129. package/vendor/latte/latte/src/Latte/Essential/Nodes/BlockNode.php +161 -0
  130. package/vendor/latte/latte/src/Latte/Essential/Nodes/CaptureNode.php +98 -0
  131. package/vendor/latte/latte/src/Latte/Essential/Nodes/ContentTypeNode.php +74 -0
  132. package/vendor/latte/latte/src/Latte/Essential/Nodes/DebugbreakNode.php +53 -0
  133. package/vendor/latte/latte/src/Latte/Essential/Nodes/DefineNode.php +134 -0
  134. package/vendor/latte/latte/src/Latte/Essential/Nodes/DoNode.php +49 -0
  135. package/vendor/latte/latte/src/Latte/Essential/Nodes/DumpNode.php +58 -0
  136. package/vendor/latte/latte/src/Latte/Essential/Nodes/EmbedNode.php +125 -0
  137. package/vendor/latte/latte/src/Latte/Essential/Nodes/ExtendsNode.php +60 -0
  138. package/vendor/latte/latte/src/Latte/Essential/Nodes/FirstLastSepNode.php +81 -0
  139. package/vendor/latte/latte/src/Latte/Essential/Nodes/ForNode.php +91 -0
  140. package/vendor/latte/latte/src/Latte/Essential/Nodes/ForeachNode.php +155 -0
  141. package/vendor/latte/latte/src/Latte/Essential/Nodes/IfChangedNode.php +141 -0
  142. package/vendor/latte/latte/src/Latte/Essential/Nodes/IfContentNode.php +82 -0
  143. package/vendor/latte/latte/src/Latte/Essential/Nodes/IfNode.php +180 -0
  144. package/vendor/latte/latte/src/Latte/Essential/Nodes/ImportNode.php +49 -0
  145. package/vendor/latte/latte/src/Latte/Essential/Nodes/IncludeBlockNode.php +140 -0
  146. package/vendor/latte/latte/src/Latte/Essential/Nodes/IncludeFileNode.php +81 -0
  147. package/vendor/latte/latte/src/Latte/Essential/Nodes/IterateWhileNode.php +94 -0
  148. package/vendor/latte/latte/src/Latte/Essential/Nodes/NAttrNode.php +105 -0
  149. package/vendor/latte/latte/src/Latte/Essential/Nodes/NClassNode.php +54 -0
  150. package/vendor/latte/latte/src/Latte/Essential/Nodes/NTagNode.php +79 -0
  151. package/vendor/latte/latte/src/Latte/Essential/Nodes/ParametersNode.php +84 -0
  152. package/vendor/latte/latte/src/Latte/Essential/Nodes/PrintNode.php +70 -0
  153. package/vendor/latte/latte/src/Latte/Essential/Nodes/RawPhpNode.php +46 -0
  154. package/vendor/latte/latte/src/Latte/Essential/Nodes/RollbackNode.php +37 -0
  155. package/vendor/latte/latte/src/Latte/Essential/Nodes/SkipNode.php +71 -0
  156. package/vendor/latte/latte/src/Latte/Essential/Nodes/SpacelessNode.php +62 -0
  157. package/vendor/latte/latte/src/Latte/Essential/Nodes/SwitchNode.php +118 -0
  158. package/vendor/latte/latte/src/Latte/Essential/Nodes/TemplatePrintNode.php +39 -0
  159. package/vendor/latte/latte/src/Latte/Essential/Nodes/TemplateTypeNode.php +38 -0
  160. package/vendor/latte/latte/src/Latte/Essential/Nodes/TraceNode.php +35 -0
  161. package/vendor/latte/latte/src/Latte/Essential/Nodes/TranslateNode.php +104 -0
  162. package/vendor/latte/latte/src/Latte/Essential/Nodes/TryNode.php +75 -0
  163. package/vendor/latte/latte/src/Latte/Essential/Nodes/VarNode.php +106 -0
  164. package/vendor/latte/latte/src/Latte/Essential/Nodes/VarPrintNode.php +40 -0
  165. package/vendor/latte/latte/src/Latte/Essential/Nodes/VarTypeNode.php +36 -0
  166. package/vendor/latte/latte/src/Latte/Essential/Nodes/WhileNode.php +81 -0
  167. package/vendor/latte/latte/src/Latte/Essential/Passes.php +115 -0
  168. package/vendor/latte/latte/src/Latte/Essential/RawPhpExtension.php +28 -0
  169. package/vendor/latte/latte/src/Latte/Essential/RollbackException.php +16 -0
  170. package/vendor/latte/latte/src/Latte/Essential/Tracer.php +117 -0
  171. package/vendor/latte/latte/src/Latte/Essential/TranslatorExtension.php +101 -0
  172. package/vendor/latte/latte/src/Latte/Extension.php +97 -0
  173. package/vendor/latte/latte/src/Latte/Helpers.php +96 -0
  174. package/vendor/latte/latte/src/Latte/Loader.php +37 -0
  175. package/vendor/latte/latte/src/Latte/Loaders/FileLoader.php +95 -0
  176. package/vendor/latte/latte/src/Latte/Loaders/StringLoader.php +76 -0
  177. package/vendor/latte/latte/src/Latte/Policy.php +24 -0
  178. package/vendor/latte/latte/src/Latte/PositionAwareException.php +50 -0
  179. package/vendor/latte/latte/src/Latte/Runtime/Block.php +24 -0
  180. package/vendor/latte/latte/src/Latte/Runtime/FilterExecutor.php +151 -0
  181. package/vendor/latte/latte/src/Latte/Runtime/FilterInfo.php +39 -0
  182. package/vendor/latte/latte/src/Latte/Runtime/Filters.php +234 -0
  183. package/vendor/latte/latte/src/Latte/Runtime/Html.php +35 -0
  184. package/vendor/latte/latte/src/Latte/Runtime/HtmlStringable.php +17 -0
  185. package/vendor/latte/latte/src/Latte/Runtime/Template.php +381 -0
  186. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/FunctionCallNode.php +30 -0
  187. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/FunctionCallableNode.php +29 -0
  188. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/MethodCallNode.php +32 -0
  189. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/MethodCallableNode.php +30 -0
  190. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/PropertyFetchNode.php +32 -0
  191. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/SandboxNode.php +73 -0
  192. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/StaticCallNode.php +31 -0
  193. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/StaticCallableNode.php +30 -0
  194. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/StaticPropertyFetchNode.php +32 -0
  195. package/vendor/latte/latte/src/Latte/Sandbox/RuntimeChecker.php +116 -0
  196. package/vendor/latte/latte/src/Latte/Sandbox/SandboxExtension.php +132 -0
  197. package/vendor/latte/latte/src/Latte/Sandbox/SecurityPolicy.php +185 -0
  198. package/vendor/latte/latte/src/Latte/Strict.php +101 -0
  199. package/vendor/latte/latte/src/Latte/attributes.php +24 -0
  200. package/vendor/latte/latte/src/Latte/exceptions.php +69 -0
  201. package/vendor/latte/latte/src/Tools/Linter.php +175 -0
@@ -0,0 +1,388 @@
1
+ <?php
2
+
3
+ /**
4
+ * This file is part of the Latte (https://latte.nette.org)
5
+ * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6
+ */
7
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Latte\Compiler;
11
+
12
+ use Latte;
13
+ use Latte\Compiler\Nodes\Php as Node;
14
+ use Latte\Compiler\Nodes\Php\Expression;
15
+ use Latte\Compiler\Nodes\Php\ExpressionNode;
16
+ use Latte\Compiler\Nodes\Php\NameNode;
17
+ use Latte\Compiler\Nodes\Php\Scalar;
18
+
19
+
20
+ /**
21
+ * Parser for PHP-like expression language used in tags.
22
+ * Based on works by Nikita Popov, Moriyoshi Koizumi and Masato Bito.
23
+ */
24
+ final class TagParser extends TagParserData
25
+ {
26
+ use Latte\Strict;
27
+
28
+ private const
29
+ SchemaExpression = 'e',
30
+ SchemaArguments = 'a',
31
+ SchemaFilters = 'm';
32
+
33
+ private const SymbolNone = -1;
34
+
35
+ public TokenStream /*readonly*/ $stream;
36
+ public string $text;
37
+ private int /*readonly*/ $offsetDelta;
38
+
39
+
40
+ public function __construct(array $tokens)
41
+ {
42
+ $this->offsetDelta = $tokens[0]->position->offset ?? 0;
43
+ $tokens = $this->filterTokens($tokens);
44
+ $this->stream = new TokenStream(new \ArrayIterator($tokens));
45
+ }
46
+
47
+
48
+ public function parseExpression(): ExpressionNode
49
+ {
50
+ return $this->parse(self::SchemaExpression, recovery: true);
51
+ }
52
+
53
+
54
+ public function parseArguments(): Expression\ArrayNode
55
+ {
56
+ return $this->parse(self::SchemaArguments, recovery: true);
57
+ }
58
+
59
+
60
+ public function parseModifier(): Node\ModifierNode
61
+ {
62
+ return $this->isEnd()
63
+ ? new Node\ModifierNode([])
64
+ : $this->parse(self::SchemaFilters);
65
+ }
66
+
67
+
68
+ public function isEnd(): bool
69
+ {
70
+ return $this->stream->peek()->isEnd();
71
+ }
72
+
73
+
74
+ public function parseUnquotedStringOrExpression(bool $colon = true): ExpressionNode
75
+ {
76
+ $position = $this->stream->peek()->position;
77
+ $lexer = new TagLexer;
78
+ $tokens = $lexer->tokenizeUnquotedString($this->text, $position, $colon, $this->offsetDelta);
79
+
80
+ if (!$tokens) {
81
+ return $this->parseExpression();
82
+ }
83
+
84
+ $parser = new self($tokens);
85
+ $end = $position->offset + strlen($parser->text) - 2; // 2 quotes
86
+ do {
87
+ $this->stream->consume();
88
+ } while ($this->stream->peek()->position->offset < $end);
89
+
90
+ return $parser->parseExpression();
91
+ }
92
+
93
+
94
+ public function tryConsumeModifier(string ...$modifiers): ?Token
95
+ {
96
+ $token = $this->stream->peek();
97
+ return $token->is(...$modifiers) // is followed by whitespace
98
+ && $this->stream->peek(1)->position->offset > $token->position->offset + strlen($token->text)
99
+ ? $this->stream->consume()
100
+ : null;
101
+ }
102
+
103
+
104
+ public function parseType(): ?Node\SuperiorTypeNode
105
+ {
106
+ $kind = [
107
+ Token::Php_Identifier, Token::Php_Constant, Token::Php_Ellipsis, Token::Php_Array, Token::Php_Integer,
108
+ Token::Php_NameFullyQualified, Token::Php_NameQualified, Token::Php_Null, Token::Php_False,
109
+ '(', ')', '<', '>', '[', ']', '|', '&', '{', '}', ':', ',', '=', '?',
110
+ ];
111
+ $res = null;
112
+ while ($token = $this->stream->tryConsume(...$kind)) {
113
+ $res .= $token->text;
114
+ }
115
+
116
+ return $res ? new Node\SuperiorTypeNode($res) : null;
117
+ }
118
+
119
+
120
+ /** @throws Latte\CompileException */
121
+ private function parse(string $schema, bool $recovery = false): mixed
122
+ {
123
+ $symbol = self::SymbolNone; // We start off with no lookahead-token
124
+ $this->startTokenStack = []; // Keep stack of start token
125
+ $token = null;
126
+ $state = 0; // Start off in the initial state and keep a stack of previous states
127
+ $stateStack = [$state];
128
+ $this->semStack = []; // Semantic value stack (contains values of tokens and semantic action results)
129
+ $stackPos = 0; // Current position in the stack(s)
130
+
131
+ do {
132
+ if (self::ActionBase[$state] === 0) {
133
+ $rule = self::ActionDefault[$state];
134
+ } else {
135
+ if ($symbol === self::SymbolNone) {
136
+ $recovery = $recovery
137
+ ? [$this->stream->getIndex(), $state, $stateStack, $stackPos, $this->semValue, $this->semStack, $this->startTokenStack]
138
+ : null;
139
+
140
+
141
+ if ($token) {
142
+ $prevToken = $token;
143
+ $token = $this->stream->consume();
144
+ } else {
145
+ $token = new Token(ord($schema), $schema);
146
+ }
147
+
148
+ recovery:
149
+ $symbol = self::TokenToSymbol[$token->type];
150
+ }
151
+
152
+ $idx = self::ActionBase[$state] + $symbol;
153
+ if ((($idx >= 0 && $idx < count(self::Action) && self::ActionCheck[$idx] === $symbol)
154
+ || ($state < self::Yy2Tblstate
155
+ && ($idx = self::ActionBase[$state + self::NumNonLeafStates] + $symbol) >= 0
156
+ && $idx < count(self::Action) && self::ActionCheck[$idx] === $symbol))
157
+ && ($action = self::Action[$idx]) !== self::DefaultAction
158
+ ) {
159
+ /*
160
+ >= numNonLeafStates: shift and reduce
161
+ > 0: shift
162
+ = 0: accept
163
+ < 0: reduce
164
+ = -YYUNEXPECTED: error
165
+ */
166
+ if ($action > 0) { // shift
167
+ ++$stackPos;
168
+ $stateStack[$stackPos] = $state = $action;
169
+ $this->semStack[$stackPos] = $token->text;
170
+ $this->startTokenStack[$stackPos] = $token;
171
+ $symbol = self::SymbolNone;
172
+ if ($action < self::NumNonLeafStates) {
173
+ continue;
174
+ }
175
+
176
+ $rule = $action - self::NumNonLeafStates; // shift-and-reduce
177
+ } else {
178
+ $rule = -$action;
179
+ }
180
+ } else {
181
+ $rule = self::ActionDefault[$state];
182
+ }
183
+ }
184
+
185
+ do {
186
+ if ($rule === 0) { // accept
187
+ return $this->semValue;
188
+
189
+ } elseif ($rule !== self::UnexpectedTokenRule) { // reduce
190
+ $this->reduce($rule, $stackPos);
191
+
192
+ // Goto - shift nonterminal
193
+ $ruleLength = self::RuleToLength[$rule];
194
+ $stackPos -= $ruleLength;
195
+ $nonTerminal = self::RuleToNonTerminal[$rule];
196
+ $idx = self::GotoBase[$nonTerminal] + $stateStack[$stackPos];
197
+ if ($idx >= 0 && $idx < count(self::Goto) && self::GotoCheck[$idx] === $nonTerminal) {
198
+ $state = self::Goto[$idx];
199
+ } else {
200
+ $state = self::GotoDefault[$nonTerminal];
201
+ }
202
+
203
+ ++$stackPos;
204
+ $stateStack[$stackPos] = $state;
205
+ $this->semStack[$stackPos] = $this->semValue;
206
+ if ($ruleLength === 0) {
207
+ $this->startTokenStack[$stackPos] = $token;
208
+ }
209
+
210
+ } else { // error
211
+ if ($prevToken->is('echo', 'print', 'return', 'yield', 'throw', 'if', 'foreach', 'unset')) {
212
+ throw new Latte\CompileException("Keyword '$prevToken->text' is forbidden in Latte", $prevToken->position);
213
+ }
214
+
215
+ if ($recovery && $this->isExpectedEof($state)) {
216
+ [, $state, $stateStack, $stackPos, $this->semValue, $this->semStack, $this->startTokenStack] = $recovery;
217
+ $this->stream->seek($recovery[0]);
218
+ $token = new Token(Token::End, '');
219
+ goto recovery;
220
+ }
221
+
222
+ throw new Latte\CompileException('Unexpected ' . ($token->text ? "'$token->text'" : 'end'), $token->position);
223
+ }
224
+
225
+ if ($state < self::NumNonLeafStates) {
226
+ break;
227
+ }
228
+
229
+ $rule = $state - self::NumNonLeafStates; // shift-and-reduce
230
+ } while (true);
231
+ } while (true);
232
+ }
233
+
234
+
235
+ /**
236
+ * Can EOF be the next token?
237
+ */
238
+ private function isExpectedEof(int $state): bool
239
+ {
240
+ foreach (self::SymbolToName as $symbol => $name) {
241
+ $idx = self::ActionBase[$state] + $symbol;
242
+ if (($idx >= 0 && $idx < count(self::Action) && self::ActionCheck[$idx] === $symbol
243
+ || $state < self::Yy2Tblstate
244
+ && ($idx = self::ActionBase[$state + self::NumNonLeafStates] + $symbol) >= 0
245
+ && $idx < count(self::Action) && self::ActionCheck[$idx] === $symbol)
246
+ && self::Action[$idx] !== self::UnexpectedTokenRule
247
+ && self::Action[$idx] !== self::DefaultAction
248
+ && $symbol === 0
249
+ ) {
250
+ return true;
251
+ }
252
+ }
253
+
254
+ return false;
255
+ }
256
+
257
+
258
+ protected static function handleBuiltinTypes(NameNode $name): NameNode|Node\IdentifierNode
259
+ {
260
+ $builtinTypes = [
261
+ 'bool' => true, 'int' => true, 'float' => true, 'string' => true, 'iterable' => true, 'void' => true,
262
+ 'object' => true, 'null' => true, 'false' => true, 'mixed' => true, 'never' => true,
263
+ ];
264
+
265
+ $lowerName = strtolower($name->toCodeString());
266
+ return isset($builtinTypes[$lowerName])
267
+ ? new Node\IdentifierNode($lowerName, $name->position)
268
+ : $name;
269
+ }
270
+
271
+
272
+ protected static function parseOffset(string $str, Position $position): Scalar\StringNode|Scalar\IntegerNode
273
+ {
274
+ if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
275
+ return new Scalar\StringNode($str, $position);
276
+ }
277
+
278
+ $num = +$str;
279
+ if (!is_int($num)) {
280
+ return new Scalar\StringNode($str, $position);
281
+ }
282
+
283
+ return new Scalar\IntegerNode($num, Scalar\IntegerNode::KindDecimal, $position);
284
+ }
285
+
286
+
287
+ /** @param ExpressionNode[] $parts */
288
+ protected function parseDocString(
289
+ string $startToken,
290
+ array $parts,
291
+ string $endToken,
292
+ Position $startPos,
293
+ Position $endPos,
294
+ ): Scalar\StringNode|Scalar\EncapsedStringNode {
295
+ $hereDoc = !str_contains($startToken, "'");
296
+ preg_match('/\A[ \t]*/', $endToken, $matches);
297
+ $indentation = $matches[0];
298
+ if (str_contains($indentation, ' ') && str_contains($indentation, "\t")) {
299
+ throw new CompileException('Invalid indentation - tabs and spaces cannot be mixed', $endPos);
300
+
301
+ } elseif (!$parts) {
302
+ return new Scalar\StringNode('', $startPos);
303
+
304
+ } elseif (!$parts[0] instanceof Scalar\EncapsedStringPartNode) {
305
+ // If there is no leading encapsed string part, pretend there is an empty one
306
+ $this->stripIndentation('', $indentation, true, false, $parts[0]->position);
307
+ }
308
+
309
+ $newParts = [];
310
+ foreach ($parts as $i => $part) {
311
+ if ($part instanceof Scalar\EncapsedStringPartNode) {
312
+ $isLast = $i === \count($parts) - 1;
313
+ $part->value = $this->stripIndentation(
314
+ $part->value,
315
+ $indentation,
316
+ $i === 0,
317
+ $isLast,
318
+ $part->position,
319
+ );
320
+ if ($isLast) {
321
+ $part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value);
322
+ }
323
+ if ($hereDoc) {
324
+ $part->value = PhpHelpers::decodeEscapeSequences($part->value, null);
325
+ }
326
+ if ($i === 0 && $isLast) {
327
+ return new Scalar\StringNode($part->value, $startPos);
328
+ }
329
+ if ($part->value === '') {
330
+ continue;
331
+ }
332
+ }
333
+ $newParts[] = $part;
334
+ }
335
+
336
+ return new Scalar\EncapsedStringNode($newParts, $startPos);
337
+ }
338
+
339
+
340
+ private function stripIndentation(
341
+ string $str,
342
+ string $indentation,
343
+ bool $atStart,
344
+ bool $atEnd,
345
+ Position $position,
346
+ ): string {
347
+ if ($indentation === '') {
348
+ return $str;
349
+ }
350
+ $start = $atStart ? '(?:(?<=\n)|\A)' : '(?<=\n)';
351
+ $end = $atEnd ? '(?:(?=[\r\n])|\z)' : '(?=[\r\n])';
352
+ $regex = '/' . $start . '([ \t]*)(' . $end . ')?/D';
353
+ return preg_replace_callback(
354
+ $regex,
355
+ function ($matches) use ($indentation, $position) {
356
+ $indentLen = \strlen($indentation);
357
+ $prefix = substr($matches[1], 0, $indentLen);
358
+ if (str_contains($prefix, $indentation[0] === ' ' ? "\t" : ' ')) {
359
+ throw new CompileException('Invalid indentation - tabs and spaces cannot be mixed', $position);
360
+ } elseif (strlen($prefix) < $indentLen && !isset($matches[2])) {
361
+ throw new CompileException(
362
+ 'Invalid body indentation level ' .
363
+ '(expecting an indentation level of at least ' . $indentLen . ')',
364
+ $position,
365
+ );
366
+ }
367
+ return substr($matches[0], strlen($prefix));
368
+ },
369
+ $str,
370
+ );
371
+ }
372
+
373
+
374
+ /** @param Token[] $tokens */
375
+ private function filterTokens(array $tokens): array
376
+ {
377
+ $this->text = '';
378
+ $res = [];
379
+ foreach ($tokens as $token) {
380
+ $this->text .= $token->text;
381
+ if (!$token->is(Token::Php_Whitespace, Token::Php_Comment)) {
382
+ $res[] = $token;
383
+ }
384
+ }
385
+
386
+ return $res;
387
+ }
388
+ }