8000 Improve F# (#1573) · PrismJS/prism@00bfc96 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 00bfc96

Browse files

8 files changed

+238
-15
lines changed

components/prism-fsharp.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,26 @@ Prism.languages.fsharp = Prism.languages.extend('clike', {
99
lookbehind: true
1010
}
1111
],
12-
'keyword': /\b(?:let|return|use|yield)(?:!\B|\b)|\b(abstract|and|as|assert|base|begin|class|default|delegate|do|done|downcast|downto|elif|else|end|exception|extern|false|finally|for|fun|function|global|if|in|inherit|inline|interface|internal|lazy|match|member|module|mutable|namespace|new|not|null|of|open|or|override|private|public|rec|select|static|struct|then|to|true|try|type|upcast|val|void|when|while|with|asr|land|lor|lsl|lsr|lxor|mod|sig|atomic|break|checked|component|const|constraint|constructor|continue|eager|event|external|fixed|functor|include|method|mixin|object|parallel|process|protected|pure|sealed|tailcall|trait|virtual|volatile)\b/,
1312
'string': {
1413
pattern: /(?:"""[\s\S]*?"""|@"(?:""|[^"])*"|"(?:\\[\s\S]|[^\\"])*")B?|'(?:[^\\']|\\.)'B?/,
1514
greedy: true
1615
},
16+
'class-name': {
17+
pattern: /(\b(?:exception|inherit|interface|new|of|type)\s+|\w\s*:\s*|\s:\??>\s*)[.\w]+\b(?:\s*(?:->|\*)\s*[.\w]+\b)*(?!\s*[:.])/,
18+
lookbehind: true,
19+
inside: {
20+
'operator': /->|\*/,
21+
'punctuation': /\./
22+
}
23+
},
24+
'keyword': /\b(?:let|return|use|yield)(?:!\B|\b)|\b(abstract|and|as|assert|base|begin|class|default|delegate|do|done|downcast|downto|elif|else|end|exception|extern|false|finally|for|fun|function|global|if|in|inherit|inline|interface|internal|lazy|match|member|module|mutable|namespace|new|not|null|of|open|or|override|private|public|rec|select|static|struct|then|to|true|try|type|upcast|val|void|when|while|with|asr|land|lor|lsl|lsr|lxor|mod|sig|atomic|break|checked|component|const|constraint|constructor|continue|eager|event|external|fixed|functor|include|method|mixin|object|parallel|process|protected|pure|sealed|tailcall|trait|virtual|volatile)\b/,
1725
'number': [
1826
/\b0x[\da-fA-F]+(?:un|lf|LF)?\b/,
1927
/\b0b[01]+(?:y|uy)?\b/,
2028
/(?:\b\d+\.?\d*|\B\.\d+)(?:[fm]|e[+-]?\d+)?\b/i,
2129
/\b\d+(?:[IlLsy]|u[lsy]?|UL)?\b/
22-
]
30+
],
31+
'operator': /([<>~&^])\1\1|([*.:<>&])\2|<-|->|[!=:]=|<?\|{1,3}>?|\??(?:<=|>=|<>|[-+*/%=<>])\??|[!?^&]|~[+~-]|:>|:\?>?/
2332
});
2433
Prism.languages.insertBefore('fsharp', 'keyword', {
2534
'preprocessor': {
@@ -34,3 +43,25 @@ Prism.languages.insertBefore('fsharp', 'keyword', {
3443
}
3544
}
3645
});
46+
Prism.languages.insertBefore('fsharp', 'punctuation', {
47+
'computation-expression': {
48+
pattern: /[_a-z]\w*(?=\s*\{)/i,
49+
alias: 'keyword'
50+
}
51+
10000 });
52+
Prism.languages.insertBefore('fsharp', 'string', {
53+
'annotation': {
54+
pattern: /\[<.+?>\]/,
55+
inside: {
56+
'punctuation': /^\[<|>\]$/,
57+
'class-name': {
58+
pattern: /^\w+$|(^|;\s*)[A-Z]\w*(?=\()/,
59+
lookbehind: true
60+
},
61+
'annotation-content': {
62+
pattern: /[\s\S]*/,
63+
inside: Prism.languages.fsharp
64+
}
65+
}
66+
}
67+
});

components/prism-fsharp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[<Foo>]
2+
[<Bar("bar"); Foo(1, 2)>]
3+
4+
----------------------------------------------------
5+
6+
[
7+
["annotation", [
8+
["punctuation", "[<"],
9+
["class-name", "Foo"],
10+
["punctuation", ">]"]
11+
]],
12+
["annotation", [
13+
["punctuation", "[<"],
14+
["class-name", "Bar"],
15+
["annotation-content", [
16+
["punctuation", "("],
17+
["string", "\"bar\""],
18+
["punctuation", ")"],
19+
["punctuation", ";"]
20+
]],
21+
["class-name", "Foo"],
22+
["annotation-content", [
23+
["punctuation", "("],
24+
["number", "1"],
25+
["punctuation", ","],
26+
["number", "2"],
27+
["punctuation", ")"]
28+
]],
29+
["punctuation", ">]"]
30+
]]
31+
]
32+
33+
----------------------------------------------------
34+
35+
Checks for annotations.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
let func : HttpFunc = handler (Some >> Task.FromResult)
2+
3+
type Base1() =
4+
abstract member F : unit -> unit
5+
default u.F() =
6+
printfn "F Base1"
7+
8+
type Derived1() =
9+
inherit Base1()
10+
override u.F() =
11+
printfn "F Derived1"
12+
13+
let d1 : Derived1 = Derived1()
14+
15+
let base1 = d1 :> Base1
16+
let derived1 = base1 :?> Derived1
17+
18+
type PersonName =
19+
| FirstOnly of string
20+
| LastOnly of string
21+
| FirstLast of string * string
22+
23+
type Shape =
24+
| Rectangle of height : float * width : float
25+
| Circle of radius : float
26+
27+
type MyInterface =
28+
abstract member Add: int -> int -> int
29+
abstract member Pi : float
30+
31+
exception Error1 of string
32+
exception Error2 of string * int
33+
34+
----------------------------------------------------
35+
36+
[
37+
["keyword", "let"], " func ",
38+
["punctuation", ":"], ["class-name", ["HttpFunc"]],
39+
["operator", "="], " handler ", ["punctuation", "("],
40+
"Some ", ["operator", ">>"], " Task", ["punctuation", "."], "FromResult",
41+
["punctuation", ")"],
42+
43+
["keyword", "type"], ["class-name", ["Base1"]], ["punctuation", "("], ["punctuation", ")"], ["operator", "="],
44+
["keyword", "abstract"], ["keyword", "member"], " F ", ["punctuation", ":"],
45+
["class-name", [
46+
"unit ", ["operator", "->"], " unit"]
47+
],
48+
["keyword", "default"], " u", ["punctuation", "."], ["function", "F"], ["punctuation", "("], ["punctuation", ")"],
49+
["operator", "="], "\n printfn ", ["string", "\"F Base1\""],
50+
51+
["keyword", "type"], ["class-name", ["Derived1"]], ["punctuation", "("], ["punctuation", ")"], ["operator", "="],
52+
["keyword", "inherit"], ["class-name", ["Base1"]], ["punctuation", "("], ["punctuation", ")"],
53+
["keyword", "override"], " u", ["punctuation", "."], ["function", "F"], ["punctuation", "("], ["punctuation", ")"], ["operator", "="],
54+
"\n printfn ", ["string", "\"F Derived1\""],
55+
56+
["keyword", "let"], " d1 ", ["punctuation", ":"], ["class-name", ["Derived1"]], ["operator", "="],
57+
["function", "Derived1"], ["punctuation", "("], ["punctuation", ")"],
58+
59+
["keyword", "let"], " base1 ", ["operator", "="], " d1 ", ["operator", ":>"], ["class-name", ["Base1"]],
60+
61+
["keyword", "let"], " derived1 ", ["operator", "="], " base1 ", ["operator", ":?>"], ["class-name", ["Derived1"]],
62+
63+
["keyword", "type"], ["class-name", ["PersonName"]], ["operator", "="],
64+
["operator", "|"], " FirstOnly ", ["keyword", "of"], ["class-name", ["string"]],
65+
["operator", "|"], " LastOnly ", ["keyword", "of"], ["class-name", ["string"]],
66+
["operator", "|"], " FirstLast ", ["keyword", "of"], ["class-name", ["string ", ["operator", "*"], " string"]],
67+
68+
["keyword", "type"], ["class-name", ["Shape"]], ["operator", "="],
69+
["operator", "|"], " Rectangle ", ["keyword", "of"],
70+
" height ", ["punctuation", ":"], ["class-name", ["float"]], ["operator", "*"],
71+
" width ", ["punctuation", ":"], ["class-name", ["float"]],
72+
["operator", "|"], " Circle ", ["keyword", "of"], " radius ", ["punctuation", ":"], ["class-name", ["float"]],
73+
74+
["keyword", "type"], ["class-name", ["MyInterface"]], ["operator", "="],
75+
["keyword", "abstract"], ["keyword", "member"], " Add", ["punctuation", ":"],
76+
["class-name", ["int ", ["operator", "->"], " int ", ["operator", "->"], " int"]],
77+
["keyword", "abstract"], ["keyword", "member"], " Pi ", ["punctuation", ":"], ["class-name", ["float"]],
78+
79+
["keyword", "exception"], ["class-name", ["Error1"]], ["keyword", "of"], ["class-name", ["string"]],
80+
81+
["keyword", "exception"], ["class-name", ["Error2"]], ["keyword", "of"], ["class-name", ["string ", ["operator", "*"], " int"]]
82+
]
83+
84+
----------------------------------------------------
85+
86+
Checks for class-names.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
async {}
2+
task {}
3+
seq {}
4+
foo {}
5+
6+
----------------------------------------------------
7+
8+
[
9+
["computation-expression", "async"], ["punctuation", "{"], ["punctuation", "}"],
10+
["computation-expression", "task"], ["punctuation", "{"], ["punctuation", "}"],
11+
["computation-expression", "seq"], ["punctuation", "{"], ["punctuation", "}"],
12+
["computation-expression", "foo"], ["punctuation", "{"], ["punctuation", "}"]
13+
]
14+
15+
----------------------------------------------------
16+
17+
Checks for computation expressions.

tests/languages/fsharp/issue1480.test

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ let map (f: 'a -> 'b) (xs: 'a list): 'b list = failWith "not implemented"
1616
"f",
1717
["punctuation", ":"],
1818
" 'a ",
19-
["operator", "-"],
20-
["operator", ">"],
19+
["operator", "->"],
2120
" 'b",
2221
["punctuation", ")"],
2322
["punctuation", "("],
@@ -34,4 +33,4 @@ let map (f: 'a -> 'b) (xs: 'a list): 'b list = failWith "not implemented"
3433

3534
----------------------------------------------------
3635

37-
Checks for apostrophes in names. See #1480.
36+
Checks for apostrophes in names. See #1480.

tests/languages/fsharp/keyword_feature.test

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ class;
44
default
55
delegate do done downcast
66
downto elif else end
7-
exception extern false finally
7+
exception;
8+
extern false finally
89
for fun function global
9-
if in inherit inline
10+
if in inherit; inline
1011
interface;
1112
internal lazy let
1213
let! match member module
1314
mutable namespace
1415
new;
1516
not
16-
null of open or override
17+
null of; open or override
1718
private public rec return
1819
return! select static struct
19-
then to true try type
20+
then to true try type;
2021
upcast use use! val void
2122
when while with yield
2223
yield! asr land lor lsl
@@ -39,19 +40,20 @@ virtual volatile
3940
["keyword", "default"],
4041
["keyword", "delegate"], ["keyword", "do"], ["keyword", "done"], ["keyword", "downcast"],
4142
["keyword", "downto"], ["keyword", "elif"], ["keyword", "else"], ["keyword", "end"],
42-
["keyword", "exception"], ["keyword", "extern"], ["keyword", "false"], ["keyword", "finally"],
43+
["keyword", "exception"], ["punctuation", ";"],
44+
["keyword", "extern"], ["keyword", "false"], ["keyword", "finally"],
4345
["keyword", "for"], ["keyword", "fun"], ["keyword", "function"], ["keyword", "global"],
44-
["keyword", "if"], ["keyword", "in"], ["keyword", "inherit"], ["keyword", "inline"],
46+
["keyword", "if"], ["keyword", "in"], ["keyword", "inherit"], ["punctuation", ";"], ["keyword", "inline"],
4547
["keyword", "interface"], ["punctuation", ";"],
4648
["keyword", "internal"], ["keyword", "lazy"], ["keyword", "let"],
4749
["keyword", "let!"], ["keyword", "match"], ["keyword", "member"], ["keyword", "module"],
4850
["keyword", "mutable"], ["keyword", "namespace"],
4951
["keyword", "new"], ["punctuation", ";"],
5052
["keyword", "not"],
51-
["keyword", "null"], ["keyword", "of"], ["keyword", "open"], ["keyword", "or"], ["keyword", "override"],
53+
["keyword", "null"], ["keyword", "of"], ["punctuation", ";"], ["keyword", "open"], ["keyword", "or"], ["keyword", "override"],
5254
["keyword", "private"], ["keyword", "public"], ["keyword", "rec"], ["keyword", "return"],
5355
["keyword", "return!"], ["keyword", "select"], ["keyword", "static"], ["keyword", "struct"],
54-
["keyword", "then"], ["keyword", "to"], ["keyword", "true"], ["keyword", "try"], ["keyword", "type"],
56+
["keyword", "then"], ["keyword", "to"], ["keyword", "true"], ["keyword", "try"], ["keyword", "type"], ["punctuation", ";"],
5557
["keyword", "upcast"], ["keyword", "use"], ["keyword", "use!"], ["keyword", "val"], ["keyword", "void"],
5658
["keyword", "when"], ["keyword", "while"], ["keyword", "with"], ["keyword", "yield"],
5759
["keyword", "yield!"], ["keyword", "asr"], ["keyword", "land"], ["keyword", "lor"], ["keyword", "lsl"],
@@ -68,4 +70,4 @@ virtual volatile
6870

6971
----------------------------------------------------
7072

71-
Checks for all keywords.
73+
Checks for all keywords.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
["operator", ">="], ["operator", "<="], ["operator", "<>"], ["operator", ">"], ["operator", "<"], ["operator", "="], ["operator", "+"], ["operator", "-"], ["operator", "*"], ["operator", "/"], ["operator", "%"],
28+
["operator", ">=?"], ["operator", "<=?"], ["operator", "<>?"], ["operator", ">?"], ["operator", "<?"], ["operator", "=?"], ["operator", "+?"], ["operator", "-?"], ["operator", "*?"], ["operator", "/?"], ["operator", "%?"],
29+
["operator", "?>=?"], ["operator", "?<=?"], ["operator", "?<>?"], ["operator", "?>?"], ["operator", "?<?"], ["operator", "?=?"], ["operator", "?+?"], ["operator", "?-?"], ["operator", "?*?"], ["operator", "?/?"], ["operator", "?%?"],
30+
["operator", "?>="], ["operator", "?<="], ["operator", "?<>"], ["operator", "?>"], ["operator", "?<"], ["operator", "?="], ["operator", "?+"], ["operator", "?-"], ["operator", "?*"], ["operator", "?/"], ["operator", "?%"],
31+
32+
["operator", "**"],
33+
34+
["operator", "<-"], ["operator", "->"],
35+
["operator", ".."],
36+
["operator", "::"],
37+
["operator", ":="],
38+
["operator", ":>"], ["operator", ":?"], ["operator", ":?>"],
39+
["operator", "<<"], ["operator", ">>"],
40+
["operator", "<<<"], ["operator", ">>>"], ["operator", "~~~"], ["operator", "^^^"], ["operator", "&&&"], ["operator", "|||"],
41+
["operator", "|"], ["operator", "||"],
42+
["operator", "<|"], ["operator", "<||"], ["operator", "<|||"],
43+
["operator", "|>"], ["operator", "||>"], ["operator", "|||>"],
44+
["operator", "~~"], ["operator", "~-"], ["operator", "~+"],
45+
46+
["operator", "?"], ["operator", "^"], ["operator", "!"],
47+
["operator", "!="], ["operator", "=="],
48+
["operator", "&"], ["operator", "&&"]
49+
]
50+
51+
----------------------------------------------------
52+
53+
Checks for operators.

0 commit comments

Comments
 (0)
0