@@ -169,172 +169,193 @@ class DartSyntaxHighlighter extends SyntaxCostomHighlighter {
169169 bool _generateSpans () {
170170 int lastLoopPosition = _scanner.position;
171171
172- while (! _scanner.isDone) {
173- // Skip White space
174- _scanner.scan (new RegExp (r"\s+" ));
175-
176- // Block comments
177- if (_scanner.scan (new RegExp (r"/\*(.|\n)*\*/" ))) {
178- _spans.add (new _HighlightSpan (_HighlightType .comment,
179- _scanner.lastMatch.start, _scanner.lastMatch.end));
180- continue ;
181- }
172+ try {
173+ while (! _scanner.isDone) {
174+ // Skip White space
175+ _scanner.scan (new RegExp (r"\s+" ));
176+
177+ // Block comments
178+ if (_scanner.scan (new RegExp (r"/\*(.|\n)*\*/" ))) {
179+ _spans.add (new _HighlightSpan (_HighlightType .comment,
180+ _scanner.lastMatch.start, _scanner.lastMatch.end));
181+ continue ;
182+ }
183+
184+
185+ // Line comments
186+ if (_scanner.scan ("//" )) {
187+ int startComment = _scanner.lastMatch.start;
182188
183- // Line comments
184- if (_scanner.scan ("//" )) {
185- int startComment = _scanner.lastMatch.start;
186-
187- bool eof = false ;
188- int endComment;
189- if (_scanner.scan (new RegExp (r".*\n" ))) {
190- endComment = _scanner.lastMatch.end - 1 ;
191- } else {
192- eof = true ;
193- endComment = _src.length;
189+ bool eof = false ;
190+ int endComment;
191+ if (_scanner.scan (new RegExp (r".*\n" ))) {
192+ endComment = _scanner.lastMatch.end - 1 ;
193+ } else {
194+ eof = true ;
195+ endComment = _src.length;
196+ }
197+
198+
199+ _spans.add (new _HighlightSpan (
200+ _HighlightType .comment, startComment, endComment));
201+
202+ if (eof) break ;
203+
204+ continue ;
194205 }
195206
196- _spans. add ( new _HighlightSpan (
197- _HighlightType .comment, startComment, endComment)) ;
207+ if (_scanner. scan ( "#" )) {
208+ int startComment = _scanner.lastMatch.start ;
198209
199- if (eof) break ;
210+ bool eof = false ;
211+ int endComment;
200212
201- continue ;
202- }
213+ if (_scanner.scan (new RegExp (r".*\n" ))) {
214+ endComment = _scanner.lastMatch.end - 1 ;
215+ } else {
216+ eof = true ;
217+ endComment = _src.length;
218+ }
219+
220+
221+ _spans.add (new _HighlightSpan (
222+ _HighlightType .comment, startComment, endComment));
203223
204- if (_scanner.scan ("#" )) {
205- int startComment = _scanner.lastMatch.start;
224+ if (eof) break ;
206225
207- bool eof = false ;
208- int endComment;
209- if (_scanner. scan ( new RegExp ( r".*\n" ))) {
210- endComment = _scanner.lastMatch.end - 1 ;
211- } else {
212- eof = true ;
213- endComment = _src.length ;
226+ continue ;
227+ }
228+ // Raw r"String"
229+ if ( _scanner.scan ( new RegExp ( r'r".*"' ))) {
230+ _spans. add ( new _HighlightSpan ( _HighlightType .string,
231+ _scanner.lastMatch.start, _scanner.lastMatch.end)) ;
232+ continue ;
214233 }
215234
216- _spans.add (new _HighlightSpan (
217- _HighlightType .comment, startComment, endComment));
218235
219- if (eof) break ;
236+ // Raw r'String'
237+ if (_scanner.scan (new RegExp (r"r'.*'" ))) {
238+ _spans.add (new _HighlightSpan (_HighlightType .string,
239+ _scanner.lastMatch.start, _scanner.lastMatch.end));
240+ continue ;
241+ }
220242
221- continue ;
222- }
223- // Raw r"String"
224- if (_scanner.scan (new RegExp (r'r".*"' ))) {
225- _spans.add (new _HighlightSpan (_HighlightType .string,
226- _scanner.lastMatch.start, _scanner.lastMatch.end));
227- continue ;
228- }
229243
230- // Raw r'String'
231- if (_scanner.scan (new RegExp (r"r'.*'" ))) {
232- _spans.add (new _HighlightSpan (_HighlightType .string,
233- _scanner.lastMatch.start, _scanner.lastMatch.end));
234- continue ;
235- }
236244
237- // Multiline """String"""
238- if (_scanner.scan (new RegExp (r'"""(?:[^"\\]|\\(.|\n))*"""' ))) {
239- _spans.add (new _HighlightSpan (_HighlightType .string,
240- _scanner.lastMatch.start, _scanner.lastMatch.end));
241- continue ;
242- }
245+ // Multiline """String"""
246+ if (_scanner.scan (new RegExp (r'"""(?:[^"\\]|\\(.|\n))*"""' ))) {
247+ _spans.add (new _HighlightSpan (_HighlightType .string,
248+ _scanner.lastMatch.start, _scanner.lastMatch.end));
249+ continue ;
250+ }
243251
244- // Multiline '''String'''
245- if (_scanner.scan (new RegExp (r"'''(?:[^'\\]|\\(.|\n))*'''" ))) {
246- _spans.add (new _HighlightSpan (_HighlightType .string,
247- _scanner.lastMatch.start, _scanner.lastMatch.end));
248- continue ;
249- }
250252
251- // "String"
252- if (_scanner.scan (new RegExp (r'"(?:[^"\\]|\\.)*"' ))) {
253- _spans.add (new _HighlightSpan (_HighlightType .string,
254- _scanner.lastMatch.start, _scanner.lastMatch.end));
255- continue ;
256- }
257253
258- // ' String'
259- if (_scanner.scan (new RegExp (r"'(?:[^'\\]|\\.)* '" ))) {
260- _spans.add (new _HighlightSpan (_HighlightType .string,
261- _scanner.lastMatch.start, _scanner.lastMatch.end));
262- continue ;
263- }
254+ // Multiline ''' String'' '
255+ if (_scanner.scan (new RegExp (r"''' (?:[^'\\]|\\(.|\n))*'' '" ))) {
256+ _spans.add (new _HighlightSpan (_HighlightType .string,
257+ _scanner.lastMatch.start, _scanner.lastMatch.end));
258+ continue ;
259+ }
264260
265- // Double
266- if (_scanner.scan (new RegExp (r"\d+\.\d+" ))) {
267- _spans.add (new _HighlightSpan (_HighlightType .number,
268- _scanner.lastMatch.start, _scanner.lastMatch.end));
269- continue ;
270- }
271261
272- // Integer
273- if (_scanner.scan (new RegExp (r"\d+" ))) {
274- _spans.add (new _HighlightSpan (_HighlightType .number ,
275- _scanner.lastMatch.start, _scanner.lastMatch.end));
276- continue ;
277- }
262+ // "String"
263+ if (_scanner.scan (new RegExp (r'"(?:[^"\\]|\\.)*"' ))) {
264+ _spans.add (new _HighlightSpan (_HighlightType .string ,
265+ _scanner.lastMatch.start, _scanner.lastMatch.end));
266+ continue ;
267+ }
278268
279- // Punctuation
280- if (_scanner.scan (new RegExp (r"[\[\]{}().!=<>&\|\?\+\-\*/%\^~;:,]" ))) {
281- _spans.add (new _HighlightSpan (_HighlightType .punctuation,
282- _scanner.lastMatch.start, _scanner.lastMatch.end));
283- continue ;
284- }
285- //中文
286- if (_scanner.scan (new RegExp (r"[\u4e00-\u9fa5]" ))) {
287- _spans.add (new _HighlightSpan (_HighlightType .punctuation,
288- _scanner.lastMatch.start, _scanner.lastMatch.end));
289- continue ;
290- }
269+ // 'String'
270+ if (_scanner.scan (new RegExp (r"'(?:[^'\\]|\\.)*'" ))) {
271+ _spans.add (new _HighlightSpan (_HighlightType .string,
272+ _scanner.lastMatch.start, _scanner.lastMatch.end));
273+ continue ;
274+ }
291275
292- // Metadata
293- if (_scanner.scan (new RegExp (r"@\w+" ))) {
294- _spans.add (new _HighlightSpan (_HighlightType .keyword,
295- _scanner.lastMatch.start, _scanner.lastMatch.end));
296- continue ;
297- }
298276
299- // Words
300- if (_scanner.scan (new RegExp (r"\w+" ))) {
301- _HighlightType type;
302-
303- String word = _scanner.lastMatch[0 ];
304- if (word.startsWith ("_" )) word = word.substring (1 );
305-
306- if (_kKeywords.contains (word))
307- type = _HighlightType .keyword;
308- else if (_kBuiltInTypes.contains (word))
309- type = _HighlightType .keyword;
310- else if (_firstLetterIsUpperCase (word))
311- type = _HighlightType .klass;
312- else if (word.length >= 2 &&
313- word.startsWith ("k" ) &&
314- _firstLetterIsUpperCase (word.substring (1 )))
315- type = _HighlightType .constant;
316-
317- if (type != null ) {
318- _spans.add (new _HighlightSpan (
319- type, _scanner.lastMatch.start, _scanner.lastMatch.end));
277+ // Double
278+ if (_scanner.scan (new RegExp (r"\d+\.\d+" ))) {
279+ _spans.add (new _HighlightSpan (_HighlightType .number,
280+ _scanner.lastMatch.start, _scanner.lastMatch.end));
281+ continue ;
320282 }
321- }
322283
323- // Check if this loop did anything
324- if (lastLoopPosition == _scanner.position) {
325- // Failed to parse this file, abort gracefully
326- if (_spans.length > 0 ) {
284+ // Integer
285+ if (_scanner.scan (new RegExp (r"\d+" ))) {
286+ _spans.add (new _HighlightSpan (_HighlightType .number,
287+ _scanner.lastMatch.start, _scanner.lastMatch.end));
288+ continue ;
289+ }
290+
291+
292+ // Punctuation
293+ if (_scanner.scan (new RegExp (r"[\[\]{}().!=<>&\|\?\+\-\*/%\^~;:,]" ))) {
327294 _spans.add (new _HighlightSpan (_HighlightType .punctuation,
328- lastLoopPosition, _scanner.string.length - 1 ));
329- _simplify ();
330- return true ;
295+ _scanner.lastMatch.start, _scanner.lastMatch.end));
296+ continue ;
331297 }
332- return false ;
333- }
334298
335- lastLoopPosition = _scanner.position;
299+
300+ //中文
301+ if (_scanner.scan (new RegExp (r"[\u4e00-\u9fa5]" ))) {
302+ _spans.add (new _HighlightSpan (_HighlightType .punctuation,
303+ _scanner.lastMatch.start, _scanner.lastMatch.end));
304+ continue ;
305+ }
306+
307+
308+ // Metadata
309+ if (_scanner.scan (new RegExp (r"@\w+" ))) {
310+ _spans.add (new _HighlightSpan (_HighlightType .keyword,
311+ _scanner.lastMatch.start, _scanner.lastMatch.end));
312+ continue ;
313+ }
314+
315+
316+ // Words
317+ if (_scanner.scan (new RegExp (r"\w+" ))) {
318+ _HighlightType type;
319+
320+ String word = _scanner.lastMatch[0 ];
321+ if (word.startsWith ("_" )) word = word.substring (1 );
322+
323+ if (_kKeywords.contains (word))
324+ type = _HighlightType .keyword;
325+ else if (_kBuiltInTypes.contains (word))
326+ type = _HighlightType .keyword;
327+ else if (_firstLetterIsUpperCase (word))
328+ type = _HighlightType .klass;
329+ else if (word.length >= 2 &&
330+ word.startsWith ("k" ) &&
331+ _firstLetterIsUpperCase (word.substring (1 )))
332+ type = _HighlightType .constant;
333+
334+ if (type != null ) {
335+ _spans.add (new _HighlightSpan (
336+ type, _scanner.lastMatch.start, _scanner.lastMatch.end));
337+ }
338+ }
339+
340+ // Check if this loop did anything
341+ if (lastLoopPosition == _scanner.position) {
342+ // Failed to parse this file, abort gracefully
343+ if (_spans.length > 0 ) {
344+ _spans.add (new _HighlightSpan (_HighlightType .punctuation,
345+ lastLoopPosition, _scanner.string.length - 1 ));
346+ _simplify ();
347+ return true ;
348+ }
349+ return false ;
350+ }
351+
352+ lastLoopPosition = _scanner.position;
353+ }
354+ } catch (e) {
355+ print (e.toString ());
336356 }
337357
358+
338359 _simplify ();
339360 return true ;
340361 }
0 commit comments