Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Persistence/TransformationProcessor/DefinitionBased/Parser.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased;
8
9 use eZ\Publish\Core\Persistence\TransformationProcessor;
10 use RuntimeException;
11
34 class Parser
35 {
44 protected $tokenSpecifications = null;
45
49 public function __construct()
50 {
51 $character = '(?:U\\+[0-9a-fA-F]{4}|remove|keep|[0-9a-fA-F]+|"(?:[^\\\\"]+|\\\\\\\\|\\\\\'|\\\\")*?")';
52
53 $this->tokenSpecifications = [
54 TransformationProcessor::T_COMMENT => '(\\A#(?P<comment>.*)$)m',
55 TransformationProcessor::T_WHITESPACE => '(\\A\\s+)',
56 TransformationProcessor::T_SECTION => '(\\A(?P<section>[a-z0-9_-]+):\s*$)m',
57 TransformationProcessor::T_MAP => '(\\A(?P<src>' . $character . ')\\s*=\\s*(?P<dest>' . $character . '))',
58 TransformationProcessor::T_REPLACE => '(\\A(?P<srcStart>' . $character . ')\\s*-\\s*' .
59 '(?P<srcEnd>' . $character . ')\\s*=\\s*' .
60 '(?P<dest>' . $character . '))',
61 TransformationProcessor::T_TRANSPOSE => '(\\A(?P<srcStart>' . $character . ')\\s*-\\s*' .
62 '(?P<srcEnd>' . $character . ')\\s*' .
63 '(?P<op>[+-])\\s*' .
64 '(?P<dest>' . $character . '))',
65 TransformationProcessor::T_TRANSPOSE_MODULO => '(\\A(?P<srcStart>' . $character . ')\\s*-\\s*' .
66 '(?P<srcEnd>' . $character . ')\\s*%\\s*' .
67 '(?P<modulo>' . $character . ')\\s*' .
68 '(?P<op>[+-])\\s*' .
69 '(?P<dest>' . $character . '))',
70 ];
71 }
72
80 public function parse($file)
81 {
82 return $this->parseString(
83 file_get_contents($file)
84 );
85 }
86
94 public function parseString($string)
95 {
96 $tokens = $this->tokenize($string);
97
98 $tokens = array_filter(
99 $tokens,
100 function ($token) {
101 return !($token['type'] === TransformationProcessor::T_WHITESPACE ||
102 $token['type'] === TransformationProcessor::T_COMMENT);
103 }
104 );
105
106 $ast = [];
107 $section = null;
108 while ($token = array_shift($tokens)) {
109 if ($token['type'] === TransformationProcessor::T_SECTION) {
110 $section = $token['data']['section'];
111 continue;
112 }
113
114 if ($section === null) {
115 throw new RuntimeException('Expected section.');
116 }
117
118 $ast[$section][] = $token;
119 }
120
121 return $ast;
122 }
123
133 protected function tokenize($string)
134 {
135 $string = preg_replace('(\\r\\n|\\r)', "\n", $string);
136 $tokens = [];
137 $line = 1;
138
139 while (strlen($string)) {
140 foreach ($this->tokenSpecifications as $token => $regexp) {
141 if (!preg_match($regexp, $string, $matches)) {
142 continue;
143 }
144
145 // Remove matched string
146 $string = substr($string, strlen($matches[0]));
147 $line += substr_count($matches[0], "\n");
148
149 // Append token to list
150 $tokens[] = [
151 'type' => $token,
152 'data' => $this->filterValues($matches),
153 ];
154
155 // Continue with outer loop
156 continue 2;
157 }
158
159 throw new RuntimeException("Parse error in line $line: " . substr($string, 0, 100));
160 }
161
162 return $tokens;
163 }
164
172 protected function filterValues(array $data)
173 {
174 foreach ($data as $key => $value) {
175 if (is_int($key)) {
176 unset($data[$key]);
177 }
178 }
179
180 return $data;
181 }
182 }
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser
Parser for transformation specifications.
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:35
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser\parse
parse($file)
Parse the specified transformation file into an AST.
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:80
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser\tokenize
tokenize($string)
Tokenize transformation input file.
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:133
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser\parseString
parseString($string)
Parse the given string into an AST.
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:94
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser\filterValues
filterValues(array $data)
Filter out numeric array keys.
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:172
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser\__construct
__construct()
Construct.
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:49
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser\$tokenSpecifications
$tokenSpecifications
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:44
eZ\Publish\Core\Persistence\TransformationProcessor
Interface for processing a set of transformations on a string.
Definition: TransformationProcessor.php:15
eZ\Publish\Core\Persistence\TransformationProcessor\T_MAP
const T_MAP
Definition: TransformationProcessor.php:19
eZ\Publish\Core\Persistence\TransformationProcessor\T_REPLACE
const T_REPLACE
Definition: TransformationProcessor.php:20
eZ\Publish\Core\Persistence\TransformationProcessor\T_COMMENT
const T_COMMENT
Definition: TransformationProcessor.php:16
eZ\Publish\Core\Persistence\TransformationProcessor\T_TRANSPOSE
const T_TRANSPOSE
Definition: TransformationProcessor.php:21
eZ\Publish\Core\Persistence\TransformationProcessor\T_WHITESPACE
const T_WHITESPACE
Definition: TransformationProcessor.php:17
eZ\Publish\Core\Persistence\TransformationProcessor\T_TRANSPOSE_MODULO
const T_TRANSPOSE_MODULO
Definition: TransformationProcessor.php:22
eZ\Publish\Core\Persistence\TransformationProcessor\T_SECTION
const T_SECTION
Definition: TransformationProcessor.php:18
eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased
Definition: Persistence/TransformationProcessor/DefinitionBased/Parser.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.