Exemple #1
0
}), "ESCAPED_BACKSLASH" => new StringParser("\\\\", function ($string) {
    return substr($string, 1, 1);
}), "ESCAPED_SLASH" => new StringParser("\\/", function ($string) {
    return substr($string, 1, 1);
}), "ESCAPED_B" => new StringParser("\\b", function ($string) {
    return "";
}), "ESCAPED_F" => new StringParser("\\f", function ($string) {
    return "\f";
}), "ESCAPED_N" => new StringParser("\\n", function ($string) {
    return "\n";
}), "ESCAPED_R" => new StringParser("\\r", function ($string) {
    return "\r";
}), "ESCAPED_T" => new StringParser("\\t", function ($string) {
    return "\t";
}), "ESCAPED_UTF8" => new RegexParser("#^\\\\u[0-9a-fA-F]{4}#", function ($match) {
    return Utf8Parser::getBytes(hexdec(substr($match, 2, 4)));
})));
// if executing this file directly, run unit tests
if (__FILE__ !== $_SERVER["SCRIPT_FILENAME"]) {
    return;
}
$start = microtime(true);
$parseTree = $jsonGrammar->parse(" { \"string\" : true, \"\\\"\" : false, \"\\u9874asdh\" : [ null, { }, -9488.44E+093 ] } ");
print "Parsing completed in " . (microtime(true) - $start) . " seconds\n";
var_dump(true);
// for successful parsing
// print_r($parseTree);
var_dump(count($parseTree) === 3);
var_dump($parseTree["string"] === true);
var_dump($parseTree["\""] === false);
var_dump($parseTree["顴asdh"] === array(null, array(), -9.488439999999999E+96));
Exemple #2
0
    var_dump(true);
}
print "3A\n";
$parser = new RegexParser("#^boo#");
var_dump($parser->match("boo", 0) === array("j" => 3, "value" => "boo"));
try {
    $parser->match("aboo", 0);
    var_dump(false);
} catch (ParseFailureException $e) {
    var_dump(true);
}
var_dump($parser->match("aboo", 1) == array("j" => 4, "value" => "boo"));
$parser = new RegexParser("#^-?(0|[1-9][0-9]*)(\\.[0-9]*)?([eE][-+]?[0-9]*)?#");
var_dump($parser->match("-24.444E-009", 2) === array("j" => 12, "value" => "4.444E-009"));
print "5A\n";
$parser = new Utf8Parser(array());
try {
    $parser->match("", 0);
    var_dump(false);
} catch (ParseFailureException $e) {
    var_dump(true);
}
var_dump($parser->match("A", 0) === array("j" => 1, "value" => "A"));
# 1-byte character "A"
var_dump($parser->match("¯", 0) === array("j" => 2, "value" => "¯"));
# 2-byte character "¯"
var_dump($parser->match("♥", 0) === array("j" => 3, "value" => "♥"));
# 3-byte character "♥"
var_dump($parser->match("񋁂", 0) === array("j" => 4, "value" => "񋁂"));
# 4-byte character "񋁂"
var_dump($parser->match("", 0) === array("j" => 3, "value" => ""));