Created
December 19, 2016 18:58
-
-
Save ohader/ed0093e95f7f88714903425021f2b426 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
<?php | |
// execute with PHP 7.1.0 CGI | |
class Lexer | |
{ | |
// All tokens that are not valid identifiers must be < 100 | |
const T_NONE = 1; | |
const T_STRING = 2; | |
const T_DOT = 8; | |
/** | |
* Retrieve token type. Also processes the token value if necessary. | |
* | |
* @param string $value | |
* @return int | |
*/ | |
protected function getType($value): int | |
{ | |
$type = self::T_NONE; | |
switch (true) { | |
// Recognize identifiers, aliased or qualified names | |
case ctype_alpha($value[0]): | |
$name = 'Lexer::T_' . strtoupper($value); | |
if (defined($name)) { | |
$type = constant($name); | |
if ($type > 100) { | |
return $type; | |
} | |
} | |
return self::T_STRING; | |
// Recognize symbols | |
case $value === '.': | |
return self::T_DOT; | |
// Default | |
default: | |
// Do nothing | |
} | |
return $type; | |
} | |
} | |
new Lexer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nic work