Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
 function __construct()
 {
     parent::__construct(0);
     $ocb = mk_action('$context++; $state="code";');
     $ccb = mk_action('$context--; if ($context <= 0) $state = "rule";');
     $m1 = mk_action('$text = substr($text,1);');
     $m11 = mk_action('$text = substr($text,1,strlen($text)-2);');
     $go_rule = mk_action("\$state = 'rule';");
     $go_regex = mk_action("\$state = 'regex';");
     $test_reserved_word = mk_action("if (\$text=='ignore') \$type='ignore';");
     $nl = preg_pattern('([\\n\\r]+)', 'newline', 1, $go_regex);
     $cb = preg_pattern('(})', 'closebrace', 0, $go_rule);
     $ws = preg_pattern('(\\s+)', 'whitespace', 1, '');
     $cm = preg_pattern('(#[^\\n\\r]*)', 'comment', 1, '');
     $id = preg_pattern('(\\w+)', 'identifier', 0, $test_reserved_word);
     $dir = preg_pattern('(%(?:\\w+))', 'directive', 0, $m1);
     $div = preg_pattern('(%%)', 'divider', 0, $go_regex);
     $scope_tag = preg_pattern('(\\<(?:\\w+)>)', 'scope_tag', 0, $m11);
     $ob = preg_pattern('(\\{)', 'openbrace', 0, '');
     $regex = preg_pattern('(\\S+)', 'regex', 0, $go_rule);
     $modifier = preg_pattern('(/(?:[misxU]+))', 'modifier', 0, $m1);
     $openbrace_rule = preg_pattern('({)', 'openbrace', 0, $ocb);
     $this->add_state('code', array(preg_pattern('(\'(?:\\.|[^\'])*\')', 'sstring', 0, ''), preg_pattern('("(?:\\.|[^"])*")', 'dstring', 0, ''), preg_pattern('((?m)(?:#|//).*?$)', 'linecomment', 0, ''), preg_pattern('(/\\*.*?\\*/)', 'blockcomment', 0, ''), $openbrace_rule, preg_pattern('(})', 'closebrace', 0, $ccb), preg_pattern('([^\'"#/}{]+|.)', 'php', 0, '')));
     $this->add_state('INITIAL', array($dir, $div, $ws, $cm, $id));
     $this->add_state('regex', array($nl, $cb, $ws, $cm, $id, $scope_tag, $ob, $regex));
     $this->add_state('rule', array($nl, $cb, $ws, $cm, $id, $openbrace_rule, $modifier));
 }