[英]use Exception class in namespace in php
I have two files file.index.php and lexical.php I use namespace in lexical.php and in this file I use from EXCEPTION class. 我有两个文件file.index.php和lexical.php,我在lexical.php中使用名称空间,在这个文件中,我使用EXCEPTION类。 When I run the code I receive error and I don't know why!!! 当我运行代码时,我收到错误,我也不知道为什么!!!
( ! ) Fatal error: Uncaught exception 'Exception' with message 'invalid character: ' in C:\wamp\www\oop\lexical\lexical.php on line 50
( ! ) Exception: invalid character: in C:\wamp\www\oop\lexical\lexical.php on line 50
this is index.php :: 这是index.php ::
<?php
require_once 'lexical/lexical.php';
require_once('Token.php');
use lexical;
$input = '[ a, b, c, d]';
$lexer = new lexical\ListLexer($input);
$token = $lexer->nextToken();
...
and this is lexical.php code :: 这是lexical.php代码::
<?php
namespace lexical;
use \Exception;
require_once('lexical/lexer.php');
class ListLexer extends \lexical\Lexer {
const NAME = 2;
const COMMA = 3;
const LBRACK = 4;
const RBRACK = 5;
static $tokenNames = array("n/a", "<EOF>",
"NAME", "COMMA",
"LBRACK", "RBRACK" );
public function getTokenName($x) {
return ListLexer::$tokenNames[$x];
}
public function ListLexer($input) {
parent::__construct($input);
}
public function isLETTER() {
return $this->c >= 'a' &&
$this->c <= 'z' ||
$this->c >= 'A' &&
$this->c <= 'Z';
}
public function nextToken() {
while ( $this->c != self::EOF ) {
switch ( $this->c ) {
case ' ' : case '\t': case '\n': case '\r': $this->WS();
continue;
case ',' : $this->consume();
return new Token(self::COMMA, ",");
case '[' : $this->consume();
return new Token(self::LBRACK, "[");
case ']' : $this->consume();
return new Token(self::RBRACK, "]");
default:
if ($this->isLETTER() ) return $this->NAME();
throw new Exception("invalid character: " . $this->c);
}
}
return new Token(self::EOF_TYPE,"<EOF>");
}
.
.
.
this is lexer.php :: 这是lexer.php ::
abstract class Lexer {
const EOF = -1; // represent end of file char
const EOF_TYPE = 1; // represent EOF token type
protected $input; // input string
protected $p = 0; // index into input of current character
protected $c; // current character
public function Lexer($input) {
$this->input = $input;
// prime lookahead
$this->c = substr($input, $this->p, 1);
}
/** Move one character; detect "end of file" */
public function consume() {
$this->p++;
if ($this->p >= strlen($this->input)) {
$this->c = Lexer::EOF;
}
else {
$this->c = substr($this->input, $this->p, 1);
}
}
public abstract function nextToken();
public abstract function getTokenName($tokenType);
}
What i do ? 我所做的 ?
Read your exception, $this->c is empty. 读取您的异常,$ this-> c为空。
default:
if ($this->isLETTER() ) return $this->NAME();
+ var_dump($this->c);
throw new Exception("invalid character: " . $this->c);
What does the self::EOF equal? self :: EOF等于什么? I think the issue is that input string you provided '[ a, b, c, d]' does not contain self::EOF character and hence you get an Exception when $this->c equal empty string at end of the input. 我认为问题在于您提供的[[a,b,c,d]'输入字符串不包含self :: EOF字符,因此当$ this-> c在输入末尾等于空字符串时,您将获得异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.