Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OCaml #628

Merged
merged 4 commits into from Sep 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions components.js
Expand Up @@ -283,6 +283,10 @@ var components = {
"require": "c",
"owner": "uranusjr"
},
"ocaml": {
"title": "OCaml",
"owner": "Golmote"
},
"pascal": {
"title": "Pascal",
"owner": "Golmote"
Expand Down
21 changes: 21 additions & 0 deletions 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': /[(){}\[\]|_.,:;]/
};
1 change: 1 addition & 0 deletions components/prism-ocaml.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions 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>
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/languages/ocaml/boolean_feature.test
@@ -0,0 +1,13 @@
false
true

----------------------------------------------------

[
["boolean", "false"],
["boolean", "true"]
]

----------------------------------------------------

Checks for booleans.
14 changes: 14 additions & 0 deletions tests/languages/ocaml/comment_feature.test
@@ -0,0 +1,14 @@
(**)
(* foo
bar *)

----------------------------------------------------

[
["comment", "(**)"],
["comment", "(* foo\r\nbar *)"]
]

----------------------------------------------------

Checks for comments.
15 changes: 15 additions & 0 deletions tests/languages/ocaml/directive_feature.test
@@ -0,0 +1,15 @@
#quit
#load
#load_rec

----------------------------------------------------

[
["directive", "#quit"],
["directive", "#load"],
["directive", "#load_rec"]
]

----------------------------------------------------

Checks for directives.
101 changes: 101 additions & 0 deletions 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.
25 changes: 25 additions & 0 deletions 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.
31 changes: 31 additions & 0 deletions 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.
25 changes: 25 additions & 0 deletions 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.
17 changes: 17 additions & 0 deletions 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.