Поскольку люди склонны бросать регулярные выражения на все, даже то, которые не могут быть проанализированы с помощью регулярных выражений (т. е. нерегулярных языков): я написал синтаксический анализатор доказательств для этого формата данных:
$input = '{
"idArray" = (
"99516",
"99518",
"97344",
"97345",
"98425"
);
"frame" = {
"size" = {
"width" = "8";
"height" = "8";
};
"origin" = {
"x" = "244";
"y" = "345";
};
};
}';
echo json_encode(parse($input));
function parse($input) {
$tokens = tokenize($input);
$index = 0;
$result = parse_value($tokens, $index);
if ($result[1] !== count($tokens)) {
throw new Exception("parsing stopped at token " . $result[1] . " but there is more input");
}
return $result[0][1];
}
function tokenize($input) {
$tokens = array();
$length = strlen($input);
$pos = 0;
while($pos < $length) {
list($token, $pos) = find_token($input, $pos);
$tokens[] = $token;
}
return $tokens;
}
function find_token($input, $pos) {
$static_tokens = array("=", "{", "}", "(", ")", ";", ",");
while(preg_match("/\s/mis", substr($input, $pos, 1))) { // eat whitespace
$pos += 1;
}
foreach ($static_tokens as $static_token) {
if (substr($input, $pos, strlen($static_token)) === $static_token) {
return array($static_token, $pos + strlen($static_token));
}
}
if (substr($input, $pos, 1) === '"') {
$length = strlen($input);
$token_length = 1;
while ($pos + $token_length < $length) {
if (substr($input, $pos + $token_length, 1) === '"') {
return array(array("value", substr($input, $pos + 1, $token_length - 1)), $pos + $token_length + 1);
}
$token_length += 1;
}
}
throw new Exception("invalid input at " . $pos . ": `" . substr($input, $pos - 10, 20) . "`");
}
// value is either an object {}, an array(), or a literal ""
function parse_value($tokens, $index) {
if ($tokens[$index] === "{") { // object: a list of key-value pairs, glued together by ";"
$return_value = array();
$index += 1;
while ($tokens[$index] !== "}") {
list($key, $value, $index) = parse_key_value($tokens, $index);
$return_value[$key] = $value[1];
if ($tokens[$index] !== ";") {
throw new Exception("Unexpected: " . print_r($tokens[$index], true));
}
$index += 1;
}
return array(array("object", $return_value), $index + 1);
}
if ($tokens[$index] === "(") { // array: a list of values, glued together by ",", the last "," is optional
$return_value = array();
$index += 1;
while ($tokens[$index] !== ")") {
list($value, $index) = parse_value($tokens, $index);
$return_value[] = $value[1];
if ($tokens[$index] === ",") { // last, is optional
$index += 1;
} else {
if ($tokens[$index] !== ")") {
throw new Exception("Unexpected: " . print_r($tokens[$index], true));
}
return array(array("array", $return_value), $index + 1);
}
}
return array(array("array", $return_value), $index + 1);
}
if ($tokens[$index][0] === "value") {
return array(array("string", $tokens[$index][1]), $index + 1);
}
throw new Exception("Unexpected: " . print_r($tokens[$index], true));
}
// find a key (string) followed by '=' followed by a value (any value)
function parse_key_value($tokens, $index) {
list($key, $index) = parse_value($tokens, $index);
if ($key[0] !== "string") { // key must be a string
throw new Exception("Unexpected: " . print_r($key, true));
}
if ($tokens[$index] !== "=") {
throw new Exception("'=' expected");
}
$index += 1;
list($value, $index) = parse_value($tokens, $index);
return array($key[1], $value, $index);
}
Выход:
{"idArray":["99516","99518","97344","97345","98425"],"frame":{"size":{"width":"8","height":"8"},"origin":{"x":"244","y":"345"}}}
Примечания
оригинальный вход имеет заднюю ,
. Я удалил этого персонажа. Он выдает ошибку (больше ввода), если вы положите ее обратно.
Этот парсер наивен в том смысле, что он все токенизирует все входные данные, прежде чем он начнет синтаксический анализ. Это не хорошо для большого ввода.
Я не добавил обнаружение побега для строк в токенизаторе. Вроде: "foo\"bar"
.
Это было веселое мероприятие. Если у вас есть какие-либо вопросы, дайте мне знать.
Редактировать: Я вижу, что это вопрос JavaScript. Портирование PHP на JavaScript не должно быть слишком сложным. list($foo, $bar) = func()
эквивалентен: var res = func(); var foo = res[0]; var bar = res[1];
@DLeh - Это не JSON. Почему вы ожидаете, что теги, добавленные кем-то, кто не знает, какой формат данных что-то содержит, включают тег для правильного формата данных? – Quentin
Это ненадлежащее JSON DLeh ... Знак равенства – Mechkov
@Mechkov. В формате данных есть как минимум три других функции, отличных от JSON. – Quentin