Chromium Code Reviews| Index: tools/codemirror/dart/lib/codemirror.dart |
| =================================================================== |
| --- tools/codemirror/dart/lib/codemirror.dart (revision 0) |
| +++ tools/codemirror/dart/lib/codemirror.dart (revision 0) |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2011, 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. |
|
mattsh
2011/12/30 13:48:16
maybe include a reference back to the original cod
|
| + |
| +#library('codemirror'); |
| + |
| +#import('dart:html'); |
| +#import('dart:core'); |
| + |
| +/* The character stream used by a [Mode]'s parser. */ |
| +class StringStream { |
| + int _pos, start; |
| + String _string; |
| + |
| + StringStream(this._string) { _pos = 0; start = 0; } |
| + |
| + bool eol() => _pos >= _string.length; |
| + |
| + bool sol() => _pos == 0; |
| + |
| + String peek() => _string[_pos]; |
| + |
| + String next() { |
| + if (_pos < _string.length) return _string[_pos++]; |
| + return null; |
| + } |
| + |
| + String eat(match) { |
| + var ch = peek(); |
| + //TODO(pquitslund): patterns? |
| + if (ch == match){ |
| + ++_pos; |
| + return ch; |
| + } |
| + return null; |
| + } |
| + |
| + bool eatWhile(match) { |
| + var start = _pos; |
| + while (eat(match) != null){} |
| + return _pos > start; |
| + } |
| + |
| + bool eatSpace() { |
| + return eatWhile(' '); |
| + } |
| + |
| + void skipToEnd() { |
| + _pos = _string.length; |
| + } |
| + |
| + bool skipTo(ch) { |
| + int found = _string.indexOf(ch, _pos); |
| + if (found == -1) return false; |
| + _pos = found; |
| + return true; |
| + } |
| + |
| + void backUp(n) { |
| + _pos -= n; |
| + } |
| + |
| + int column() => countColumn(_string, start); |
| + |
| + int indentation() => countColumn(_string); |
| + |
| + bool match(String pattern, [bool consume =false, |
|
mattsh
2011/12/30 13:48:16
space before false
|
| + bool caseInsensitive = false]) { |
| + |
| + if (caseInsensitive) { |
| + String cased(str) => caseInsensitive ? str.toLowerCase() : str; |
| + if (cased(_string).indexOf(cased(pattern), _pos) != _pos) return false; |
|
mattsh
2011/12/30 13:48:16
my personal preference is braces around all 'if' b
Bob Nystrom
2012/01/04 00:22:40
I believe we generally don't do braces for single-
|
| + } |
| + else if (!_string.substring(_pos).contains(pattern)) return false; |
| + |
| + if (consume) _pos += pattern.length; |
| + return true; |
| + } |
| + |
| + String current() => _string.substring(start, _pos); |
| +} |
| + |
| +//TODO(pquitslund): the codemirror default, surely gets overwritten... |
| +var tabSize = 8; |
|
mattsh
2011/12/30 13:48:16
int instead of var
|
| + |
| +// Counts the column offset in a string, taking tabs into account. |
| +// Used mostly to find indentation. |
| +int countColumn(String string, [int end = null]) { |
| + if (end == null) { |
| + end = string.indexOf(' '); |
| + if (end == -1) end = string.length; |
| + } |
| + int n = 0; |
| + for (int i = 0; i < end; ++i) { |
| + if (string[i] == "\t") { |
| + n += tabSize - (n % tabSize); |
| + } else{ |
| + ++n; |
| + } |
| + } |
| + return n; |
| +} |
| + |
| +//TODO(pquitslund): remove once frog doesn't require a main in libs |
| +void main(){ |
| + |
| +} |