static function compile($codestr) { #-- ideally, we want to avoid generating our parser table at every compilation. #-- 2 layers of caching: using an external file, and using static vars. global $def_fun_track; static $lex = NULL; static $parser = NULL; if ($lex == NULL) { $t0 = microtime(1); $path = dirname(__FILE__) . "/js.ly.php"; if (file_exists($path)) { include $path; } else { $path = JS_CACHE_DIR . "/js.ly.php"; if (file_exists($path)) { include $path; } else { require_once dirname(__FILE__) . "/parse/generator.so.php"; $lexp = generate_scanner_from_file(dirname(__FILE__) . "/js.l", 0)->pattern; $dpa = generate_parser_from_file(dirname(__FILE__) . "/js.y"); file_put_contents($path, "<?php\n" . "// This file is dynamically generated from js.l and js.y by metaphp's CFG parser\n" . "// Do not waste your time editing it or reading it. Move along. Thank you.\n" . "\n" . $GLOBALS['func_def'] . "\$lexp = " . var_export($lexp, 1) . ";\n\$dpa = " . var_export($dpa, 1) . ";\n?>"); } } $t1 = microtime(1); $lex = new preg_scanner(0, $lexp); $parser = new easy_parser($dpa); $t2 = microtime(1); #echo "Table loading: ".($t1-$t0)." seconds<br>"; #echo "Pre generation: ".($t2-$t1)." seconds<br>"; } $t3 = microtime(1); $lex->start($codestr); $program = $parser->parse("Program", $lex); $t4 = microtime(1); #echo "Parse time: ".($t4-$t3)." seconds<br>"; # convert into usable php code try { $php = $program->emit(); } catch (Exception $e) { #-- Compilation error. should be pretty rare. usually the parser will barf long before this. echo "Compilation Error: " . $e->value->msg . "<hr>"; } return $php; }
function scanner_parser() { return new easy_parser(generate_parser_from_file(location_of_scanner_grammar())); }