Skip to content

Commit 098368f

Browse files
Rafael-SOWNetclaude
andcommitted
The MCP server reads beta(a, b) and norm(v, p) as calls
Its check of whether a name is a function parses name(x), which for beta and norm is still the product it was, so beta(a, b) was warned about as a variable times a bracket. A call with a comma at its own depth is now asked of name(x, y), which no product parses, and beta(z) is still warned about. Part of #2182. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 18d2715 commit 098368f

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎Sources/MCP/AngouriMath.Mcp/Parsing.cs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,47 @@ internal static bool IsFunction(string name) => Calls.GetOrAdd(name, static cand
6868

6969
private static readonly ConcurrentDictionary<string, bool> Calls = new(StringComparer.Ordinal);
7070

71+
/// <summary>
72+
/// Whether the grammar reads <c>name(x, y)</c> as a call. <c>beta</c> and <c>norm</c> are functions of two
73+
/// arguments only, and with one are still the product they were, so <see cref="IsFunction"/> says no for them;
74+
/// with a comma, a name that is no function does not parse at all, the bracket not being a factor.
75+
/// https://github.com/asc-community/AngouriMath/issues/2182
76+
/// </summary>
77+
internal static bool IsFunctionOfSeveralArguments(string name) => SeveralArgumentCalls.GetOrAdd(name, static candidate =>
78+
{
79+
using var _ = MathS.Settings.ExplicitParsingOnly.Set(false);
80+
try
81+
{
82+
return MathS.Parse(candidate + "(x, y)").Switch(_ => true, _ => false);
83+
}
84+
catch (Exception)
85+
{
86+
return true;
87+
}
88+
});
89+
90+
private static readonly ConcurrentDictionary<string, bool> SeveralArgumentCalls = new(StringComparer.Ordinal);
91+
92+
// Whether the bracket opening at `open` has a comma at its own depth before it closes.
93+
private static bool HasACommaAtItsDepth(string source, int open)
94+
{
95+
var depth = 0;
96+
for (var i = open + 1; i < source.Length; i++)
97+
switch (source[i])
98+
{
99+
case ',' when depth == 0:
100+
return true;
101+
case '(' or '[' or '{':
102+
depth++;
103+
break;
104+
case ')' or ']' or '}':
105+
if (depth-- == 0)
106+
return false;
107+
break;
108+
}
109+
return false;
110+
}
111+
71112
/// <summary>Names people reach for that this grammar spells differently, and that parse
72113
/// as something else rather than failing. A name the library refuses outright is not
73114
/// here: its own exception says more than this table could.</summary>
@@ -147,6 +188,8 @@ private static List<string> Warnings(string source)
147188
{
148189
var name = m.Groups[1].Value;
149190
if (IsFunction(name)) continue;
191+
// beta(a, b) is the function, where beta(z) is the product.
192+
if (HasACommaAtItsDepth(source, m.Index + m.Length - 1) && IsFunctionOfSeveralArguments(name)) continue;
150193

151194
var hint = Misspellings.TryGetValue(name, out var spelling)
152195
? $" Use {spelling}."

‎Sources/Tests/McpUnitTests/ParsingTest.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public static IEnumerable<object[]> GrammarFunctions()
3939
[Theory]
4040
[MemberData(nameof(GrammarFunctions))]
4141
public void EveryFunctionInTheGrammarIsReadAsOne(string name) =>
42-
Assert.True(Parsing.IsFunction(name), $"{name}(x) is read as a product");
42+
// beta and norm are functions of two arguments, and beta(x) is still a product.
43+
Assert.True(Parsing.IsFunction(name) || Parsing.IsFunctionOfSeveralArguments(name), $"{name}(x) is read as a product");
4344

4445
[Theory]
4546
[MemberData(nameof(GrammarFunctions))]

‎Sources/Tests/McpUnitTests/ToolsTest.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void ADigitAfterANameIsWarnedAboutAsAPower(string expression, string pars
4141
[InlineData("1.5e3 + 2", "1500 + 2")]
4242
[InlineData("erf(x) + Si(x)", "erf(x) + Si(x)")]
4343
[InlineData("t_0 + x", "t_0 + x")]
44+
[InlineData("beta(a, b) + norm(v, 3)", "beta(a, b) + norm(v, 3)")]
4445
public void ACorrectSpellingIsNotWarnedAbout(string expression, string parsed)
4546
{
4647
var answer = Mcp.Call("am_parse", $$"""{"expression":"{{expression}}"}""");

0 commit comments

Comments
 (0)