Commit 2a570fd49ffb0af98e419a6f3444570a5e1d564f

Saswat Padhi 2020-01-12T02:23:06

OCaml: Improvements (#2179) - Added some missing keywords - Added highlighting for modules, labels, and constructors - Split polymorphic variant types and type variables

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