Skip to content

Commit f11b86e

Browse files
authoredDec 5, 2021
Go: Added char token and improved string and number tokens (#3208)
1 parent d85a64a commit f11b86e

File tree

6 files changed

+140
-23
lines changed

6 files changed

+140
-23
lines changed
 

‎components/prism-go.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
Prism.languages.go = Prism.languages.extend('clike', {
22
'string': {
3-
pattern: /(["'`])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
3+
pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"|`[^`]*`/,
4+
lookbehind: true,
45
greedy: true
56
},
67
'keyword': /\b(?:break|case|chan|const|continue|default|defer|else|fallthrough|for|func|go(?:to)?|if|import|interface|map|package|range|return|select|struct|switch|type|var)\b/,
78
'boolean': /\b(?:_|false|iota|nil|true)\b/,
8-
'number': /(?:\b0x[a-f\d]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[-+]?\d+)?)i?/i,
9+
'number': [
10+
// binary and octal integers
11+
/\b0(?:b[01_]+|o[0-7_]+)i?\b/i,
12+
// hexadecimal integers and floats
13+
/\b0x(?:[a-f\d_]+(?:\.[a-f\d_]*)?|\.[a-f\d_]+)(?:p[+-]?\d+(?:_\d+)*)?i?(?!\w)/i,
14+
// decimal integers and floats
15+
/(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?[\d_]+)?i?(?!\w)/i
16+
],
917
'operator': /[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\./,
1018
'builtin': /\b(?:append|bool|byte|cap|close|complex|complex(?:64|128)|copy|delete|error|float(?:32|64)|u?int(?:8|16|32|64)?|imag|len|make|new|panic|print(?:ln)?|real|recover|rune|string|uintptr)\b/
1119
});
20+
21+
Prism.languages.insertBefore('go', 'string', {
22+
'char': {
23+
pattern: /'(?:\\.|[^'\\\r\n]){0,10}'/,
24+
greedy: true
25+
}
26+
});
27+
1228
delete Prism.languages.go['class-name'];

‎components/prism-go.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/languages/go/char_feature.test

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'a'
2+
'ä'
3+
'本'
4+
'\t'
5+
'\000'
6+
'\007'
7+
'\377'
8+
'\x07'
9+
'\xff'
10+
'\u12e4'
11+
'\U00101234'
12+
'\''
13+
14+
----------------------------------------------------
15+
16+
[
17+
["char", "'a'"],
18+
["char", "'ä'"],
19+
["char", "'本'"],
20+
["char", "'\\t'"],
21+
["char", "'\\000'"],
22+
["char", "'\\007'"],
23+
["char", "'\\377'"],
24+
["char", "'\\x07'"],
25+
["char", "'\\xff'"],
26+
["char", "'\\u12e4'"],
27+
["char", "'\\U00101234'"],
28+
["char", "'\\''"]
29+
]

‎tests/languages/go/number_feature.test

+65-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,48 @@
22
0600
33
0xBadFace
44
170141183460469231731687303715884105727
5+
42
6+
4_2
7+
0600
8+
0_600
9+
0o600
10+
0O600 // second character is capital letter 'O'
11+
0xBadFace
12+
0xBad_Face
13+
0x_67_7a_2f_cc_40_c6
14+
170141183460469231731687303715884105727
15+
170_141183_460469_231731_687303_715884_105727
16+
17+
0.
518
72.40
6-
072.40
19+
072.40 // == 72.40
720
2.71828
821
1.e+0
922
6.67428e-11
1023
1E6
24+
.25
25+
.12345E+5
26+
1_5. // == 15.0
27+
0.15e+0_2 // == 15.0
28+
29+
0x1p-2 // == 0.25
30+
0x2.p10 // == 2048.0
31+
0x1.Fp+0 // == 1.9375
32+
0X.8p-0 // == 0.5
33+
0X_1FFFP-16 // == 0.1249847412109375
34+
1135
0i
12-
011i
36+
0123i // == 123i for backward-compatibility
37+
0o123i // == 0o123 * 1i == 83i
38+
0xabci // == 0xabc * 1i == 2748i
1339
0.i
1440
2.71828i
1541
1.e+0i
1642
6.67428e-11i
1743
1E6i
44+
.25i
45+
.12345E+5i
46+
0x1p-2i // == 0x1p-2 * 1i == 0.25i
1847

1948
----------------------------------------------------
2049

@@ -23,21 +52,51 @@
2352
["number", "0600"],
2453
["number", "0xBadFace"],
2554
["number", "170141183460469231731687303715884105727"],
55+
["number", "42"],
56+
["number", "4_2"],
57+
["number", "0600"],
58+
["number", "0_600"],
59+
["number", "0o600"],
60+
["number", "0O600"],
61+
["comment", "// second character is capital letter 'O'"],
62+
["number", "0xBadFace"],
63+
["number", "0xBad_Face"],
64+
["number", "0x_67_7a_2f_cc_40_c6"],
65+
["number", "170141183460469231731687303715884105727"],
66+
["number", "170_141183_460469_231731_687303_715884_105727"],
67+
68+
["number", "0."],
2669
["number", "72.40"],
27-
["number", "072.40"],
70+
["number", "072.40"], ["comment", "// == 72.40"],
2871
["number", "2.71828"],
2972
["number", "1.e+0"],
3073
["number", "6.67428e-11"],
3174
["number", "1E6"],
75+
["number", ".25"],
76+
["number", ".12345E+5"],
77+
["number", "1_5."], ["comment", "// == 15.0"],
78+
["number", "0.15e+0_2"], ["comment", "// == 15.0"],
79+
80+
["number", "0x1p-2"], ["comment", "// == 0.25"],
81+
["number", "0x2.p10"], ["comment", "// == 2048.0"],
82+
["number", "0x1.Fp+0"], ["comment", "// == 1.9375"],
83+
["number", "0X.8p-0"], ["comment", "// == 0.5"],
84+
["number", "0X_1FFFP-16"], ["comment", "// == 0.1249847412109375"],
85+
3286
["number", "0i"],
33-
["number", "011i"],
87+
["number", "0123i"], ["comment", "// == 123i for backward-compatibility"],
88+
["number", "0o123i"], ["comment", "// == 0o123 * 1i == 83i"],
89+
["number", "0xabci"], ["comment", "// == 0xabc * 1i == 2748i"],
3490
["number", "0.i"],
3591
["number", "2.71828i"],
3692
["number", "1.e+0i"],
3793
["number", "6.67428e-11i"],
38-
["number", "1E6i"]
94+
["number", "1E6i"],
95+
["number", ".25i"],
96+
["number", ".12345E+5i"],
97+
["number", "0x1p-2i"], ["comment", "// == 0x1p-2 * 1i == 0.25i"]
3998
]
4099

41100
----------------------------------------------------
42101

43-
Checks for integers, floats and imaginary numbers.
102+
Checks for integers, floats and imaginary numbers.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
( ) { } [ ]
2+
, ; . :
3+
4+
----------------------------------------------------
5+
6+
[
7+
["punctuation", "("],
8+
["punctuation", ")"],
9+
["punctuation", "{"],
10+
["punctuation", "}"],
11+
["punctuation", "["],
12+
["punctuation", "]"],
13+
14+
["punctuation", ","],
15+
["punctuation", ";"],
16+
["punctuation", "."],
17+
["punctuation", ":"]
18+
]
+9-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
'a'
2-
'ä'
3-
'本'
4-
'\t'
5-
'\xff'
6-
'\u12e4'
7-
1+
``
82
`abc`
93
`\n
104
\n`
5+
`\`
6+
7+
""
118
"\n"
129
"\""
1310
"Hello, world!\n"
@@ -17,14 +14,12 @@
1714
----------------------------------------------------
1815

1916
[
20-
["string", "'a'"],
21-
["string", "'ä'"],
22-
["string", "'本'"],
23-
["string", "'\\t'"],
24-
["string", "'\\xff'"],
25-
["string", "'\\u12e4'"],
17+
["string", "``"],
2618
["string", "`abc`"],
2719
["string", "`\\n\r\n\\n`"],
20+
["string", "`\\`"],
21+
22+
["string", "\"\""],
2823
["string", "\"\\n\""],
2924
["string", "\"\\\"\""],
3025
["string", "\"Hello, world!\\n\""],
@@ -34,4 +29,4 @@
3429

3530
----------------------------------------------------
3631

37-
Checks for runes and strings.
32+
Checks for runes and strings.

0 commit comments

Comments
 (0)
Please sign in to comment.