I'm trying to make a syntax coloring MOD for phpBB. I'm doing it for ActionScript, but I think it can be reused for other languages (with some changes) if they have more or less the same rules.
What I've done so far works just fine (coloring keywords), but the strings, comments and operators need an experienced regexp coder as soon as I'm not very good with php and specially with regexp... This is a good chance for me to learn using them
I'm going to post the code....
The most of the code are the ActionScript keywords, so don't get scared about the length.
Code: Select all
<?php
// types of ActionScript keywords separated by spaces
// some chars are escaped -> / * + ( ) [ ] etc....
$flash_operators = "/\b\-\-\b/ /\b\+\+\b/ /\b!\b/ /\b!=\b/ /\b!==\b/ /\b%\b/ /\b%=\b/ /\b&\b/ /\b&&\b/ /\b&=\b/ /\b\(\b/ /\b\)\b/ /\b\-\b/ /\b\*\b/ /\b\*=\b/ /\b,\b/ /\b\.\b/ /\b\?:\b/ /\b\/\b/ /\b\/=\b/ /\b\[\b/ /\b\]\b/ /\b\^\b/ /\b\^=\b/ /\b\{\b/ /\b\}\b/ /\b\|\b/ /\b\|\|\b/ /\b\|=\b/ /\b~\b/ /\b\+\b/ /\b\+=\b/ /\b<\b/ /\b<<\b/ /\b<<=\b/ /\b<=\b/ /\b<>\b/ /\b=\b/ /\b\-=\b/ /\b==\b/ /\b===\b/ /\b>\b/ /\b>=\b/ /\b>>\b/ /\b>>=\b/ /\b>>>\b/ /\b>>>=\b/";
$flash_core = "/\b_global\b/ /\b_level\b/ /\b_parent\b/ /\b_root\b/ /\bAccessibility\b/ /\bArray\b/ /\bBoolean\b/ /\bButton\b/ /\bColor\b/ /\bDate\b/ /\bKey\b/ /\bMath\b/ /\bMouse\b/ /\bMovieClip\b/ /\bNumber\b/ /\bObject\b/ /\bSelection\b/ /\bSound\b/ /\bStage\b/ /\bString\b/ /\bSystem\b/ /\bXML\b/ /\bXMLSocket\b/";
$flash_objects = "/\bapply\b/ /\barguments\b/ /\bconstructor\b/ /\bdo\b/ /\belse\b/ /\bfalse\b/ /\bfor\b/ /\bfunction\b/ /\bif\b/ /\bin\b/ /\bnew\b/ /\bnewline\b/ /\bnull\b/ /\breturn\b/ /\bsuper\b/ /\bswitch\b/ /\bthis\b/ /\btrue\b/ /\bundefined\b/ /\bvar\b/ /\bvoid\b/ /\bwhile\b/ /\bwith\b/";
$flash_methods = "/\badd\b/ /\babs\b/ /\bacos\b/ /\basin\b/ /\batan\b/ /\batan2\b/ /\bisNaN\b/ /\bceil\b/ /\baddListener\b/ /\baddProperty\b/ /\bappendChild\b/ /\basfunction\b/ /\battachMovie\b/ /\battachSound\b/ /\bbeginFill\b/ /\bbeginGradientFill\b/ /\bblockIndent\b/ /\bbreak\b/ /\bbroadcastMessage\b/ /\bcall\b/ /\bcase\b/ /\bcharAt\b/ /\bcharCodeAt\b/ /\bclear\b/ /\bclearInterval\b/ /\bcloneNode\b/ /\bclose\b/ /\bconcat\b/ /\bcontinue\b/ /\bconnect\b/ /\bcos\b/ /\bcreateElement\b/ /\bcreateEmptyMovieClip\b/ /\bcreateTextField\b/ /\bcreateTextNode\b/ /\bcurveTo\b/ /\bdefault\b/ /\bdelete\b/ /\bduplicateMovieClip\b/ /\bendFill\b/ /\bescape\b/ /\beval\b/ /\bevaluate\b/ /\bexp\b/ /\bfromCharCode\b/ /\bFSCommand\b/ /\bget\b/ /\bgetAscii\b/ /\bgetBeginIndex\b/ /\bgetBounds\b/ /\bgetBytesLoaded\b/ /\bgetBytesTotal\b/ /\bgetCaretIndex\b/ /\bgetCode\b/ /\bgetDate\b/ /\bgetDay\b/ /\bgetDepth\b/ /\bgetEndIndex\b/ /\bgetFocus\b/ /\bgetFontList\b/ /\bgetFullYear\b/ /\bgetHours\b/ /\bgetMilliseconds\b/ /\bgetMinutes\b/ /\bgetMonth\b/ /\bgetNewTextFormat\b/ /\bgetPan\b/ /\bgetProperty\b/ /\bgetRGB\b/ /\bgetSeconds\b/ /\bgetTextExtent\b/ /\bgetTextFormat\b/ /\bgetTime\b/ /\bgetTimer\b/ /\bgetTimezoneOffset\b/ /\bgetTransform\b/ /\bgetURL\b/ /\bgetUTCDate\b/ /\bgetUTCDay\b/ /\bgetUTCFullYear\b/ /\bgetUTCHours\b/ /\bgetUTCMilliseconds\b/ /\bgetUTCMinutes\b/ /\bgetUTCMonth\b/ /\bgetUTCSeconds\b/ /\bgetVersion\b/ /\bgetVolume\b/ /\bgetYear\b/ /\bglobalToLocal\b/ /\bgotoAndPlay\b/ /\bgotoAndStop\b/ /\bhasAccessibility\b/ /\bhasAudio\b/ /\bhasAudioEncoder\b/ /\bhasChildNodes\b/ /\bhasMP3\b/ /\bhasVideoEncoder\b/ /\bheight\b/ /\bhide\b/ /\bhitArea\b/ /\bhitTest\b/ /\bindexOf\b/ /\binsertBefore\b/ /\binstall\b/ /\bjoin\b/ /\blineStyle\b/ /\blineTo\b/ /\blastIndexOf\b/ /\bload\b/ /\bloadMovie\b/ /\bloadMovieNum\b/ /\bloadSound\b/ /\bloadVariables\b/ /\bloadVariablesNum\b/ /\bLoadVars\b/ /\blog\b/ /\bmax\b/ /\bmin\b/ /\bmbchr\b/ /\bmblength\b/ /\bmbord\b/ /\bmbsubstring\b/ /\bnextFrame\b/ /\bnextScene\b/ /\bnextSibling\b/ /\bon\b/ /\bonChanged\b/ /\bonClipEvent\b/ /\bonClose\b/ /\bonConnect\b/ /\bonData\b/ /\bonDragOut\b/ /\bonDragOver\b/ /\bonEnterFrame\b/ /\bonKeyDown\b/ /\bonKeyUp\b/ /\bonKillFocus\b/ /\bonLoad\b/ /\bonMouseDown\b/ /\bonMouseMove\b/ /\bonMouseUp\b/ /\bonPress\b/ /\bonRelease\b/ /\bonReleaseOutside\b/ /\bonResize\b/ /\bonRollOut\b/ /\bonRollOver\b/ /\bonScroller\b/ /\bonSetFocus\b/ /\bonSort\b/ /\bonSoundComplete\b/ /\bonUnload\b/ /\bonUpdate\b/ /\bonXML\b/ /\bparseFloat\b/ /\bparseInt\b/ /\bparseXML\b/ /\bplay\b/ /\bpop\b/ /\bpow\b/ /\bpush\b/ /\bprevScene\b/ /\bprevFrame\b/ /\bpreviousSibling\b/ /\bprint\b/ /\bprintAsBitmap\b/ /\bprintAsBitmapNum\b/ /\bprintNum\b/ /\bregisterClass\b/ /\bremoveMovieClip\b/ /\bremoveNode\b/ /\bremoveTextField\b/ /\breplaceSel\b/ /\breverse\b/ /\bsetDate\b/ /\bsetFocus\b/ /\bsetFullYear\b/ /\bsetHours\b/ /\bsetInterval\b/ /\bsetMask\b/ /\bsetMilliseconds\b/ /\bsetMinutes\b/ /\bsetMonth\b/ /\bsetNewTextFormat\b/ /\bsetPan\b/ /\bsetProperty\b/ /\bsetRGB\b/ /\bsetSeconds\b/ /\bsetSelection\b/ /\bsetTextFormat\b/ /\bsetTime\b/ /\bsetTransform\b/ /\bsetUTCDate\b/ /\bsetUTCFullYear\b/ /\bsetUTCHours\b/ /\bsetUTCMilliseconds\b/ /\bsetUTCMinutes\b/ /\bsetUTCMonth\b/ /\bsetUTCSeconds\b/ /\bsetVolume\b/ /\bsetYear\b/ /\bsend\b/ /\bsendAndLoad\b/ /\bset\b/ /\bshift\b/ /\bshow\b/ /\bsin\b/ /\bslice\b/ /\bsort\b/ /\bsplice\b/ /\bsplit\b/ /\bsqrt\b/ /\bstart\b/ /\bsortOn\b/ /\bstartDrag\b/ /\bstop\b/ /\bstopAllSounds\b/ /\bstopDrag\b/ /\bsubstr\b/ /\bsubstring\b/ /\bswapDepths\b/ /\btan\b/ /\btoggleHighQuality\b/ /\btoLowerCase\b/ /\btoString\b/ /\btoUpperCase\b/ /\btrace\b/ /\bunescape\b/ /\buninstall\b/ /\bunloadMovie\b/ /\bunloadMovieNum\b/ /\bunshift\b/ /\bunwatch\b/ /\bupdateAfterEvent\b/ /\bwatch\b/ /\binstanceof\b/ /\bint\b/ /\bround\b/ /\bfloor\b/ /\bmoveTo\b/";
$flash_components = "/\bFPushButton\b/ /\bFRadioButton\b/ /\bFScrollBar\b/ /\bFScrollPane\b/ /\bFStyleFormat\b/ /\bFAccordionPane\b/ /\bFSlider\b/ /\bFTabView\b/ /\bFTextField\b/ /\bFweekView\b/";
$flash_components_methods = "/\baddItem\b/ /\baddItemAt\b/ /\bapplyChanges\b/ /\barrow\b/ /\bbackground\b/ /\bbackgroundDisabled\b/ /\bcheck\b/ /\bdarkshadow\b/ /\bface\b/ /\bFCheckBox\b/ /\bFComboBox\b/ /\bFListBox\b/ /\bfocusRectInner\b/ /\bfocusRectOuter\b/ /\bforegroundDisabled\b/ /\bgetData\b/ /\bgetEnabled\b/ /\bgetGroupName\b/ /\bgetItemAt\b/ /\bgetLabel\b/ /\bgetLength\b/ /\bgetPaneHeight\b/ /\bgetPaneWidth\b/ /\bgetRowCount\b/ /\bgetScrollContent\b/ /\bgetScrollPosition\b/ /\bgetSelectedIndex\b/ /\bgetSelectedIndices\b/ /\bgetSelectedItem\b/ /\bgetSelectedItems\b/ /\bgetSelectMultiple\b/ /\bgetState\b/ /\bgetValue\b/ /\bglobalStyleFormat\b/ /\bhighlight\b/ /\bhighlight3D\b/ /\bloadScrollContent\b/ /\bradioDot\b/ /\brefreshPane\b/ /\bregisterSkinElement\b/ /\bremoveAll\b/ /\bremoveitemAt\b/ /\bremoveListener\b/ /\breplaceItemAt\b/ /\bScrollTrack\b/ /\bselection\b/ /\bselectionDisabled\b/ /\bselectionUnfocused\b/ /\bsetAutoHideScrollBar\b/ /\bsetChangeHandler\b/ /\bsetClickHandler\b/ /\bsetData\b/ /\bsetDataProvider\b/ /\bsetDragContent\b/ /\bsetEditable\b/ /\bsetEnabled\b/ /\bsetGroupName\b/ /\bsetHorizontal\b/ /\bsetHScroll\b/ /\bsetItemSymbol\b/ /\bsetLabel\b/ /\bsetLabelPlacement\b/ /\bsetLargeScroll\b/ /\bsetRowCount\b/ /\bsetScrollContent\b/ /\bsetScrollPosition\b/ /\bsetScrollProperties\b/ /\bsetScrollTarget\b/ /\bsetSelectedIndex\b/ /\bsetSelectedIndices\b/ /\bsetSelectionIndex\b/ /\bsetSelectMultiple\b/ /\bsetSize\b/ /\bsetSmallScroll\b/ /\bsetState\b/ /\bsetStyleProperty\b/ /\bsetValue\b/ /\bsetVScroll\b/ /\bsetWidth\b/ /\bshadow\b/ /\bsortItemsBy\b/ /\btextAlign\b/ /\btextBold\b/ /\btextColor\b/ /\btextDisabled\b/ /\btextFont\b/ /\btextIndent\b/ /\btextItalic\b/ /\btextLeftMargin\b/ /\btextRightMargin\b/ /\btextSelected\b/ /\btextSize\b/ /\btextUnderline\b/";
$flash_properties = "/\b__proto__\b/ /\b_alpha\b/ /\b_currentframe\b/ /\b_droptarget\b/ /\b_focusrect\b/ /\b_framesloaded\b/ /\b_height\b/ /\b_name\b/ /\b_quality\b/ /\b_rotation\b/ /\b_soundbuftime\b/ /\b_target\b/ /\b_totalframes\b/ /\b_url\b/ /\b_visible\b/ /\b_width\b/ /\b_x\b/ /\b_xmouse\b/ /\b_xscale\b/ /\b_y\b/ /\b_ymouse\b/ /\b_yscale\b/ /\balign\b/ /\battributes\b/ /\bautoSize\b/ /\bbackgroundColor\b/ /\bbold\b/ /\bborder\b/ /\bborderColor\b/ /\bbottomScroll\b/ /\bbullet\b/ /\bcallee\b/ /\bcaller\b/ /\bcapabilities\b/ /\bchildNodes\b/ /\bcolor\b/ /\bcontentType\b/ /\bdocTypeDecl\b/ /\bduration\b/ /\bembedFonts\b/ /\benabled\b/ /\bfirstChild\b/ /\bfocusEnabled\b/ /\bhscroll\b/ /\bhtml\b/ /\bhtmlText\b/ /\bindent\b/ /\bignoreWhite\b/ /\bisActive\b/ /\bisDown\b/ /\bisFinite\b/ /\bisNaN\b/ /\bisToggled\b/ /\bitalic\b/ /\blastChild\b/ /\bleading\b/ /\bleftMargin\b/ /\blength\b/ /\bmanufacturer\b/ /\bmaxChars\b/ /\bmaxhscroll\b/ /\bmultiline\b/ /\bnodeName\b/ /\bnodeType\b/ /\bnodeValue\b/ /\bparentNode\b/ /\bpixelAspectRatio\b/ /\bprototype\b/ /\bresolutionX\b/ /\bresolutionY\b/ /\brestrict\b/ /\brightMargin\b/ /\bscaleMode\b/ /\bscreenColor\b/ /\bscreenDPI\b/ /\bscreenResolution\b/ /\bscreenResolutionX\b/ /\bscreenResolutionY\b/ /\bscroll\b/ /\bselectable\b/ /\bshowMenu\b/ /\bsize\b/ /\bstatus\b/ /\btabChildren\b/ /\btabEnabled\b/ /\btabIndex\b/ /\btabStops\b/ /\btarget\b/ /\btargetPath\b/ /\btext\b/ /\btextHeight\b/ /\btextWidth\b/ /\btrackAsMenu\b/ /\btype\b/ /\btypeof\b/ /\bunderline\b/ /\burl\b/ /\buseHandCursor\b/ /\bvalueOf\b/ /\bvariable\b/ /\bversion\b/ /\bwidth\b/ /\bwordWrap\b/ /\bxmlDecl\b/ /\bloaded\b/ /\bos\b/ /\bpassword\b/ /\bposition\b/ /\bfont\b/ /\bitalic\b/ /\blanguage\b/ /\blist\b/ /\bor\b/";
$flash_misc = "/\b#endinitclip\b/ /\b#include\b/ /\b#initclip\b/";
$flash_constants = "/\bBACKSPACE\b/ /\bCAPSLOCK\b/ /\bCONTROL\b/ /\bDELETEKEY\b/ /\bDOWN\b/ /\bEND\b/ /\bENTER\b/ /\bESCAPE\b/ /\bHOME\b/ /\bInfinity\b/ /\bINSERT\b/ /\bLEFT\b/ /\bLN10\b/ /\bLN2\b/ /\bLOG10E\b/ /\bLOG2E\b/ /\bMAX_VALUE\b/ /\bMIN_VALUE\b/ /\bNaN\b/ /\bNEGATIVE_INFINITY\b/ /\bPGDN\b/ /\bPGUP\b/ /\bPI\b/ /\bPOSITIVE_INFINITY\b/ /\bRIGHT\b/ /\bSHIFT\b/ /\bSPACE\b/ /\bSQRT1_2\b/ /\bSQRT2\b/ /\bTAB\b/ /\bUP\b/ /\bUTC\b/";
$flash_deprecated = "/\b_highquality\b/ /\badd\b/ /\band\b/ /\bchr\b/ /\beq\b/ /\bge\b/ /\bgt\b/ /\bifFrameLoaded\b/ /\bint\b/ /\ble\b/ /\blength\b/ /\blt\b/ /\bmaxscroll\b/ /\bmbchr\b/ /\bmblength\b/ /\bmbord\b/ /\bmbsubstring\b/ /\bne\b/ /\bnot\b/ /\bor\b/ /\bord\b/ /\brandom\b/ /\bsubstring\b/ /\btellTarget\b/ /\btoggleHighQuality\b/ /\bchr\b/ /\bord\b/ /\bnot\b/";
// creating an array for each type
$flash_operators = explode (" " , $flash_operators);
$flash_core = explode (" " , $flash_core);
$flash_objects = explode (" " , $flash_objects);
$flash_methods = explode (" " , $flash_methods);
$flash_components = explode (" " , $flash_components);
$flash_components_methods = explode (" " , $flash_components_methods);
$flash_properties = explode (" " , $flash_properties);
$flash_misc = explode (" " , $flash_misc);
$flash_constants = explode (" " , $flash_constants);
$flash_deprecated = explode (" " , $flash_deprecated);
// CSS Styles names
$operators = "as_operators";
$core = "as_core";
$objects = "as_objects";
$methods = "as_methods";
$components = "as_components";
$components_methods = "as_components_methods";
$properties = "as_properties";
$misc = "as_misc";
$constants = "as_constants";
$deprecated = "as_deprecated";
//----------------------------------------------------------------------//
$comments = "as_comments";
$strings = "as_strings";
//----------------------------------------------------------------------//
// itinerates the given array and feeds the replacement array with the colored version of the keyword
function itinerate_type_arr ( $type_arr , $class)
{
$replacement_arr = array();
for ( $i=0 ; $i<sizeof($type_arr) ; $i++ )
{
$replacement_arr[$i] = "<span class=" . $class . ">" . substr ($type_arr[$i], 3,-3)."</span>";
}
return $replacement_arr;
}
// declaration of replacement arrays
$replacement_operators = array();
$replacement_core = array();
$replacement_objects = array();
$replacement_methods = array();
$replacement_components = array();
$replacement_components_methods = array();
$replacement_properties = array();
$replacement_misc = array();
$replacement_constants = array();
$replacement_deprecated = array();
// creation of the keywords replacement arrays by calling the function itinerate_type_arr()
$replacement_operators = itinerate_type_arr ( $flash_operators , $operators , $replacement_operators );
$replacement_core = itinerate_type_arr ( $flash_core , $core );
$replacement_objects = itinerate_type_arr ( $flash_objects , $objects );
$replacement_methods = itinerate_type_arr ( $flash_methods , $methods );
$replacement_components = itinerate_type_arr ( $flash_components , $components );
$replacement_components_methods = itinerate_type_arr ( $flash_components_methods , $components_methods );
$replacement_properties = itinerate_type_arr ( $flash_properties , $properties );
$replacement_misc = itinerate_type_arr ( $flash_misc , $misc );
$replacement_constants = itinerate_type_arr ( $flash_constants , $constants );
$replacement_deprecated = itinerate_type_arr ( $flash_deprecated , $deprecated );
// sample ActionScript code string for testing
$flash_str = '
hernan = new LoadVars();
this.onEnterFrame = function() {
hernan.onLoad = function(cargao) {
tl = this;
if (cargao) {
txt.text = "";
txt.text = this.hernan;
myComponent.setScrollTarget(txt);
myComponent.setScrollPosition(100);
}
};
hernan.load("datos.txt");
};
if (Key.isDown(Key.ENTER){
trace ("key is pressed");
}
';
// execute the replacement
//$flash_str = /*todo -> strings replacement*/
//$flash_str = /*todo -> operators replacement*/ preg_replace($flash_operators, $replacement_operators, $flash_str);
//$flash_str = /*todo -> comments replacements*/
$flash_str = preg_replace($flash_core, $replacement_core, $flash_str);
$flash_str = preg_replace($flash_objects, $replacement_objects, $flash_str);
$flash_str = preg_replace($flash_methods, $replacement_methods, $flash_str);
$flash_str = preg_replace($flash_components, $replacement_components, $flash_str);
$flash_str = preg_replace($flash_components_methods, $replacement_components_methods, $flash_str);
$flash_str = preg_replace($flash_properties, $replacement_properties, $flash_str);
$flash_str = preg_replace($flash_misc, $replacement_misc, $flash_str);
$flash_str = preg_replace($flash_constants, $replacement_constants, $flash_str);
$flash_str = preg_replace($flash_deprecated, $replacement_deprecated, $flash_str);
?>
<HTML>
<HEAD>
<TITLE>flash BBcode</TITLE>
<style type="text/css">
.as_operators { font-family: Courier; font-size : 12px; font-weight : bold; color : #DD0000; }
.as_core { font-family: Courier; font-size : 12px; font-weight : bold; color : #000099; }
.as_objects { font-family: Courier; font-size : 12px; font-weight : bold; color : #000099; }
.as_methods { font-family: Courier; font-size : 12px; font-weight : normal;color: #000099; }
.as_components { font-family: Courier; font-size : 12px; font-weight : normal;color: #DD0000; }
.as_components_methods { font-family: Courier; font-size : 12px; font-weight : normal;color: #DD0000; }
.as_properties { font-family: Courier; font-size : 12px; font-weight : normal;color: #DD0000; }
.as_misc { font-family: Courier; font-size : 12px; font-weight : bold; color : #DD6600; }
.as_constants { font-family: Courier; font-size : 12px; font-weight : normal; color:#DD0000; }
.as_deprecated { font-family: Courier; font-size : 12px; font-weight : bold; color : #9966FF; }
.as_comments { font-family: Courier; font-size : 12px; font-weight : normal; color:#CCCCCC; }
.as_strings { font-family: Courier; font-size : 12px; font-weight : normal; color:#008200; }
.as_gen { font-family: Courier; font-size : 12px; font-weight : normal; color:#000000; }
</style>
</HEAD>
<BODY>
<?PHP
// print the string
echo "<pre><span class=as_gen>";
echo $flash_str;
echo "</span></pre>";
?>
</BODY>
</HTML>