hello people's i have been trying to master this get parser itch i have been wanting to scratch. so i building this scanner class that opens a file and attempts to get the first char from the filehandle and convert it to a known type. After that Get The Next Char from the file handle and try and convert that aswell.
but my mind just went blank when it came to trying to convert the first returned char to and the next char to a known type and while all the time keep calling nextchar();
At the bottom i have a working testcase that converts and does what i need to do but how to implement it in code is a different story and where i'am currently stuck.
<?php
class Scanner
{
const string = 'STRING';
const equals = 'EQUALS';
const quote = 'QUOTE';
const space = 'SPACE';
const eol = 'EOL';
const int = 'INTEGER';
private static $source;
private static $handle;
private static $line;
private static $token;
private static $type;
private static $stack = array();
public function __construct($source)
{
if(file_exists($source))
{
self::$source = $source;
self::OpenSource();
}
}
public static function OpenSource()
{
self::$handle=fopen(self::GetSource() , 'r');
return self::$handle;
}
public static function GetSource()
{
return self::$source;
}
public static function GetChar()
{
while(!feof(self::$handle))
{
self::$token = fgetc(self::$handle);
return self::$token;
}
}
public static function NextChar()
{
self::$token++;
return self::GetChar();
}
public static function ConvertToType($char)
{
if($char == '\r' or $char == '\n' or $char =='\t')
{
return self::$type = self::eol;
}
elseif($char == ' ')
{
return self::$type = self::space;
}
elseif(is_numeric($char))
{
return self::$type = self::int;
}
elseif($char == "'" or $char == '"')
{
return self::$type = self::quote;
}
elseif($char == "=")
{
return self::$type = self::equals;
}
elseif(self::TokenIsString($char) == true)
{
return self::$type = self::string;
}
else
{
return self::$type = 'Char';
}
}
private static function TokenIsString($char)
{
if(preg_match('/[a-zA-Z_]/' , $char) >= 1)
{
return true;
}
else
{
return false;
}
}
}
$scan = new Scanner('text.txt');
print_r($scan->ConvertToType($scan->GetChar()));
print_r($scan->ConvertToType($scan->NextChar()));
print_r($scan->ConvertToType($scan->NextChar()));
print_r($scan->ConvertToType($scan->NextChar()));
print_r($scan->ConvertToType($scan->NextChar()));