コード例 #1
0
 /**
  * Generate an array of lexer states.
  *
  * @param string Mode selector, one of "data" or "file".
  * @param string Plex data if mode is "data", path to the plex file if mode
  * is "file".
  * @return array Array containing snapshot of lexer state after each call to
  * advance(), error count and any output.
  */
 function process($mode, $plex)
 {
     $result = array();
     switch ($mode) {
         case 'data':
             $lex = new PHP_LexerGenerator_Lexer($plex);
             break;
         case 'file':
             $lex = new PHP_LexerGenerator_Lexer(file_get_contents(dirname(__FILE__) . '/' . $plex));
             $result['source'] = str_replace($plex, '\\', '/');
             break;
         default:
             return false;
     }
     $states = array();
     ob_start();
     try {
         while ($lex->advance(null)) {
             $states[] = array('line' => $lex->line, 'token' => $lex->token, 'value' => $lex->value);
         }
     } catch (Exception $e) {
         $result['exception'] = (string) $e;
     }
     $result['output'] = ob_get_clean();
     $result['states'] = $states;
     $result['errors'] = $lex->errors;
     return $result;
 }
コード例 #2
0
 /**
  * Create a lexer file from its skeleton plex file.
  *
  * @param string Path to the plex file.
  * @param string Optional path to output file. Default is lexerfile with
  * extension of ".php".
  */
 function create($lexerfile, $outfile = '')
 {
     $this->_lex = new PHP_LexerGenerator_Lexer(file_get_contents($lexerfile));
     $info = pathinfo($lexerfile);
     if ($outfile) {
         $this->outfile = $outfile;
     } else {
         $this->outfile = $info['dirname'] . DIRECTORY_SEPARATOR . substr($info['basename'], 0, strlen($info['basename']) - strlen($info['extension'])) . 'php';
     }
     $this->_parser = new PHP_LexerGenerator_Parser($this->outfile, $this->_lex);
     if ($this->debug) {
         $this->_parser->PrintTrace();
     }
     while ($this->_lex->advance($this->_parser)) {
         $this->_parser->doParse($this->_lex->token, $this->_lex->value);
     }
     $this->_parser->doParse(0, 0);
 }