Actionscript Syntax Highlighter

A syntax highlighter written in actionscript for actionscript.

Seeing I will be posting code snippets on this blog I thought it might be fun to write a syntax highlighter. I have never written anything like this before and I am sure there are be better ways to do it. However, for my purposes it is good enough :)

You can download the AS class here.

Or here it is highlighted below

Note: Care must be taken with the following characters > <

//
// CodeFormatter
//
// Created by Simon Rodwell on 2008-11-19.
//

package com.roddeh.text{

public class CodeFormatter{

//==================================================================
// PUBLIC PROPERTIES

public static var defaultColour:String = "#EEEEEE";
public static var keywordColour:String = "#FBDE2C";
public static var commentColour:String = "#AEAEA9";
public static var quoteColour:String = "#5BB231";
public static var typeColour:String = "#779ECE";
public static var constantColour:String = "#D14DEE";

//==================================================================
// PROTECTED PROPERTIES

//==================================================================
// PRIVATE PROPERTIES

private static const KEYWORDS:Array = [
"dynamic",
"final",
"internal",
"native",
"override",
"private",
"protected",
"public",
"static",
"class",
"const",
"extends",
"function",
"get",
"implements",
"interface",
"namespace",
"package",
"set",
"var",
"include",
"import",
"false",
"null",
"this",
"true",
"break",
"case",
"continue",
"do",
"while",
"else",
"for",
"each",
"in",
"if",
"label",
"return",
"super",
"switch",
"try",
"catch",
"finally",
"throw",
"with",
":",
"="
];

private static const TYPES:Array = [
"Number",
"String",
"Boolean",
"int",
]

private static const BREAK_CHARS:Array = [
" ",
" ",
"(",
")",
":",
";",
"+",
"-",
"*",
"/",
"%",
"n"
]

//==================================================================
// CONSTRUCTOR

public function CodeFormatter(){

}

//==================================================================
// PUBLIC METHODS

public static function format(code:String, colours:Object = null):String{
if(colours){
for(var i:String in colours){
try{
CodeFormatter[i] = colours[i];
}
catch(e:Error){};
}
}

var coloured:String = “”;
var counter:int = 0;
var quoting:Boolean = false;
var openQuoteChar:String;
var commenting:Boolean = false;
var lineCommenting:Boolean = false;
var c:String;
var chunk:String = “”;
while(counter < code.length){
c = code.charAt(counter);

// Check to see if we are quoting.
if(quoting){
chunk += c;
if((c == “"” || c == “‘”) && c == openQuoteChar){
quoting = false;
coloured += setTextColour(chunk, quoteColour, false);
chunk = “”;
}
counter++;
continue;
}

// Check to see if we are commenting
if(commenting){
chunk += c;
if(c == “*” && code.charAt(counter + 1) == “/”){
commenting = false;
chunk += “/”;
coloured += setTextColour(chunk, commentColour, false);
chunk = “”;
counter++; // Increment a second time to allow for the closing comment
}
counter++;
continue;
}

// Check to see if we are line commenting.
if(lineCommenting){
chunk += c;
if(c.charCodeAt(0) == 13){
lineCommenting = false;
coloured += setTextColour(chunk, commentColour, false);
chunk = “”;
}
counter ++
continue;
}

// Check to see if we need to start quoting.
if(c == “"” || c == “‘”){
if(!commenting && !lineCommenting){
quoting = true;
openQuoteChar = c;
coloured += colourText(chunk);
chunk = c;
counter++;
continue;
}
}

// Check to see if we need to start commenting.
if(c == “/”){
if(code.charAt(counter + 1) == “/”){
lineCommenting = true;
coloured += colourText(chunk);
chunk += c;
counter++
continue;
}
if(code.charAt(counter + 1) == “*”){
commenting = true;
coloured += colourText(chunk);
chunk = c + “*”;
counter += 2;
continue;
}
}

// Otherwise we are writing normal code.
if(BREAK_CHARS.indexOf(c) != - 1){
coloured += colourText(chunk);
coloured += colourText(c);
chunk = “”;
counter++;
continue;
}

chunk += c;
counter++;
}

if(chunk != “”){
coloured += colourText(chunk);
}

function colourText(text:String):String{
if(KEYWORDS.indexOf(text) != - 1){
return setTextColour(text, keywordColour, false);
}
if(TYPES.indexOf(text) != - 1){
return setTextColour(text, typeColour, false);
}
if(isConstant(text)){
return setTextColour(text, constantColour, false);
}
return text;
}

// TODO: I am sure this could be rewritten more robustly as a RegExp
function isConstant(t:String):Boolean{
var i:int = 0;
while(i < t.length){
var charCode:int = t.charCodeAt(i);
if(!((charCode &gts 47 && charCode < 58) || (charCode > 64 && charCode < 91) || charCode == 46 || charCode == 95)){
return false;
}
i++
}
return true;
}

// Colour everything else to the default text colour.
coloured = setTextColour(coloured, defaultColour, false);
// Replace tabs with 4 spaces.
return coloured.replace(/t/g, ” “);
}

//==================================================================
// PROTECTED METHODS

//==================================================================
// PRIVATE METHODS

private static function setTextColour(text:String, colour:int, stripInner:Boolean = true):String{
if(stripInner){
text = stripInnerColour(text);
}
return “<font color="” + getHTMLColour(colour) + “">” + text + “</font>”;
}

private static function stripInnerColour(text:String):String{
var reg:RegExp
reg = /<fontb[^>]*>/g
text = text.replace(reg, “”);
reg = /</font>/g;
text = text.replace(reg, “”);
return text;
}

private static function stripWhite(text:String):String{
var reg:RegExp = /^[ t]+|[t]+$/
return text.replace(reg, “”);
}

private function getHTMLColour(color:int):String{
var col:Colour = new Colour(color);

function getHexString(hex:int):String{
return getHexChar(Math.floor(hex / 0×10)) + getHexChar(hex % 0×10);
}

function getHexChar(hex:int):String{
if(hex < 10){
return String(hex);
}
else{
switch(hex){
case 10: return “A”;
case 11: return “B”;
case 12: return “C”;
case 13: return “D”;
case 14: return “E”;
case 15: return “F”;
}
}
return String(hex);
}

return “#” + getHexString(col.red) + getHexString(col.green) + getHexString(col.blue);
}

//==================================================================
// SET METHODS

//==================================================================
// GET METHODS

}
}

Leave a Reply

You must be logged in to post a comment.