Add parser for Expression and VariableDeclaration

This commit is contained in:
Jen Thakar 2018-04-14 11:34:36 -07:00
parent 2e42c749a0
commit 9939b70ffb
3 changed files with 37 additions and 0 deletions

View File

@ -2,6 +2,8 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import '../../logger.dart';
import '../../parse/scss.dart';
import '../../visitor/interface/expression.dart';
import 'node.dart';
@ -9,4 +11,12 @@ import 'node.dart';
abstract class Expression implements SassNode {
/// Calls the appropriate visit method on [visitor].
T accept<T>(ExpressionVisitor<T> visitor);
/// Parses an expression from [contents].
///
/// If passed, [url] is the name of the file from which [contents] comes.
///
/// Throws a [SassFormatException] if parsing fails.
factory Expression.parse(String contents, {url, Logger logger}) =>
new ScssParser("$contents", url: url, logger: logger).parseExpression();
}

View File

@ -4,6 +4,8 @@
import 'package:source_span/source_span.dart';
import '../../../logger.dart';
import '../../../parse/scss.dart';
import '../../../visitor/interface/statement.dart';
import '../expression.dart';
import '../statement.dart';
@ -35,6 +37,15 @@ class VariableDeclaration implements Statement {
: isGuarded = guarded,
isGlobal = global;
/// Parses a variable declaration from [contents].
///
/// If passed, [url] is the name of the file from which [contents] comes.
///
/// Throws a [SassFormatException] if parsing fails.
factory VariableDeclaration.parse(String contents, {url, Logger logger}) =>
new ScssParser(contents, url: url, logger: logger)
.parseVariableDeclaration();
T accept<T>(StatementVisitor<T> visitor) =>
visitor.visitVariableDeclaration(this);

View File

@ -80,6 +80,22 @@ abstract class StylesheetParser extends Parser {
});
}
Expression parseExpression() {
return wrapSpanFormatException(() {
var expression = _expression();
scanner.expectDone();
return expression;
});
}
VariableDeclaration parseVariableDeclaration() {
return wrapSpanFormatException(() {
var declaration = variableDeclaration();
scanner.expectDone();
return declaration;
});
}
/// Parses a function signature of the format allowed by Node Sass's functions
/// option and returns its name and declaration.
///