... # the second parameter is the callback function for processing the text between the tags $wgParser->setHook('asm','ShowAsm'); $wgParser->setHook('cpp','ShowCpp'); $wgParser->setHook('pas','ShowPas'); } */ # Retrieve Wiki link for specified label name function getLink($s){ # global $wgParser,$wgTitle,$wgOut; # $output = $wgParser->parse("[[Code:KernelSrc/$s|$s]]",$wgTitle,$wgOut->mParserOptions,true); # $output = str_replace(array('

','

'),array('',''),$output->getText()); # return str_replace(array('<','>','"','&'),array("\1","\2","\3","\4"),trim($output)); return "\1a href='/index.php?title=$s\4action=edit'\2$s\1/a\2"; } # Syntax highlighting for FASM code by Mike V. Semenyako (mike.dld) function ShowAsm($input){ $sym="#&*\\:/<>|{}()[]=+-,"; $result=''; $input=trim($input,"\r\n"); $l=strlen($input); $input.="\5"; $i=0;$ac=true; while($i<$l){ $c=$input[$i]; if($c==';'){ $result.="\1tt class='hl1'\2"; while(1) { while(!strchr("\r\n\5",$input[$i]))$result.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]!=';')break; for($k=$i;$k<$j;$k++)$result.=$input[$k]; $i=$j; } $result.="\1/tt\2"; $i--; }else if($c=='"'||$c=="'"){ $result.="\1tt class='hl2'\2"; $result.=$c; while(!strchr("$c\r\n",$input[++$i]))$result.=$input[$i]; if($input[$i]==$c)$result.=$c;else $i--; $result.="\1/tt\2"; $ac=false; }else if(preg_match('/[A-Za-z_\.\%@]/',$c)){ $result .= "\1tt class='hl3'\2"; while(1){ $res=''; while(preg_match('/[A-Za-z0-9_\.\%@]/i',$input[$i]))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if(!preg_match('/[A-Za-z_\.\%]/',$input[$j])) { if($ac&&$input[$j]==':'){ if($res[0]=='.'||$res=='@@'){ $result.="\1/tt\2\1tt class='hl6'\2".$res; }else $result.=getLink($res); }else{$result.=$res;} break; } $result.=$res; $ac=false; for($k=$i;$k<$j;$k++){$result.=$input[$k];if($input[$k]=="\r"||$input[$k]=="\n")$ac=true;} $i=$j; } $result.="\1/tt\2"; $i--; }else if(strstr('0123456789$',$c)){ if($c=='$'&&strchr($sym."\t\r\n ",$input[$i+1])){ $result.="\1tt class='hl3'\2\$\1/tt\2"; }else{ $result.="\1tt class='hl5'\2"; while(!strchr($sym."\t\r\n\5 ",$input[$i]))$result.=$input[$i++]; $result.="\1/tt\2"; $i--; } $ac=false; }else if(strchr($sym,$c)){ $result.="\1tt class='hl4'\2"; while(1){ while(strchr($sym,$input[$i]))$result.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if(!strchr($sym,$input[$j]))break; for($k=$i;$k<$j;$k++)$result.=$input[$k]; $i=$j; } $result.="\1/tt\2"; $i--; $ac=false; }else { $result.=$c; if($c=="\r"||$c=="\n")$ac=true; } $i++; } $result=htmlspecialchars($result); $result=str_replace(array("\1","\2","\3","\4"),array('<','>','"','&'),$result); return '
'.$result.'
'; } $tbl_resvd_cpp=array("asm","auto","break","case","cdecl","char","const", "continue","default","defined","do","double","else","enum","explicit","export", "extern","far","fastcall","float","finally","for","fortran","goto","huge","if", "import","inline","int","interrupt","long","near","pascal","register","return", "short","signed","sizeof","static","stdcall","struct","switch","this","typedef", "union","unsigned","using","void","volatile","wchar_t","while","_cdecl", "_export","_fastcall","_import","_pascal","_stdcall","__cdecl","__declspec", "__except","__export","__far","__far16","__fastcall","__finally","__fortran", "__huge","__import","__inline","__int128","__int16","__int32","__int64", "__int8","__interrupt","__leave","__loadds","__near","__nounwind","__pascal", "__property","__ptr64","__published","__restrict","__resume","__rtti", "__saveregs","__segment","__segname","__stdcall","__sysapi","__syscall", "__thiscall","__thread","__try","__unaligned","__wchar_t", "and","and_eq","bitand","bitor","bool","catch","compl","const_cast","class", "delete","dynamic_cast","false","friend","mutable","namespace","new","not", "not_eq","operator","or","or_eq","private","protected","public", "reinterpret_cast","static_cast","template","this","throw","true","try", "typeid","typename","virtual","xor","xor_eq","__based","__novtordisp","__self", "__single_inheritance","__uuidof","__virtual_inheritance"); function lookup_resvd_cpp($s){ global $tbl_resvd_cpp; $cnt=sizeof($tbl_resvd_cpp); for($i=0;$i<$cnt;$i++) if($s==$tbl_resvd_cpp[$i])return true; return false; } function check_comments_cpp(&$result,$input,$c,&$i){ $res=''; while($c=='/'&&($input[$i+1]=='/'||$input[$i+1]=='*')){ if($input[$i+1]=='/'){ while(1) { while(!strchr("\r\n\5",$input[$i]))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]!='/'||$input[$j+1]!='/')break; for($k=$i;$k<$j;$k++)$res.=$input[$k]; $i=$j; } // $i--; }else if($input[$i+1]=='*'){ while(1) { $i+=2; $res.='/*'; while($input[$i]!="\5"&&($input[$i-2]!='*'||$input[$i-1]!='/'))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]!='/'||$input[$j+1]!='*')break; for($k=$i;$k<$j;$k++)$res.=$input[$k]; $i=$j; } // $i--; } while(strchr("\r\n\t ",$input[$i]))$res.=$input[$i++]; } if($res!=''){ $result.="\1tt class='cpp1'\2$res\1/tt\2"; $i--; return true; } return false; } # Syntax highlighting for C++ code by Mike V. Semenyako (mike.dld) function ShowCpp($input){ $sym="!&%*?:^<>|{}()[]=+-,.;"; $result=''; $input=trim($input,"\r\n"); $l=strlen($input); $input.="\5"; $i=0;$ac=false; while($i<$l){ $c=$input[$i]; if(!check_comments_cpp($result,$input,$c,$i)) if($c=='#'){ $result .= "\1tt class='cpp6'\2".$c; while(preg_match('/[a-z]/i',$input[++$i]))$result.=$input[$i]; $result .= "\1/tt\2"; $i--; $ac=true; }else if($c=='"'||$c=="'"||($ac&&$c=='<')){ $result.="\1tt class='cpp2'\2"; $result.=$c; if($ac)$c='>'; while(!strchr("$c\r\n",$input[++$i])){ if($input[$i]=='\\'){ $result.="\1tt class='cpp7'\2"; while($input[$i]=='\\'){$result.=$input[$i].$input[$i+1];$i+=2;} $result.="\1/tt\2"; $i--; }else $result.=$input[$i]; } if($input[$i]==$c)$result.=$c;else $i--; $result.="\1/tt\2"; $ac=false; }else if(preg_match('/[a-z_]/i',$c)){ $res2=$res3=''; while(1){ $res=''; while(preg_match('/[a-z0-9_]/i',$input[$i]))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if(!preg_match('/[A-Za-z_]/',$input[$j])) { if(lookup_resvd_cpp($res)){ if($res2!=''){$result.="\1tt class='cpp3'\2$res2\1/tt\2";$res2=$res3='';} $res3.=$res; for($k=$i;$k<$j;$k++){$res3.=$input[$k];} $i=$j; if($res=='asm'&&$input[$j]=='{'){ $result.="\1tt class='cpp6'\2".$res3."\1/tt\2"; $res=$res3=''; while($input[$i]!="\5"&&$input[$i]!='}') if(!check_comments_cpp($res,$input,$input[$i],$i))$res.=$input[$i++];else $i++; $result.="\1tt class='cpp8'\2".$res."\1/tt\2"; } }else $res2.=$res; if($res3!=''){$result.="\1tt class='cpp6'\2$res3\1/tt\2";$res3='';} break; }else{ if(lookup_resvd_cpp($res)){ if($res2!=''){$result.="\1tt class='cpp3'\2$res2\1/tt\2";$res2='';} $res3.=$res; for($k=$i;$k<$j;$k++){$res3.=$input[$k];} $i=$j; if($res=='asm'&&$input[$j]!='{'){ $result.="\1tt class='cpp6'\2".$res3."\1/tt\2"; $res=$res3=''; while($input[$i]!="\5"&&$input[$i]!=';') if(!check_comments_cpp($res,$input,$input[$i],$i))$res.=$input[$i++];else $i++; $result.="\1tt class='cpp8'\2".$res."\1/tt\2"; } }else{ if($res3!=''){$result.="\1tt class='cpp6'\2$res3\1/tt\2";$res2=$res3='';} $res2.=$res; for($k=$i;$k<$j;$k++){$res2.=$input[$k];} $i=$j; } } } if($res2!='')$result.="\1tt class='cpp3'\2$res2\1/tt\2"; $i--; }else if(strstr('0123456789',$c)){ $result.="\1tt class='cpp5'\2"; if($c=='0'&&($input[$i+1]=='x'||$input[$i+1]=='X')){ $result.='0'.$input[$i+1]; $i+=2; while(preg_match('/[a-f0-9]/i',$input[$i]))$result.=$input[$i++]; }else while(strchr('0123456789',$input[$i]))$result.=$input[$i++]; $result.="\1/tt\2"; $i--; }else if(strchr($sym.'/',$c)){ $result.="\1tt class='cpp4'\2"; while(1){ while(strchr($sym,$input[$i])||($input[$i]=='/'&&$input[$i+1]!='/'))$result.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]=='/'&&($input[$j+1]=='/')) break; if(!strchr($sym,$input[$j])&&($input[$j]!='/'))break; for($k=$i;$k<$j;$k++)$result.=$input[$k]; $i=$j; } $result.="\1/tt\2"; $i--; }else{ $result.=$c; } $i++; } $result=htmlspecialchars($result); $result=str_replace(array("\1","\2","\3","\4"),array('<','>','"','&'),$result); return '
'.$result.'
'; } $tbl_resvd_pas=array("asm","assembler","begin","case","cdecl","class", "constructor","destructor","do","downto","else","end","except","export", "exports","external","finalization","finally","for","forward","function","goto", "if","implementation","initialization","inline","interface","label","library", "object","on","pascal","private","procedure","program","protected","public", "published","record","repeat","result","safecall","set","self","stdcall", "register","then","to","try","type","unit","until","uses","virtual","while", "with","and","array","as","const","div","file","in","is","mod","nil","not","of", "or","out","shl","shr","string","var","xor"); function lookup_resvd_pas($s){ global $tbl_resvd_pas; $cnt=sizeof($tbl_resvd_pas); for($i=0;$i<$cnt;$i++) if(!strcasecmp($s,$tbl_resvd_pas[$i]))return true; return false; } function check_comments_pas(&$result,$input,$c,&$i){ $res=''; while($c=='{'||($c=='/'&&$input[$i+1]=='/')||($c=='('&&$input[$i+1]=='*')){ if($c=='/'){ while(1) { while(!strchr("\r\n\5",$input[$i]))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]!='/'||$input[$j+1]!='/')break; for($k=$i;$k<$j;$k++)$res.=$input[$k]; $i=$j; } }else if($c=='('){ while(1) { $i+=2; $res.='(*'; while($input[$i]!="\5"&&($input[$i-2]!='*'||$input[$i-1]!=')'))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]!='('||$input[$j+1]!='*')break; for($k=$i;$k<$j;$k++)$res.=$input[$k]; $i=$j; } }else if($c=='{'){ while(1) { $i++; $res.='{'; while($input[$i]!="\5"&&($input[$i-1]!='}'))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if($input[$j]!='{')break; for($k=$i;$k<$j;$k++)$res.=$input[$k]; $i=$j; } } while(strchr("\r\n\t ",$input[$i]))$res.=$input[$i++]; $c=$input[$i]; } if($res!=''){ $result.="\1tt class='pas1'\2$res\1/tt\2"; $i--; return true; } return false; } function get_number_pas(&$result,$input,$c,&$i){ if(strstr('0123456789$',$c)){ if($c=='$'){ $result.='$'; $i++; while(preg_match('/[a-f0-9]/i',$input[$i]))$result.=$input[$i++]; }else while(strchr('0123456789',$input[$i]))$result.=$input[$i++]; $i--; return true; } return false; } # Syntax highlighting for Pascal code by Mike V. Semenyako (mike.dld) function ShowPas($input){ $sym="@*:^<>|()[]=+-,.;"; $result=''; $input=trim($input,"\r\n"); $l=strlen($input); $input.="\5"; $i=0;$ac=false; while($i<$l){ $c=$input[$i]; if (!check_comments_pas($result,$input,$c,$i)) if($c=='#'){ $res='';$i++; if(get_number_pas($res,$input,$input[$i],$i)) $result.="\1tt class='pas2'\2#$res\1/tt\2"; else{$result.="\1tt class='pas4'\2#\1/tt\2";$i--;} }else if($c=="'"){ $result.="\1tt class='pas2'\2"; while(1){ $result.="'"; while(!strchr("$c\r\n",$input[++$i]))$result.=$input[$i]; if($input[$i]=="'"){ $result.="'";$i++; while($input[$i]=='#'){ $res='';$j=$i;$i++; if(get_number_pas($res,$input,$input[$i],$i)) {$result.="#$res";$i++;} else{$i=$j;break;} } $c=$input[$i]; if($c!="'")break; }else{$i--;break;} } $i--; $result.="\1/tt\2"; }else if(preg_match('/[a-z_]/i',$c)){ $res2=$res3=''; while(1){ $res=''; while(preg_match('/[a-z0-9_]/i',$input[$i]))$res.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if(!preg_match('/[A-Za-z_]/',$input[$j])) { if(lookup_resvd_pas($res)){ if($res2!=''){$result.="\1tt class='pas3'\2$res2\1/tt\2";$res2=$res3='';} $res3.=$res; for($k=$i;$k<$j;$k++){$res3.=$input[$k];} $i=$j; if(!strcasecmp($res,'asm')){ $result.="\1tt class='pas6'\2".$res3."\1/tt\2"; $res=$res3=''; while($input[$i]!="\5"&&strcasecmp($input[$i].$input[$i+1].$input[$i+2],'end')!=0) if(!check_comments_pas($res,$input,$input[$i],$i))$res.=$input[$i++];else $i++; $result.="\1tt class='pas7'\2".$res."\1/tt\2"; } }else $res2.=$res; if($res3!=''){$result.="\1tt class='pas6'\2$res3\1/tt\2";$res3='';} break; }else{ if(lookup_resvd_pas($res)){ if($res2!=''){$result.="\1tt class='pas3'\2$res2\1/tt\2";$res2='';} $res3.=$res; for($k=$i;$k<$j;$k++){$res3.=$input[$k];} $i=$j; if(!strcasecmp($res,'asm')){ $result.="\1tt class='pas6'\2".$res3."\1/tt\2"; $res=$res3=''; while($input[$i]!="\5"&&strcasecmp($input[$i].$input[$i+1].$input[$i+2],'end')!=0) if(!check_comments_pas($res,$input,$input[$i],$i))$res.=$input[$i++];else $i++; $result.="\1tt class='pas7'\2".$res."\1/tt\2"; } }else{ if($res3!=''){$result.="\1tt class='pas6'\2$res3\1/tt\2";$res2=$res3='';} $res2.=$res; for($k=$i;$k<$j;$k++){$res2.=$input[$k];} $i=$j; } } } if($res2!='')$result.="\1tt class='pas3'\2$res2\1/tt\2"; $i--; }else if(strstr('0123456789$',$c)){ $result.="\1tt class='pas5'\2"; if($c=='$'){ $result.='$'; $i++; while(preg_match('/[a-f0-9]/i',$input[$i]))$result.=$input[$i++]; }else while(strchr('0123456789',$input[$i]))$result.=$input[$i++]; $result.="\1/tt\2"; $i--; }else if(strchr($sym.'/',$c)){ $result.="\1tt class='pas4'\2"; while(1){ while(strchr($sym,$input[$i])||($input[$i]=='/'&&$input[$i+1]!='/')||($input[$i]=='('&&$input[$i+1]!='*'))$result.=$input[$i++]; $j=$i; while(strchr("\r\n\t ",$input[$j]))$j++; if(($input[$j]=='/'&&$input[$j+1]=='/')||($input[$j]=='('&&$input[$j+1]=='*')) break; if(!strchr($sym,$input[$j])&&($input[$j]!='/'))break; for($k=$i;$k<$j;$k++)$result.=$input[$k]; $i=$j; } $result.="\1/tt\2"; $i--; }else{ $result.=$c; } $i++; } $result=htmlspecialchars($result); $result=str_replace(array("\1","\2","\3","\4"),array('<','>','"','&'),$result); return '
'.$result.'
'; } # testing code: function microtime_f() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } echo " "; //$fn='commctrl.pas'; $fn='gen.cpp'; $tm=microtime_f(); $f=fopen($fn,'r'); $inp=fread($f,filesize($fn)); fclose($f); echo ShowCpp($inp)."\n"; printf("script execution time: %.2f sec",microtime_f()-$tm); echo ""; ?>