Use built-in string scanner methods.

This commit is contained in:
Natalie Weizenbaum 2016-06-09 15:47:59 -07:00
parent 4b0f18def7
commit 162ea29c14
2 changed files with 53 additions and 58 deletions

View File

@ -70,19 +70,19 @@ class Parser {
} }
VariableDeclaration _variableDeclaration() { VariableDeclaration _variableDeclaration() {
if (!_scanChar($dollar)) return null; if (!_scanner.scanChar($dollar)) return null;
var start = _scanner.state; var start = _scanner.state;
var name = _identifier(); var name = _identifier();
_ignoreComments(); _ignoreComments();
_expectChar($colon); _scanner.expectChar($colon);
_ignoreComments(); _ignoreComments();
var expression = _expression(); var expression = _expression();
var guarded = false; var guarded = false;
var global = false; var global = false;
while (_scanChar($exclamation)) { while (_scanner.scanChar($exclamation)) {
var flagStart = _scanner.position - 1; var flagStart = _scanner.position - 1;
var flag = _identifier(); var flag = _identifier();
if (flag == 'default') { if (flag == 'default') {
@ -113,7 +113,7 @@ class Parser {
} }
List<Statement> _styleRuleChildren() { List<Statement> _styleRuleChildren() {
_expectChar($lbrace); _scanner.expectChar($lbrace);
var children = <Statement>[]; var children = <Statement>[];
loop: while (true) { loop: while (true) {
children.addAll(_comments()); children.addAll(_comments());
@ -140,7 +140,7 @@ class Parser {
} }
children.addAll(_comments()); children.addAll(_comments());
_expectChar($rbrace); _scanner.expectChar($rbrace);
return children; return children;
} }
@ -223,7 +223,7 @@ class Parser {
var midBuffer = new StringBuffer(); var midBuffer = new StringBuffer();
midBuffer.write(_commentText()); midBuffer.write(_commentText());
if (!_scanChar($colon)) return nameBuffer; if (!_scanner.scanChar($colon)) return nameBuffer;
midBuffer.writeCharCode($colon); midBuffer.writeCharCode($colon);
// Parse custom properties as declarations no matter what. // Parse custom properties as declarations no matter what.
@ -232,7 +232,7 @@ class Parser {
return _customPropertyDeclaration(name); return _customPropertyDeclaration(name);
} }
if (_scanChar($colon)) { if (_scanner.scanChar($colon)) {
return nameBuffer..write(midBuffer)..writeCharCode($colon); return nameBuffer..write(midBuffer)..writeCharCode($colon);
} }
@ -248,11 +248,11 @@ class Parser {
if (next == $lbrace) { if (next == $lbrace) {
// Properties that are ambiguous with selectors can't have additional // Properties that are ambiguous with selectors can't have additional
// properties nested beneath them, so we force an error. // properties nested beneath them, so we force an error.
if (couldBeSelector) _expectChar($semicolon); if (couldBeSelector) _scanner.expectChar($semicolon);
} else if (next != $semicolon && next != $rbrace) { } else if (next != $semicolon && next != $rbrace) {
// Force an exception if there isn't a valid end-of-property character // Force an exception if there isn't a valid end-of-property character
// but don't consume that character. // but don't consume that character.
_expectChar($semicolon); _scanner.expectChar($semicolon);
} }
} on FormatException catch (_) { } on FormatException catch (_) {
if (!couldBeSelector) rethrow; if (!couldBeSelector) rethrow;
@ -308,7 +308,7 @@ class Parser {
new ListExpression(spaceExpressions, ListSeparator.space)); new ListExpression(spaceExpressions, ListSeparator.space));
} }
if (!_scanChar($comma)) break; if (!_scanner.scanChar($comma)) break;
hadComma = true; hadComma = true;
_ignoreComments(); _ignoreComments();
} }
@ -366,17 +366,17 @@ class Parser {
Expression _parentheses() { Expression _parentheses() {
var start = _scanner.state; var start = _scanner.state;
_expectChar($lparen); _scanner.expectChar($lparen);
_ignoreComments(); _ignoreComments();
if (!isExpressionStart(_scanner.peekChar())) { if (!isExpressionStart(_scanner.peekChar())) {
_expectChar($rparen); _scanner.expectChar($rparen);
return new ListExpression([], ListSeparator.undecided, return new ListExpression([], ListSeparator.undecided,
span: _scanner.spanFrom(start)); span: _scanner.spanFrom(start));
} }
// TODO: support maps // TODO: support maps
var result = _expression(); var result = _expression();
_expectChar($rparen); _scanner.expectChar($rparen);
return result; return result;
} }
@ -444,7 +444,7 @@ class Parser {
VariableExpression _variable() { VariableExpression _variable() {
var start = _scanner.state; var start = _scanner.state;
_expectChar($dollar); _scanner.expectChar($dollar);
var name = _identifier(); var name = _identifier();
return new VariableExpression(name, span: _scanner.spanFrom(start)); return new VariableExpression(name, span: _scanner.spanFrom(start));
} }
@ -488,7 +488,7 @@ class Parser {
Expression _hexColorOrID() { Expression _hexColorOrID() {
var start = _scanner.state; var start = _scanner.state;
_expectChar($hash); _scanner.expectChar($hash);
var first = _scanner.peekChar(); var first = _scanner.peekChar();
if (first != null && isDigit(first)) { if (first != null && isDigit(first)) {
@ -622,7 +622,7 @@ class Parser {
var start = _scanner.state; var start = _scanner.state;
var buffer = new InterpolationBuffer(); var buffer = new InterpolationBuffer();
while (_scanChar($dash)) { while (_scanner.scanChar($dash)) {
buffer.writeCharCode($dash); buffer.writeCharCode($dash);
} }
@ -659,7 +659,7 @@ class Parser {
Expression _singleInterpolation() { Expression _singleInterpolation() {
_scanner.expect('#{'); _scanner.expect('#{');
var expression = _expression(); var expression = _expression();
_expectChar($rbrace); _scanner.expectChar($rbrace);
return expression; return expression;
} }
@ -682,7 +682,7 @@ class Parser {
previousLine = _scanner.line; previousLine = _scanner.line;
} }
components.add(_complexSelector()); components.add(_complexSelector());
} while (_scanChar($comma)); } while (_scanner.scanChar($comma));
return new SelectorList(components, lineBreaks: lineBreaks); return new SelectorList(components, lineBreaks: lineBreaks);
} }
@ -753,12 +753,12 @@ class Parser {
} }
AttributeSelector _attributeSelector() { AttributeSelector _attributeSelector() {
_expectChar($lbracket); _scanner.expectChar($lbracket);
_ignoreComments(); _ignoreComments();
var name = _attributeName(); var name = _attributeName();
_ignoreComments(); _ignoreComments();
if (_scanChar($rbracket)) { if (_scanner.scanChar($rbracket)) {
_scanner.readChar(); _scanner.readChar();
return new AttributeSelector(name); return new AttributeSelector(name);
} }
@ -772,13 +772,13 @@ class Parser {
: _identifier(); : _identifier();
_ignoreComments(); _ignoreComments();
_expectChar($rbracket); _scanner.expectChar($rbracket);
return new AttributeSelector.withOperator(name, operator, value); return new AttributeSelector.withOperator(name, operator, value);
} }
NamespacedIdentifier _attributeName() { NamespacedIdentifier _attributeName() {
if (_scanChar($asterisk)) { if (_scanner.scanChar($asterisk)) {
_expectChar($pipe); _scanner.expectChar($pipe);
return new NamespacedIdentifier(_identifier(), namespace: "*"); return new NamespacedIdentifier(_identifier(), namespace: "*");
} }
@ -797,23 +797,23 @@ class Parser {
case $equal: return AttributeOperator.equal; case $equal: return AttributeOperator.equal;
case $tilde: case $tilde:
_expectChar($equal); _scanner.expectChar($equal);
return AttributeOperator.include; return AttributeOperator.include;
case $pipe: case $pipe:
_expectChar($equal); _scanner.expectChar($equal);
return AttributeOperator.dash; return AttributeOperator.dash;
case $caret: case $caret:
_expectChar($equal); _scanner.expectChar($equal);
return AttributeOperator.prefix; return AttributeOperator.prefix;
case $dollar: case $dollar:
_expectChar($equal); _scanner.expectChar($equal);
return AttributeOperator.suffix; return AttributeOperator.suffix;
case $asterisk: case $asterisk:
_expectChar($equal); _scanner.expectChar($equal);
return AttributeOperator.substring; return AttributeOperator.substring;
default: default:
@ -823,29 +823,30 @@ class Parser {
} }
ClassSelector _classSelector() { ClassSelector _classSelector() {
_expectChar($dot); _scanner.expectChar($dot);
var name = _identifier(); var name = _identifier();
return new ClassSelector(name); return new ClassSelector(name);
} }
IDSelector _idSelector() { IDSelector _idSelector() {
_expectChar($hash); _scanner.expectChar($hash);
var name = _identifier(); var name = _identifier();
return new IDSelector(name); return new IDSelector(name);
} }
PlaceholderSelector _placeholderSelector() { PlaceholderSelector _placeholderSelector() {
_expectChar($percent); _scanner.expectChar($percent);
var name = _identifier(); var name = _identifier();
return new PlaceholderSelector(name); return new PlaceholderSelector(name);
} }
PseudoSelector _pseudoSelector() { PseudoSelector _pseudoSelector() {
_expectChar($colon); _scanner.expectChar($colon);
var type = _scanChar($colon) ? PseudoType.element : PseudoType.klass; var type =
_scanner.scanChar($colon) ? PseudoType.element : PseudoType.klass;
var name = _identifier(); var name = _identifier();
if (!_scanChar($lparen)) { if (!_scanner.scanChar($lparen)) {
return new PseudoSelector(name, type); return new PseudoSelector(name, type);
} }
_ignoreComments(); _ignoreComments();
@ -869,7 +870,7 @@ class Parser {
} else { } else {
argument = _rawText(_pseudoArgument); argument = _rawText(_pseudoArgument);
} }
_expectChar($rparen); _scanner.expectChar($rparen);
return new PseudoSelector(name, type, return new PseudoSelector(name, type,
argument: argument, selector: selector); argument: argument, selector: selector);
@ -924,19 +925,24 @@ class Parser {
SimpleSelector _typeOrUniversalSelector() { SimpleSelector _typeOrUniversalSelector() {
var first = _scanner.peekChar(); var first = _scanner.peekChar();
if (first == $asterisk) { if (first == $asterisk) {
if (!_scanChar($pipe)) return new UniversalSelector(); if (!_scanner.scanChar($pipe)) return new UniversalSelector();
if (_scanChar($asterisk)) return new UniversalSelector(namespace: "*"); if (_scanner.scanChar($asterisk)) {
return new UniversalSelector(namespace: "*");
} else {
return new TypeSelector( return new TypeSelector(
new NamespacedIdentifier(_identifier(), namespace: "*")); new NamespacedIdentifier(_identifier(), namespace: "*"));
}
} else if (first == $pipe) { } else if (first == $pipe) {
if (_scanChar($asterisk)) return new UniversalSelector( namespace: ""); if (_scanner.scanChar($asterisk)) {
return new UniversalSelector( namespace: "");
} else {
return new TypeSelector( return new TypeSelector(
new NamespacedIdentifier(_identifier(), namespace: "")); new NamespacedIdentifier(_identifier(), namespace: ""));
} }
}
var nameOrNamespace = _identifier(); var nameOrNamespace = _identifier();
if (!_scanChar($pipe)) { if (!_scanner.scanChar($pipe)) {
return new TypeSelector(new NamespacedIdentifier(nameOrNamespace)); return new TypeSelector(new NamespacedIdentifier(nameOrNamespace));
} }
@ -1006,7 +1012,7 @@ class Parser {
String _identifier() { String _identifier() {
var text = new StringBuffer(); var text = new StringBuffer();
while (_scanChar($dash)) { while (_scanner.scanChar($dash)) {
text.writeCharCode($dash); text.writeCharCode($dash);
} }
@ -1052,7 +1058,7 @@ class Parser {
int _escape() { int _escape() {
// See https://drafts.csswg.org/css-syntax-3/#consume-escaped-code-point. // See https://drafts.csswg.org/css-syntax-3/#consume-escaped-code-point.
_expectChar($backslash); _scanner.expectChar($backslash);
var first = _scanner.peekChar(); var first = _scanner.peekChar();
if (first == null) { if (first == null) {
return 0xFFFD; return 0xFFFD;
@ -1085,17 +1091,6 @@ class Parser {
return asHex(_scanner.readChar()); return asHex(_scanner.readChar());
} }
bool _scanChar(int character) {
if (_scanner.peekChar() != character) return false;
_scanner.readChar();
return true;
}
void _expectChar(int character) {
if (_scanChar(character)) return;
_scanner.error('Expected "${new String.fromCharCode(character)}".');
}
bool _scanCharCaseInsensitive(int character) { bool _scanCharCaseInsensitive(int character) {
assert(character >= $a && character <= $z); assert(character >= $a && character <= $z);
var actual = _scanner.readChar(); var actual = _scanner.readChar();

View File

@ -11,7 +11,7 @@ dependencies:
charcode: "^1.1.0" charcode: "^1.1.0"
path: "^1.0.0" path: "^1.0.0"
source_span: "^1.0.0" source_span: "^1.0.0"
string_scanner: "^0.1.4" string_scanner: ">=0.1.5 <2.0.0"
dev_dependencies: dev_dependencies:
test: "^0.12.0" test: "^0.12.0"