OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 const int _LF = 0x0A; | |
6 const int _CR = 0x0D; | |
7 const int _LBRACE = 0x7B; | |
8 | |
9 class Annotation { | |
10 /// 1-based line number of the annotation. | |
11 final int lineNo; | |
12 | |
13 /// 1-based column number of the annotation. | |
14 final int columnNo; | |
15 | |
16 /// 0-based character offset with | |
Siggi Cherem (dart-lang)
2017/01/24 21:05:59
with => of the annotation?
Johnni Winther
2017/01/25 13:28:13
Done.
| |
17 final int offset; | |
18 | |
19 /// The text in the annotation. | |
20 final String text; | |
21 | |
22 Annotation(this.lineNo, this.columnNo, this.offset, this.text); | |
23 } | |
24 | |
25 class AnnotatedCode { | |
Siggi Cherem (dart-lang)
2017/01/24 21:05:59
add a short dart doc with an example of the syntax
Johnni Winther
2017/01/25 13:28:13
Done.
| |
26 final String sourceCode; | |
Siggi Cherem (dart-lang)
2017/01/24 21:05:59
+ dartdoc or rename to unannotatedCode?
Johnni Winther
2017/01/25 13:28:13
Done.
| |
27 final List<Annotation> annotations; | |
28 | |
29 AnnotatedCode(this.sourceCode, this.annotations); | |
30 } | |
31 | |
32 AnnotatedCode processAnnotatedCode(String code) { | |
Siggi Cherem (dart-lang)
2017/01/24 21:05:59
optional nit: rename or move as a factory construc
Johnni Winther
2017/01/25 13:28:13
Done.
| |
33 StringBuffer codeBuffer = new StringBuffer(); | |
34 List<Annotation> annotations = <Annotation>[]; | |
35 int index = 0; | |
36 int offset = 0; | |
37 int lineNo = 1; | |
38 int columnNo = 1; | |
39 while (index < code.length) { | |
40 int charCode = code.codeUnitAt(index); | |
41 switch (charCode) { | |
42 case _LF: | |
43 codeBuffer.write('\n'); | |
44 offset++; | |
45 lineNo++; | |
46 columnNo = 1; | |
47 break; | |
48 case _CR: | |
49 if (index + 1 < code.length && code.codeUnitAt(index + 1) == _LF) { | |
50 index++; | |
51 } | |
52 codeBuffer.write('\n'); | |
53 offset++; | |
54 lineNo++; | |
55 columnNo = 1; | |
56 break; | |
57 case 0x40: | |
Siggi Cherem (dart-lang)
2017/01/24 21:05:59
define 0x40 too?
| |
58 if (index + 1 < code.length && code.codeUnitAt(index + 1) == _LBRACE) { | |
59 int endIndex = code.indexOf('}', index); | |
60 String text = code.substring(index + 2, endIndex); | |
61 annotations.add(new Annotation(lineNo, columnNo, offset, text)); | |
62 index = endIndex; | |
63 } else { | |
64 codeBuffer.writeCharCode(charCode); | |
65 offset++; | |
66 columnNo++; | |
67 } | |
68 break; | |
69 default: | |
70 codeBuffer.writeCharCode(charCode); | |
71 offset++; | |
72 columnNo++; | |
73 } | |
74 index++; | |
75 } | |
76 return new AnnotatedCode(codeBuffer.toString(), annotations); | |
77 } | |
OLD | NEW |