@ 2014-06-14T15:44:56Z <?php
namespace Fu {
final class Exception extends \Exception {
}
final class Token {
public $tokenType;
public $content;
public $contentLength;
final public function __construct($tokenType, $content, $contentLength) {
$this->tokenType = $tokenType;
$this->content = $content;
$this->contentLength = $contentLength;
}
}
final class Parser {
const FU_TOKENTYPE_WHITESPACE = 0x00000001;
const FU_TOKENTYPE_DATATYPE_CHAR = 0x00000002;
const FU_TOKENTYPE_VARIABLE = 0x00000003;
const FU_TOKENTYPE_SEMICOLON = 0x00000004;
const FU_TOKENTYPE_OPERATOR_ASSIGN = 0x00000005;
const FU_TOKENTYPE_LITERAL_INTEGER = 0x00000006;
const FU_TOKENTYPE_OPERATOR_ADD = 0x00000007;
//const FU_SCOPE_ROOT = 0x00000001;
//const FU_SCOPE_DECLARATION_VARIABLE = 0x00000002;
//const FU_SCOPE_
final public static function parse($source) {
$tokens = [];
while ($source !== FALSE) {
$token = NULL;
switch (substr($source, 0, 1)) {
case ' ':
case "\t":
case "\n":
$i = 1;
while ($source{$i} === ' ' || $source{$i} === "\t" || $source{$i} === "\n") {
++$i;
}
$tokens[] = new Token(self::FU_TOKENTYPE_WHITESPACE, substr($source, 0, $i), $i);
$source = substr($source, $i + 1);
break;
case 'c':
if (substr($source, 0, 4) === 'char') {
$tokens[] = new Token(self::FU_TOKENTYPE_DATATYPE_CHAR, 'char', 4);
$source = substr($source, 4);
} else {
throw new Exception('Parse error at line x, position x; unidentified symbol');
}
break;
case '$':
$i = 1;
while (in_array($source{$i}, ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])) {
++$i;
}
$tokens[] = new Token(self::FU_TOKENTYPE_VARIABLE, substr($source, 0, $i), $i);
$source = substr($source, $i + 1);
break;
case ';':
$tokens[] = new Token(self::FU_TOKENTYPE_SEMICOLON, ';', 1);
$source = substr($source, 1);
break;
case '=':
$tokens[] = new Token(self::FU_TOKENTYPE_OPERATOR_ASSIGN, '=', 1);
$source = substr($source, 1);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
$i = 1;
while (in_array($source{$i}, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])) {
++$i;
}
// TODO: float parsing
$tokens[] = new Token(self::FU_TOKENTYPE_LITERAL_INTEGER, substr($source, 0, $i), $i);
$source = substr($source, $i + 1);
break;
case '+':
$tokens[] = new Token(self::FU_TOKENTYPE_OPERATOR_ADD, '+', 1);
$source = substr($source, 1);
break;
}
}
return $tokens;
}
}
}
namespace {
ini_set('memory_limit', '16M');
var_dump(Fu\Parser::parse('char $x;$x=1+2;'));
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Array and string offset access syntax with curly braces is no longer supported in /in/j8nqj on line 36
Process exited with code 255 . Output for 7.4.0 - 7.4.33 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/j8nqj on line 36
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/j8nqj on line 36
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/j8nqj on line 36
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/j8nqj on line 52
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/j8nqj on line 77
Process exited with code 137 . Output for 5.4.34 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.10 , 7.2.0 - 7.2.33 , 7.3.12 - 7.3.33 Process exited with code 137 . Output for 5.4.0 - 5.4.32 Warning: ini_set() has been disabled for security reasons in /in/j8nqj on line 96
Process exited with code 137 . Output for 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/j8nqj on line 28
Process exited with code 255 . Output for 4.4.2 - 4.4.9 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 Parse error: syntax error, unexpected T_STRING in /in/j8nqj on line 3
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 , 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_STRING in /in/j8nqj on line 3
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/j8nqj on line 3
Process exited with code 255 . preferences:dark mode live preview
240.55 ms | 401 KiB | 312 Q