parse static method

ComputeKernel parse(
  1. String source, {
  2. String? name,
})

The kernel called name in source, or its only kernel when name is null. Throws ComputeKernelException when there is no such kernel, or more than one and no name.

Implementation

static ComputeKernel parse(String source, {String? name}) {
  final all = parseAll(source);
  if (name != null) {
    for (final kernel in all) {
      if (kernel.name == name) return kernel;
    }
    throw ComputeKernelException(
      'there is no kernel called "$name" here; there '
      '${all.length == 1 ? 'is' : 'are'} '
      '${all.map((k) => '"${k.name}"').join(', ')}',
      line: 1,
      column: 1,
    );
  }
  if (all.length != 1) {
    throw ComputeKernelException(
      'expected one kernel and found ${all.length}; name the one you mean',
      line: 1,
      column: 1,
    );
  }
  return all.single;
}