Refactor comment parsing.

This commit is contained in:
Natalie Weizenbaum 2016-05-24 07:22:35 -07:00
parent f8ef789b0d
commit cee3c26963

View File

@ -100,19 +100,10 @@ class Parser {
while (true) { while (true) {
_whitespace(); _whitespace();
var silent = _trySilentComment(); var comment = _tryComment();
if (silent != null) { if (comment == null) return nodes;
nodes.add(silent);
continue;
}
var loud = _tryLoudComment(); nodes.add(comment);
if (loud != null) {
nodes.add(loud);
continue;
}
return nodes;
} }
} }
@ -262,33 +253,39 @@ class Parser {
void _ignoreComments() { void _ignoreComments() {
do { do {
_whitespace(); _whitespace();
} while (_trySilentComment() != null || _tryLoudComment() != null); } while (_tryComment() != null);
} }
CommentNode _trySilentComment() { CommentNode _tryComment() {
if (_scanner.peekChar() != $slash) return null;
switch (_scanner.peekChar(1)) {
case $slash: return _silentComment();
case $asterisk: return _loudComment();
default: return null;
}
}
CommentNode _silentComment() {
var start = _scanner.state; var start = _scanner.state;
while (_scanner.scan("//")) { _scanner.expect("//");
do {
while (!_scanner.isDone && !_isNewline(_scanner.readChar())) {} while (!_scanner.isDone && !_isNewline(_scanner.readChar())) {}
if (_scanner.isDone) break; if (_scanner.isDone) break;
_whitespace(); _whitespace();
} } while (_scanner.scan("//"));
if (_scanner.position == start.position) return null;
return new CommentNode(_scanner.substring(start.position), return new CommentNode(_scanner.substring(start.position),
silent: true, silent: true,
span: _scanner.spanFrom(start)); span: _scanner.spanFrom(start));
} }
CommentNode _tryLoudComment() { CommentNode _loudComment() {
var start = _scanner.state; var start = _scanner.state;
while (_scanner.scan("/*")) { _scanner.expect("/*");
do { do {
while (_scanner.readChar() != $asterisk) {} while (_scanner.readChar() != $asterisk) {}
} while (_scanner.readChar() != $slash); } while (_scanner.readChar() != $slash);
}
if (_scanner.position == start.position) return null;
return new CommentNode(_scanner.substring(start.position), return new CommentNode(_scanner.substring(start.position),
silent: false, silent: false,