Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: lib/compiler/implementation/code_buffer.dart

Issue 10696194: Introduce CodeBuffer as StringBuffer replacement in compiler. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: . Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 class CodeBuffer {
6 StringBuffer _buffer;
floitsch 2012/07/12 16:29:09 Any reason those fields are marked as private? Unl
podivilov 2012/07/13 12:49:57 No, they aren't exposed. Made the fields public.
7 List<_SourceLocation> _sourceLocations;
8 int _lastBufferOffset = 0;
9
10 CodeBuffer()
11 : _buffer = new StringBuffer(),
12 _sourceLocations = new List<_SourceLocation>();
13
14 int get length() {
15 return _buffer.length;
16 }
17
18 bool isEmpty() {
19 return _buffer.isEmpty();
20 }
21
22 void add(Object object) {
floitsch 2012/07/12 16:29:09 Use "var" if you want to show that this function a
podivilov 2012/07/13 12:49:57 Done.
23 if (object is CodeBuffer) {
24 addBuffer(object);
25 return;
26 }
27 _buffer.add(object);
28 }
29
30 void addBuffer(CodeBuffer other) {
31 if (other._sourceLocations.length > 0) {
32 _SourceLocation firstMapping = other._sourceLocations[0];
33 int offsetDelta = _buffer.length + firstMapping.offsetDelta -
floitsch 2012/07/12 16:29:09 minor nit. we generally prefer to indent as follow
podivilov 2012/07/13 12:49:57 Done.
34 _lastBufferOffset;
35 _sourceLocations.add(new _SourceLocation(firstMapping.element,
36 firstMapping.token,
floitsch 2012/07/12 16:29:09 indentation.
podivilov 2012/07/13 12:49:57 Done.
37 offsetDelta));
38 for (int i = 1; i < other._sourceLocations.length; ++i) {
39 _sourceLocations.add(other._sourceLocations[i]);
40 }
41 _lastBufferOffset = _buffer.length + other._lastBufferOffset;
42 }
43 _buffer.add(other);
floitsch 2012/07/12 16:29:09 I prefer _buffer.add(other.toString()) but not a s
podivilov 2012/07/13 12:49:57 Done.
44 }
45
46 void clear() {
47 _buffer.clear();
48 _sourceLocations.clear();
49 _lastBufferOffset = 0;
50 }
51
52 String toString() {
53 return _buffer.toString();
54 }
55
56 void setSourceLocation(Element element, Token token) {
57 int offsetDelta = _buffer.length - _lastBufferOffset;
58 _sourceLocations.add(new _SourceLocation(element, token, offsetDelta));
59 _lastBufferOffset = _buffer.length;
60 }
61
62 void visitSourceLocations(void visitor(Element element,
floitsch 2012/07/12 16:29:09 This is not a visitor, rather call it "forEach". v
podivilov 2012/07/13 12:49:57 Done.
63 Token token,
64 int offset)) {
65 int offset = 0;
66 _sourceLocations.forEach((sourceLocation) {
67 offset += sourceLocation.offsetDelta;
68 visitor(sourceLocation.element, sourceLocation.token, offset);
69 });
70 }
71 }
72
73 class _SourceLocation {
floitsch 2012/07/12 16:29:09 ditto. why make this class private?
podivilov 2012/07/13 12:49:57 Done.
74 Element element;
75 Token token;
76 int offsetDelta;
77 _SourceLocation(this.element, this.token, this.offsetDelta);
78 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698