19 declare(strict_types=1);
30 Operators::SUBTRACTION,
31 Operators::MULTIPLICATION,
55 return new Token(trim($token));
64 public function tokenize(
string $expression): array
66 $expression = ltrim($expression,
'=');
67 $expression = trim($expression);
71 preg_match_all(
"/\[\[[^\]]*&[^\]]*\]\]/ui", $expression, $matches);
72 $matches_inside_brackets = $matches[0];
73 $replace_random = sha1(
"replacement_string");
76 foreach ($matches_inside_brackets as $match) {
80 $match_save = str_replace(
"&", $replace_random, $match);
81 $expression = str_replace($match, $match_save, $expression);
85 preg_match_all(
"/([^\\\\&]|\\\\&)*/ui", $expression, $matches);
93 $replace = str_ireplace(
'\&',
'&', $result);
96 $return[] = str_replace($replace_random,
"&", $replace);
105 static fn(
Operators $operator):
string => $operator->value,
110 static fn(
Functions $functions):
string => $functions->value,
114 $result = (bool) preg_match(
115 '#(\\' . implode(
"|\\", $operators) .
'|' . implode(
'|', $functions) .
')#',
130 static fn(
Operators $operator):
string => $operator->value,
133 $pattern =
'#((^\[\[)[\d\.]+)|(\(|\)|\\' . implode(
"|\\", $operators) .
')#';
134 $tokens = preg_split($pattern, $math_expression, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);