uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

Location = [int]

Property

Product: 

Fast Math Parser

Class: 

ErrorInfo

Returns the zero-based character position where a parsing error was detected.

Remarks

The Location property is a crucial tool for diagnosing issues within an expression, designed to be called from inside an error handler callback registered with AddErrorHandler.

🎯 Parsing vs. Evaluation Errors

The value returned by ErrorLocation depends on the type of error that occurred:

  • Parsing-Stage Errors: For syntax errors, unrecognized tokens, or mismatched brackets, ErrorLocation returns the zero-based character index in the original expression string where the parser first detected the problem. This allows you to pinpoint the exact location of the faulty syntax.

  • Evaluation-Stage Errors: For mathematical or logical errors that occur during calculation (e.g., division by zero), ErrorLocation will return 0. This is because the error is not tied to a specific character in the input string but is a result of the computation itself. To get information about these types of errors, you should rely on Error.Code and ErrorMessage.

The provided examples clearly illustrate this distinction.

ErrorLocation is part of a suite of functions available within an error handler:

⚖️ Comparative Analysis

In other parsing systems, obtaining error locations can be complex. For example:

  • Manual Parsing: A developer would need to meticulously track the current index or cursor position while iterating through the string.
  • Parser Generators (e.g., ANTLR): These tools often provide rich context objects with line and column numbers but require a deeper understanding of the generated parse tree and visitor patterns.

uCalc simplifies this by providing a direct, single-function call—uc.ErrorLocation()—that gives immediate access to the relevant character position without any manual tracking or complex object navigation. This strikes a balance between simplicity and utility, making it easy to build user-friendly feedback systems.

Examples

Capturing both parsing-stage and evaluation-stage errors to see how ErrorLocation behaves differently.
				
					using uCalcSoftware;

var uc = new uCalc();

static void MyErrorHandler(Handle_uCalc h) {
   var uc = new uCalc(h);
   Console.WriteLine("--- Error Captured ---");
   Console.WriteLine($"Message: {uc.Error.Message}");
   Console.WriteLine($"Symbol: '{uc.Error.Symbol}'");
   Console.WriteLine($"Location: {uc.Error.Location}");
   Console.WriteLine($"Expression: '{uc.Error.Expression}'");
}


uc.Error.AddHandler(MyErrorHandler);

Console.WriteLine("Demonstrating a PARSING error:");
uc.EvalStr("123//456");

Console.WriteLine("");
Console.WriteLine("Demonstrating an EVALUATION error:");
uc.Error.TrapOnDivideByZero = true;
uc.EvalStr("5/0");
				
			
Demonstrating a PARSING error:
--- Error Captured ---
Message: Syntax error
Symbol: '/'
Location: 3
Expression: '123//456'

Demonstrating an EVALUATION error:
--- Error Captured ---
Message: Division by 0
Symbol: ''
Location: 0
Expression: ''
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyErrorHandler(Handle_uCalc h) {
   auto uc = uCalc(h);
   cout << "--- Error Captured ---" << endl;
   cout << "Message: " << uc.Error().Message() << endl;
   cout << "Symbol: '" << uc.Error().Symbol() << "'" << endl;
   cout << "Location: " << uc.Error().Location() << endl;
   cout << "Expression: '" << uc.Error().Expression() << "'" << endl;
}

int main() {
   uCalc uc;
   uc.Error().AddHandler(MyErrorHandler);

   cout << "Demonstrating a PARSING error:" << endl;
   uc.EvalStr("123//456");

   cout << "" << endl;
   cout << "Demonstrating an EVALUATION error:" << endl;
   uc.Error().TrapOnDivideByZero(true);
   uc.EvalStr("5/0");
}
				
			
Demonstrating a PARSING error:
--- Error Captured ---
Message: Syntax error
Symbol: '/'
Location: 3
Expression: '123//456'

Demonstrating an EVALUATION error:
--- Error Captured ---
Message: Division by 0
Symbol: ''
Location: 0
Expression: ''
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyErrorHandler(ByVal h As Handle_uCalc)
      Dim uc As New uCalc(h)
      Console.WriteLine("--- Error Captured ---")
      Console.WriteLine($"Message: {uc.Error.Message}")
      Console.WriteLine($"Symbol: '{uc.Error.Symbol}'")
      Console.WriteLine($"Location: {uc.Error.Location}")
      Console.WriteLine($"Expression: '{uc.Error.Expression}'")
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      uc.Error.AddHandler(AddressOf MyErrorHandler)
      
      Console.WriteLine("Demonstrating a PARSING error:")
      uc.EvalStr("123//456")
      
      Console.WriteLine("")
      Console.WriteLine("Demonstrating an EVALUATION error:")
      uc.Error.TrapOnDivideByZero = true
      uc.EvalStr("5/0")
   End Sub
End Module
				
			
Demonstrating a PARSING error:
--- Error Captured ---
Message: Syntax error
Symbol: '/'
Location: 3
Expression: '123//456'

Demonstrating an EVALUATION error:
--- Error Captured ---
Message: Division by 0
Symbol: ''
Location: 0
Expression: ''
Adding an error handler callback
				
					using uCalcSoftware;

var uc = new uCalc();

static void MyErrorHandler(Handle_uCalc h) {
   var uc = new uCalc(h);
   Console.WriteLine("An error has occurred!");
   Console.WriteLine($"Error #: {(int)uc.Error.Code}");
   Console.WriteLine($"Error Message: {uc.Error.Message}");
   Console.WriteLine($"Error Symbol: {uc.Error.Symbol}");
   Console.WriteLine($"Error Location: {uc.Error.Location}");
   Console.WriteLine($"Error Expression: {uc.Error.Expression}");
}


uc.Error.AddHandler(MyErrorHandler);
Console.WriteLine(uc.EvalStr("123+"));
Console.WriteLine("");

uc.Error.TrapOnDivideByZero = true;
Console.WriteLine(uc.EvalStr("5/0"));
				
			
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error

An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol: 
Error Location: 0
Error Expression: 
Division by 0
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyErrorHandler(Handle_uCalc h) {
   auto uc = uCalc(h);
   cout << "An error has occurred!" << endl;
   cout << "Error #: " << (int)uc.Error().Code() << endl;
   cout << "Error Message: " << uc.Error().Message() << endl;
   cout << "Error Symbol: " << uc.Error().Symbol() << endl;
   cout << "Error Location: " << uc.Error().Location() << endl;
   cout << "Error Expression: " << uc.Error().Expression() << endl;
}

int main() {
   uCalc uc;
   uc.Error().AddHandler(MyErrorHandler);
   cout << uc.EvalStr("123+") << endl;
   cout << "" << endl;

   uc.Error().TrapOnDivideByZero(true);
   cout << uc.EvalStr("5/0") << endl;
}
				
			
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error

An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol: 
Error Location: 0
Error Expression: 
Division by 0
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyErrorHandler(ByVal h As Handle_uCalc)
      Dim uc As New uCalc(h)
      Console.WriteLine("An error has occurred!")
      Console.WriteLine($"Error #: {CInt(uc.Error.Code)}")
      Console.WriteLine($"Error Message: {uc.Error.Message}")
      Console.WriteLine($"Error Symbol: {uc.Error.Symbol}")
      Console.WriteLine($"Error Location: {uc.Error.Location}")
      Console.WriteLine($"Error Expression: {uc.Error.Expression}")
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      uc.Error.AddHandler(AddressOf MyErrorHandler)
      Console.WriteLine(uc.EvalStr("123+"))
      Console.WriteLine("")
      
      uc.Error.TrapOnDivideByZero = true
      Console.WriteLine(uc.EvalStr("5/0"))
   End Sub
End Module
				
			
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error

An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol: 
Error Location: 0
Error Expression: 
Division by 0