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

Side by Side Diff: tools/codemirror/dart/lib/codemirror.dart

Issue 9034030: Codemirror dart port StringStream datatype babysteps. (Closed) Base URL: http://dart.googlecode.com/svn/experimental/
Patch Set: '' Created 8 years, 11 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
« no previous file with comments | « tools/codemirror/dart/LICENSE ('k') | tools/codemirror/dart/test/codemirror_tests.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // This source code is licensed under the terms described in the LICENSE file.
2
3 #library('codemirror');
4
5 #import('dart:html');
6 #import('dart:core');
7
8 /** The character stream used by a [Mode]'s parser. */
9 class StringStream {
10 int _pos, start;
11 String _string;
12
13 /** Create a stream for the given [String]. */
14 StringStream(this._string) { _pos = 0; start = 0; }
15
16 /** Returns [true] only if the stream is at the end of the line. */
17 bool eol() => _pos >= _string.length;
18
19 /** Returns [true] only if the stream is at the start of the line. */
20 bool sol() => _pos == 0;
21
22 /**
23 * Returns the next character in the stream without advancing it.
24 * Returns [null] at the end of the line.
25 */
26 String peek() => _string[_pos];
27
28 /**
29 * Returns the next character in the stream and advances it. Returns [null]
30 * when no more characters are available.
31 */
32 String next() {
33 if (_pos < _string.length) return _string[_pos++];
34 return null;
35 }
36
37 /**
38 * If the next character in the stream 'matches' the given argument, it is
39 * consumed and returned. Otherwise, returns [null].
40 */
41 String eat(match) {
42 var ch = peek();
43 //TODO(pquitslund): patterns?
44 if (ch == match){
45 ++_pos;
46 return ch;
47 }
48 return null;
49 }
50
51 /**
52 * Repeatedly calls [eat] with the given argument, until it fails. Returns
53 * [true] if any characters were eaten, [false] otherwise.
54 */
55 bool eatWhile(match) {
56 var start = _pos;
57 while (eat(match) != null){}
58 return _pos > start;
59 }
60
61 /** Shortcut for [eatWhile] when matching white-space. */
62 bool eatSpace() {
63 return eatWhile(' ');
64 }
65
66 /** Moves the position to the end of the line. */
67 void skipToEnd() {
68 _pos = _string.length;
69 }
70
71 /**
72 * Skips to the next occurrence of the given character, if found on the
73 * current line (doesn't advance the stream if the character does not occur
74 * on the line). Returns [true] if the character was found.
75 */
76 bool skipTo(ch) {
77 int found = _string.indexOf(ch, _pos);
78 if (found == -1) return false;
79 _pos = found;
80 return true;
81 }
82
83 /**
84 * Backs up the stream n characters. Backing it up further than the start of
85 * the current token will cause things to break, so be careful.
86 */
87 void backUp(n) {
88 _pos -= n;
89 }
90
91 /**
92 * Returns the column (taking into account tabs) at which the current token
93 * starts. Can be used to find out whether a token starts a new line.
94 */
95 int column() => countColumn(_string, start);
96
97 /**
98 * Returns how far the current line has been indented, in spaces. Corrects
99 * for tab characters.
100 */
101 int indentation() => countColumn(_string);
102
103 /**
104 * Acts like a multi-character eat—if consume is [true] or not given, else as
105 * a look-ahead that doesn't update the stream position.
106 */
107 bool match(String pattern, [bool consume = false,
108 bool caseInsensitive = false]) {
109
110 if (caseInsensitive) {
111 String cased(str) => caseInsensitive ? str.toLowerCase() : str;
112 if (cased(_string).indexOf(cased(pattern), _pos) != _pos) return false;
113 }
114 else if (!_string.substring(_pos).contains(pattern)) return false;
115
116 if (consume) _pos += pattern.length;
117 return true;
118 }
119
120 /**
121 * Get the [String] between the start of the current token and the current
122 * stream position.
123 */
124 String current() => _string.substring(start, _pos);
125 }
126
127 //TODO(pquitslund): the codemirror default, surely gets overwritten... and needs
128 //to get moved somewhere else to boot.
129 int tabSize = 8;
130
131 /**
132 * Counts the column offset in a [String], taking tabs into account. Used
133 * mostly to find indentation.
134 */
135 int countColumn(String string, [int end = null]) {
136 if (end == null) {
137 end = string.indexOf(' ');
138 if (end == -1) end = string.length;
139 }
140 int n = 0;
141 for (int i = 0; i < end; ++i) {
142 if (string[i] == "\t") {
143 n += tabSize - (n % tabSize);
144 } else{
145 ++n;
146 }
147 }
148 return n;
149 }
150
151 //TODO(pquitslund): remove once frog doesn't require a main in libs
152 void main(){
153
154 }
OLDNEW
« no previous file with comments | « tools/codemirror/dart/LICENSE ('k') | tools/codemirror/dart/test/codemirror_tests.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698