Exemple de code source permettant d'affichage en mode télétypé de différentes couleurs un code source de langage Pascal. Ainsi par exemple des mots réservé comme «Procedure» ou «Begin» sortiront en blanc et en gras tandis que les symboles en cyan par exemple. Voici donc l'exemple montrant le fonctionnement :
- <?php
- // Bibliothèque de Mentronix
- // Tous droits réservés par Gladir.com XXIe siècle
- // Programmeur: Sylvain Maltais
-
- function ShowTextToPascalHighlighting($Text) {
- echo "<TABLE BGCOLOR=\"#000080\" Width=100%><TR><TD>";
- echo TextToPascalHighlighting($Text);
- echo "</TD></TR></TABLE>";
- }
-
- function TextToPascalHighlighting($Text) {
- $ResWord = array("AND","ARRAY","ASM","ASSEMBLER","BEGIN","CASE","CONST","CONSTRUCTOR","DESTRUCTOR",
- "DIV","DO","ELSE","END","FAR","FOR","FUNCTION","IF","IN","INLINE","MOD","NEAR","NIL","NOT",
- "OBJECT","OF","OR","TO","PROCEDURE","PROGRAM","RECORD","REPEAT","SET","STRING","THEN","TYPE",
- "UNIT","UNTIL","USES","VAR","WHILE","XOR");
- $ResWordAsm = array("AAA","AAD","AAM","ADC","ADD","AND","CLD","CLI","CMP","DEC","DIV","IN","INC","JA","JB",
- "JC","JCXZ","JE","JMP","JNB","JNC","JNE","JNZ","JZ","INT","LDS","LES","LODSB","LODSD",
- "LODSW","LOOP","MOV","MOVSB","MOVSD","MOVSW","MUL","NOT","OUT","OUTSB","OUTSD","OUTSD",
- "POP","POPF","PUSH","PUSHF","OR","REP","ROL","SHL","SHR","STC","STD","STI","STOSB","STOSD",
- "STOSW","SBB","SUB","TEST","XCHG","XOR");
- $RegAsm = array("AH","AL","AX","BH","BL","BP","BX","CH","CL","CX","DI","DH","DL","DS","DX","ES","SI","SP","SS");
- $Output = "";
- $ModeASM = false;
- $Rem = false;
- $Line = explode("\n",$Text);
- for($J=0;$J<count($Line);$J++) {
- $CurrLine = "";
- for($I=0;$I<strlen($Line[$J]);$I++) {
- $Chr = strtoupper($Line[$J][$I]);
- if((($Chr>='A')&&($Chr<='Z'))||($Chr=='_')) {
- $Word = "";
- while((($Chr>='A')&&($Chr<='Z'))||(($Chr>='0')&&($Chr<='9'))||($Chr=='_')) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- }
- $I--;
- if($ModeASM) {
- if(strtoupper($Word) == "END") {
- $ModeASM = false;
- $CurrLine .= "<B><FONT COLOR=\"#ffffff\">" . $Word . "</FONT></B>";
- } else {
- if(array_keys($RegAsm,strtoupper($Word))) {
- $CurrLine .= "<FONT COLOR=\"#008000\">" . $Word . "</FONT>";
- } elseif(array_keys($ResWordAsm,strtoupper($Word))) {
- $CurrLine .= "<FONT COLOR=\"#00FF00\">" . $Word . "</FONT>";
- } else {
- $CurrLine .= "<FONT COLOR=\"#ff00ff\">" . $Word . "</FONT>";
- }
- }
- } else {
- if(array_keys($ResWord,strtoupper($Word))) {
- $CurrLine .= "<B><FONT COLOR=\"#ffffff\">" . $Word . "</FONT></B>";
- } else {
- $CurrLine .= "<FONT COLOR=\"#ff00ff\">" . $Word . "</FONT>";
- }
- }
- if(!$ModeASM) {
- if(strtoupper($Word) == "ASM") $ModeASM = true;
- }
- } elseif(($Chr>='0')&&($Chr<='9')) {
- $Word = "";
- if($ModeASM) {
- while((($Chr>='A')&&($Chr<='F'))||(($Chr>='0')&&($Chr<='9'))) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- }
- } else {
- while(($Chr>='0')&&($Chr<='9')) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = $Line[$J][$I];
- }
- }
- if(strtoupper($Chr)=="H") {
- $Word .= $Line[$J][$I];
- $I++;
- }
- $I--;
- $CurrLine .= "<FONT COLOR=\"#7777ff\">" . $Word . "</FONT>";
- } else switch($Line[$J][$I]) {
- case "$":
- $Word = $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- while((($Chr>='A')&&($Chr<='F'))||(($Chr>='0')&&($Chr<='9'))) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- }
- $I--;
- $CurrLine .= "<FONT COLOR=\"#7777ff\">" . $Word . "</FONT>";
- break;
- case "@":
- if($ModeASM) {
- $Rem = true;
- $Word = "";
- while(($Chr!=':')&&($Chr!=' ')&&($Chr!=null)) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- }
- if($Chr=='}') {
- $Word .= $Line[$J][$I];
- $I++;
- }
- $I--;
- $CurrLine .= "<FONT COLOR=\"#ccffff\">" . $Word . "</FONT>";
- break;
- }
- case "(":case ")":case ";":case ":":case "=":case "-":case "+":case ",":case "[":case "]":
- case ".":case ">":case "<":case "*":case "/":
- $CurrLine .= "<FONT COLOR=\"#00ffff\">" . $Line[$J][$I] . "</FONT>";
- break;
- case "{":
- $Rem = true;
- $Word = "";
- while(($Chr!='}')&&($Chr!=null)) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- }
- if($Chr=='}') {
- $Word .= $Line[$J][$I];
- $I++;
- }
- $I--;
- $CurrLine .= "<FONT COLOR=\"#ffff00\">" . $Word . "</FONT>";
- break;
- case "'":
- $Rem = true;
- $Word = $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- while(($Chr!="'")&&($Chr!=null)) {
- $Word .= $Line[$J][$I];
- $I++;
- $Chr = strtoupper($Line[$J][$I]);
- }
- if($Chr=="'") {
- $Word .= $Line[$J][$I];
- $I++;
- }
- $I--;
- $CurrLine .= "<FONT COLOR=\"#ff0000\">" . $Word . "</FONT>";
- break;
- case " ":
- $CurrLine .= " ";
- break;
- default:
- $CurrLine .= $Line[$J][$I];
- break;
- }
- }
- $Output .= $CurrLine . "</BR>";
- }
- return $Output;
- }
- ?>
Dernière mise à jour : Lundi, le 19 octobre 2009