Skip to content

Instantly share code, notes, and snippets.

@ohader
Created December 19, 2016 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohader/ed0093e95f7f88714903425021f2b426 to your computer and use it in GitHub Desktop.
Save ohader/ed0093e95f7f88714903425021f2b426 to your computer and use it in GitHub Desktop.
<?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();
@sirmagid
Copy link

nic work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment