Thursday, August 13, 2009

Improved Syntax Highlight Brush for Erlang

Updating last December's post, about a SyntaxHighlighter brush for Erlang, to allow for compounds like 'wxErlang' with interior capitals and not mark them as values:

//WTFPL licensed
dp.sh.Brushes.Erlang = function() {
    var keywords = 'after begin case catch cond end fun if let of query receive when '+
        'define record export import include include_lib ifdef ifndef else endif undef '+
        'apply attribute call do in letrec module primop try';
  
    this.values = new Object();
    this.values.regex = new RegExp('[^\\w"\'](_?[A-Z]\\w*)', 'g');
    this.values.exec = function(value) { m = this.regex.exec(value);  if (m && m.length) {m[0] = m[0].substring(1, m[0].length); ++m.index;} return m;}

    this.regexList = [
       { regex: new RegExp('%.*$', 'gm'),    css: 'comment' },   // one line comments  
       { regex: dp.sh.RegexLib.DoubleQuotedString,     css: 'string' },   // strings  
       { regex: dp.sh.RegexLib.SingleQuotedString,     css: 'atom' },   // strings  
       { regex: new RegExp(this.GetKeywords(keywords), 'g'),  css: 'keyword' },  // Erlang keyword  
       { regex: new RegExp('\\?\\w*', 'g'), css: 'macros'},    
       { regex: this.values, css: 'values'},  
       { regex: new RegExp('\\w+/\\d+', 'g'), css: 'funs'}
      ];

    this.CssClass = 'dp-erl';
    this.Style = '.dp-erl .values { color: #c88; font-weight: bold;} '+
      '.dp-erl .funs { color: #f00; } .dp-erl .atom { color: #000; } ' +
      '.dp-erl .macros { color: silver; font-weight: bold;}' ;
}

dp.sh.Brushes.Erlang.prototype = new dp.sh.Highlighter();
dp.sh.Brushes.Erlang.Aliases = ['erlang', 'erl'];

This manipulates the output of the RegExp to discard the first (non-identifier) character, to work around the lack of lookbehind (in one of the many possible ways the same effect could be achieved).

Post a Comment