parseExpression method
Implementation
@visibleForTesting
ast.AST parseExpression(
String input,
String location, {
bool? allowAssignments,
bool? allowPipes,
List<CompileIdentifierMetadata>? exports,
}) {
if (input.isEmpty) {
return ast.EmptyExpr();
}
// This is a hack; currently analyzer can only accept valid compilation
// units into `parseString`, which means we need something valid at the
// top-level of a Dart file.
final wrapper = 'void __EXPRESSION__() => $input;';
final result = parseString(
content: wrapper,
path: location,
throwIfDiagnostics: false,
featureSet: FeatureSet.fromEnableFlags2(
sdkLanguageVersion: ExperimentStatus.currentVersion,
flags: const [
'non-nullable',
],
),
);
if (result.errors.isNotEmpty) {
throw ParseException(
result.errors.map((e) => e.message).join('\n'),
input,
location,
);
}
final declared = result.unit.declarations;
if (declared.length != 1) {
throw ParseException('Not a valid expression', input, location);
}
final function = declared.first as FunctionDeclaration;
final innerBody = function.functionExpression.body;
final innerAst = (innerBody as ExpressionFunctionBody).expression;
return _convertAndValididateExpression(
innerAst,
input,
location,
allowAssignments: allowAssignments,
allowPipes: allowPipes,
exports: exports,
);
}