Wednesday, December 03, 2008

Syntax Highlight Brush for F#

Following on from Scott Hanselman's post about the SyntaxHighlighter script, here's the secret sauce I needed to get it to work.

And here's a first stab at an F# brush, which is in use for this post.

// Offered under the WTFPL -- http://sam.zoy.org/wtfpl/
// scratching the surface of http://research.microsoft.com/fsharp/manual/spec2.aspx#_Toc207785562
// omits reserved-ident-formats, reserved-symbolic-sequence, quote-op-*, symbolic-op, ...
dp.sh.Brushes.FSharp = function()
{
 var keywords = 'abstract and as assert base begin class default delegate do done ' +
  'downcast downto elif else end exception extern false finally for '+
  'fun function if in inherit inline interface internal lazy let ' +
  'match member module mutable namespace new null of open or '+
  'override private public rec return static struct then to '+
  'true try type upcast use val void when while with yield';
  var ocaml = 'asr land lor lsl lsr lxor mod sig';
  var reserved ='atomic break checked component const constraint constructor '+
  'continue eager event external fixed functor global include '+
  'method mixin object parallel process protected pure '+
  'sealed tailcall trait virtual volatile';
  var symbolic = 'let! use! do! yield! return! \\| -> <- \\. : \\( \\) \\[ \\] \\[< >\\] \\[\\| \\|\\] '+
  '\\{ \\} \' # :\\?> :\\? ; ;; :> := _ \\.\\. ::';

 this.regexList = [
  { regex: dp.sh.RegexLib.SingleLineCComments,    css: 'comment' },
  { regex: new RegExp('\\(\\*[\\s\\S]*?\\*\\)', 'gm'),   css: 'comment' },
  { regex: dp.sh.RegexLib.DoubleQuotedString,     css: 'string' },   // strings
  { regex: new RegExp('^\\s*#.*', 'gm'),      css: 'preprocessor' },  // preprocessor tags like #light
  { regex: new RegExp(this.GetKeywords(keywords), 'g'),  css: 'keyword' },  // f# keyword
  { regex: new RegExp(this.GetKeywords(ocaml), 'g'),  css: 'ocaml' },   // caml keyword
  { regex: new RegExp(this.GetKeywords(reserved), 'g'),  css: 'reserved' },   // reserved keyword
  { regex: new RegExp(this.GetKeywords(symbolic), 'g'),  css: 'symbolic' }   // symbolic keyword
  ];

 this.CssClass = 'dp-fs';
 this.Style = '.dp-fs .ocaml { color: #d00; } ' + 
  '.dp-fs .reserved { color: #d00; font-style: italic;} '+ 
  '.dp-fs .symbolic { color: #00d; font-weight: bold;} ';
}

dp.sh.Brushes.FSharp.prototype = new dp.sh.Highlighter();
dp.sh.Brushes.FSharp.Aliases = ['f#', 'f-sharp', 'fsharp'];

The symbolic class is not yet working; ocaml and reserved do work, and there's more to add, as noted in the introductory comment, if I can get past the symbolic hurdle.

Post a Comment