Skip to content

Instantly share code, notes, and snippets.

@NathanSweet
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanSweet/72a9f769a4884b2aa15d to your computer and use it in GitHub Desktop.
Save NathanSweet/72a9f769a4884b2aa15d to your computer and use it in GitHub Desktop.
Generic language syntax highlighting for Hyperlight, which is a PHP-based syntax highlighter: http://code.google.com/p/hyperlight/ Tested with: Java, C#, JavaScript, AS3, C, C++, Lua
.code {
font-family: Consolas, 'Lucida Console', monospace;
font-size: 85%;
line-height: 1.5em;
color: #000;
border: 1px solid #DBDBDB;
padding: 0.5em;
margin-bottom: 1.25em;
white-space: nowrap;
max-height: 600px;
background-color: #F5F5F5;
overflow: auto;
}
.code .comment {
color: #00a452;
font-style: italic;
}
.code .identifier {
color: #000080;
}
.code .keyword {
color: #000080;
font-weight: bold;
}
.code .method, .code .ctor {
font-weight: bold;
}
.code .operator {
color: #0000ff;
}
.code .string {
color: #408080;
}
<?php
// Highlights most languages reasonably: Java, C#, JavaScript, AS3, C, C++, Lua
class GenericLanguage extends HyperLanguage {
public function __construct() {
$this->setInfo(array(
parent::NAME => 'Generic',
parent::VERSION => '0.1',
parent::AUTHOR => array(
parent::NAME => 'Nathan Sweet', // based Konrad Rudolph's csharp.php!
parent::WEBSITE => 'esotericsoftware.com',
parent::EMAIL => 'misc@n4te.com'
)
));
$this->setExtensions(array('java', 'cs', 'js', 'as', 'c', 'cpp', 'lua'));
$this->setCaseInsensitive(false);
$this->addStates(array(
'init' => array(
'string',
'char',
'number',
'comment',
'keyword' => array('', 'preprocessor'),
'method',
'ctor',
'function',
'operator',
'type',
'identifier',
'whitespace',
),
));
$this->addRules(array(
'whitespace' => Rule::ALL_WHITESPACE,
'operator' => '/[-+*\/%&|^!~=<>?{}()\[\].,:;]|&&|\|\||<<|>>|[-=!<>+*\/%&|^]=|<<=|>>=|->/',
'string' => Rule::C_DOUBLEQUOTESTRING,
'char' => Rule::C_SINGLEQUOTESTRING,
'number' => Rule::C_NUMBER,
'comment' => '#(//|--)(?:[^/].*?)?(\n|\z)|/\*.*?\*/#s',
'keyword' => array(
array(
// C#
'abstract', 'break', 'case', 'catch', 'checked', 'class',
'const', 'continue', 'default', 'delegate', 'do', 'else',
'enum', 'explicit', 'extern', 'finally', 'fixed',
'for', 'foreach', 'goto', 'if', 'implicit', 'in', 'interface',
'internal', 'lock', 'namespace', 'operator', 'out', 'override',
'params', 'private', 'protected', 'public', 'readonly', 'ref',
'return', 'sealed', 'static', 'struct', 'switch', 'throw',
'try', 'unchecked', 'unsafe', 'using', 'var', 'virtual',
'volatile', 'while',
'bool', 'byte', 'char', 'decimal', 'double', 'float', 'int',
'long', 'object', 'sbyte', 'short', 'string', 'uint', 'ulong',
'ushort', 'void',
'base', 'false', 'null', 'this', 'true',
'as', 'is', 'new', 'sizeof', 'stackallock', 'typeof',
// Java
'extends', 'throws', 'implements',
'final', 'native', 'synchronized',
'transient', 'assert',
'package', 'strictfp', 'super',
'import', 'boolean', 'instanceof',
// JavaScript
'function',
// Lua
'local', 'not', 'then', 'end', 'nil', 'pairs', 'ipairs', 'and', 'or',
// C
'typedef', 'auto', 'extern', 'goto', 'register', 'signed', 'sizeof', 'struct', 'union', 'unsigned',
// C++
'alignas', 'alignof', 'and_eq', 'asm', 'bitand', 'bitor', 'char16_t', 'char32_t', 'compl', 'constexpr',
'const_cast', 'decltype', 'delete', 'dynamic_cast', 'explicit', 'export', 'friend', 'inline', 'mutable',
'noexcept', 'not_eq', 'nullptr', 'or_eq', 'reinterpret_cast', 'static_assert', 'static_cast', 'template',
'thread_local', 'typeid', 'typename', 'wchar_t', 'xor', 'xor_eq',
),
'preprocessor' => '/#(?:if|else|elif|endif|define|undef|warning|error|line|region|endregion|include|import)[^\r\n]*/',
),
'method' => '/(?<=\.)\s*([a-zA-Z][^\s\.\(:]+)(?=\s*\()/',
'ctor' => '/(?<=new)\s+([a-zA-Z][^\s\(:]+)(?=\s*\()/',
'function' => '/([a-zA-Z][^\s\.\(:]+)(?=\s*\()/',
'type' => '/@?(sp)?[A-Z][a-z0-9_]*/',
'identifier' => '/@?[a-z_][a-z0-9_]*/i',
));
$this->addMappings(array(
'whitespace' => '',
));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment