nbLines= nbNl($buffer)+1; $this->tokens=Array(); $this->tokenTypes=Array(); $this->tokenArgs=Array(); $this->currTokenIdx=-1; $avant=""; while($len>0) { $next= strcspn($buffer, "\n|{}[]-<"); if($next!=0) { $avant.=substr($buffer, 0, $next); $buffer=substr($buffer, $next); $len-=$next; if($len==0) break; } $token=""; $car=$buffer{0}; switch($car) { # AVANT SWITCH SPLIT case "\n": if(strncmp($buffer, "\n\n|", 3)==0) { $match=Array("\n\n|"); $token="debTableau"; } elseif(preg_match("/^\n\n*(-{4,}|_{4,})\\s*/", $buffer, $match)) { $token="ligne"; } elseif(preg_match("/^\n\n*-(\\*+|#+)\\s+/", $buffer, $match)) { $token="puce"; } elseif(preg_match("/^\n\n*_\\s+/", $buffer, $match)) { $token="br"; } elseif(preg_match("/^\n\n*(\n|-\\s+)/", $buffer, $match)) { $token="saut"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "|": if(strncmp($buffer, "|\n|", 3)==0) { $match=Array("|\n|"); $token="sepLigneTableau"; } elseif(strncmp($buffer, "|\n\n", 3)==0) { $match=Array("|\n\n"); $token="finTableau"; } elseif(strncmp($buffer, "|", 1)==0) { $match=Array("|"); $token="sepTableau"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "{": if(strncmp($buffer, "{{{", 3)==0) { $match=Array("{{{"); $token="debInterTitre"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "}": if(strncmp($buffer, "}}}", 3)==0) { $match=Array("}}}"); $token="finInterTitre"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "[": if(strncmp($buffer, "[[", 2)==0) { $match=Array("[["); $token="debNote"; } elseif(strncmp($buffer, "[", 1)==0) { $match=Array("["); $token="debLien"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "]": if(strncmp($buffer, "]]", 2)==0) { $match=Array("]]"); $token="finNote"; } elseif(strncmp($buffer, "]", 1)==0) { $match=Array("]"); $token="finLien"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "-": if(strncmp($buffer, "->", 2)==0) { $match=Array("->"); $token="sepLien"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; case "<": if(strncmp($buffer, "", 7)==0) { $match=Array(""); $token="debQuote"; } elseif(strncmp($buffer, "", 8)==0) { $match=Array(""); $token="finQuote"; } elseif(strncmp($buffer, "", 6)==0) { $match=Array(""); $token="debCode"; } elseif(strncmp($buffer, "", 7)==0) { $match=Array(""); $token="finCode"; } elseif(strncmp($buffer, "", 7)==0) { $match=Array(""); $token="debCadre"; } elseif(strncmp($buffer, "", 8)==0) { $match=Array(""); $token="finCadre"; } elseif(strncmp($buffer, "", 6)==0) { $match=Array(""); $token="debHtml"; } elseif(strncmp($buffer, "", 7)==0) { $match=Array(""); $token="finHtml"; } elseif(strncmp($buffer, "", 8)==0) { $match=Array(""); $token="debPoesie1"; } elseif(strncmp($buffer, "", 8)==0) { $match=Array(""); $token="debPoesie2"; } elseif(strncmp($buffer, "", 9)==0) { $match=Array(""); $token="finPoesie1"; } elseif(strncmp($buffer, "", 9)==0) { $match=Array(""); $token="finPoesie2"; } else { $avant.=$car; $buffer=substr($buffer, 1); $len--; } break; # APRES SWITCH SPLIT default: if($avant!="") { $this->tokens[]=$avant; $this->tokenTypes[]="TEXTE"; $this->tokenArgs[]=null; $avant=""; } $this->tokenTypes[]=$car; $this->tokens[]=$car; $this->tokenArgs[]=null; $buffer=substr($buffer, 1); $len--; break; } if($token!="") { $t=array_shift($match); $l=strlen($t); # on garde le reste $buffer=substr($buffer, $l); $len-=$l; if($avant!="") { $this->tokens[]=$avant; $this->tokenTypes[]="TEXTE"; $this->tokenArgs[]=null; $avant=""; } $this->tokenTypes[]=$token; $this->tokens[]=$t; $this->tokenArgs[]=$match; } } if($avant!="") { $this->tokens[]=$avant; $this->tokenTypes[]="TEXTE"; $this->tokenArgs[]=null; $avant=""; } $this->tokens[]=null; $this->tokenTypes[]="EOF"; $this->tokenArgs[]=null; $this->lastToken=count($this->tokens)-1; return $tokens; } function parse($buffer) { //tic(); $this->buffer=$buffer; $this->split($buffer); //tac("split -> ".$this->lastToken." tokens"); //print_r($this->tokens); # AVANT_PARSE global $courant; # FIN AVANT_PARSE $__spipBlocs_etat=array('global'); $__spipBlocs_fin=FALSE; $this->nextToken(); do { # REGLES $found=0; switch($__spipBlocs_etat[0]) { case '_InterTitre': switch($this->currTokenType) { case "finInterTitre": array_shift($__spipBlocs_etat); popElement("InterTitre"); $found=1; break; } break; case '_Note': switch($this->currTokenType) { case "finNote": array_shift($__spipBlocs_etat); popElement("Note"); $found=1; break; } break; case '_Lien': switch($this->currTokenType) { case "sepLien": $__spipBlocs_etat[0]= '_apresLien'; $courant->content=array("URL" => ""); $found=1; break; case "finLien": array_shift($__spipBlocs_etat); $courant->content=array("URL" => null); popElement("Lien"); $found=1; break; } break; case '_apresLien': switch($this->currTokenType) { case "finLien": array_shift($__spipBlocs_etat); popElement("Lien"); $found=1; break; default: $courant->content["URL"].=$this->currToken; $found=1; break; } break; case '_Tableau': switch($this->currTokenType) { case "sepTableau": popElement("Cellule"); $courant= &pushElement("Cellule", false); $found=1; break; case "sepLigneTableau": popElement("Cellule"); popElement("Ligne"); pushElement("Ligne", false); $courant= &pushElement("Cellule", false); $found=1; break; case "finTableau": array_shift($__spipBlocs_etat); popElement("Cellule"); popElement("Ligne"); popElement("Tableau"); $found=1; break; } break; case '_Quote': switch($this->currTokenType) { case "finQuote": array_shift($__spipBlocs_etat); popElement("Quote"); $found=1; break; } break; case '_Code': switch($this->currTokenType) { case "finCode": array_shift($__spipBlocs_etat); popElement("Code"); $found=1; break; default: pushText($this->currToken); $found=1; break; } break; case '_Cadre': switch($this->currTokenType) { case "finCadre": array_shift($__spipBlocs_etat); popElement("Cadre"); $found=1; break; default: pushText($this->currToken); $found=1; break; } break; case '_Html': switch($this->currTokenType) { case "finHtml": array_shift($__spipBlocs_etat); popElement("Html"); $found=1; break; default: pushText($this->currToken); $found=1; break; } break; case '_Poesie': switch($this->currTokenType) { case "finPoesie1": array_shift($__spipBlocs_etat); popElement("Poesie"); $found=1; break; case "finPoesie2": array_shift($__spipBlocs_etat); popElement("Poesie"); $found=1; break; } break; case '_Puce': switch($this->currTokenType) { case "saut": array_shift($__spipBlocs_etat); popElement("Puce"); $found=1; break; case "puce": array_shift($__spipBlocs_etat); popElement("Puce"); continue; $found=1; break; case "debInterTitre": array_shift($__spipBlocs_etat); popElement("Puce"); continue; $found=1; break; } break; } if($found) { $this->nextToken(); continue; } switch($this->currTokenType) { case "EOF": $__spipBlocs_etat[0]= 'EOF'; echo "FIN\n"; $found=1;$__spipBlocs_fin=TRUE;break; $found=1; break; case "saut": $courant= &addElement("Saut", true); $courant->content= trim($this->currToken); $found=1; break; case "br": $courant= &addElement("Saut", true); $courant->content= '_'; $found=1; break; case "puce": $__spipBlocs_etat[0]= '_Puce'; $puce= trim($this->currToken); $niveau= strlen($puce)-1; $type= ($puce{1}=="*")?"ul":"ol"; $courant= &pushElement("Puce", false); $courant->content= array("type" => $type, "niveau" => $niveau); $found=1; break; case "debInterTitre": array_unshift($__spipBlocs_etat, '_InterTitre'); $courant= &pushElement("InterTitre", false); $found=1; break; case "debNote": array_unshift($__spipBlocs_etat, '_Note'); $courant= &pushElement("Note", false); $found=1; break; case "debLien": array_unshift($__spipBlocs_etat, '_Lien'); $courant= &pushElement("Lien", true); $found=1; break; case "debTableau": array_unshift($__spipBlocs_etat, '_Tableau'); $courant= &pushElement("Tableau", false); $courant= &pushElement("Ligne", false); $courant= &pushElement("Cellule", false); $found=1; break; case "debQuote": array_unshift($__spipBlocs_etat, '_Quote'); $courant= &pushElement("Quote", false); $found=1; break; case "debCode": array_unshift($__spipBlocs_etat, '_Code'); $courant= &pushElement("Code", false); $found=1; break; case "debCadre": array_unshift($__spipBlocs_etat, '_Cadre'); $courant= &pushElement("Cadre", false); $found=1; break; case "debHtml": array_unshift($__spipBlocs_etat, '_Html'); $courant= &pushElement("Html", false); $found=1; break; case "debPoesie1": array_unshift($__spipBlocs_etat, '_Poesie'); $courant= &pushElement("Poesie", false); $found=1; break; case "debPoesie2": array_unshift($__spipBlocs_etat, '_Poesie'); $courant= &pushElement("Poesie", false); $found=1; break; default: pushText($this->currToken); $found=1; break; } if($found) { $this->nextToken(); continue; } switch($__spipBlocs_etat[0]) { default: die('Parsing error'); break; } # FIN REGLES $this->nextToken(); } while(!$__spipBlocs_fin && $this->currToken!="EOF"); # APRES_PARSE # FIN APRES_PARSE } } ?>