Parse functions -> constructors

This commit is contained in:
Natalie Weizenbaum 2016-09-24 03:39:06 -07:00 committed by Natalie Weizenbaum
parent 4289011a84
commit f460ebb2a9
12 changed files with 54 additions and 52 deletions

View File

@ -8,8 +8,8 @@ import 'package:args/args.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:path/path.dart' as p;
import 'package:sass/src/ast/sass.dart';
import 'package:sass/src/exception.dart';
import 'package:sass/src/parse.dart';
import 'package:sass/src/visitor/perform.dart';
import 'package:sass/src/visitor/serialize.dart';
@ -35,8 +35,11 @@ void main(List<String> args) {
try {
var file = options.rest.first;
var parse = p.extension(file) == '.sass' ? parseSass : parseScss;
var sassTree = parse(new File(file).readAsStringSync(), url: p.toUri(file));
var contents = new File(file).readAsStringSync();
var url = p.toUri(file);
var sassTree = p.extension(file) == '.sass'
? new Stylesheet.parseSass(contents, url: url)
: new Stylesheet.parseScss(contents, url: url);
var cssTree = new PerformVisitor().visitStylesheet(sassTree);
var css = toCss(cssTree);
if (css.isNotEmpty) print(css);

View File

@ -4,6 +4,7 @@
import 'package:source_span/source_span.dart';
import '../../parse/scss.dart';
import 'argument.dart';
import 'node.dart';
@ -22,6 +23,9 @@ class ArgumentDeclaration implements SassNode {
: arguments = const [],
restArgument = null;
factory ArgumentDeclaration.parse(String contents, {url}) =>
new ScssParser("($contents)", url: url).parseArgumentDeclaration();
String toString() =>
arguments.join(', ') + (restArgument == null ? '' : ", $restArgument...");
}

View File

@ -4,7 +4,7 @@
import 'package:source_span/source_span.dart';
import '../../../parse.dart';
import '../../../ast/sass.dart';
import '../../../visitor/interface/expression.dart';
import '../expression.dart';
import '../argument_invocation.dart';
@ -12,7 +12,7 @@ import '../callable_invocation.dart';
class IfExpression implements Expression, CallableInvocation {
static final declaration =
parseArgumentDeclaration(r"($condition, $if-true, $if-false)");
new ArgumentDeclaration.parse(r"$condition, $if-true, $if-false");
final ArgumentInvocation arguments;

View File

@ -5,6 +5,7 @@
import 'package:collection/collection.dart';
import 'package:source_span/source_span.dart';
import '../../../parse/at_root_query.dart';
import '../../../visitor/interface/statement.dart';
import '../../css.dart';
import '../interpolation.dart';
@ -56,6 +57,9 @@ class AtRootQuery {
_all = false,
_rule = true;
factory AtRootQuery.parse(String contents, {url}) =>
new AtRootQueryParser(contents, url: url).parse();
bool excludes(CssParentNode node) {
if (_all) return !include;
if (_rule && node is CssStyleRule) return !include;

View File

@ -5,6 +5,8 @@
import 'package:source_span/source_span.dart';
import '../../../visitor/interface/statement.dart';
import '../../../parse/sass.dart';
import '../../../parse/scss.dart';
import '../statement.dart';
class Stylesheet implements Statement {
@ -15,6 +17,12 @@ class Stylesheet implements Statement {
Stylesheet(Iterable<Statement> children, this.span)
: children = new List.unmodifiable(children);
factory Stylesheet.parseSass(String contents, {url}) =>
new SassParser(contents, url: url).parse();
factory Stylesheet.parseScss(String contents, {url}) =>
new ScssParser(contents, url: url).parse();
/*=T*/ accept/*<T>*/(StatementVisitor/*<T>*/ visitor) =>
visitor.visitStylesheet(this);

View File

@ -3,6 +3,7 @@
// https://opensource.org/licenses/MIT.
import '../../extend/functions.dart';
import '../../parse/selector.dart';
import '../../utils.dart';
import '../../visitor/interface/selector.dart';
import '../selector.dart';
@ -27,6 +28,11 @@ class CompoundSelector extends Selector implements ComplexSelectorComponent {
CompoundSelector(Iterable<SimpleSelector> components)
: components = new List.unmodifiable(components);
factory CompoundSelector.parse(String contents,
{url, bool allowParent: true}) =>
new SelectorParser(contents, url: url, allowParent: allowParent)
.parseCompoundSelector();
/*=T*/ accept/*<T>*/(SelectorVisitor/*<T>*/ visitor) =>
visitor.visitCompoundSelector(this);

View File

@ -3,6 +3,7 @@
// https://opensource.org/licenses/MIT.
import '../../extend/functions.dart';
import '../../parse/selector.dart';
import '../../utils.dart';
import '../../exception.dart';
import '../../value.dart';
@ -41,6 +42,9 @@ class SelectorList extends Selector {
lineBreaks =
lineBreaks == null ? const [] : new List.unmodifiable(lineBreaks);
factory SelectorList.parse(String contents, {url, bool allowParent: true}) =>
new SelectorParser(contents, url: url, allowParent: allowParent).parse();
/*=T*/ accept/*<T>*/(SelectorVisitor/*<T>*/ visitor) =>
visitor.visitSelectorList(this);

View File

@ -3,6 +3,7 @@
// https://opensource.org/licenses/MIT.
import '../../exception.dart';
import '../../parse/selector.dart';
import '../selector.dart';
abstract class SimpleSelector extends Selector {
@ -13,6 +14,13 @@ abstract class SimpleSelector extends Selector {
int get minSpecificity => 1000;
int get maxSpecificity => minSpecificity;
SimpleSelector();
factory SimpleSelector.parse(String contents,
{url, bool allowParent: true}) =>
new SelectorParser(contents, url: url, allowParent: allowParent)
.parseSimpleSelector();
SimpleSelector addSuffix(String suffix) =>
throw new InternalException('Invalid parent selector "$this"');

View File

@ -4,7 +4,6 @@
import '../ast/sass.dart';
import '../callable.dart';
import '../parse.dart';
import '../value.dart';
typedef Value _Callback(List<Value> arguments);
@ -21,6 +20,6 @@ class BuiltInCallable implements Callable {
BuiltInCallable.overloaded(
this.name, Iterable<String> overloads, Iterable<_Callback> callbacks)
: overloads = new List.unmodifiable(overloads
.map((overload) => parseArgumentDeclaration("($overload)"))),
.map((overload) => new ArgumentDeclaration.parse("$overload"))),
callbacks = new List.unmodifiable(callbacks);
}

View File

@ -1,35 +0,0 @@
// 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 'ast/sass.dart';
import 'ast/selector.dart';
import 'parse/at_root_query.dart';
import 'parse/sass.dart';
import 'parse/scss.dart';
import 'parse/selector.dart';
Stylesheet parseSass(String contents, {url}) =>
new SassParser(contents, url: url).parse();
Stylesheet parseScss(String contents, {url}) =>
new ScssParser(contents, url: url).parse();
ArgumentDeclaration parseArgumentDeclaration(String contents, {url}) =>
new ScssParser(contents, url: url).parseArgumentDeclaration();
SelectorList parseSelector(String contents, {url, bool allowParent: true}) =>
new SelectorParser(contents, url: url, allowParent: allowParent).parse();
CompoundSelector parseCompoundSelector(String contents,
{url, bool allowParent: true}) =>
new SelectorParser(contents, url: url, allowParent: allowParent)
.parseCompoundSelector();
SimpleSelector parseSimpleSelector(String contents,
{url, bool allowParent: true}) =>
new SelectorParser(contents, url: url, allowParent: allowParent)
.parseSimpleSelector();
AtRootQuery parseAtRootQuery(String contents, {url}) =>
new AtRootQueryParser(contents, url: url).parse();

View File

@ -4,7 +4,6 @@
import 'ast/selector.dart';
import 'exception.dart';
import 'parse.dart';
import 'value/boolean.dart';
import 'value/color.dart';
import 'value/list.dart';
@ -57,7 +56,7 @@ abstract class Value {
SelectorList assertSelector({String name, bool allowParent: false}) {
var string = _selectorString(name);
try {
return parseSelector(string, allowParent: allowParent);
return new SelectorList.parse(string, allowParent: allowParent);
} on SassFormatException catch (error) {
// TODO(nweiz): colorize this if we're running in an environment where
// that works.
@ -68,7 +67,7 @@ abstract class Value {
SimpleSelector assertSimpleSelector({String name, bool allowParent: false}) {
var string = _selectorString(name);
try {
return parseSimpleSelector(string, allowParent: allowParent);
return new SimpleSelector.parse(string, allowParent: allowParent);
} on SassFormatException catch (error) {
// TODO(nweiz): colorize this if we're running in an environment where
// that works.
@ -80,7 +79,7 @@ abstract class Value {
{String name, bool allowParent: false}) {
var string = _selectorString(name);
try {
return parseCompoundSelector(string, allowParent: allowParent);
return new CompoundSelector.parse(string, allowParent: allowParent);
} on SassFormatException catch (error) {
// TODO(nweiz): colorize this if we're running in an environment where
// that works.

View File

@ -17,7 +17,6 @@ import '../callable.dart';
import '../environment.dart';
import '../exception.dart';
import '../extend/extender.dart';
import '../parse.dart';
import '../utils.dart';
import '../value.dart';
import 'interface/statement.dart';
@ -94,7 +93,7 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
void visitAtRootRule(AtRootRule node) {
var query = node.query == null
? AtRootQuery.defaultQuery
: parseAtRootQuery(_performInterpolation(node.query));
: new AtRootQuery.parse(_performInterpolation(node.query));
var parent = _parent;
var included = <CssParentNode>[];
@ -263,7 +262,7 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
// TODO: recontextualize parse errors.
// TODO: disallow parent selectors.
var target = parseSimpleSelector(targetText.value.trim());
var target = new SimpleSelector.parse(targetText.value.trim());
_extender.addExtension(_selector.value, target, node);
}
@ -373,8 +372,11 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
}
return _importedFiles.putIfAbsent(path, () {
var parse = p.extension(path) == '.sass' ? parseSass : parseScss;
return parse(new File(path).readAsStringSync(), url: p.toUri(path));
var contents = new File(path).readAsStringSync();
var url = p.toUri(path);
return p.extension(path) == '.sass'
? new Stylesheet.parseSass(contents, url: url)
: new Stylesheet.parseScss(contents, url: url);
});
}
@ -486,7 +488,7 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
}
var selectorText = _interpolationToValue(node.selector, trim: true);
var parsedSelector = parseSelector(selectorText.value);
var parsedSelector = new SelectorList.parse(selectorText.value);
parsedSelector = _addExceptionSpan(
() => parsedSelector.resolveParentSelectors(_selector?.value),
node.selector.span);