Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit f73def3

Browse files
committed
feat(parser): Do not use exceptions for control flow.
1 parent 5026444 commit f73def3

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

lib/parser.dart

+10-12
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ Map<String, String> ESCAPE = {"n":"\n", "f":"\f", "r":"\r", "t":"\t", "v":"\v",
123123

124124
ParsedFn ZERO = new ParsedFn((_, _x) => 0);
125125

126-
class BreakException {}
127-
128126
class Setter {
129127
operator[]=(name, value){}
130128
}
@@ -267,16 +265,16 @@ class Parser {
267265

268266
String peek() => index + 1 < textLength ? text[index + 1] : "EOF";
269267

270-
breakWhile() { throw new BreakException(); }
268+
// whileChars takes two functions: One called for each character
269+
// and a second, optional function call at the end of the file.
270+
// If the first function returns false, the the loop stops and endFn
271+
// is not run.
271272
whileChars(fn(), [endFn()]) {
272273
while (index < textLength) {
273274
ch = text[index];
274275
int lastIndex = index;
275-
try {
276-
fn();
277-
} on BreakException catch(e) {
278-
endFn = null;
279-
break;
276+
if (fn() == false) {
277+
return;
280278
}
281279
if (lastIndex >= index) {
282280
throw "while chars loop must advance at index $index";
@@ -315,14 +313,14 @@ class Parser {
315313
}
316314
index++;
317315
}
318-
breakWhile();
316+
return false; // BREAK
319317
});
320318
} else if (ch == quote) {
321319
index++;
322320
tokens.add(new Token(start, rawString)
323321
..withString(string)
324322
..withFn0(() => string));
325-
breakWhile();
323+
return false; // BREAK
326324
} else {
327325
string += ch;
328326
index++;
@@ -354,7 +352,7 @@ class Parser {
354352
isIn(EXP_OP, number[number.length - 1])) {
355353
throw "Lexer Error: Invalid exponent at column $index in expression [$text].";
356354
} else {
357-
breakWhile();
355+
return false; // BREAK
358356
}
359357
}
360358
index++;
@@ -377,7 +375,7 @@ class Parser {
377375
}
378376
ident += ch;
379377
} else {
380-
breakWhile();
378+
return false; // BREAK
381379
}
382380
index++;
383381
});

0 commit comments

Comments
 (0)