OCaml: Improvements (#2179) - Added some missing keywords - Added highlighting for modules, labels, and constructors - Split polymorphic variant types and type variables
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
diff --git a/components/prism-ocaml.js b/components/prism-ocaml.js
index 09e13bb..849c3af 100644
--- a/components/prism-ocaml.js
+++ b/components/prism-ocaml.js
@@ -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': /[(){}\[\]|_.,:;]/
};
\ No newline at end of file
diff --git a/components/prism-ocaml.min.js b/components/prism-ocaml.min.js
index 704dada..25114ed 100644
--- a/components/prism-ocaml.min.js
+++ b/components/prism-ocaml.min.js
@@ -1 +1 @@
-Prism.languages.ocaml={comment:/\(\*[\s\S]*?\*\)/,string:[{pattern:/"(?:\\.|[^\\\r\n"])*"/,greedy:!0},{pattern:/(['`])(?:\\(?:\d+|x[\da-f]+|.)|(?!\1)[^\\\r\n])\1/i,greedy:!0}],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:"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
+Prism.languages.ocaml={comment:/\(\*[\s\S]*?\*\)/,string:[{pattern:/"(?:\\.|[^\\\r\n"])*"/,greedy:!0},{pattern:/(['`])(?:\\(?:\d+|x[\da-f]+|.)|(?!\1)[^\\\r\n])\1/i,greedy:!0}],number:/\b(?:0x[\da-f][\da-f_]+|(?:0[bo])?\d[\d_]*\.?[\d_]*(?:e[+-]?[\d_]+)?)/i,directive:{pattern:/\B#\w+/,alias:"important"},label:{pattern:/\B~\w+/,alias:"function"},type_variable:{pattern:/\B'\w+/,alias:"function"},variant:{pattern:/`\w+/,alias:"variable"},module:{pattern:/\b[A-Z]\w+/,alias:"variable"},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/,operator:/:=|[=<>@^|&+\-*\/$%!?~][!$%&*+\-.\/:<=>?@^|~]*|\b(?:and|asr|land|lor|lsl|lsr|lxor|mod|or)\b/,punctuation:/[(){}\[\]|_.,:;]/};
\ No newline at end of file
diff --git a/examples/prism-ocaml.html b/examples/prism-ocaml.html
index 46ee6dc..85aa107 100644
--- a/examples/prism-ocaml.html
+++ b/examples/prism-ocaml.html
@@ -9,7 +9,8 @@ comment *)</code></pre>
42.
2.4E+2
10_452_102
-0xf4 0xff_10_41
+0xf4
+0xff_10_41
0o427
0b1100_1111_0000</code></pre>
@@ -29,7 +30,7 @@ comment *)</code></pre>
(** [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)
@@ -54,6 +55,6 @@ comment *)</code></pre>
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>
diff --git a/tests/languages/ocaml/keyword_feature.test b/tests/languages/ocaml/keyword_feature.test
index 2259da6..dd86480 100644
--- a/tests/languages/ocaml/keyword_feature.test
+++ b/tests/languages/ocaml/keyword_feature.test
@@ -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
@@ -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"]
diff --git a/tests/languages/ocaml/label_feature.test b/tests/languages/ocaml/label_feature.test
new file mode 100644
index 0000000..71b9bd4
--- /dev/null
+++ b/tests/languages/ocaml/label_feature.test
@@ -0,0 +1,13 @@
+~foo
+~bar_42
+
+----------------------------------------------------
+
+[
+ ["label", "~foo"],
+ ["label", "~bar_42"]
+]
+
+----------------------------------------------------
+
+Checks for labels.
\ No newline at end of file
diff --git a/tests/languages/ocaml/module_feature.test b/tests/languages/ocaml/module_feature.test
new file mode 100644
index 0000000..006d259
--- /dev/null
+++ b/tests/languages/ocaml/module_feature.test
@@ -0,0 +1,15 @@
+Foo
+Bar42
+Baz_42
+
+----------------------------------------------------
+
+[
+ ["module", "Foo"],
+ ["module", "Bar42"],
+ ["module", "Baz_42"]
+]
+
+----------------------------------------------------
+
+Checks for modules.
\ No newline at end of file
diff --git a/tests/languages/ocaml/number_feature.test b/tests/languages/ocaml/number_feature.test
index 2076a5c..6ade211 100644
--- a/tests/languages/ocaml/number_feature.test
+++ b/tests/languages/ocaml/number_feature.test
@@ -1,3 +1,5 @@
+1234
+32.
0xBad_Face
0o754_672
0b1010_1111
@@ -10,6 +12,8 @@
----------------------------------------------------
[
+ ["number", "1234"],
+ ["number", "32."],
["number", "0xBad_Face"],
["number", "0o754_672"],
["number", "0b1010_1111"],
diff --git a/tests/languages/ocaml/operator_feature.test b/tests/languages/ocaml/operator_feature.test
index 75c8f01..50047fe 100644
--- a/tests/languages/ocaml/operator_feature.test
+++ b/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
:=
= < > @
@@ -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", "@"],
diff --git a/tests/languages/ocaml/type_feature.test b/tests/languages/ocaml/type_feature.test
deleted file mode 100644
index f6fdd68..0000000
--- a/tests/languages/ocaml/type_feature.test
+++ /dev/null
@@ -1,17 +0,0 @@
-'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
diff --git a/tests/languages/ocaml/type_variable_feature.test b/tests/languages/ocaml/type_variable_feature.test
new file mode 100644
index 0000000..7f680e4
--- /dev/null
+++ b/tests/languages/ocaml/type_variable_feature.test
@@ -0,0 +1,13 @@
+'Foo
+'bar_42
+
+----------------------------------------------------
+
+[
+ ["type_variable", "'Foo"],
+ ["type_variable", "'bar_42"]
+]
+
+----------------------------------------------------
+
+Checks for type variables.
\ No newline at end of file
diff --git a/tests/languages/ocaml/variant_feature.test b/tests/languages/ocaml/variant_feature.test
new file mode 100644
index 0000000..5cc7242
--- /dev/null
+++ b/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.
\ No newline at end of file