Permalink
Browse files

Added support for the D programming language.

  • Loading branch information...
1 parent 3bb414c commit f0adb0997887eb8a907ae8285d4325e5d43c5489 @workhorsy workhorsy committed Feb 12, 2013
Showing with 355 additions and 0 deletions.
  1. +1 −0 README.md
  2. +129 −0 demos/d.html
  3. +81 −0 js/language/d.js
  4. +1 −0 tests/index.html
  5. +143 −0 tests/language/d-test.js
View
@@ -60,6 +60,7 @@ Currently supported languages are:
- C#
- Coffeescript
- CSS
+- D
- Go
- Haskell
- HTML
View
@@ -0,0 +1,129 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<title>Syntax Highlighting</title>
+<link href="../themes/obsidian.css" rel="stylesheet" type="text/css" media="screen">
+<body>
+
+<pre>
+<code data-language="d">
+// Single line comment
+
+/*
+ Multi line comment
+*/
+
+/+
+ Multi line doc comment
++/
+
+import std.stdio;
+
+// Integers
+ushort a = 0;
+short b = 1;
+int c = 2;
+uint d = 3;
+long e = 4;
+ulong f = 5;
+size_t g = 90;
+
+// Boolean
+bool h = true;
+bool m = false;
+
+// Floating point
+float j = 7.9;
+real k = 0;
+double l = 0.8;
+
+// Strings, chars, bytes
+string s = "derp";
+string t = null;
+char u = 'a';
+wchar v = 'b';
+byte w = 0;
+ubyte x = 0;
+
+// Struct
+struct Thing {
+ int count;
+}
+
+auto thing = Thing();
+thing.count = 35;
+writefln("thing.count: %d", thing.count);
+
+// Class
+class Animal {
+ string _name;
+
+ string name() { return _name; }
+
+ this(string name) {
+ _name = name;
+ }
+}
+
+auto ani = new Animal("dog");
+writefln("ani.name: %s", ani.name);
+
+// Class Inheritance
+class Dog : Animal {
+ this(string name) {
+ super(name);
+ }
+
+ void bark() {
+ writeln("Woof!");
+ }
+}
+
+auto dog = new Dog("Rover");
+writefln("dog.name: %s", dog.name);
+dog.bark();
+
+// For loop
+for(size_t i=0; i<5; ++i) {
+ writefln("i: %d", i);
+}
+
+// For each loop
+foreach(i; [0, 1, 2, 3, 4]) {
+ writefln("i: %d", i);
+}
+
+// While loop
+int counter = 0;
+while(counter < 5) {
+ writefln("counter: %d", counter);
+ counter++;
+}
+
+// Do while loop
+counter = 0;
+do {
+ writefln("counter: %d", counter);
+ counter++;
+} while(counter < 5);
+
+
+// If condition
+bool flag_a = true;
+bool flag_b = false;
+if(flag_a && flag_b) {
+
+}
+
+// Switch condition
+string name = "Derper";
+switch(name) {
+ case "Bobrick": write("Stupid name"); break;
+ case "Rickyrick": write("Awesome name"); break;
+ default: write("Unexpected name"); break;
+}
+</code>
+</pre>
+ <script src="../js/rainbow.js"></script>
+ <script src="../js/language/d.js"></script>
+</body>
+
View
@@ -0,0 +1,81 @@
+/**
+* D patterns
+*
+* @author Matthew Brennan Jones
+* @version 1.0.0
+*/
+Rainbow.extend('d', [
+ {
+ 'name': 'constant',
+ 'pattern': /\b(false|null|true)\b/g
+ },
+ {
+ // http://dlang.org/lex.html
+ 'name': 'keyword',
+ 'pattern': /\b(abstract|alias|align|asm|assert|auto|body|bool|break|byte|case|cast|catch|cdouble|cent|cfloat|char|class|const|continue|creal|dchar|debug|default|delegate|delete|deprecated|do|double|else|enum|export|extern|false|final|finally|float|for|foreach|foreach_reverse|function|goto|idouble|if|ifloat|immutable|import|in|inout|int|interface|invariant|ireal|is|lazy|long|macro|mixin|module|new|nothrow|null|out|override|package|pragma|private|protected|public|pure|real|ref|return|scope|shared|short|size_t|static|string|struct|super|switch|synchronized|template|this|throw|true|try|typedef|typeid|typeof|ubyte|ucent|uint|ulong|union|unittest|ushort|version|void|volatile|wchar|while|with|__FILE__|__LINE__|__gshared|__traits|__vector|__parameters)\b/g
+ },
+ {
+ 'matches': {
+ 1: 'keyword',
+ 2: {
+ 'name': 'support.class',
+ 'pattern': /\w+/g
+ }
+ },
+ 'pattern': /(typeof)\s([^\$].*?)(\)|;)/g
+ },
+ {
+ 'matches': {
+ 1: 'keyword.namespace',
+ 2: {
+ 'name': 'support.namespace',
+ 'pattern': /\w+/g
+ }
+ },
+ 'pattern': /\b(namespace)\s(.*?);/g
+ },
+ {
+ 'matches': {
+ 1: 'storage.modifier',
+ 2: 'storage.class',
+ 3: 'entity.name.class',
+ 4: 'storage.modifier.extends',
+ 5: 'entity.other.inherited-class'
+ },
+ 'pattern': /\b(abstract|sealed)?\s?(class)\s(\w+)(\sextends\s)?([\w\\]*)?\s?\{?(\n|\})/g
+ },
+ {
+ 'name': 'keyword.static',
+ 'pattern': /\b(static)\b/g
+ },
+ {
+ 'matches': {
+ 1: 'keyword.new',
+ 2: {
+ 'name': 'support.class',
+ 'pattern': /\w+/g
+ }
+
+ },
+ 'pattern': /\b(new)\s([^\$].*?)(?=\)|\(|;|&)/g
+ },
+ {
+ 'name': 'string',
+ 'pattern': /("|')(.*?)\1/g
+ },
+ {
+ 'name': 'integer',
+ 'pattern': /\b(0x[\da-f]+|\d+)\b/g
+ },
+ {
+ 'name': 'comment',
+ 'pattern': /\/\*[\s\S]*?\*\/|\/\+[\s\S]*?\+\/|(\/\/)[\s\S]*?$/gm
+ },
+ {
+ // http://dlang.org/operatoroverloading.html
+ 'name': 'operator',
+ // / /= . .. ... & &= && | |= || - -= -- + += ++ < <= << <<= <> <>= > >= >>= >>>= >> >>> ! != !<> !<>= !< !<= !> !>= ( ) [ ] { } ? , ; : $ = == * *= % %= ^ ^= ^^ ^^= ~ ~= @ => #
+ 'pattern': /(\/|\/=|.|..|...|&amp;|&amp;=|&amp;&amp;|\||\|=|\|\||\-|\-=|\-\-|\+|\+=|\+\+|&lt;|&lt;=|&lt;&lt;|&lt;&lt;=|&lt;&gt;|&lt;&gt;=|&gt;|&gt;=|&gt;&gt;=|&gt;&gt;&gt;=|&gt;&gt;|&gt;&gt;&gt;|!|!=|!&lt;&gt;|!&lt;&gt;=|!&lt;|!&lt;=|!&gt;|!&gt;=|\(|\)|[|]|\{|\}|\?|,|;|:|\$|=|==|\*|\*=|%|%=|\^|\^=|\^\^|\^\^=|~|~=|@|=&gt;|\#)/g
+ }
+], true);
+
View
@@ -12,6 +12,7 @@
<option selected value="csharp">csharp</option>
<option selected value="css">css</option>
<option selected value="coffeescript">coffeescript</option>
+ <option selected value="d">d</option>
<option selected value="generic">generic</option>
<option selected value="html">html</option>
<option selected value="haskell">haskell</option>
@@ -0,0 +1,143 @@
+/**
+ * D tests
+ *
+ * @author Matthew Brennan Jones
+ */
+RainbowTester.startTest('d');
+
+RainbowTester.run(
+ 'echo',
+
+ 'writeln("hello world");',
+
+ 'writeln(<span class="string">"hello world"</span>);'
+);
+
+RainbowTester.run(
+ 'variable',
+
+ 'bool foo = true;',
+
+ '<span class="keyword">bool</span> foo <span class="operator">=</span> <span class="constant">true</span>;'
+);
+
+RainbowTester.run(
+ 'string concatenation',
+
+ 'string foo = "test " ~ "string " ~ "concatenation";',
+
+ '<span class="keyword">string</span> foo <span class="operator">=</span> <span class="string">"test "</span> <span class="operator">~</span> <span class="string">"string "</span> <span class="operator">~</span> <span class="string">"concatenation"</span>;'
+);
+
+RainbowTester.run(
+ 'typeof',
+
+ "var is_array_object = typeof(System.Array);",
+
+ '<span class="keyword">var</span> is_array_object <span class="operator">=</span> <span class="keyword">typeof</span>(System.Array);'
+);
+
+RainbowTester.run(
+ 'array stuff',
+
+ 'string[] turtles = new string[] {\n' +
+ ' "leonardo",\n' +
+ ' "michaelangelo",\n' +
+ ' "donatello",\n' +
+ ' "raphael"\n' +
+ '};\n' +
+ '\n' +
+ 'bool exists = turtles[0] == "leonardo";',
+
+ '<span class="keyword">string</span>[] turtles <span class="operator">=</span> <span class="keyword">new</span> <span class="keyword">string</span>[] {\n' +
+ ' <span class="string">"leonardo"</span>,\n' +
+ ' <span class="string">"michaelangelo"</span>,\n' +
+ ' <span class="string">"donatello"</span>,\n' +
+ ' <span class="string">"raphael"</span>\n' +
+ '};\n' +
+ '\n' +
+ '<span class="keyword">bool</span> exists <span class="operator">=</span> turtles[<span class="integer">0</span>] <span class="operator">==</span> <span class="string">"leonardo"</span>;'
+);
+
+RainbowTester.run(
+ 'module declaration',
+
+ 'module SonicDatabase;',
+
+ '<span class="keyword">module</span> SonicDatabase;'
+);
+
+RainbowTester.run(
+ 'class declaration',
+
+ 'class MyClass {}',
+
+ '<span class="storage class">class</span> <span class="entity name class">MyClass</span> {}'
+);
+
+RainbowTester.run(
+ 'abstract class declaration',
+
+ 'abstract class MyClass {}',
+
+ '<span class="storage modifier">abstract</span> <span class="storage class">class</span> <span class="entity name class">MyClass</span> {}'
+);
+
+RainbowTester.run(
+ 'child class declaration',
+
+ 'class MyCollection : ICollection {}',
+
+ '<span class="keyword">class</span> MyCollection <span class="operator">:</span> ICollection {}'
+);
+
+RainbowTester.run(
+ 'test static',
+
+ 'static void doSomethingElse() {}',
+
+ '<span class="keyword">static</span> <span class="keyword">void</span> doSomethingElse() {}'
+);
+
+RainbowTester.run(
+ 'test new class',
+
+ 'new SomeClass();',
+
+ '<span class="keyword new">new</span> <span class="support class">SomeClass</span>();'
+);
+
+RainbowTester.run(
+ 'test new namespace class',
+
+ 'auto s = new Sonic.Database.Query();',
+
+ '<span class="keyword">auto</span> s <span class="operator">=</span> <span class="keyword new">new</span> <span class="support class">Sonic</span>.<span class="support class">Database</span>.<span class="support class">Query</span>();'
+);
+
+RainbowTester.run(
+ 'test static class call',
+
+ 'auto path = Sonic.App.getInstance();',
+
+ '<span class="keyword">auto</span> path <span class="operator">=</span> Sonic.App.getInstance();'
+);
+
+RainbowTester.run(
+ 'type hint',
+
+ 'public static string getForUser(User user, Sort sort) {}',
+
+ '<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">string</span> getForUser(User user, Sort sort) {}'
+);
+
+RainbowTester.run(
+ 'template',
+
+ 'public List!string firstNames = new List!string()',
+
+ '<span class="keyword">public</span> List!<span class="keyword">string</span> firstNames <span class="operator">=</span> <span class="keyword new">new</span> List!<span class="keyword">string</span>()'
+);
+
+RainbowTester.endTest('d');
+

0 comments on commit f0adb09

Please sign in to comment.