UNPKG

3.79 kBJavaScriptView Raw
1/*
2Language: Scala
3Category: functional
4Author: Jan Berkel <jan.berkel@gmail.com>
5Contributors: Erik Osheim <d_m@plastic-idolatry.com>
6Website: https://www.scala-lang.org
7*/
8
9function scala(hljs) {
10 const regex = hljs.regex;
11 const ANNOTATION = {
12 className: 'meta',
13 begin: '@[A-Za-z]+'
14 };
15
16 // used in strings for escaping/interpolation/substitution
17 const SUBST = {
18 className: 'subst',
19 variants: [
20 {
21 begin: '\\$[A-Za-z0-9_]+'
22 },
23 {
24 begin: /\$\{/,
25 end: /\}/
26 }
27 ]
28 };
29
30 const STRING = {
31 className: 'string',
32 variants: [
33 {
34 begin: '"""',
35 end: '"""'
36 },
37 {
38 begin: '"',
39 end: '"',
40 illegal: '\\n',
41 contains: [ hljs.BACKSLASH_ESCAPE ]
42 },
43 {
44 begin: '[a-z]+"',
45 end: '"',
46 illegal: '\\n',
47 contains: [
48 hljs.BACKSLASH_ESCAPE,
49 SUBST
50 ]
51 },
52 {
53 className: 'string',
54 begin: '[a-z]+"""',
55 end: '"""',
56 contains: [ SUBST ],
57 relevance: 10
58 }
59 ]
60
61 };
62
63 const TYPE = {
64 className: 'type',
65 begin: '\\b[A-Z][A-Za-z0-9_]*',
66 relevance: 0
67 };
68
69 const NAME = {
70 className: 'title',
71 begin: /[^0-9\n\t "'(),.`{}\[\]:;][^\n\t "'(),.`{}\[\]:;]+|[^0-9\n\t "'(),.`{}\[\]:;=]/,
72 relevance: 0
73 };
74
75 const CLASS = {
76 className: 'class',
77 beginKeywords: 'class object trait type',
78 end: /[:={\[\n;]/,
79 excludeEnd: true,
80 contains: [
81 hljs.C_LINE_COMMENT_MODE,
82 hljs.C_BLOCK_COMMENT_MODE,
83 {
84 beginKeywords: 'extends with',
85 relevance: 10
86 },
87 {
88 begin: /\[/,
89 end: /\]/,
90 excludeBegin: true,
91 excludeEnd: true,
92 relevance: 0,
93 contains: [ TYPE ]
94 },
95 {
96 className: 'params',
97 begin: /\(/,
98 end: /\)/,
99 excludeBegin: true,
100 excludeEnd: true,
101 relevance: 0,
102 contains: [ TYPE ]
103 },
104 NAME
105 ]
106 };
107
108 const METHOD = {
109 className: 'function',
110 beginKeywords: 'def',
111 end: regex.lookahead(/[:={\[(\n;]/),
112 contains: [ NAME ]
113 };
114
115 const EXTENSION = {
116 begin: [
117 /^\s*/, // Is first token on the line
118 'extension',
119 /\s+(?=[[(])/, // followed by at least one space and `[` or `(`
120 ],
121 beginScope: {
122 2: "keyword",
123 }
124 };
125
126 const END = [{
127 begin: [
128 /^\s*/, // Is first token on the line
129 /end/,
130 /\s+/,
131 /(extension\b)?/, // `extension` is the only marker that follows an `end` that cannot be captured by another rule.
132 ],
133 beginScope: {
134 2: "keyword",
135 4: "keyword",
136 }
137 }];
138
139 // TODO: use negative look-behind in future
140 // /(?<!\.)\binline(?=\s)/
141 const INLINE_MODES = [{
142 match: /\.inline\b/
143 },
144 {
145 begin: /\binline(?=\s)/,
146 keywords: 'inline'
147 }];
148
149 const USING_PARAM_CLAUSE = {
150 begin: [
151 /\(\s*/, // Opening `(` of a parameter or argument list
152 /using/,
153 /\s+(?!\))/, // Spaces not followed by `)`
154 ],
155 beginScope: {
156 2: "keyword",
157 }
158 };
159
160 return {
161 name: 'Scala',
162 keywords: {
163 literal: 'true false null',
164 keyword: 'type yield lazy override def with val var sealed abstract private trait object if then forSome for while do throw finally protected extends import final return else break new catch super class case package default try this match continue throws implicit export enum given'
165 },
166 contains: [
167 hljs.C_LINE_COMMENT_MODE,
168 hljs.C_BLOCK_COMMENT_MODE,
169 STRING,
170 TYPE,
171 METHOD,
172 CLASS,
173 hljs.C_NUMBER_MODE,
174 EXTENSION,
175 END,
176 ...INLINE_MODES,
177 USING_PARAM_CLAUSE,
178 ANNOTATION
179 ]
180 };
181}
182
183export { scala as default };