asc-community/AngouriMath

Unable to parse variable names with numbers correctly

Closed this issue · 3 comments

The version I use:
AngouriMath 1.3

Unexpected behaviour or bug:
I try to parse the expression "X1+X2+H2Ca2", output was "X, H and Ca" .
I expect the output should be "X1, X2 and H2Ca2".
Thanks.

Entity expr = "X1+X2+H2Ca2";
foreach (var v in expr.Vars)
{
    Console.WriteLine(v.Name);
 }

Output

X
H
Ca

Expect Output

X1
X2
H2Ca2

Use Settings.ExplicitParsingOnly

/// <summary>
/// Determine if it should only parse if it is explicit
/// </summary>
/// <example>
/// <code>
/// using System;
/// using AngouriMath;
/// using AngouriMath.Core.Exceptions;
/// using static AngouriMath.MathS;
///
/// Entity expr = "a2 + 2x + a b + 2(g + e)3";
/// Console.WriteLine(expr);
/// using var _ = Settings.ExplicitParsingOnly.Set(true);
/// try
/// {
/// Entity expr2 = "a2 + 2x + a b + 2(g + e)3";
/// Console.WriteLine(expr2);
/// }
/// catch (MissingOperatorParseException)
/// {
/// Entity expr3 = "a^2 + 2*x + a * b + 2*(g + e)^3";
/// Console.WriteLine($"Exception, but we still can parse {expr3}");
/// }
/// </code>
/// Prints
/// <code>
/// a ^ 2 + 2 * x + a * b + 2 * (g + e) ^ 3
/// Exception, but we still can parse a ^ 2 + 2 * x + a * b + 2 * (g + e) ^ 3
/// </code>
/// </example>
public static Setting<bool> ExplicitParsingOnly => explicitParsingOnly ??= false;

I add Settings.ExplicitParsingOnly.Set(true) will cause There should be an operator between [@0,0:0='X',<123>,1:0] and [@1,1:1='1',<120>,1:1] error.

using (var _ = Settings.ExplicitParsingOnly.Set(true))
{
	Entity expr = "X1+X2+H2Ca2";
	foreach (var v in expr.Vars)
	{
		Console.WriteLine(v.Name);
	}
}
AngouriMath.Core.Exceptions.MissingOperatorParseException
  HResult=0x80131500
  Message=There should be an operator between [@0,0:0='X',<123>,1:0] and [@1,1:1='1',<120>,1:1]
  Source=AngouriMath
  StackTrace: 
   於 AngouriMath.Core.Parser.<>c.<Parse>b__4_3(MissingOperator missingOperator)
   於 HonkSharp.Functional.Either`3.Switch[TOut](Func`2 case1, Func`2 case2, Func`2 case3)
   於 AngouriMath.Core.Parser.<>c.<Parse>b__4_1(Failure`1 failure)
   於 HonkSharp.Functional.Either`2.Switch[TOut](Func`2 case1, Func`2 case2)
   於 AngouriMath.Core.Parser.Parse(String source)
   於 HonkSharp.Fluency.ControlExtensions.LazyEval`2.get_Value()
   於 System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key, CreateValueCallback createValueCallback)
   於 AngouriMath.Entity.op_Implicit(String expr)

Yes because you need to use underscore x_1 instead of x1