1 | 'use strict'
|
---|
2 |
|
---|
3 | module.exports = regex
|
---|
4 | regex.displayName = 'regex'
|
---|
5 | regex.aliases = []
|
---|
6 | function regex(Prism) {
|
---|
7 | ;(function (Prism) {
|
---|
8 | var specialEscape = {
|
---|
9 | pattern: /\\[\\(){}[\]^$+*?|.]/,
|
---|
10 | alias: 'escape'
|
---|
11 | }
|
---|
12 | var escape =
|
---|
13 | /\\(?:x[\da-fA-F]{2}|u[\da-fA-F]{4}|u\{[\da-fA-F]+\}|0[0-7]{0,2}|[123][0-7]{2}|c[a-zA-Z]|.)/
|
---|
14 | var charSet = {
|
---|
15 | pattern: /\.|\\[wsd]|\\p\{[^{}]+\}/i,
|
---|
16 | alias: 'class-name'
|
---|
17 | }
|
---|
18 | var charSetWithoutDot = {
|
---|
19 | pattern: /\\[wsd]|\\p\{[^{}]+\}/i,
|
---|
20 | alias: 'class-name'
|
---|
21 | }
|
---|
22 | var rangeChar = '(?:[^\\\\-]|' + escape.source + ')'
|
---|
23 | var range = RegExp(rangeChar + '-' + rangeChar) // the name of a capturing group
|
---|
24 | var groupName = {
|
---|
25 | pattern: /(<|')[^<>']+(?=[>']$)/,
|
---|
26 | lookbehind: true,
|
---|
27 | alias: 'variable'
|
---|
28 | }
|
---|
29 | Prism.languages.regex = {
|
---|
30 | 'char-class': {
|
---|
31 | pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:[^\\\]]|\\[\s\S])*\]/,
|
---|
32 | lookbehind: true,
|
---|
33 | inside: {
|
---|
34 | 'char-class-negation': {
|
---|
35 | pattern: /(^\[)\^/,
|
---|
36 | lookbehind: true,
|
---|
37 | alias: 'operator'
|
---|
38 | },
|
---|
39 | 'char-class-punctuation': {
|
---|
40 | pattern: /^\[|\]$/,
|
---|
41 | alias: 'punctuation'
|
---|
42 | },
|
---|
43 | range: {
|
---|
44 | pattern: range,
|
---|
45 | inside: {
|
---|
46 | escape: escape,
|
---|
47 | 'range-punctuation': {
|
---|
48 | pattern: /-/,
|
---|
49 | alias: 'operator'
|
---|
50 | }
|
---|
51 | }
|
---|
52 | },
|
---|
53 | 'special-escape': specialEscape,
|
---|
54 | 'char-set': charSetWithoutDot,
|
---|
55 | escape: escape
|
---|
56 | }
|
---|
57 | },
|
---|
58 | 'special-escape': specialEscape,
|
---|
59 | 'char-set': charSet,
|
---|
60 | backreference: [
|
---|
61 | {
|
---|
62 | // a backreference which is not an octal escape
|
---|
63 | pattern: /\\(?![123][0-7]{2})[1-9]/,
|
---|
64 | alias: 'keyword'
|
---|
65 | },
|
---|
66 | {
|
---|
67 | pattern: /\\k<[^<>']+>/,
|
---|
68 | alias: 'keyword',
|
---|
69 | inside: {
|
---|
70 | 'group-name': groupName
|
---|
71 | }
|
---|
72 | }
|
---|
73 | ],
|
---|
74 | anchor: {
|
---|
75 | pattern: /[$^]|\\[ABbGZz]/,
|
---|
76 | alias: 'function'
|
---|
77 | },
|
---|
78 | escape: escape,
|
---|
79 | group: [
|
---|
80 | {
|
---|
81 | // https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html
|
---|
82 | // https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2#grouping-constructs
|
---|
83 | // (), (?<name>), (?'name'), (?>), (?:), (?=), (?!), (?<=), (?<!), (?is-m), (?i-m:)
|
---|
84 | pattern:
|
---|
85 | /\((?:\?(?:<[^<>']+>|'[^<>']+'|[>:]|<?[=!]|[idmnsuxU]+(?:-[idmnsuxU]+)?:?))?/,
|
---|
86 | alias: 'punctuation',
|
---|
87 | inside: {
|
---|
88 | 'group-name': groupName
|
---|
89 | }
|
---|
90 | },
|
---|
91 | {
|
---|
92 | pattern: /\)/,
|
---|
93 | alias: 'punctuation'
|
---|
94 | }
|
---|
95 | ],
|
---|
96 | quantifier: {
|
---|
97 | pattern: /(?:[+*?]|\{\d+(?:,\d*)?\})[?+]?/,
|
---|
98 | alias: 'number'
|
---|
99 | },
|
---|
100 | alternation: {
|
---|
101 | pattern: /\|/,
|
---|
102 | alias: 'keyword'
|
---|
103 | }
|
---|
104 | }
|
---|
105 | })(Prism)
|
---|
106 | }
|
---|