public function testIsComment() { $this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('#')); $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*')); $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('*/')); $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- ')); $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\t")); $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\n")); $this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('# a comment')); $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*comment */')); $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- my comment')); $this->assertEquals(null, Context::isComment('--not a comment')); }
/** * Parses a comment. * * @return Token */ public function parseComment() { $iBak = $this->last; $token = $this->str[$this->last]; // Bash style comments. (#comment\n) if (Context::isComment($token)) { while (++$this->last < $this->len && $this->str[$this->last] !== "\n") { $token .= $this->str[$this->last]; } $token .= $this->str[$this->last]; return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_BASH); } // C style comments. (/*comment*\/) if (++$this->last < $this->len) { $token .= $this->str[$this->last]; if (Context::isComment($token)) { $flags = Token::FLAG_COMMENT_C; if ($this->last + 1 < $this->len && $this->str[$this->last + 1] === '!') { // It is a MySQL-specific command. $flags |= Token::FLAG_COMMENT_MYSQL_CMD; } while (++$this->last < $this->len && ($this->str[$this->last - 1] !== '*' || $this->str[$this->last] !== '/')) { $token .= $this->str[$this->last]; } $token .= $this->str[$this->last]; return new Token($token, Token::TYPE_COMMENT, $flags); } } // SQL style comments. (-- comment\n) if (++$this->last < $this->len) { $token .= $this->str[$this->last]; if (Context::isComment($token)) { if ($this->str[$this->last] !== "\n") { // Checking if this comment did not end already (```--\n```). while (++$this->last < $this->len && $this->str[$this->last] !== "\n") { $token .= $this->str[$this->last]; } if ($this->last < $this->len) { $token .= $this->str[$this->last]; } } return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_SQL); } } $this->last = $iBak; return null; }
/** * Parses a comment. * * @return Token */ public function parseComment() { $iBak = $this->last; $token = $this->str[$this->last]; // Bash style comments. (#comment\n) if (Context::isComment($token)) { while (++$this->last < $this->len && $this->str[$this->last] !== "\n") { $token .= $this->str[$this->last]; } $token .= "\n"; // Adding the line ending. return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_BASH); } // C style comments. (/*comment*\/) if (++$this->last < $this->len) { $token .= $this->str[$this->last]; if (Context::isComment($token)) { $flags = Token::FLAG_COMMENT_C; // This comment already ended. It may be a part of a // previous MySQL specific command. if ($token === '*/') { return new Token($token, Token::TYPE_COMMENT, $flags); } // Checking if this is a MySQL-specific command. if ($this->last + 1 < $this->len && $this->str[$this->last + 1] === '!') { $flags |= Token::FLAG_COMMENT_MYSQL_CMD; $token .= $this->str[++$this->last]; while (++$this->last < $this->len && '0' <= $this->str[$this->last] && $this->str[$this->last] <= '9') { $token .= $this->str[$this->last]; } --$this->last; // We split this comment and parse only its beginning // here. return new Token($token, Token::TYPE_COMMENT, $flags); } // Parsing the comment. while (++$this->last < $this->len && ($this->str[$this->last - 1] !== '*' || $this->str[$this->last] !== '/')) { $token .= $this->str[$this->last]; } // Adding the ending. if ($this->last < $this->len) { $token .= $this->str[$this->last]; } return new Token($token, Token::TYPE_COMMENT, $flags); } } // SQL style comments. (-- comment\n) if (++$this->last < $this->len) { $token .= $this->str[$this->last]; if (Context::isComment($token)) { // Checking if this comment did not end already (```--\n```). if ($this->str[$this->last] !== "\n") { while (++$this->last < $this->len && $this->str[$this->last] !== "\n") { $token .= $this->str[$this->last]; } $token .= "\n"; // Adding the line ending. } return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_SQL); } } $this->last = $iBak; return null; }