2016-08-27 11:06:15 +02:00
|
|
|
// 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.
|
|
|
|
|
2016-08-28 01:12:17 +02:00
|
|
|
import '../ast/sass.dart';
|
2016-08-27 11:06:15 +02:00
|
|
|
import '../callable.dart';
|
|
|
|
import '../value.dart';
|
|
|
|
|
2016-10-10 05:57:10 +02:00
|
|
|
/// A [BuiltInCallable]'s callback.
|
2016-08-27 11:06:15 +02:00
|
|
|
typedef Value _Callback(List<Value> arguments);
|
|
|
|
|
2016-10-10 05:57:10 +02:00
|
|
|
/// A callable defined in Dart code.
|
|
|
|
///
|
|
|
|
/// Unlike user-defined callables, built-in callables support overloads. They
|
|
|
|
/// may declare multiple different callbacks with multiple different sets of
|
|
|
|
/// arguments. When the callable is invoked, the first callback with matching
|
|
|
|
/// arguments is invoked.
|
2016-08-27 11:06:15 +02:00
|
|
|
class BuiltInCallable implements Callable {
|
|
|
|
final String name;
|
2016-10-10 05:57:10 +02:00
|
|
|
|
|
|
|
/// The arguments declared for this callable.
|
|
|
|
///
|
|
|
|
/// The declaration at index `i` corresponds to the callback at index `i` in
|
|
|
|
/// [callbacks].
|
2016-09-21 01:02:26 +02:00
|
|
|
final List<ArgumentDeclaration> overloads;
|
2016-10-10 05:57:10 +02:00
|
|
|
|
|
|
|
/// The callbacks declared for this callable.
|
|
|
|
///
|
|
|
|
/// The callback at index `i` corresponds to the arguments at index `i` in
|
|
|
|
/// [overloads].
|
2016-09-21 01:02:26 +02:00
|
|
|
final List<_Callback> callbacks;
|
|
|
|
|
2016-10-10 05:57:10 +02:00
|
|
|
/// Creates a callable with a single [arguments] declaration and a single
|
|
|
|
/// [callback].
|
|
|
|
///
|
|
|
|
/// The argument declaration is parsed from [arguments], which should not
|
|
|
|
/// include parentheses. Throws a [SassFormatException] if parsing fails.
|
2016-09-21 18:30:41 +02:00
|
|
|
BuiltInCallable(
|
|
|
|
String name, String arguments, Value callback(List<Value> arguments))
|
2016-09-21 01:02:26 +02:00
|
|
|
: this.overloaded(name, [arguments], [callback]);
|
2016-08-27 11:06:15 +02:00
|
|
|
|
2016-10-10 05:57:10 +02:00
|
|
|
/// Creates a callable that declares multiple overloads.
|
|
|
|
///
|
|
|
|
/// The argument declarations are parsed from [overloads], whose contents
|
|
|
|
/// should not include parentheses. Throws a [SassFormatException] if parsing
|
|
|
|
/// fails.
|
|
|
|
///
|
|
|
|
/// Throws an [ArgumentError] if [overloads] doesn't have the same length as
|
|
|
|
/// [callbacks].
|
2016-09-21 18:30:41 +02:00
|
|
|
BuiltInCallable.overloaded(
|
|
|
|
this.name, Iterable<String> overloads, Iterable<_Callback> callbacks)
|
|
|
|
: overloads = new List.unmodifiable(overloads
|
2016-09-24 12:39:06 +02:00
|
|
|
.map((overload) => new ArgumentDeclaration.parse("$overload"))),
|
2016-10-10 05:57:10 +02:00
|
|
|
callbacks = new List.unmodifiable(callbacks) {
|
|
|
|
if (this.overloads.length != this.callbacks.length) {
|
|
|
|
throw new ArgumentError(
|
|
|
|
"overloads must be the same length as callbacks.");
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 00:04:48 +02:00
|
|
|
}
|