dart-sass/lib/src/parse/scss.dart

130 lines
3.2 KiB
Dart
Raw Normal View History

// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:charcode/charcode.dart';
import '../ast/sass.dart';
import '../util/character.dart';
2016-09-11 02:15:49 +02:00
import 'stylesheet.dart';
2016-09-11 02:15:49 +02:00
class ScssParser extends StylesheetParser {
bool get indented => false;
ScssParser(String contents, {url}) : super(contents, url: url);
2016-09-11 02:15:49 +02:00
bool atEndOfStatement() {
var next = scanner.peekChar();
2016-09-11 02:15:49 +02:00
return next == null ||
next == $semicolon ||
next == $rbrace ||
next == $lbrace;
}
2016-09-11 02:15:49 +02:00
bool lookingAtChildren() => scanner.peekChar() == $lbrace;
2016-09-11 02:15:49 +02:00
List<Statement> children(Statement child()) {
scanner.expectChar($lbrace);
2016-09-10 23:48:24 +02:00
whitespaceWithoutComments();
var children = <Statement>[];
while (true) {
switch (scanner.peekChar()) {
case $dollar:
2016-09-11 02:15:49 +02:00
children.add(variableDeclaration());
break;
2016-09-10 23:45:12 +02:00
case $slash:
switch (scanner.peekChar(1)) {
case $slash:
children.add(_silentComment());
break;
case $asterisk:
children.add(_loudComment());
break;
default:
children.add(child());
break;
}
break;
case $semicolon:
scanner.readChar();
2016-09-10 23:48:24 +02:00
whitespaceWithoutComments();
break;
case $rbrace:
scanner.expectChar($rbrace);
2016-09-10 23:48:24 +02:00
whitespaceWithoutComments();
return children;
default:
children.add(child());
break;
}
}
}
2016-09-11 02:15:49 +02:00
List<Statement> statements(Statement statement()) {
2016-09-10 23:45:12 +02:00
var statements = <Statement>[];
2016-09-10 23:48:24 +02:00
whitespaceWithoutComments();
while (!scanner.isDone) {
switch (scanner.peekChar()) {
case $dollar:
2016-09-11 02:15:49 +02:00
statements.add(variableDeclaration());
break;
2016-09-10 23:45:12 +02:00
case $slash:
switch (scanner.peekChar(1)) {
case $slash:
statements.add(_silentComment());
break;
case $asterisk:
statements.add(_loudComment());
break;
default:
statements.add(statement());
break;
}
break;
case $semicolon:
scanner.readChar();
2016-09-10 23:48:24 +02:00
whitespaceWithoutComments();
break;
default:
statements.add(statement());
break;
}
}
return statements;
}
2016-09-11 02:15:49 +02:00
Comment _silentComment() {
var start = scanner.state;
scanner.expect("//");
do {
while (!scanner.isDone && !isNewline(scanner.readChar())) {}
if (scanner.isDone) break;
whitespaceWithoutComments();
} while (scanner.scan("//"));
return new Comment(
scanner.substring(start.position), scanner.spanFrom(start),
silent: true);
}
Comment _loudComment() {
var start = scanner.state;
scanner.expect("/*");
do {
while (scanner.readChar() != $asterisk) {}
} while (scanner.readChar() != $slash);
return new Comment(
scanner.substring(start.position), scanner.spanFrom(start),
silent: false);
}
}