Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit a7cc6fe

Browse files
author
mikesamuel@gmail.com
committed
Timothy Armstrong's dart prettify mode
1 parent 53dc37c commit a7cc6fe

2 files changed

Lines changed: 195 additions & 1 deletion

File tree

src/lang-dart.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (C) 2013 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
17+
/**
18+
* @fileoverview
19+
* Registers a language handler Dart.
20+
* Loosely structured based on the DartLexer in Pygments: http://pygments.org/.
21+
*
22+
* To use, include prettify.js and this file in your HTML page.
23+
* Then put your code in an HTML tag like
24+
* <pre class="prettyprint lang-dart">(Dart code)</pre>
25+
*
26+
* @author armstrong.timothy@gmail.com
27+
*/
28+
29+
PR['registerLangHandler'](
30+
PR['createSimpleLexer'](
31+
[
32+
// Whitespace.
33+
[PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0']
34+
],
35+
[
36+
// Script tag.
37+
[PR['PR_COMMENT'], /^#!(?:.*)/],
38+
39+
// `import`, `library`, `part of`, `part`, `as`, `show`, and `hide`
40+
// keywords.
41+
[PR['PR_KEYWORD'], /^\b(?:import|library|part of|part|as|show|hide)\b/i],
42+
43+
// Single-line comments.
44+
[PR['PR_COMMENT'], /^\/\/(?:.*)/],
45+
46+
// Multiline comments.
47+
[PR['PR_COMMENT'], /^\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\//], // */
48+
49+
// `class` and `interface` keywords.
50+
[PR['PR_KEYWORD'], /^\b(?:class|interface)\b/i],
51+
52+
// General keywords.
53+
[PR['PR_KEYWORD'], /^\b(?:assert|break|case|catch|continue|default|do|else|finally|for|if|in|is|new|return|super|switch|this|throw|try|while)\b/i],
54+
55+
// Declaration keywords.
56+
[PR['PR_KEYWORD'], /^\b(?:abstract|const|extends|factory|final|get|implements|native|operator|set|static|typedef|var)\b/i],
57+
58+
// Keywords for types.
59+
[PR['PR_TYPE'], /^\b(?:bool|double|Dynamic|int|num|Object|String|void)\b/i],
60+
61+
// Keywords for constants.
62+
[PR['PR_KEYWORD'], /^\b(?:false|null|true)\b/i],
63+
64+
// Multiline strings, single- and double-quoted.
65+
[PR['PR_STRING'], /^r?[\']{3}[\s|\S]*?[^\\][\']{3}/],
66+
[PR['PR_STRING'], /^r?[\"]{3}[\s|\S]*?[^\\][\"]{3}/],
67+
68+
// Normal and raw strings, single- and double-quoted.
69+
[PR['PR_STRING'], /^r?\'(\'|(?:[^\n\r\f])*?[^\\]\')/],
70+
[PR['PR_STRING'], /^r?\"(\"|(?:[^\n\r\f])*?[^\\]\")/],
71+
72+
// Identifiers.
73+
[PR['PR_PLAIN'], /^[a-z_$][a-z0-9_]*/i],
74+
75+
// Operators.
76+
[PR['PR_PUNCTUATION'], /^[~!%^&*+=|?:<>/-]/],
77+
78+
// Hex numbers.
79+
[PR['PR_LITERAL'], /^\b0x[0-9a-f]+/i],
80+
81+
// Decimal numbers.
82+
[PR['PR_LITERAL'], /^\b\d+(?:\.\d*)?(?:e[+-]?\d+)?/i],
83+
[PR['PR_LITERAL'], /^\b\.\d+(?:e[+-]?\d+)?/i],
84+
85+
// Punctuation.
86+
[PR['PR_PUNCTUATION'], /^[(){}\[\],.;]/]
87+
]),
88+
['dart']);

tests/prettify_test_2.html

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,51 @@ <h1>Issue 201: C type not full word</h1>
493493
>static Persistent&lt;String&gt; listeners_symbol;</code>
494494
</body>
495495

496+
<h1>Dart language handler</h1>
497+
<pre class="prettyprint lang-dart" id="dart">
498+
part of myLib;
499+
500+
part 'something.dart';
501+
502+
import 'dart:math' as test show foo, bar;
503+
504+
class Point {
505+
final num x, y;
506+
507+
Point(this.x, this.y);
508+
Point.zero() : x = 0, y = 0; // Named constructor
509+
// with an initializer list.
510+
511+
num distanceTo(Point other) {
512+
var dx = x - other.x;
513+
var dy = y - other.y;
514+
return sqrt(dx * dx + dy * dy);
515+
}
516+
}
517+
518+
// This is a single-line comment.
519+
520+
/*
521+
This is a
522+
multiline comment.
523+
*/
524+
525+
main() {
526+
Point p = new Point(7, 12);
527+
String thing = 'It\'s awesome!';
528+
String thing2 = '''
529+
This is a test! \'''
530+
This is the end of the test''';
531+
String thing3 = r"""
532+
This is a raw
533+
multiline string!""";
534+
num x = 0x123ABC;
535+
num y = 1.8e-12;
536+
bool flag = false;
537+
String raw = r"This is a raw string, where \n doesn't matter";
538+
}
539+
</pre>
540+
496541
<script type="text/javascript">
497542
/**
498543
* maps ids of rewritten code to the expected output.
@@ -896,7 +941,68 @@ <h1>Issue 201: C type not full word</h1>
896941
'`END<code class="language-lisp">`PUN;`END`PLN foo`END</code>\n'),
897942
issue185: '`STR"No tag backs."`END',
898943
issue201: '`KWDstatic`END`PLN `END`TYPPersistent`END' +
899-
'`PUN&lt;`END`TYPString`END`PUN&gt;`END`PLN listeners_symbol`END`PUN;`END'
944+
'`PUN&lt;`END`TYPString`END`PUN&gt;`END`PLN listeners_symbol`END`PUN;`END',
945+
dart: '`PLNpart of myLib`END`PUN;`END`PLN\n' +
946+
'\n' +
947+
'part `END`STR\'something.dart\'`END`PUN;`END`PLN\n' +
948+
'\n' +
949+
'`END`KWDimport`END`PLN `END`STR\'dart:math\'`END`PLN `END' +
950+
'`KWDas`END`PLN test show foo`END`PUN,`END`PLN bar`END`PUN;`END' +
951+
'`PLN\n' +
952+
'\n' +
953+
'`END`KWDclass`END`PLN `END`TYPPoint`END`PLN `END`PUN{`END`PLN\n' +
954+
' `END`KWDfinal`END`PLN num x`END`PUN,`END`PLN y`END`PUN;`END`PLN\n' +
955+
'\n' +
956+
' `END`TYPPoint`END`PUN(`END`KWDthis`END`PUN.`END`PLNx`END' +
957+
'`PUN,`END`PLN `END`KWDthis`END`PUN.`END`PLNy`END`PUN);`END`PLN\n' +
958+
' `END`TYPPoint`END`PUN.`END`PLNzero`END`PUN()`END`PLN `END' +
959+
'`PUN:`END`PLN x `END`PUN=`END`PLN `END`LIT0`END`PUN,`END' +
960+
'`PLN y `END`PUN=`END`PLN `END`LIT0`END`PUN;`END`PLN `END' +
961+
'`COM// Named constructor`END`PLN\n' +
962+
' `END' +
963+
'`COM// with an initializer list.`END`PLN\n' +
964+
'\n' +
965+
' num distanceTo`END`PUN(`END`TYPPoint`END`PLN other`END' +
966+
'`PUN)`END`PLN `END`PUN{`END`PLN\n' +
967+
' `END`KWDvar`END`PLN dx `END`PUN=`END`PLN x `END`PUN-`END' +
968+
'`PLN other`END`PUN.`END`PLNx`END`PUN;`END`PLN\n' +
969+
' `END`KWDvar`END`PLN dy `END`PUN=`END`PLN y `END`PUN-`END' +
970+
'`PLN other`END`PUN.`END`PLNy`END`PUN;`END`PLN\n' +
971+
' `END`KWDreturn`END`PLN sqrt`END`PUN(`END`PLNdx `END`PUN*`END' +
972+
'`PLN dx `END`PUN+`END`PLN dy `END`PUN*`END`PLN dy`END`PUN);`END' +
973+
'`PLN\n' +
974+
' `END`PUN}`END`PLN\n' +
975+
'`END`PUN}`END`PLN\n' +
976+
'\n' +
977+
'`END`COM// This is a single-line comment.`END`PLN\n' +
978+
'\n' +
979+
'`END`COM/*\n' +
980+
'This is a\n' +
981+
'multiline comment.\n' +
982+
'*/`END`PLN\n' +
983+
'\n' +
984+
'main`END`PUN()`END`PLN `END`PUN{`END`PLN\n' +
985+
' `END`TYPPoint`END`PLN p `END`PUN=`END`PLN `END`KWDnew`END' +
986+
'`PLN `END`TYPPoint`END`PUN(`END`LIT7`END`PUN,`END`PLN `END' +
987+
'`LIT12`END`PUN);`END`PLN\n' +
988+
' `END`TYPString`END`PLN thing `END`PUN=`END`PLN `END' +
989+
'`STR\'It\\\'s awesome!\'`END`PUN;`END`PLN\n' +
990+
' `END`TYPString`END`PLN thing2 `END`PUN=`END`PLN `END' +
991+
'`STR\'\'\'\n' +
992+
'This is a test! \\\'\'\'\n' +
993+
'This is the end of the test\'\'\'`END`PUN;`END`PLN\n' +
994+
' `END`TYPString`END`PLN thing3 `END`PUN=`END`PLN r`END' +
995+
'`STR\"\"\"\n' +
996+
'This is a raw\n' +
997+
'multiline string!\"\"\"`END`PUN;`END`PLN\n' +
998+
' num x `END`PUN=`END`PLN `END`LIT0x123ABC`END`PUN;`END`PLN\n' +
999+
' num y `END`PUN=`END`PLN `END`LIT1.8e-12`END`PUN;`END`PLN\n' +
1000+
' `END`KWDbool`END`PLN flag `END`PUN=`END`PLN `END`KWDfalse`END' +
1001+
'`PUN;`END`PLN\n' +
1002+
' `END`TYPString`END`PLN raw `END`PUN=`END`PLN r`END' +
1003+
'`STR\"This is a raw string, where \\n doesn\'t matter\"`END' +
1004+
'`PUN;`END`PLN\n' +
1005+
'`END`PUN}`END'
9001006
};
9011007
</script>
9021008

0 commit comments

Comments
 (0)