| 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,154 @@
|
| +// This source code is licensed under the terms described in the LICENSE file.
|
| +
|
| +#library('codemirror');
|
| +
|
| +#import('dart:html');
|
| +#import('dart:core');
|
| +
|
| +/** The character stream used by a [Mode]'s parser. */
|
| +class StringStream {
|
| + int _pos, start;
|
| + String _string;
|
| +
|
| + /** Create a stream for the given [String]. */
|
| + StringStream(this._string) { _pos = 0; start = 0; }
|
| +
|
| + /** Returns [true] only if the stream is at the end of the line. */
|
| + bool eol() => _pos >= _string.length;
|
| +
|
| + /** Returns [true] only if the stream is at the start of the line. */
|
| + bool sol() => _pos == 0;
|
| +
|
| + /**
|
| + * Returns the next character in the stream without advancing it.
|
| + * Returns [null] at the end of the line.
|
| + */
|
| + String peek() => _string[_pos];
|
| +
|
| + /**
|
| + * Returns the next character in the stream and advances it. Returns [null]
|
| + * when no more characters are available.
|
| + */
|
| + String next() {
|
| + if (_pos < _string.length) return _string[_pos++];
|
| + return null;
|
| + }
|
| +
|
| + /**
|
| + * If the next character in the stream 'matches' the given argument, it is
|
| + * consumed and returned. Otherwise, returns [null].
|
| + */
|
| + String eat(match) {
|
| + var ch = peek();
|
| + //TODO(pquitslund): patterns?
|
| + if (ch == match){
|
| + ++_pos;
|
| + return ch;
|
| + }
|
| + return null;
|
| + }
|
| +
|
| + /**
|
| + * Repeatedly calls [eat] with the given argument, until it fails. Returns
|
| + * [true] if any characters were eaten, [false] otherwise.
|
| + */
|
| + bool eatWhile(match) {
|
| + var start = _pos;
|
| + while (eat(match) != null){}
|
| + return _pos > start;
|
| + }
|
| +
|
| + /** Shortcut for [eatWhile] when matching white-space. */
|
| + bool eatSpace() {
|
| + return eatWhile(' ');
|
| + }
|
| +
|
| + /** Moves the position to the end of the line. */
|
| + void skipToEnd() {
|
| + _pos = _string.length;
|
| + }
|
| +
|
| + /**
|
| + * Skips to the next occurrence of the given character, if found on the
|
| + * current line (doesn't advance the stream if the character does not occur
|
| + * on the line). Returns [true] if the character was found.
|
| + */
|
| + bool skipTo(ch) {
|
| + int found = _string.indexOf(ch, _pos);
|
| + if (found == -1) return false;
|
| + _pos = found;
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| + * Backs up the stream n characters. Backing it up further than the start of
|
| + * the current token will cause things to break, so be careful.
|
| + */
|
| + void backUp(n) {
|
| + _pos -= n;
|
| + }
|
| +
|
| + /**
|
| + * Returns the column (taking into account tabs) at which the current token
|
| + * starts. Can be used to find out whether a token starts a new line.
|
| + */
|
| + int column() => countColumn(_string, start);
|
| +
|
| + /**
|
| + * Returns how far the current line has been indented, in spaces. Corrects
|
| + * for tab characters.
|
| + */
|
| + int indentation() => countColumn(_string);
|
| +
|
| + /**
|
| + * Acts like a multi-character eat—if consume is [true] or not given, else as
|
| + * a look-ahead that doesn't update the stream position.
|
| + */
|
| + bool match(String pattern, [bool consume = false,
|
| + bool caseInsensitive = false]) {
|
| +
|
| + if (caseInsensitive) {
|
| + String cased(str) => caseInsensitive ? str.toLowerCase() : str;
|
| + if (cased(_string).indexOf(cased(pattern), _pos) != _pos) return false;
|
| + }
|
| + else if (!_string.substring(_pos).contains(pattern)) return false;
|
| +
|
| + if (consume) _pos += pattern.length;
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| + * Get the [String] between the start of the current token and the current
|
| + * stream position.
|
| + */
|
| + String current() => _string.substring(start, _pos);
|
| +}
|
| +
|
| +//TODO(pquitslund): the codemirror default, surely gets overwritten... and needs
|
| +//to get moved somewhere else to boot.
|
| +int tabSize = 8;
|
| +
|
| +/**
|
| + * 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(){
|
| +
|
| +}
|
|
|