Skip to content

Commit

Permalink
OCaml: Improvements (#2179)
Browse files Browse the repository at this point in the history
- Added some missing keywords
- Added highlighting for modules, labels, and constructors
- Split polymorphic variant types and type variables
  • Loading branch information
SaswatPadhi authored and RunDevelopment committed Jan 12, 2020
1 parent e5678a0 commit 2a570fd
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 25 deletions.
26 changes: 20 additions & 6 deletions components/prism-ocaml.js
Expand Up @@ -11,17 +11,31 @@ Prism.languages.ocaml = {
}
],
'number': /\b(?:0x[\da-f][\da-f_]+|(?:0[bo])?\d[\d_]*\.?[\d_]*(?:e[+-]?[\d_]+)?)/i,
'type': {
pattern: /\B['`]\w*/,
alias: 'variable'
},
'directive': {
pattern: /\B#\w+/,
alias: 'important'
},
'label': {
pattern: /\B~\w+/,
alias: 'function'
},
'type_variable': {
pattern: /\B'\w+/,
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/,
'variant': {
pattern: /`\w+/,
alias: 'variable'
},
'module': {
pattern: /\b[A-Z]\w+/,
alias: 'variable'
},
// For the list of keywords and operators,
// see: http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#sec84
'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|nonrec|object|of|open|private|rec|sig|struct|then|to|try|type|val|value|virtual|when|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/,
'operator': /:=|[=<>@^|&+\-*\/$%!?~][!$%&*+\-.\/:<=>?@^|~]*|\b(?:and|asr|land|lor|lsl|lsr|lxor|mod|or)\b/,
'punctuation': /[(){}\[\]|_.,:;]/
};
2 changes: 1 addition & 1 deletion components/prism-ocaml.min.js

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

7 changes: 4 additions & 3 deletions examples/prism-ocaml.html
Expand Up @@ -9,7 +9,8 @@ <h2>Numbers</h2>
42.
2.4E+2
10_452_102
0xf4 0xff_10_41
0xf4
0xff_10_41
0o427
0b1100_1111_0000</code></pre>

Expand All @@ -29,7 +30,7 @@ <h2>Full example</h2>

(** [create low high] creates a new interval from [low] to
[high]. If [low > high], then the interval is empty *)
let create low high =
let create ~low ~high =
if Endpoint.compare low high > 0 then Empty
else Interval (low,high)

Expand All @@ -54,6 +55,6 @@ <h2>Full example</h2>
match t1,t2 with
| Empty, _ | _, Empty -> Empty
| Interval (l1,h1), Interval (l2,h2) ->
create (max l1 l2) (min h1 h2)
create ~low:(max l1 l2) ~high:(min h1 h2)

end ;;</code></pre>
10 changes: 6 additions & 4 deletions tests/languages/ocaml/keyword_feature.test
Expand Up @@ -26,21 +26,22 @@ method
module
mutable
new
nonrec
object
of
open
prefix
private
rec
then
sig
struct
then
to
try
type
val
value
virtual
when
where
while
with
Expand Down Expand Up @@ -76,21 +77,22 @@ with
["keyword", "module"],
["keyword", "mutable"],
["keyword", "new"],
["keyword", "nonrec"],
["keyword", "object"],
["keyword", "of"],
["keyword", "open"],
["keyword", "prefix"],
["keyword", "private"],
["keyword", "rec"],
["keyword", "then"],
["keyword", "sig"],
["keyword", "struct"],
["keyword", "then"],
["keyword", "to"],
["keyword", "try"],
["keyword", "type"],
["keyword", "val"],
["keyword", "value"],
["keyword", "virtual"],
["keyword", "when"],
["keyword", "where"],
["keyword", "while"],
["keyword", "with"]
Expand Down
13 changes: 13 additions & 0 deletions tests/languages/ocaml/label_feature.test
@@ -0,0 +1,13 @@
~foo
~bar_42

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

[
["label", "~foo"],
["label", "~bar_42"]
]

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

Checks for labels.
15 changes: 15 additions & 0 deletions tests/languages/ocaml/module_feature.test
@@ -0,0 +1,15 @@
Foo
Bar42
Baz_42

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

[
["module", "Foo"],
["module", "Bar42"],
["module", "Baz_42"]
]

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

Checks for modules.
4 changes: 4 additions & 0 deletions tests/languages/ocaml/number_feature.test
@@ -1,3 +1,5 @@
1234
32.
0xBad_Face
0o754_672
0b1010_1111
Expand All @@ -10,6 +12,8 @@
----------------------------------------------------

[
["number", "1234"],
["number", "32."],
["number", "0xBad_Face"],
["number", "0o754_672"],
["number", "0b1010_1111"],
Expand Down
8 changes: 4 additions & 4 deletions tests/languages/ocaml/operator_feature.test
@@ -1,6 +1,6 @@
and asr land
lor lxor lsl lsr
mod nor or
lor lsl lsr
lxor mod or

:=
= < > @
Expand All @@ -14,8 +14,8 @@ $ % ! ?

[
["operator", "and"], ["operator", "asr"], ["operator", "land"],
["operator", "lor"], ["operator", "lxor"], ["operator", "lsl"], ["operator", "lsr"],
["operator", "mod"], ["operator", "nor"], ["operator", "or"],
["operator", "lor"], ["operator", "lsl"], ["operator", "lsr"],
["operator", "lxor"], ["operator", "mod"], ["operator", "or"],

["operator", ":="],
["operator", "="], ["operator", "<"], ["operator", ">"], ["operator", "@"],
Expand Down
@@ -1,17 +1,13 @@
'Foo
'bar_42
`Foo
`bar_42

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

[
["type", "'Foo"],
["type", "'bar_42"],
["type", "`Foo"],
["type", "`bar_42"]
["type_variable", "'Foo"],
["type_variable", "'bar_42"]
]

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

Checks for types.
Checks for type variables.
15 changes: 15 additions & 0 deletions tests/languages/ocaml/variant_feature.test
@@ -0,0 +1,15 @@
`Foo
`bar32
`Baz_42

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

[
["variant", "`Foo"],
["variant", "`bar32"],
["variant", "`Baz_42"]
]

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

Checks for polymorphic variants.

0 comments on commit 2a570fd

Please sign in to comment.