242242 end
243243end
244244
245+ @unstable parse_expression (ex:: String ; kws... ) = parse_expression (Meta. parse (ex); kws... )
246+
247+ """
248+ Find an operator function by its name in the OperatorEnum, considering the arity.
249+ Throws appropriate errors for ambiguous or missing matches.
250+ """
251+ @unstable function _find_operator_by_name (func_symbol, degree, operators)
252+ matches = Tuple{Function,Int}[]
253+
254+ for arity in 1 : length (operators. ops)
255+ for op in operators. ops[arity]
256+ if nameof (op) == func_symbol
257+ push! (matches, (op, arity))
258+ end
259+ end
260+ end
261+
262+ if isempty (matches)
263+ throw (
264+ ArgumentError (
265+ " Tried to interpolate function `$(func_symbol) ` but failed. " *
266+ " Function not found in operators." ,
267+ ),
268+ )
269+ end
270+
271+ arity_matches = filter (m -> m[2 ] == degree, matches)
272+
273+ if length (arity_matches) > 1
274+ throw (
275+ ArgumentError (
276+ " Ambiguous operator `$(func_symbol) ` with arity $(degree) . " *
277+ " Multiple matches found: $(arity_matches) " ,
278+ ),
279+ )
280+ elseif length (arity_matches) == 0
281+ available_arities = [m[2 ] for m in matches]
282+ throw (
283+ ArgumentError (
284+ " Operator `$(func_symbol) ` found but not with arity $(degree) . " *
285+ " Available arities: $(available_arities) " ,
286+ ),
287+ )
288+ end
289+
290+ return arity_matches[1 ][1 ]:: Function
291+ end
292+
245293""" An empty module for evaluation without collisions."""
246294module EmptyModule end
247295
@@ -264,9 +312,9 @@ module EmptyModule end
264312 func = try
265313 Core. eval (EmptyModule, first (ex. args))
266314 catch
267- throw (
268- ArgumentError ( " Tried to interpolate function ` $( first (ex . args)) ` but failed. " ),
269- )
315+ # Try to find the function in operators by name
316+ degree = length ( args) - 1
317+ _find_operator_by_name ( first (ex . args), degree, operators )
270318 end :: Function
271319 return _parse_expression (
272320 func, args, operators, variable_names, N, E, evaluate_on; kws...
0 commit comments