Support multiline arrays. Precompiler before parse.

This commit is contained in:
Leonel Quinteros 2013-02-24 18:16:53 -03:00
parent 02beb044e8
commit 89a75ff630
3 changed files with 60 additions and 4 deletions

View File

@ -44,6 +44,9 @@ class Toml
// Cleanup TABs
$toml = str_replace("\t", " ", $toml);
// Pre-compile
$toml = self::normalize($toml);
// Split lines
$aToml = explode("\n", $toml);
@ -132,8 +135,8 @@ class Toml
$parsedVal = (float) $val;
}
}
// Datetime. parsed to time format.
elseif(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/', $val))
// Datetime. Parsed to UNIX time value.
elseif(preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', $val))
{
$parsedVal = strtotime($val);
}
@ -146,9 +149,56 @@ class Toml
}
else
{
//throw new Exception('Unknown value: ' . $val);
throw new Exception('Unknown value type: ' . $val);
}
return $parsedVal;
}
/**
* Performs text modifications in order to normalize the TOML file for the parser.
* Kind of pre-compiler.
*
* @param (string) $toml TOML string.
*
* @return (string) Normalized TOML string
*/
private static function normalize($toml)
{
$normalized = '';
$open = 0;
for($i = 0; $i < strlen($toml); $i++)
{
$keep = true;
if($toml[$i] == '[')
{
$open++;
}
elseif($toml[$i] == ']')
{
if($open > 0)
{
$open--;
}
else
{
throw new Exception("Unexpected ']' on line " . ($i + 1));
}
}
elseif($open > 0 && $toml[$i] == "\n")
{
$keep = false;
}
if($keep)
{
$normalized .= $toml[$i];
}
}
return $normalized;
}
}

View File

@ -27,3 +27,8 @@ enabled = true
[clients]
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
# Multi-line array check
key = [
1, 2, 3
]

View File

@ -4,5 +4,6 @@ require("../src/toml.php");
$result = Toml::parseFile('example.toml');
echo "\n\nToml::parseFile() test \n";
echo "\n\nToml::parseFile('example.toml'): \n";
echo "--------------------------------\n";
print_r($result);