Skip to content

Commit 81cf234

Browse files
Julia: Improved strings, comments, and other patterns (#2363)
1 parent ab1e34a commit 81cf234

10 files changed

+222
-29
lines changed

components/prism-julia.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
Prism.languages.julia= {
1+
Prism.languages.julia = {
22
'comment': {
3-
pattern: /(^|[^\\])#.*/,
3+
// support one level of nested comments
4+
// https://github.com/JuliaLang/julia/pull/6128
5+
pattern: /(^|[^\\])(?:#=(?:[^#=]|=(?!#)|#(?!=)|#=(?:[^#=]|=(?!#)|#(?!=))*=#)*=#|#.*)/,
46
lookbehind: true
57
},
6-
'string': /("""|''')[\s\S]+?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2/,
7-
'keyword' : /\b(?:abstract|baremodule|begin|bitstype|break|catch|ccall|const|continue|do|else|elseif|end|export|finally|for|function|global|if|immutable|import|importall|in|let|local|macro|module|print|println|quote|return|struct|try|type|typealias|using|while)\b/,
8-
'boolean' : /\b(?:true|false)\b/,
9-
'number' : /(?:\b(?=\d)|\B(?=\.))(?:0[box])?(?:[\da-f]+\.?\d*|\.\d+)(?:[efp][+-]?\d+)?j?/i,
10-
'operator': /[-+*^%÷&$\\]=?|\/[\/=]?|!=?=?|\|[=>]?|<(?:<=?|[=:])?|>(?:=|>>?=?)?|==?=?|[~]/,
11-
'punctuation' : /[{}[\];(),.:]/,
12-
'constant': /\b(?:(?:NaN|Inf)(?:16|32|64)?)\b/
8+
'regex': {
9+
// https://docs.julialang.org/en/v1/manual/strings/#Regular-Expressions-1
10+
pattern: /r"(?:\\.|[^"\\\r\n])*"[imsx]{0,4}/,
11+
greedy: true
12+
},
13+
'string': {
14+
// https://docs.julialang.org/en/v1/manual/strings/#man-characters-1
15+
// https://docs.julialang.org/en/v1/manual/strings/#String-Basics-1
16+
// https://docs.julialang.org/en/v1/manual/strings/#non-standard-string-literals-1
17+
// https://docs.julialang.org/en/v1/manual/running-external-programs/#Running-External-Programs-1
18+
pattern: /"""[\s\S]+?"""|\w*"(?:\\.|[^"\\\r\n])*"|(^|[^\w'])'(?:\\[^\r\n][^'\r\n]*|[^\\\r\n])'|`(?:[^\\`\r\n]|\\.)*`/,
19+
lookbehind: true,
20+
greedy: true
21+
},
22+
'keyword': /\b(?:abstract|baremodule|begin|bitstype|break|catch|ccall|const|continue|do|else|elseif|end|export|finally|for|function|global|if|immutable|import|importall|in|let|local|macro|module|print|println|quote|return|struct|try|type|typealias|using|while)\b/,
23+
'boolean': /\b(?:true|false)\b/,
24+
'number': /(?:\b(?=\d)|\B(?=\.))(?:0[box])?(?:[\da-f]+(?:_[\da-f]+)*\.?(?:\d+(?:_\d+)*)?|\.\d+(?:_\d+)*)(?:[efp][+-]?\d+(?:_\d+)*)?j?/i,
25+
// https://docs.julialang.org/en/v1/manual/mathematical-operations/
26+
// https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1
27+
'operator': /&&|\|\||[-+*^%÷&$\\]=?|\/[\/=]?|!=?=?|\|[=>]?|<(?:<=?|[=:|])?|>(?:=|>>?=?)?|==?=?|[~']/,
28+
'punctuation': /::?|[{}[\]();,.?]/,
29+
// https://docs.julialang.org/en/v1/base/numbers/#Base.im
30+
'constant': /\b(?:(?:NaN|Inf)(?:16|32|64)?|im|pi|e|catalan|eulergamma|golden)\b|[πγφ]/
1331
};

components/prism-julia.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+11-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
#
22
# foobar
3+
#=#
4+
#=
5+
multi line
6+
=#
7+
8+
#= #= nested =# =#
39

410
----------------------------------------------------
511

612
[
713
["comment", "#"],
8-
["comment", "# foobar"]
14+
["comment", "# foobar"],
15+
["comment", "#=#"],
16+
["comment", "#=\r\n multi line\r\n =#"],
17+
["comment", "#= #= nested =# =#"]
918
]
1019

1120
----------------------------------------------------
1221

13-
Checks for comments.
22+
Checks for comments.

tests/languages/julia/constant_feature.test

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
NaN NaN16 NaN32 NaN64
22
Inf Inf16 Inf32 Inf64
33

4+
im
5+
pi π
6+
e ℯ
7+
catalan
8+
eulergamma γ
9+
golden φ
10+
411
----------------------------------------------------
512

613
[
@@ -11,9 +18,20 @@ Inf Inf16 Inf32 Inf64
1118
["constant", "Inf"],
1219
["constant", "Inf16"],
1320
["constant", "Inf32"],
14-
["constant", "Inf64"]
21+
["constant", "Inf64"],
22+
23+
["constant", "im"],
24+
["constant", "pi"],
25+
["constant", "π"],
26+
["constant", "e"],
27+
["constant", "ℯ"],
28+
["constant", "catalan"],
29+
["constant", "eulergamma"],
30+
["constant", "γ"],
31+
["constant", "golden"],
32+
["constant", "φ"]
1533
]
1634

1735
----------------------------------------------------
1836

19-
Checks for all constants.
37+
Checks for all constants.

tests/languages/julia/issue2360.test

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
count_consecutive(str::AbstractString, start::Int)
3+
4+
Counts the number of identical characters in a given string `str` starting from index `start`.
5+
6+
# Examples
7+
8+
```julia-repl
9+
julia> count_consecutive("AAAB", 3)
10+
('A', 3)
11+
12+
julia> count_consecutive("x y z", 3)
13+
('y', 1)
14+
```
15+
"""
16+
function count_consecutive(str, start)
17+
...
18+
end
19+
20+
----------------------------------------------------
21+
22+
[
23+
["string", "\"\"\"\r\n\tcount_consecutive(str::AbstractString, start::Int)\r\n\r\nCounts the number of identical characters in a given string `str` starting from index `start`.\r\n\r\n# Examples\r\n\r\n```julia-repl\r\njulia> count_consecutive(\"AAAB\", 3)\r\n('A', 3)\r\n\r\njulia> count_consecutive(\"x y z\", 3)\r\n('y', 1)\r\n```\r\n\"\"\""],
24+
["keyword", "function"],
25+
" count_consecutive",
26+
["punctuation", "("],
27+
"str",
28+
["punctuation", ","],
29+
" start",
30+
["punctuation", ")"],
31+
["punctuation", "."],
32+
["punctuation", "."],
33+
["punctuation", "."],
34+
["keyword", "end"]
35+
]
36+
37+
----------------------------------------------------
38+
39+
Multi-line strings didn't work correctly.
+16-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
0b0011
22
0o274
33
0xBadFace
4+
0x123456789abcdef
45
42
6+
1.
7+
.5
58
1.23
69
1e10
710
2.5e-4
@@ -10,24 +13,35 @@
1013
0x1p0
1114
0x1.8p3
1215
0x.4p-1
16+
10_000
17+
0.000_000_005
18+
0xdead_beef
19+
0b1011_0010
1320

1421
----------------------------------------------------
1522

1623
[
1724
["number", "0b0011"],
1825
["number", "0o274"],
1926
["number", "0xBadFace"],
27+
["number", "0x123456789abcdef"],
2028
["number", "42"],
29+
["number", "1."],
30+
["number", ".5"],
2131
["number", "1.23"],
2232
["number", "1e10"],
2333
["number", "2.5e-4"],
2434
["number", "0.5f0"],
2535
["number", "2.5f-4"],
2636
["number", "0x1p0"],
2737
["number", "0x1.8p3"],
28-
["number", "0x.4p-1"]
38+
["number", "0x.4p-1"],
39+
["number", "10_000"],
40+
["number", "0.000_000_005"],
41+
["number", "0xdead_beef"],
42+
["number", "0b1011_0010"]
2943
]
3044

3145
----------------------------------------------------
3246

33-
Checks for binary, octal, hexadecimal and decimal numbers.
47+
Checks for binary, octal, hexadecimal and decimal numbers.

tests/languages/julia/operator_feature.test

+21-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
% %=
88
÷ ÷=
99
! != !==
10-
& &=
11-
| |= |>
10+
& &= &&
11+
⊻ ⊻=
12+
| |= ||
13+
|> <|
1214
$ $=
1315
< <= <: << <<=
1416
> >= >> >>= >>> >>>=
1517
= == ===
16-
~ ≠ ≤ ≥
18+
~ ≠ ≤ ≥ √ ∛
19+
20+
A'b
21+
A'b''
1722

1823
----------------------------------------------------
1924

@@ -27,15 +32,24 @@ $ $=
2732
["operator", "%"], ["operator", "%="],
2833
["operator", "÷"], ["operator", "÷="],
2934
["operator", "!"], ["operator", "!="], ["operator", "!=="],
30-
["operator", "&"], ["operator", "&="],
31-
["operator", "|"], ["operator", "|="], ["operator", "|>"],
35+
["operator", "&"], ["operator", "&="], ["operator", "&&"],
36+
["operator", "⊻"], ["operator", "⊻="],
37+
["operator", "|"], ["operator", "|="], ["operator", "||"],
38+
["operator", "|>"], ["operator", "<|"],
3239
["operator", "$"], ["operator", "$="],
3340
["operator", "<"], ["operator", "<="], ["operator", "<:"], ["operator", "<<"], ["operator", "<<="],
3441
["operator", ">"], ["operator", ">="], ["operator", ">>"], ["operator", ">>="], ["operator", ">>>"], ["operator", ">>>="],
3542
["operator", "="], ["operator", "=="], ["operator", "==="],
36-
["operator", "~"], ["operator", "≠"], ["operator", "≤"], ["operator", "≥"]
43+
["operator", "~"], ["operator", "≠"], ["operator", "≤"], ["operator", "≥"], ["operator", "√"], ["operator", "∛"],
44+
"\r\n\r\nA",
45+
["operator", "'"],
46+
"b\r\nA",
47+
["operator", "'"],
48+
"b",
49+
["operator", "'"],
50+
["operator", "'"]
3751
]
3852

3953
----------------------------------------------------
4054

41-
Checks for operators.
55+
Checks for operators.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{ } [ ] ( )
2+
; ,
3+
.
4+
::
5+
? :
6+
7+
----------------------------------------------------
8+
9+
[
10+
["punctuation", "{"],
11+
["punctuation", "}"],
12+
["punctuation", "["],
13+
["punctuation", "]"],
14+
["punctuation", "("],
15+
["punctuation", ")"],
16+
["punctuation", ";"],
17+
["punctuation", ","],
18+
["punctuation", "."],
19+
["punctuation", "::"],
20+
["punctuation", "?"],
21+
["punctuation", ":"]
22+
]
23+
24+
----------------------------------------------------
25+
26+
Checks for punctuation.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
r"foo"i
2+
r"(a|b)(c)?(d)"
3+
r"^\s*(?:#\s*(.*?)\s*$|$)"
4+
r"a+.*b+.*?d$"ism
5+
6+
----------------------------------------------------
7+
8+
[
9+
["regex", "r\"foo\"i"],
10+
["regex", "r\"(a|b)(c)?(d)\""],
11+
["regex", "r\"^\\s*(?:#\\s*(.*?)\\s*$|$)\""],
12+
["regex", "r\"a+.*b+.*?d$\"ism"]
13+
]
14+
15+
----------------------------------------------------
16+
17+
Checks for regular expressions.
+44-6
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,67 @@
11
""
22
"fo\"o"
3+
"\xe2\x88"
4+
35
'x'
46
'\''
57
'\u2200'
8+
'\x80'
9+
'\xe2\x88'
10+
'∀'
11+
612
"""foo"""
713
"""fo"o
814
bar"""
9-
'''foo'''
10-
'''fo'o
11-
bar'''
15+
16+
`echo hello`
17+
`echo "foo bar"`
18+
19+
# non-standard string
20+
s"\g<0>1"
21+
b"DATA\xff\u2200"
22+
v"0.3-"
23+
raw"\\ \\\""
24+
25+
# not a character
26+
A'b
27+
A'b''
1228

1329
----------------------------------------------------
1430

1531
[
1632
["string", "\"\""],
1733
["string", "\"fo\\\"o\""],
34+
["string", "\"\\xe2\\x88\""],
35+
1836
["string", "'x'"],
1937
["string", "'\\''"],
2038
["string", "'\\u2200'"],
39+
["string", "'\\x80'"],
40+
["string", "'\\xe2\\x88'"],
41+
["string", "'∀'"],
42+
2143
["string", "\"\"\"foo\"\"\""],
2244
["string", "\"\"\"fo\"o\r\nbar\"\"\""],
23-
["string", "'''foo'''"],
24-
["string", "'''fo'o\r\nbar'''"]
45+
46+
["string", "`echo hello`"],
47+
["string", "`echo \"foo bar\"`"],
48+
49+
["comment", "# non-standard string"],
50+
["string", "s\"\\g<0>1\""],
51+
["string", "b\"DATA\\xff\\u2200\""],
52+
["string", "v\"0.3-\""],
53+
["string", "raw\"\\\\ \\\\\\\"\""],
54+
55+
["comment", "# not a character"],
56+
"\r\nA",
57+
["operator", "'"],
58+
"b\r\nA",
59+
["operator", "'"],
60+
"b",
61+
["operator", "'"],
62+
["operator", "'"]
2563
]
2664

2765
----------------------------------------------------
2866

29-
Checks for strings and characters.
67+
Checks for strings and characters.

0 commit comments

Comments
 (0)