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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: lib/compiler/implementation/code_buffer.dart
diff --git a/lib/compiler/implementation/code_buffer.dart b/lib/compiler/implementation/code_buffer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..476c8b7fe2ac1840f5053b667c5623019ddaae3e
--- /dev/null
+++ b/lib/compiler/implementation/code_buffer.dart
@@ -0,0 +1,78 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+class CodeBuffer {
+ 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.
+ List<_SourceLocation> _sourceLocations;
+ int _lastBufferOffset = 0;
+
+ CodeBuffer()
+ : _buffer = new StringBuffer(),
+ _sourceLocations = new List<_SourceLocation>();
+
+ int get length() {
+ return _buffer.length;
+ }
+
+ bool isEmpty() {
+ return _buffer.isEmpty();
+ }
+
+ 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.
+ if (object is CodeBuffer) {
+ addBuffer(object);
+ return;
+ }
+ _buffer.add(object);
+ }
+
+ void addBuffer(CodeBuffer other) {
+ if (other._sourceLocations.length > 0) {
+ _SourceLocation firstMapping = other._sourceLocations[0];
+ 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.
+ _lastBufferOffset;
+ _sourceLocations.add(new _SourceLocation(firstMapping.element,
+ firstMapping.token,
floitsch 2012/07/12 16:29:09 indentation.
podivilov 2012/07/13 12:49:57 Done.
+ offsetDelta));
+ for (int i = 1; i < other._sourceLocations.length; ++i) {
+ _sourceLocations.add(other._sourceLocations[i]);
+ }
+ _lastBufferOffset = _buffer.length + other._lastBufferOffset;
+ }
+ _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.
+ }
+
+ void clear() {
+ _buffer.clear();
+ _sourceLocations.clear();
+ _lastBufferOffset = 0;
+ }
+
+ String toString() {
+ return _buffer.toString();
+ }
+
+ void setSourceLocation(Element element, Token token) {
+ int offsetDelta = _buffer.length - _lastBufferOffset;
+ _sourceLocations.add(new _SourceLocation(element, token, offsetDelta));
+ _lastBufferOffset = _buffer.length;
+ }
+
+ 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.
+ Token token,
+ int offset)) {
+ int offset = 0;
+ _sourceLocations.forEach((sourceLocation) {
+ offset += sourceLocation.offsetDelta;
+ visitor(sourceLocation.element, sourceLocation.token, offset);
+ });
+ }
+}
+
+class _SourceLocation {
floitsch 2012/07/12 16:29:09 ditto. why make this class private?
podivilov 2012/07/13 12:49:57 Done.
+ Element element;
+ Token token;
+ int offsetDelta;
+ _SourceLocation(this.element, this.token, this.offsetDelta);
+}

Powered by Google App Engine
This is Rietveld 408576698