Repository navigation
Expand file tree
/
Copy pathReadStream.as
More file actions
130 lines (110 loc) · 3.26 KB
/
Copy pathReadStream.as
File metadata and controls
130 lines (110 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// ReadStream.as
// John Maloney, October 2009
//
// A simple character stream with two character look-ahead and tokenization.
package util {
public class ReadStream {
private var src:String, i:int;
public function ReadStream(s:String) {
src = s;
i = 0;
}
public function atEnd():Boolean {
return i >= src.length;
}
public function next():String {
if (i >= src.length) return '';
return src.charAt(i++);
}
public function peek():String {
return (i < src.length) ? src.charAt(i) : '';
}
public function peek2():String {
return ((i + 1) < src.length) ? src.charAt(i + 1) : '';
}
public function peekString(n:int):String { return src.slice(i, i + n) }
public function nextString(n:int):String {
i += n;
return src.slice(i - n, i);
}
public function pos():int { return i }
public function setPos(newPos:int):void { i = newPos }
public function skip(count:int):void { i += count }
public function skipWhiteSpace():void {
while ((i < src.length) && (src.charCodeAt(i) <= 32)) i++;
}
public function upToEnd():String {
var result:String = (i < src.length) ? src.slice(i, src.length) : '';
i = src.length;
return result;
}
public static function tokenize(s:String):Array {
var stream:ReadStream = new ReadStream(s);
var result:Array = [];
while (!stream.atEnd()) {
var token:String = stream.nextToken();
if (token.length > 0) result.push(token);
}
return result;
}
public function nextToken():String {
skipWhiteSpace();
if (atEnd()) return '';
var token:String = '';
var isArg:Boolean;
var start:int = i;
while (i < src.length) {
if (src.charCodeAt(i) <= 32) break;
var ch:String = src.charAt(i);
if (ch == '\\') {
token += ch + src.charAt(i + 1);
i += 2;
continue;
}
if (ch == '%') {
if (i > start) break; // percent sign starts new token
isArg = true;
}
// certain punctuation marks following an argument start a new token
// example: 'touching %m?' (question mark after arg starts a new token) vs. 'loud?' (doesn't)
if (isArg && (ch == '?' || ch == '-')) break;
token += ch;
i++;
}
return token;
}
public static function escape(s:String):String {
return s.replace(/[\\%@]/g, '\\$&');
}
public static function unescape(s:String):String {
var result:String = '';
for (var i:int = 0; i < s.length; i++) {
var ch:String = s.charAt(i);
if (ch == '\\') {
result += s.charAt(i + 1);
i++;
} else {
result += ch;
}
}
return result;
}
}}