Commit 556c04ded3622f8a4df6238c26f07bbb443593cf

Golmote 2015-09-09T08:00:48

Merge pull request #628 from Golmote/prism-ocaml Add support for OCaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
diff --git a/components.js b/components.js
index 2a0b7f2..f26d011 100644
--- a/components.js
+++ b/components.js
@@ -283,6 +283,10 @@ var components = {
 			"require": "c",
 			"owner": "uranusjr"
 		},
+		"ocaml": {
+			"title": "OCaml",
+			"owner": "Golmote"
+		},
 		"pascal": {
 			"title": "Pascal",
 			"owner": "Golmote"
diff --git a/components/prism-ocaml.js b/components/prism-ocaml.js
new file mode 100644
index 0000000..fd3234f
--- /dev/null
+++ b/components/prism-ocaml.js
@@ -0,0 +1,21 @@
+Prism.languages.ocaml = {
+	'comment': /\(\*[\s\S]*?\*\)/,
+	'string': [
+		/"(?:\\.|[^\\\r\n"])*"/,
+		/(['`])(?:\\(?:\d+|x[\da-f]+|.)|(?!\1)[^\\\r\n])\1/i
+	],
+	'number': /\b-?(?:0x[\da-f][\da-f_]+|(?:0[bo])?\d[\d_]*\.?[\d_]*(?:e[+-]?[\d_]+)?)/i,
+	'type': {
+		pattern: /\B['`][a-z\d_]*/i,
+		alias: 'variable'
+	},
+	'directive': {
+		pattern: /\B#[a-z\d_]+/i,
+		alias: 'function'
+	},
+	'keyword': /\b(?:as|assert|begin|class|constraint|do|done|downto|else|end|exception|external|for|fun|function|functor|if|in|include|inherit|initializer|lazy|let|match|method|module|mutable|new|object|of|open|prefix|private|rec|then|sig|struct|to|try|type|val|value|virtual|where|while|with)\b/,
+	'boolean': /\b(?:false|true)\b/,
+	// Custom operators are allowed
+	'operator': /:=|[=<>@^|&+\-*\/$%!?~][!$%&\*+\-.\/:<=>?@^|~]*|\b(?:and|asr|land|lor|lxor|lsl|lsr|mod|nor|or)\b/,
+	'punctuation': /[(){}\[\]|_.,:;]/
+};
\ No newline at end of file
diff --git a/components/prism-ocaml.min.js b/components/prism-ocaml.min.js
new file mode 100644
index 0000000..15331e7
--- /dev/null
+++ b/components/prism-ocaml.min.js
@@ -0,0 +1 @@
+Prism.languages.ocaml={comment:/\(\*[\s\S]*?\*\)/,string:[/"(?:\\.|[^\\\r\n"])*"/,/(['`])(?:\\(?:\d+|x[\da-f]+|.)|(?!\1)[^\\\r\n])\1/i],number:/\b-?(?:0x[\da-f][\da-f_]+|(?:0[bo])?\d[\d_]*\.?[\d_]*(?:e[+-]?[\d_]+)?)/i,type:{pattern:/\B['`][a-z\d_]*/i,alias:"variable"},directive:{pattern:/\B#[a-z\d_]+/i,alias:"function"},keyword:/\b(?:as|assert|begin|class|constraint|do|done|downto|else|end|exception|external|for|fun|function|functor|if|in|include|inherit|initializer|lazy|let|match|method|module|mutable|new|object|of|open|prefix|private|rec|then|sig|struct|to|try|type|val|value|virtual|where|while|with)\b/,"boolean":/\b(?:false|true)\b/,operator:/:=|[=<>@^|&+\-*\/$%!?~][!$%&\*+\-.\/:<=>?@^|~]*|\b(?:and|asr|land|lor|lxor|lsl|lsr|mod|nor|or)\b/,punctuation:/[(){}\[\]|_.,:;]/};
\ No newline at end of file
diff --git a/examples/prism-ocaml.html b/examples/prism-ocaml.html
new file mode 100644
index 0000000..041a8c6
--- /dev/null
+++ b/examples/prism-ocaml.html
@@ -0,0 +1,72 @@
+<h1>OCaml</h1>
+<p>To use this language, use the class "language-ocaml".</p>
+
+<h2>Comments</h2>
+<pre><code>(* Simple comment *)
+(* Multi-line
+comment *)</code></pre>
+
+<h2>Numbers</h2>
+<pre><code>42
+3.14159
+42.
+2.4E+2
+10_452_102
+0xf4 0xff_10_41
+0o427
+0b1100_1111_0000</code></pre>
+
+<h2>Strings and characters</h2>
+<pre><code>"Simple string."
+"String with \"quotes\" in it."
+'c' `c`
+'\'' `\``
+'\123' `\123`
+'\xf4'</code></pre>
+
+<h2>Full example</h2>
+<pre><code>module Make_interval(Endpoint : Comparable) = struct
+
+    type t = | Interval of Endpoint.t * Endpoint.t
+             | Empty
+
+    (** [create low high] creates a new interval from [low] to
+        [high].  If [low > high], then the interval is empty *)
+    let create low high =
+      if Endpoint.compare low high > 0 then Empty
+      else Interval (low,high)
+
+    (** Returns true iff the interval is empty *)
+    let is_empty = function
+      | Empty -> true
+      | Interval _ -> false
+
+    (** [contains t x] returns true iff [x] is contained in the
+        interval [t] *)
+    let contains t x =
+      match t with
+      | Empty -> false
+      | Interval (l,h) ->
+        Endpoint.compare x l >= 0 && Endpoint.compare x h <= 0
+
+    (** [intersect t1 t2] returns the intersection of the two input
+        intervals *)
+    let intersect t1 t2 =
+      let min x y = if Endpoint.compare x y <= 0 then x else y in
+      let max x y = if Endpoint.compare x y >= 0 then x else y in
+      match t1,t2 with
+      | Empty, _ | _, Empty -> Empty
+      | Interval (l1,h1), Interval (l2,h2) ->
+        create (max l1 l2) (min h1 h2)
+
+  end ;;</code></pre>
+
+<h2>Known failures</h2>
+<p>There are certain edge cases where Prism will fail.
+	There are always such cases in every regex-based syntax highlighter.
+	However, Prism dares to be open and honest about them.
+	If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug.
+</p>
+
+<h3>Comment-like substrings</h3>
+<pre><code>"foo (* bar *) baz"</code></pre>
\ No newline at end of file
diff --git a/plugins/show-language/prism-show-language.js b/plugins/show-language/prism-show-language.js
index f48fad5..f524f7c 100644
--- a/plugins/show-language/prism-show-language.js
+++ b/plugins/show-language/prism-show-language.js
@@ -5,7 +5,7 @@ if (typeof self === 'undefined' || !self.Prism || !self.document) {
 }
 
 // The languages map is built automatically with gulp
-var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","basic":"BASIC","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","glsl":"GLSL","http":"HTTP","inform7":"Inform 7","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","mel":"MEL","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/;
+var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","basic":"BASIC","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","glsl":"GLSL","http":"HTTP","inform7":"Inform 7","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","mel":"MEL","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/;
 Prism.hooks.add('before-highlight', function(env) {
 	var pre = env.element.parentNode;
 	if (!pre || !/pre/i.test(pre.nodeName)) {
diff --git a/plugins/show-language/prism-show-language.min.js b/plugins/show-language/prism-show-language.min.js
index 67d1bf4..15ab287 100644
--- a/plugins/show-language/prism-show-language.min.js
+++ b/plugins/show-language/prism-show-language.min.js
@@ -1 +1 @@
-!function(){if("undefined"!=typeof self&&self.Prism&&self.document){var e={css:"CSS",clike:"C-like",javascript:"JavaScript",actionscript:"ActionScript",apacheconf:"Apache Configuration",apl:"APL",applescript:"AppleScript",aspnet:"ASP.NET (C#)",autohotkey:"AutoHotkey",basic:"BASIC",csharp:"C#",cpp:"C++",coffeescript:"CoffeeScript","css-extras":"CSS Extras",fsharp:"F#",glsl:"GLSL",http:"HTTP",inform7:"Inform 7",latex:"LaTeX",lolcode:"LOLCODE",matlab:"MATLAB",mel:"MEL",nasm:"NASM",nsis:"NSIS",objectivec:"Objective-C",php:"PHP","php-extras":"PHP Extras",powershell:"PowerShell",jsx:"React JSX",rest:"reST (reStructuredText)",sas:"SAS",sass:"Sass (Sass)",scss:"Sass (Scss)",sql:"SQL",typescript:"TypeScript",vhdl:"VHDL",wiki:"Wiki markup",yaml:"YAML"};Prism.hooks.add("before-highlight",function(s){var t=s.element.parentNode;if(t&&/pre/i.test(t.nodeName)){var a=e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1);t.setAttribute("data-language",a)}})}}();
\ No newline at end of file
+!function(){if("undefined"!=typeof self&&self.Prism&&self.document){var e={css:"CSS",clike:"C-like",javascript:"JavaScript",actionscript:"ActionScript",apacheconf:"Apache Configuration",apl:"APL",applescript:"AppleScript",aspnet:"ASP.NET (C#)",autohotkey:"AutoHotkey",basic:"BASIC",csharp:"C#",cpp:"C++",coffeescript:"CoffeeScript","css-extras":"CSS Extras",fsharp:"F#",glsl:"GLSL",http:"HTTP",inform7:"Inform 7",latex:"LaTeX",lolcode:"LOLCODE",matlab:"MATLAB",mel:"MEL",nasm:"NASM",nsis:"NSIS",objectivec:"Objective-C",ocaml:"OCaml",php:"PHP","php-extras":"PHP Extras",powershell:"PowerShell",jsx:"React JSX",rest:"reST (reStructuredText)",sas:"SAS",sass:"Sass (Sass)",scss:"Sass (Scss)",sql:"SQL",typescript:"TypeScript",vhdl:"VHDL",wiki:"Wiki markup",yaml:"YAML"};Prism.hooks.add("before-highlight",function(s){var a=s.element.parentNode;if(a&&/pre/i.test(a.nodeName)){var t=e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1);a.setAttribute("data-language",t)}})}}();
\ No newline at end of file
diff --git a/tests/languages/ocaml/boolean_feature.test b/tests/languages/ocaml/boolean_feature.test
new file mode 100644
index 0000000..cffed63
--- /dev/null
+++ b/tests/languages/ocaml/boolean_feature.test
@@ -0,0 +1,13 @@
+false
+true
+
+----------------------------------------------------
+
+[
+	["boolean", "false"],
+	["boolean", "true"]
+]
+
+----------------------------------------------------
+
+Checks for booleans.
\ No newline at end of file
diff --git a/tests/languages/ocaml/comment_feature.test b/tests/languages/ocaml/comment_feature.test
new file mode 100644
index 0000000..25623ef
--- /dev/null
+++ b/tests/languages/ocaml/comment_feature.test
@@ -0,0 +1,14 @@
+(**)
+(* foo
+bar *)
+
+----------------------------------------------------
+
+[
+	["comment", "(**)"],
+	["comment", "(* foo\r\nbar *)"]
+]
+
+----------------------------------------------------
+
+Checks for comments.
\ No newline at end of file
diff --git a/tests/languages/ocaml/directive_feature.test b/tests/languages/ocaml/directive_feature.test
new file mode 100644
index 0000000..704e250
--- /dev/null
+++ b/tests/languages/ocaml/directive_feature.test
@@ -0,0 +1,15 @@
+#quit
+#load
+#load_rec
+
+----------------------------------------------------
+
+[
+	["directive", "#quit"],
+	["directive", "#load"],
+	["directive", "#load_rec"]
+]
+
+----------------------------------------------------
+
+Checks for directives.
\ No newline at end of file
diff --git a/tests/languages/ocaml/keyword_feature.test b/tests/languages/ocaml/keyword_feature.test
new file mode 100644
index 0000000..2259da6
--- /dev/null
+++ b/tests/languages/ocaml/keyword_feature.test
@@ -0,0 +1,101 @@
+as
+assert
+begin
+class
+constraint
+do
+done
+downto
+else
+end
+exception
+external
+for
+fun
+function
+functor
+if
+in
+include
+inherit
+initializer
+lazy
+let
+match
+method
+module
+mutable
+new
+object
+of
+open
+prefix
+private
+rec
+then
+sig
+struct
+to
+try
+type
+val
+value
+virtual
+where
+while
+with
+
+----------------------------------------------------
+
+[
+	["keyword", "as"],
+	["keyword", "assert"],
+	["keyword", "begin"],
+	["keyword", "class"],
+	["keyword", "constraint"],
+	["keyword", "do"],
+	["keyword", "done"],
+	["keyword", "downto"],
+	["keyword", "else"],
+	["keyword", "end"],
+	["keyword", "exception"],
+	["keyword", "external"],
+	["keyword", "for"],
+	["keyword", "fun"],
+	["keyword", "function"],
+	["keyword", "functor"],
+	["keyword", "if"],
+	["keyword", "in"],
+	["keyword", "include"],
+	["keyword", "inherit"],
+	["keyword", "initializer"],
+	["keyword", "lazy"],
+	["keyword", "let"],
+	["keyword", "match"],
+	["keyword", "method"],
+	["keyword", "module"],
+	["keyword", "mutable"],
+	["keyword", "new"],
+	["keyword", "object"],
+	["keyword", "of"],
+	["keyword", "open"],
+	["keyword", "prefix"],
+	["keyword", "private"],
+	["keyword", "rec"],
+	["keyword", "then"],
+	["keyword", "sig"],
+	["keyword", "struct"],
+	["keyword", "to"],
+	["keyword", "try"],
+	["keyword", "type"],
+	["keyword", "val"],
+	["keyword", "value"],
+	["keyword", "virtual"],
+	["keyword", "where"],
+	["keyword", "while"],
+	["keyword", "with"]
+]
+
+----------------------------------------------------
+
+Checks for keywords.
\ No newline at end of file
diff --git a/tests/languages/ocaml/number_feature.test b/tests/languages/ocaml/number_feature.test
new file mode 100644
index 0000000..2076a5c
--- /dev/null
+++ b/tests/languages/ocaml/number_feature.test
@@ -0,0 +1,25 @@
+0xBad_Face
+0o754_672
+0b1010_1111
+42_000
+3.14_15_9
+3.2e8
+6.1E-7
+0.4e+12_415
+
+----------------------------------------------------
+
+[
+	["number", "0xBad_Face"],
+	["number", "0o754_672"],
+	["number", "0b1010_1111"],
+	["number", "42_000"],
+	["number", "3.14_15_9"],
+	["number", "3.2e8"],
+	["number", "6.1E-7"],
+	["number", "0.4e+12_415"]
+]
+
+----------------------------------------------------
+
+Checks for numbers.
\ No newline at end of file
diff --git a/tests/languages/ocaml/operator_feature.test b/tests/languages/ocaml/operator_feature.test
new file mode 100644
index 0000000..75c8f01
--- /dev/null
+++ b/tests/languages/ocaml/operator_feature.test
@@ -0,0 +1,31 @@
+and asr land
+lor lxor lsl lsr
+mod nor or
+
+:=
+= < > @
+^ | & ~
++ - * /
+$ % ! ?
+
+~=~
+
+----------------------------------------------------
+
+[
+	["operator", "and"], ["operator", "asr"], ["operator", "land"],
+	["operator", "lor"], ["operator", "lxor"], ["operator", "lsl"], ["operator", "lsr"],
+	["operator", "mod"], ["operator", "nor"], ["operator", "or"],
+
+	["operator", ":="],
+	["operator", "="], ["operator", "<"], ["operator", ">"], ["operator", "@"],
+	["operator", "^"], ["operator", "|"], ["operator", "&"], ["operator", "~"],
+	["operator", "+"], ["operator", "-"], ["operator", "*"], ["operator", "/"],
+	["operator", "$"], ["operator", "%"], ["operator", "!"], ["operator", "?"],
+
+	["operator", "~=~"]
+]
+
+----------------------------------------------------
+
+Checks for operators.
\ No newline at end of file
diff --git a/tests/languages/ocaml/string_feature.test b/tests/languages/ocaml/string_feature.test
new file mode 100644
index 0000000..b7eabd4
--- /dev/null
+++ b/tests/languages/ocaml/string_feature.test
@@ -0,0 +1,25 @@
+""
+"Fo\"obar"
+'\''
+'\123'
+'\xf4'
+`\``
+`\123`
+`\xf4`
+
+----------------------------------------------------
+
+[
+	["string", "\"\""],
+	["string", "\"Fo\\\"obar\""],
+	["string", "'\\''"],
+	["string", "'\\123'"],
+	["string", "'\\xf4'"],
+	["string", "`\\``"],
+	["string", "`\\123`"],
+	["string", "`\\xf4`"]
+]
+
+----------------------------------------------------
+
+Checks for strings.
\ No newline at end of file
diff --git a/tests/languages/ocaml/type_feature.test b/tests/languages/ocaml/type_feature.test
new file mode 100644
index 0000000..f6fdd68
--- /dev/null
+++ b/tests/languages/ocaml/type_feature.test
@@ -0,0 +1,17 @@
+'Foo
+'bar_42
+`Foo
+`bar_42
+
+----------------------------------------------------
+
+[
+	["type", "'Foo"],
+	["type", "'bar_42"],
+	["type", "`Foo"],
+	["type", "`bar_42"]
+]
+
+----------------------------------------------------
+
+Checks for types.
\ No newline at end of file