#3 - Moving away from JSM\Parser lib. Parser and Lexer now relying on Doctrine

This commit is contained in:
Eduardo Gulias Davis
2013-05-19 20:41:15 +02:00
parent 5462ecbd75
commit 1bb1fe652b
5 changed files with 92 additions and 61 deletions
Generated
+2 -2
View File
@@ -79,12 +79,12 @@
"version": "dev-master",
"source": {
"type": "git",
"url": "git://github.com/schmittjoh/parser-lib",
"url": "https://github.com/schmittjoh/parser-lib.git",
"reference": "4d469a70c6dd03f921cbdeadafbcb261bb23e8b0"
},
"dist": {
"type": "zip",
"url": "https://github.com/schmittjoh/parser-lib/archive/4d469a70c6dd03f921cbdeadafbcb261bb23e8b0.zip",
"url": "https://api.github.com/repos/schmittjoh/parser-lib/zipball/4d469a70c6dd03f921cbdeadafbcb261bb23e8b0",
"reference": "4d469a70c6dd03f921cbdeadafbcb261bb23e8b0",
"shasum": ""
},
+37 -34
View File
@@ -2,9 +2,9 @@
namespace egulias\EmailValidator;
use JMS\Parser\AbstractLexer;
use Doctrine\Common\Lexer;
class EmailLexer extends AbstractLexer
class EmailLexer extends Lexer
{
//ASCII values
const C_DEL = 127;
@@ -79,6 +79,7 @@ class EmailLexer extends AbstractLexer
* @param string $type
*
* @return string $name
* @throws \InvalidArgumentException
*/
public function getName($type)
{
@@ -122,7 +123,7 @@ class EmailLexer extends AbstractLexer
public function isNext($type)
{
return null !== $this->next && $type === $this->next[0];
return null !== $this->next && $type === $this->next['type'];
}
public function isNextAny(array $types)
@@ -131,56 +132,58 @@ class EmailLexer extends AbstractLexer
$types[$i] = array_search($type, $this->charValue);
}
return parent::isNextAny($types);
return parent::isNextTokenAny($types);
}
/**
* {@inherit}
* Lexical catchable patterns.
*
* @return array
*/
protected function getRegex()
protected function getCatchablePatterns()
{
return '/
# Alphabetic-chars and IPv6 or 4
([a-zA-Z]+[4,6]?)
# Numbers
| ([0-9]+)
#CRLF
| (\r\n)
#double colon
| (::)
# Whitespace
| (\s+)
#Special
| ([\x1-\x1F]+)
# Anything that is left as single character tokens
| (.)
/xu';
return array(
'[a-zA-Z]+[4,6]?',
'[0-9]+',
'\r\n',
'::',
'\s+',
'[\x1-\x1F]+',
'.'
);
}
/**
* {@inherit}
* Lexical non-catchable patterns.
*
* @return array
*/
protected function determineTypeAndValue($value)
protected function getNonCatchablePatterns()
{
return array('[\x7f-\xff]+');
}
/**
* Retrieve token type. Also processes the token value if necessary.
*
* @param string $value
* @throws \InvalidArgumentException
* @return integer
*/
protected function getType(&$value)
{
if (isset($this->charValue[$value])) {
return array($value, $this->charValue[$value]);
return $this->charValue[$value];
}
if (preg_match('/[\x1-\x1F]+/', $value)) {
return array($value, self::INVALID);
return self::INVALID;
}
if (preg_match('/[\x7f-\xff]+/', $value)) {
throw new \InvalidArgumentException(sprintf('There is no token with value %s.', json_encode($value)));
}
return array($value, self::GENERIC);
return self::GENERIC;
}
}
+46 -20
View File
@@ -9,7 +9,7 @@ use JMS\Parser\AbstractParser;
*
* @author Eduardo Gulias Davis <me@egulias.com>
*/
class EmailParser extends AbstractParser
class EmailParser
{
protected $warnings = array();
@@ -17,10 +17,36 @@ class EmailParser extends AbstractParser
protected $domainPart = '';
protected $index = 0;
public function __construct(EmailLexer $lexer)
{
$this->lexer = $lexer;
}
/**
* Parses the given input.
*
* @param string $str
* @param string $context parsing context (allows to produce better error messages)
*
* @return mixed
*/
public function parse($str)
{
$this->lexer->setInput($str);
$rs = $this->parseInternal();
if (null !== $this->lexer->next) {
throw new \Exception('Endo of input');
}
return $rs;
}
/**
* {@inherit}
*/
public function parseInternal()
protected function parseInternal()
{
$this->lexer->moveNext();
if ($this->lexer->token[0] === EmailLexer::S_AT) {
@@ -325,12 +351,12 @@ class EmailParser extends AbstractParser
private function parseLocalPart()
{
$closingQuote = false;
while ($this->lexer->token[0] !== EmailLexer::S_AT) {
while ($this->lexer->token['type'] !== EmailLexer::S_AT) {
$previous = $this->lexer->getPrevious();
if ($this->lexer->token[0] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) {
if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) {
throw new \InvalidArgumentException('ERR_DOT_START');
}
if ($this->lexer->token[0] === EmailLexer::S_DQUOTE) {
if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) {
if (!$closingQuote) {
if ($this->lexer->isNext(EmailLexer::GENERIC) && $previous[0] === EmailLexer::GENERIC) {
throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT');
@@ -349,19 +375,19 @@ class EmailParser extends AbstractParser
//$this->warnings[] = EmailValidator::DEPREC_LOCALPART;
}
//Comments
if ($this->lexer->token[0] === EmailLexer::S_OPENPARENTHESIS) {
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
$this->parseComments();
}
if ($this->lexer->token[0] === EmailLexer::S_DOT && $this->lexer->isNext(EmailLexer::S_DOT)) {
if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNext(EmailLexer::S_DOT)) {
throw new \InvalidArgumentException('ERR_CONSECUTIVEDOTS');
}
if ($this->lexer->token[0] === EmailLexer::S_DOT && $this->lexer->isNext(EmailLexer::S_AT)) {
if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNext(EmailLexer::S_AT)) {
throw new \InvalidArgumentException('ERR_DOT_END');
}
if ($this->lexer->token[0] === EmailLexer::S_BACKSLASH) {
if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) {
//if ($this->lexer->isNextAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
// $this->warnings[] = EmailValidator::DEPREC_QP;
//}
@@ -380,11 +406,11 @@ class EmailParser extends AbstractParser
}
if (
$this->lexer->token[0] === EmailLexer::S_SP ||
$this->lexer->token[0] === EmailLexer::S_HTAB ||
$this->lexer->token[0] === EmailLexer::S_CR ||
$this->lexer->token[0] === EmailLexer::S_LF ||
$this->lexer->token[0] === EmailLexer::CRLF
$this->lexer->token['type'] === EmailLexer::S_SP ||
$this->lexer->token['type'] === EmailLexer::S_HTAB ||
$this->lexer->token['type'] === EmailLexer::S_CR ||
$this->lexer->token['type'] === EmailLexer::S_LF ||
$this->lexer->token['type'] === EmailLexer::CRLF
) {
$this->parseFWS();
}
@@ -415,7 +441,7 @@ class EmailParser extends AbstractParser
//$this->lexer->moveNext();
//scaping in a comment
if ($this->lexer->token[0] === EmailLexer::S_BACKSLASH) {
if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) {
if ($this->lexer->isNextAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
$this->warnings[] = EmailValidator::DEPREC_QP;
}
@@ -441,21 +467,21 @@ class EmailParser extends AbstractParser
{
$previous = $this->lexer->getPrevious();
if ($this->lexer->token[0] === EmailLexer::CRLF && $this->lexer->isNext(EmailLexer::CRLF)) {
if ($this->lexer->token['type'] === EmailLexer::CRLF && $this->lexer->isNext(EmailLexer::CRLF)) {
throw new \InvalidArgumentException("ERR_FWS_CRLF_X2");
}
if ($this->lexer->token[0] === EmailLexer::S_CR) {
if ($this->lexer->token['type'] === EmailLexer::S_CR) {
throw new \InvalidArgumentException("ERR_CR_NO_LF");
}
if (
!$this->lexer->isNextAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB)) &&
$this->lexer->token[0] === EmailLexer::CRLF ) {
$this->lexer->token['type'] === EmailLexer::CRLF ) {
throw new \InvalidArgumentException("ERR_FWS_CRLF_END");
}
if ($this->lexer->isNext(EmailLexer::GENERIC) && $this->lexer->token[0] !== EmailLexer::S_SP) {
if ($this->lexer->isNext(EmailLexer::GENERIC) && $this->lexer->token['type'] !== EmailLexer::S_SP) {
throw new \InvalidArgumentException("ERR_ATEXT_AFTER_CFWS");
}
if ($this->lexer->token[0] === EmailLexer::S_LF || $this->lexer->token[0] === EmailLexer::C_NUL) {
if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
throw new \InvalidArgumentException('ERR_EXPECTING_CTEXT');
}
@@ -1,6 +1,6 @@
<?php
namespace egulias\Tests\EmailValidator;
namespace egulias\EmailValidator\Tests;
use egulias\EmailValidator\EmailLexer;
@@ -10,7 +10,7 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
public function testLexerExtendsLib()
{
$lexer = new EmailLexer();
$this->assertInstanceOf('JMS\Parser\AbstractLexer', $lexer);
$this->assertInstanceOf('Doctrine\Common\Lexer', $lexer);
}
/**
@@ -22,16 +22,18 @@ class EmailLexerTests extends \PHPUnit_Framework_TestCase
$lexer = new EmailLexer();
$lexer->setInput($str);
$lexer->moveNext();
$this->assertEquals($token, $lexer->token[0]);
$lexer->moveNext();
$this->assertEquals($token, $lexer->token['type']);
}
public function testLexerForTab()
{
$lexer = new EmailLexer();
$lexer->setInput("foo\tbar");
$lexer->moveNext();
$lexer->skipUntil(EmailLexer::S_HTAB);
$lexer->moveNext();
$this->assertEquals(EmailLexer::S_HTAB, $lexer->token[0]);
$this->assertEquals(EmailLexer::S_HTAB, $lexer->token['type']);
}
public function getTokens()
@@ -10,6 +10,6 @@ class EmailParserTests extends \PHPUnit_Framework_TestCase
{
$mock = $this->getMock('egulias\EmailValidator\EmailLexer');
$parser = new EmailParser($mock);
$this->assertInstanceOf('JMS\Parser\AbstractParser', $parser);
$this->markTestIncomplete();
}
}