<?php
/*
include_once 'clazz/Zincluder.php';

$me = new Membre();
$me->connect();

if($me->isAdminLevelLowerThan(15)){
	echo 'Your admin level is too low (15 or more required)';
	exit;
}elseif(!isset($_POST['command'])){
	echo 'Please set an command in the URL (POST method ,name:"command")';
	exit;
}
*/
function isAlphaNumeric($char){
	return isAlphabetic($char) or isNumeric($char);
}
function isAlphabetic($char){
	return preg_match('#^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]$#',$char) === 1;
}
function isNumeric($char){
	return preg_match('#^[0123456789]$#',$char) === 1;
}

function error($pos,$reason){
	echo 'Error at char '.$pos.' : '.$reason;
	exit;
}

function readAlphabetic($command){
	global $pos;
	$out = "";
	while (isAlphaNumeric($command[$pos])) {
		$out .= $command[$pos];
		$pos+=1;
	}
	return $out;
}

function readNumeric($command){
	//TODO Add non-integer support (virgule , puissance , autres bases ...)
	global $pos;
	$out = "";
	while (isNumeric($command[$pos])) {
		$out .= $command[$pos];
		$pos+=1;
	}
	return intval($out);
}

function readString($command,$startChar='\"'){
	global $pos;
	$out = "";
	$startChar = $command[$pos];
	$pos+=1;
	while (TRUE) {
		$char = $command[$pos];
		if($char === $startChar)
			break;
		if($char === '\\'){
			$pos+=1;
			switch ($command[$pos]){
				case '\\':
					$char = '\\';
					break;
				case $startChar:
					$char = $startChar;
					break;
				default:
					error($pos,'Unexpected "'.$command['pos'].'" after "\\"');
			}
		}
		$out .= $char;
		$pos+=1;
	}
	$pos +=1;
	return $out;
}

$operators = array('=','!=','>','<','>=','=>','<=','=<','&has;','&nhas;');
$operatorsChars = array('=','!','<','>','&');

function readOperator($command,$endChar = 'abcdefghijklmnopqrstuvwxytABCDEFGHIJKLMNOPQRSTUVWZYZ0123456789"\''){
	global $pos,$operators;
	if($command[$pos] === '&'){
		$pos+=1;
		$inOperator = readAlphabetic($command);
		if($command[$pos] !== ';'){
			error($pos, 'Unexepted character at the end of the operator ' . $inOperator);
		}
		return '&'.$inOperator.';';
	}
	$reading = '';
	$lastOperator = '';
	$maxPos=min(strlen($command),$pos+5);
	$tPos = $pos;
	while ($tPos<$maxPos) {
		$reading .= $command[$pos];
		$tPos+=1;
		if(in_array($reading,$operators,TRUE))
			$lastOperator=$reading;
	}
	$pos += strLen($lastOperator);
	if(!in_array($lastOperator,$operators))
		error($pos, 'Unknown operator : '.$out);
	return $lastOperator;
}

function readSelector($command){
	global $pos,$operatorsChars;
	$out = array();
	$pos += 1;//@
	if(!isAlphabetic($command[$pos]))
		error($pos,'Unexepted non-alphabetic char "'.$command[$pos].'" after @');
	$className = readAlphabetic($command,'[');
	$pos += 1;//[ +1
	$attributes = array();
	while($command[$pos] !== ']'){
		if($command[$pos] === ',')
			$pos+=1;
		$attributeName = readAlphabetic($command,implode($operatorsChars));
		$operator = readOperator($command);
		$data = null;
		$type = $command[$pos];
		if(isAlphabetic($type)){
			$data = array('a',readAlphabetic($command));
		}elseif (isNumeric($type)){
			$data = array('0',readNumeric($command));
		}elseif ($type === '"' or $type === '\''){
			$data = array('\"',readString($command));
		}elseif ($type === "@"){
			$data = array('@',readSelector($command));
		}
		$attribute = array();
		$attribute[0] = $attributeName;
		$attribute[1] = $operator;
		$attribute[2] = $data;
		$attributes[] = $attribute;
	}
	$pos+=1;//after "["
	if($command[$pos] == ':'){
		$pos+=1;//letter after ":"
		$selectedAttribute = readAlphabetic($command);
		return array($className,$attributes,$selectedAttribute);
	}
	return array($className,$attributes);
}


$command=$_POST['command'].' ';
$nommes=array();
$pos=0;
while ($pos<strlen($command)) {
	echo $pos;
	$nomme = array();
	$type=$command[$pos];
	if(isAlphabetic($type)){
		$nomme[0] = 'a';
		$nomme[1] = readAlphabetic($command);
	}elseif (isNumeric($type)){
		$nomme[0] = '0';
		$nomme[1] = readNumeric($command);
	}elseif ($type === '"' or $type === '\''){
		$nomme[0] = '"';
		$nomme[1] = readString($command);
	}elseif (in_array($type,$operatorsChars,TRUE)){
		$nomme[0] = "=";
		$nomme[1] = readOperator($command);
	}elseif ($type === "@"){
		$nomme[0] = "@";
		$nomme[1] = readSelector($command);
	}
	elseif ($type === ' ')break;
	else error($pos, 'Unexpected char "'.$command[$pos].'" , cannot get the term type');
	$nommes[] = $nomme;
	$pos+=1;
}



echo '<pre>';
print_r($nommes);
echo '</pre>';

function exception($reason){
	echo 'An exception occurred : '.$reason;
	exit;
}

if($nommes[0][0] == 'a'){
	//Command
	$fonction = strtoupper($nommes[0][1]);
	switch ($fonction){
		case 'SET':
			if(count($nommes) !== 42)
				exception('La fonction n\'a pas recu le bon nombre d\'arguments (42)');
			if($nommes[1][0] !== '@')	
				exception('Le deuxième argument doit etre un selecteur');
			switch ($nommes[1][0][0]){
				case 'Membre':
					break;
				case 'Projet':
					break;
				case 'Membre':
					break;
				case 'Projet':
					break;
				case 'Membre':
					break;
				default:
					exception('Unknown selector class :'.$nommes[1][0][0]);
			}
		
		case 'DELETE':
		
		default:
			exception('Unknown function '.$fonction);
		
	}
	
}


