]> luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/scala.js
Initial commit.
[flow-web.git] / static / highlight / languages / scala.js
1 /*! `scala` grammar compiled for Highlight.js 11.11.1 */
2 (function(){
3 var hljsGrammar = (function () {
4 'use strict';
5
6 /*
7 Language: Scala
8 Category: functional
9 Author: Jan Berkel <jan.berkel@gmail.com>
10 Contributors: Erik Osheim <d_m@plastic-idolatry.com>
11 Website: https://www.scala-lang.org
12 */
13
14 function scala(hljs) {
15 const regex = hljs.regex;
16 const ANNOTATION = {
17 className: 'meta',
18 begin: '@[A-Za-z]+'
19 };
20
21 // used in strings for escaping/interpolation/substitution
22 const SUBST = {
23 className: 'subst',
24 variants: [
25 { begin: '\\$[A-Za-z0-9_]+' },
26 {
27 begin: /\$\{/,
28 end: /\}/
29 }
30 ]
31 };
32
33 const STRING = {
34 className: 'string',
35 variants: [
36 {
37 begin: '"""',
38 end: '"""'
39 },
40 {
41 begin: '"',
42 end: '"',
43 illegal: '\\n',
44 contains: [ hljs.BACKSLASH_ESCAPE ]
45 },
46 {
47 begin: '[a-z]+"',
48 end: '"',
49 illegal: '\\n',
50 contains: [
51 hljs.BACKSLASH_ESCAPE,
52 SUBST
53 ]
54 },
55 {
56 className: 'string',
57 begin: '[a-z]+"""',
58 end: '"""',
59 contains: [ SUBST ],
60 relevance: 10
61 }
62 ]
63
64 };
65
66 const TYPE = {
67 className: 'type',
68 begin: '\\b[A-Z][A-Za-z0-9_]*',
69 relevance: 0
70 };
71
72 const NAME = {
73 className: 'title',
74 begin: /[^0-9\n\t "'(),.`{}\[\]:;][^\n\t "'(),.`{}\[\]:;]+|[^0-9\n\t "'(),.`{}\[\]:;=]/,
75 relevance: 0
76 };
77
78 const CLASS = {
79 className: 'class',
80 beginKeywords: 'class object trait type',
81 end: /[:={\[\n;]/,
82 excludeEnd: true,
83 contains: [
84 hljs.C_LINE_COMMENT_MODE,
85 hljs.C_BLOCK_COMMENT_MODE,
86 {
87 beginKeywords: 'extends with',
88 relevance: 10
89 },
90 {
91 begin: /\[/,
92 end: /\]/,
93 excludeBegin: true,
94 excludeEnd: true,
95 relevance: 0,
96 contains: [
97 TYPE,
98 hljs.C_LINE_COMMENT_MODE,
99 hljs.C_BLOCK_COMMENT_MODE,
100 ]
101 },
102 {
103 className: 'params',
104 begin: /\(/,
105 end: /\)/,
106 excludeBegin: true,
107 excludeEnd: true,
108 relevance: 0,
109 contains: [
110 TYPE,
111 hljs.C_LINE_COMMENT_MODE,
112 hljs.C_BLOCK_COMMENT_MODE,
113 ]
114 },
115 NAME
116 ]
117 };
118
119 const METHOD = {
120 className: 'function',
121 beginKeywords: 'def',
122 end: regex.lookahead(/[:={\[(\n;]/),
123 contains: [ NAME ]
124 };
125
126 const EXTENSION = {
127 begin: [
128 /^\s*/, // Is first token on the line
129 'extension',
130 /\s+(?=[[(])/, // followed by at least one space and `[` or `(`
131 ],
132 beginScope: { 2: "keyword", }
133 };
134
135 const END = {
136 begin: [
137 /^\s*/, // Is first token on the line
138 /end/,
139 /\s+/,
140 /(extension\b)?/, // `extension` is the only marker that follows an `end` that cannot be captured by another rule.
141 ],
142 beginScope: {
143 2: "keyword",
144 4: "keyword",
145 }
146 };
147
148 // TODO: use negative look-behind in future
149 // /(?<!\.)\binline(?=\s)/
150 const INLINE_MODES = [
151 { match: /\.inline\b/ },
152 {
153 begin: /\binline(?=\s)/,
154 keywords: 'inline'
155 }
156 ];
157
158 const USING_PARAM_CLAUSE = {
159 begin: [
160 /\(\s*/, // Opening `(` of a parameter or argument list
161 /using/,
162 /\s+(?!\))/, // Spaces not followed by `)`
163 ],
164 beginScope: { 2: "keyword", }
165 };
166
167 // glob all non-whitespace characters as a "string"
168 // sourced from https://github.com/scala/docs.scala-lang/pull/2845
169 const DIRECTIVE_VALUE = {
170 className: 'string',
171 begin: /\S+/,
172 };
173
174 // directives
175 // sourced from https://github.com/scala/docs.scala-lang/pull/2845
176 const USING_DIRECTIVE = {
177 begin: [
178 '//>',
179 /\s+/,
180 /using/,
181 /\s+/,
182 /\S+/
183 ],
184 beginScope: {
185 1: "comment",
186 3: "keyword",
187 5: "type"
188 },
189 end: /$/,
190 contains: [
191 DIRECTIVE_VALUE,
192 ]
193 };
194
195 return {
196 name: 'Scala',
197 keywords: {
198 literal: 'true false null',
199 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 transparent'
200 },
201 contains: [
202 USING_DIRECTIVE,
203 hljs.C_LINE_COMMENT_MODE,
204 hljs.C_BLOCK_COMMENT_MODE,
205 STRING,
206 TYPE,
207 METHOD,
208 CLASS,
209 hljs.C_NUMBER_MODE,
210 EXTENSION,
211 END,
212 ...INLINE_MODES,
213 USING_PARAM_CLAUSE,
214 ANNOTATION
215 ]
216 };
217 }
218
219 return scala;
220
221 })();
222
223 hljs.registerLanguage('scala', hljsGrammar);
224 })();