/** * The major task of a parser: parse a file and build class maps * @param string $file file to be parsed * @param array classes to be parsed (all if not specified) * @return bool * @access public * @see token_get_all() */ public function parse($file, $classes = null) { // get instance of class map factory $this->cmf = epClassMapFactory::instance(); // check if file exists if (!file_exists($file)) { throw new epExceptionParser('File [' . $file . '] does not exist.'); return false; } // get file content $content = file_get_contents($file); if (empty($content)) { throw new epExceptionParser('File [' . $file . '] is empty.'); return false; } // set the classes to be parsed $this->classes_to_parse = $classes; // keep track of the current file and tokens $this->file = $file; // reset comment $this->comment = ''; // setup scanner if (!$this->scanner) { include_once EP_SRC_COMPILER . '/epScanner.php'; $this->scanner = new epScanner(); } // setup FSM if (!$this->setupFSM()) { throw new epExceptionParser('Failed to setup FSM. Quit parsing.'); return false; } // set input content to scanner $this->scanner->input($content); // go through tokens while (false !== ($this->token = $this->scanner->next())) { // get token name $token_name = $this->token; if (is_array($this->token)) { $token_name = token_name($this->token[0]); } // intercept comments if ($token_name == 'T_COMMENT' || $token_name == 'T_DOC_COMMENT') { $this->comment = $this->token[1]; continue; } // drive FSM with token name we care if (!in_array($token_name, epClassParser::$tokens_to_process)) { continue; } $this->fsm->process($token_name); } return true; }