dart-sass/lib/src/callable/built_in.dart

61 lines
2.3 KiB
Dart
Raw Normal View History

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.
import 'package:tuple/tuple.dart';
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';
import 'async_built_in.dart';
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.
class BuiltInCallable implements Callable, AsyncBuiltInCallable {
2016-08-27 11:06:15 +02:00
final String name;
2016-10-10 05:57:10 +02:00
/// The overloads declared for this callable.
final _overloads = <Tuple2<ArgumentDeclaration, _Callback>>[];
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(
this.name, String arguments, Value callback(List<Value> arguments)) {
_overloads
.add(new Tuple2(new ArgumentDeclaration.parse(arguments), callback));
}
2016-08-27 11:06:15 +02:00
/// Creates a callable with multiple implementations.
2016-10-10 05:57:10 +02:00
///
/// Each key/value pair in [overloads] defines the argument declaration for
/// the overload (which should not include parentheses), and the callback to
/// execute if that argument declaration matches. Throws a
/// [SassFormatException] if parsing fails.
BuiltInCallable.overloaded(this.name, Map<String, _Callback> overloads) {
overloads.forEach((arguments, callback) {
_overloads
.add(new Tuple2(new ArgumentDeclaration.parse(arguments), callback));
});
2016-10-10 05:57:10 +02:00
}
/// Returns the argument declaration and Dart callback for the given
/// positional and named arguments.
///
/// Note that this doesn't guarantee that [positional] and [names] are valid
/// for the returned [ArgumentDeclaration].
Tuple2<ArgumentDeclaration, _Callback> callbackFor(
int positional, Set<String> names) =>
_overloads.take(_overloads.length - 1).firstWhere(
(overload) => overload.item1.matches(positional, names),
orElse: () => _overloads.last);
2016-08-29 00:04:48 +02:00
}