Kiss me on GitHub
SyntaxHighlighter Grammar
Build your own syntax-highlight grammar for SyntaxHighlighter
SyntaxHighlighter v.
0
, SyntaxHighlighterGrammar v.
0
Sample Code to be highlighted (see output below):
// this is part of the SyntaxHighlighterGrammar code itself function get_mode( grammar, SyntaxHighlighter ) { var shToken = function( t ) { return new SyntaxHighlighter.Match(t.token, t.pos, t.type); } ,HighlighterBrush = SyntaxHighlighter.Highlighter ,SyntaxHighlighterBrush = Class(HighlighterBrush, { constructor: function SyntaxHighlighterBrush( ) { var self = this; HighlighterBrush.call( self ); } ,findMatches: function( regexList, code ) { var tokens = SyntaxHighlighterBrush.$parser.parse(code, TOKENS|ERRORS|FLAT).tokens; return map( tokens, shToken ); } }) ; SyntaxHighlighterBrush.$id = uuid("syntaxhighlighter_grammar_brush"); SyntaxHighlighterBrush.$parser = new SyntaxHighlighterGrammar.Parser( parse_grammar( grammar ) ); SyntaxHighlighterBrush.dispose = function( ) { if ( SyntaxHighlighterBrush.$parser ) SyntaxHighlighterBrush.$parser.dispose( ); SyntaxHighlighterBrush.$parser = null; }; //SyntaxHighlighterBrush.aliases = [lang]; //SyntaxHighlighter.brushes[lang] = SyntaxHighlighterBrush; return SyntaxHighlighterBrush; }
Grammar (JSON format):
// a partial javascript grammar in simple JSON format { // prefix ID for regular expressions used in the grammar "RegExpID" : "RE::", // Style model "Style" : { "comment" : "comments" ,"atom" : "color2" ,"keyword" : "keyword" ,"this" : "keyword" ,"builtin" : "constants" ,"operator" : "plain" ,"identifier" : "variable" ,"property" : "variable" ,"number" : "constants" ,"string" : "string" ,"regex" : "color1" }, // Lexical model "Lex" : { "comment" : {"type":"comment","tokens":[ // line comment // start, end delims (null matches end-of-line) [ "//", null ], // block comments // start, end delims [ "/*", "*/" ] ]} ,"identifier" : "RE::/[_A-Za-z$][_A-Za-z0-9$]*/" ,"this" : "RE::/this\\b/" ,"property" : "RE::/[_A-Za-z$][_A-Za-z0-9$]*/" ,"number" : [ // floats "RE::/\\d*\\.\\d+(e[\\+\\-]?\\d+)?/", "RE::/\\d+\\.\\d*/", "RE::/\\.\\d+/", // integers // hex "RE::/0x[0-9a-fA-F]+L?/", // binary "RE::/0b[01]+L?/", // octal "RE::/0o[0-7]+L?/", // decimal "RE::/[1-9]\\d*(e[\\+\\-]?\\d+)?L?/", // just zero "RE::/0(?![\\dx])/" ] ,"string" : {"type":"escaped-block","escape":"\\","tokens": // start, end of string (can be the matched regex group ie. 1 ) [ "RE::/(['\"])/", 1 ] } ,"regex" : {"type":"escaped-block","escape":"\\","tokens": // javascript literal regular expressions can be parsed similar to strings [ "/", "RE::#/[gimy]{0,4}#" ] } ,"operator" : {"tokens":[ "+", "-", "++", "--", "%", ">>", "<<", ">>>", "*", "/", "^", "|", "&", "!", "~", ">", "<", "<=", ">=", "!=", "!==", "=", "==", "===", "+=", "-=", "%=", ">>=", ">>>=", "<<=", "*=", "/=", "|=", "&=" ]} ,"delimiter" : {"tokens":[ "(", ")", "[", "]", "{", "}", ",", "=", ";", "?", ":", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "++", "--", ">>=", "<<=" ]} ,"atom" : {"autocomplete":true,"tokens":[ "true", "false", "null", "undefined", "NaN", "Infinity" ]} ,"keyword" : {"autocomplete":true,"tokens":[ "if", "while", "with", "else", "do", "try", "finally", "return", "break", "continue", "new", "delete", "throw", "var", "const", "let", "function", "catch", "void", "for", "switch", "case", "default", "class", "import", "yield", "in", "typeof", "instanceof" ]} ,"builtin" : {"autocomplete":true,"tokens":[ "Object", "Function", "Array", "String", "Date", "Number", "RegExp", "Math", "Exception", "setTimeout", "setInterval", "parseInt", "parseFloat", "isFinite", "isNan", "alert", "prompt", "console", "window", "global", "this" ]} }, // Syntax model (optional) "Syntax" : { "dot_property" : {"sequence":[".", "property"]} ,"js" : "comment | number | string | regex | keyword | operator | atom | (('}' | ')' | this | builtin | identifier | dot_property) dot_property*)" }, // what to parse and in what order "Parser" : [ ["js"] ] }
Output: