Created
November 17, 2015 20:18
-
-
Save saxenap/7b29fd8d47de375a9a73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NamespaceSeparator | |
{ | |
public function __toString() | |
{ | |
return '\x5c'; | |
} | |
} | |
class NamedGroup | |
{ | |
private $name, $regex; | |
public function __construct($name, $regex) | |
{ | |
$this->name = $name; | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return | |
(string) | |
new Group("?P<{$this->name}>{$this->regex}") | |
; | |
} | |
} | |
class ValidClassName | |
{ | |
public function __toString() | |
{ | |
return | |
(string) | |
new ZeroOrMoreOf( | |
new CharacterClass('a-zA-Z_\x7f-\xff') | |
. new CharacterClass('a-zA-Z0-9_\x7f-\xff') | |
) | |
; | |
} | |
} | |
class ValidNamespace | |
{ | |
public function __toString() | |
{ | |
return | |
new ValidClassName | |
. new Optional( | |
new ZeroOrMoreOf( | |
new Group(new NamespaceSeparator . new ValidClassName) | |
) | |
) | |
; | |
} | |
} | |
class Optional | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return (string) new Group("?:{$this->regex}") . '?'; | |
} | |
} | |
class Group | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return "({$this->regex})"; | |
} | |
} | |
class CharacterClass | |
{ | |
private $characters; | |
public function __construct($characters) | |
{ | |
$this->characters = $characters; | |
} | |
public function __toString() | |
{ | |
return | |
'[' | |
. $this->characters | |
. ']' | |
; | |
} | |
} | |
class ZeroOrMoreOf | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return "{$this->regex}*"; | |
} | |
} | |
class OneOrMoreOf | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return "{$this->regex}+"; | |
} | |
} | |
class Space | |
{ | |
public function __toString() | |
{ | |
return '\s'; | |
} | |
} | |
class SpacesAround | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return | |
new OneOrMoreOf(new Space) | |
. $this->regex | |
. new OneOrMoreOf(new Space) | |
; | |
} | |
} | |
class WordBoundary | |
{ | |
public function __toString() | |
{ | |
return '\b'; | |
} | |
} | |
class WordBoundaryAround | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return | |
new WordBoundary | |
. $this->regex | |
. new WordBoundary | |
; | |
} | |
} | |
class OptionalSpacesAround | |
{ | |
private $regex; | |
public function __construct($regex) | |
{ | |
$this->regex = $regex; | |
} | |
public function __toString() | |
{ | |
return | |
new OneOrMoreOf(new Optional(new Space)) | |
. $this->regex | |
. new OneOrMoreOf(new Optional(new Space)) | |
; | |
} | |
} | |
class Alteration | |
{ | |
private $options; | |
public function __construct(array $options) | |
{ | |
$this->options = $options; | |
} | |
public function __toString() | |
{ | |
return implode('|', $this->options); | |
} | |
} | |
class CaseInsensitiveMatch | |
{ | |
public function __toString() | |
{ | |
return 'i'; | |
} | |
} | |
class MultiLineMatch | |
{ | |
public function __toString() | |
{ | |
return 'm'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment