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

feat(parser): Do not use exceptions for control flow. #27

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ Map<String, String> ESCAPE = {"n":"\n", "f":"\f", "r":"\r", "t":"\t", "v":"\v",

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

class BreakException {}

class Setter {
operator[]=(name, value){}
}
Expand Down Expand Up @@ -267,16 +265,16 @@ class Parser {

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

breakWhile() { throw new BreakException(); }
// whileChars takes two functions: One called for each character
// and a second, optional function call at the end of the file.
// If the first function returns false, the the loop stops and endFn
// is not run.
whileChars(fn(), [endFn()]) {
while (index < textLength) {
ch = text[index];
int lastIndex = index;
try {
fn();
} on BreakException catch(e) {
endFn = null;
break;
if (fn() == false) {
return;
}
if (lastIndex >= index) {
throw "while chars loop must advance at index $index";
Expand Down Expand Up @@ -315,14 +313,14 @@ class Parser {
}
index++;
}
breakWhile();
return false; // BREAK
});
} else if (ch == quote) {
index++;
tokens.add(new Token(start, rawString)
..withString(string)
..withFn0(() => string));
breakWhile();
return false; // BREAK
} else {
string += ch;
index++;
Expand Down Expand Up @@ -354,7 +352,7 @@ class Parser {
isIn(EXP_OP, number[number.length - 1])) {
throw "Lexer Error: Invalid exponent at column $index in expression [$text].";
} else {
breakWhile();
return false; // BREAK
}
}
index++;
Expand All @@ -377,7 +375,7 @@ class Parser {
}
ident += ch;
} else {
breakWhile();
return false; // BREAK
}
index++;
});
Expand Down