Skip to content

Commit 2a570fd

Browse files
SaswatPadhiRunDevelopment
authored andcommitted
OCaml: Improvements (#2179)
- Added some missing keywords - Added highlighting for modules, labels, and constructors - Split polymorphic variant types and type variables
1 parent e5678a0 commit 2a570fd

10 files changed

Lines changed: 85 additions & 25 deletions

components/prism-ocaml.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,31 @@ Prism.languages.ocaml = {
1111
}
1212
],
1313
'number': /\b(?:0x[\da-f][\da-f_]+|(?:0[bo])?\d[\d_]*\.?[\d_]*(?:e[+-]?[\d_]+)?)/i,
14-
'type': {
15-
pattern: /\B['`]\w*/,
16-
alias: 'variable'
17-
},
1814
'directive': {
1915
pattern: /\B#\w+/,
16+
alias: 'important'
17+
},
18+
'label': {
19+
pattern: /\B~\w+/,
20+
alias: 'function'
21+
},
22+
'type_variable': {
23+
pattern: /\B'\w+/,
2024
alias: 'function'
2125
},
22-
'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/,
26+
'variant': {
27+
pattern: /`\w+/,
28+
alias: 'variable'
29+
},
30+
'module': {
31+
pattern: /\b[A-Z]\w+/,
32+
alias: 'variable'
33+
},
34+
// For the list of keywords and operators,
35+
// see: http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#sec84
36+
'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/,
2337
'boolean': /\b(?:false|true)\b/,
2438
// Custom operators are allowed
25-
'operator': /:=|[=<>@^|&+\-*\/$%!?~][!$%&*+\-.\/:<=>?@^|~]*|\b(?:and|asr|land|lor|lxor|lsl|lsr|mod|nor|or)\b/,
39+
'operator': /:=|[=<>@^|&+\-*\/$%!?~][!$%&*+\-.\/:<=>?@^|~]*|\b(?:and|asr|land|lor|lsl|lsr|lxor|mod|or)\b/,
2640
'punctuation': /[(){}\[\]|_.,:;]/
2741
};

components/prism-ocaml.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-ocaml.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ <h2>Numbers</h2>
99
42.
1010
2.4E+2
1111
10_452_102
12-
0xf4 0xff_10_41
12+
0xf4
13+
0xff_10_41
1314
0o427
1415
0b1100_1111_0000</code></pre>
1516

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

3031
(** [create low high] creates a new interval from [low] to
3132
[high]. If [low > high], then the interval is empty *)
32-
let create low high =
33+
let create ~low ~high =
3334
if Endpoint.compare low high > 0 then Empty
3435
else Interval (low,high)
3536

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

5960
end ;;</code></pre>

tests/languages/ocaml/keyword_feature.test

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ method
2626
module
2727
mutable
2828
new
29+
nonrec
2930
object
3031
of
3132
open
32-
prefix
3333
private
3434
rec
35-
then
3635
sig
3736
struct
37+
then
3838
to
3939
try
4040
type
4141
val
4242
value
4343
virtual
44+
when
4445
where
4546
while
4647
with
@@ -76,21 +77,22 @@ with
7677
["keyword", "module"],
7778
["keyword", "mutable"],
7879
["keyword", "new"],
80+
["keyword", "nonrec"],
7981
["keyword", "object"],
8082
["keyword", "of"],
8183
["keyword", "open"],
82-
["keyword", "prefix"],
8384
["keyword", "private"],
8485
["keyword", "rec"],
85-
["keyword", "then"],
8686
["keyword", "sig"],
8787
["keyword", "struct"],
88+
["keyword", "then"],
8889
["keyword", "to"],
8990
["keyword", "try"],
9091
["keyword", "type"],
9192
["keyword", "val"],
9293
["keyword", "value"],
9394
["keyword", "virtual"],
95+
["keyword", "when"],
9496
["keyword", "where"],
9597
["keyword", "while"],
9698
["keyword", "with"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
~foo
2+
~bar_42
3+
4+
----------------------------------------------------
5+
6+
[
7+
["label", "~foo"],
8+
["label", "~bar_42"]
9+
]
10+
11+
----------------------------------------------------
12+
13+
Checks for labels.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Foo
2+
Bar42
3+
Baz_42
4+
5+
----------------------------------------------------
6+
7+
[
8+
["module", "Foo"],
9+
["module", "Bar42"],
10+
["module", "Baz_42"]
11+
]
12+
13+
----------------------------------------------------
14+
15+
Checks for modules.

tests/languages/ocaml/number_feature.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
1234
2+
32.
13
0xBad_Face
24
0o754_672
35
0b1010_1111
@@ -10,6 +12,8 @@
1012
----------------------------------------------------
1113

1214
[
15+
["number", "1234"],
16+
["number", "32."],
1317
["number", "0xBad_Face"],
1418
["number", "0o754_672"],
1519
["number", "0b1010_1111"],

tests/languages/ocaml/operator_feature.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
and asr land
2-
lor lxor lsl lsr
3-
mod nor or
2+
lor lsl lsr
3+
lxor mod or
44

55
:=
66
= < > @
@@ -14,8 +14,8 @@ $ % ! ?
1414

1515
[
1616
["operator", "and"], ["operator", "asr"], ["operator", "land"],
17-
["operator", "lor"], ["operator", "lxor"], ["operator", "lsl"], ["operator", "lsr"],
18-
["operator", "mod"], ["operator", "nor"], ["operator", "or"],
17+
["operator", "lor"], ["operator", "lsl"], ["operator", "lsr"],
18+
["operator", "lxor"], ["operator", "mod"], ["operator", "or"],
1919

2020
["operator", ":="],
2121
["operator", "="], ["operator", "<"], ["operator", ">"], ["operator", "@"],
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
'Foo
22
'bar_42
3-
`Foo
4-
`bar_42
53

64
----------------------------------------------------
75

86
[
9-
["type", "'Foo"],
10-
["type", "'bar_42"],
11-
["type", "`Foo"],
12-
["type", "`bar_42"]
7+
["type_variable", "'Foo"],
8+
["type_variable", "'bar_42"]
139
]
1410

1511
----------------------------------------------------
1612

17-
Checks for types.
13+
Checks for type variables.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
`Foo
2+
`bar32
3+
`Baz_42
4+
5+
----------------------------------------------------
6+
7+
[
8+
["variant", "`Foo"],
9+
["variant", "`bar32"],
10+
["variant", "`Baz_42"]
11+
]
12+
13+
----------------------------------------------------
14+
15+
Checks for polymorphic variants.

0 commit comments

Comments
 (0)