uCalc API Version: 5.7.0-preview.1 Released: 7/30/2026
Warning
uCalc API Preview Release Notice:This preview documentation describes the intended behavior of the API. It is not fully accurate or complete.The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.Use of the preview version in your production code is not recommended.
Explains the fundamental difference between uCalc's token-aware parsing and traditional character-aware Regex, highlighting safety and power.
One of the most important concepts to understand about uCalc is that its Transformer is structurally aware. This is the key advantage it holds over traditional text-processing tools like Regular Expressions (Regex). In short:
This fundamental difference has profound implications for safety, power, and readability, especially when transforming structured text like source code, configuration files, or markup.
Regular expressions are incredibly powerful for finding patterns in character streams. However, their greatest strength is also their greatest weakness: they are "blind" to the structure and context of the text they are processing.
Imagine you want to refactor a piece of code by renaming the variable rate to annual_rate. Your code looks like this:
csharp(Note: To enable C-style comments as shown, you would first define a comment token, for example, by calling
rate = 0.05; // Current rate
print("The rate is: ", rate);uc.ExpressionTokens().Add("//.*", TokenType.Whitespace);. This demonstrates uCalc's configurable syntax.)
A developer's first instinct might be to use a simple find-and-replace operation for the word "rate". A naive regex like \brate\b would produce this incorrect result:
csharp
annual_rate = 0.05; // Current annual_rate
print("The annual_rate is: ", annual_rate);
This is a catastrophic failure. The regex, blind to context, has incorrectly modified the text inside both a code comment and a string literal, corrupting the logic and the output.
The uCalc Transformer avoids this problem by performing a crucial first step: tokenization. Before any pattern matching occurs, the engine's tokenizer (or lexer) breaks the input string into a stream of meaningful Tokens:
rate (Identifier)= (Reducible / Operator Symbol)0.05 (Number Literal); (StatementSeparator)// Current rate (Comment / Whitespace)print (Identifier)( (Bracket)"The rate is: " (String Literal), (Argument Separator)rate (Identifier)) (Bracket); (StatementSeparator)This process categorizes each part of the string. Comment tokens can be defined as whitespace with uCalc.Tokens .Add("//.*", TokenType.Whitespace);, which tells the parser to ignore them, while string literals are recognized as atomic units.
When you define a uCalc rule to replace the identifier rate, the pattern matching engine operates on this stream of tokens. It knows that "The rate is: " is a single String Literal token and (once defined) // Current rate is a Whitespace token. By default, it will not look inside them.
A rule to replace the identifier rate will only match tokens #1 and #10, producing the correct, safe transformation:
csharp
annual_rate = 0.05; // Current rate
print("The rate is: ", annual_rate);
This structural awareness is the core reason why the uCalc Transformer is a superior tool for static analysis, code refactoring, and transpilation.
| Feature | Regular Expressions | uCalc Transformer |
|---|---|---|
| Safety | 🔴 Unsafe. Easily corrupts string literals, comments, and other structural blocks. | 🟢 Safe by Default. QuoteSensitive and BracketSensitive properties respect code structure. |
| Readability | 🔴 Low. Patterns can become cryptic and hard to maintain (e.g., (?<=\s)\d+). | 🟢 High. Patterns use readable names and categories (e.g., {@Number}). |
| Power | 🟡 Medium. Struggles with nested or recursive patterns (like matching balanced parentheses). | 🟢 High. Natively understands nested structures and token categories. |
ID: 1226
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
// This rule only targets the alphanumeric token 'x'.
t.FromTo("x", "value");
var code = """
x = 5; print("The value of x is...");
""";
Console.WriteLine(t.Transform(code));
value = 5; print("The value of x is..."); using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); // This rule only targets the alphanumeric token 'x'. t.FromTo("x", "value"); var code = """ x = 5; print("The value of x is..."); """; Console.WriteLine(t.Transform(code));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
// This rule only targets the alphanumeric token 'x'.
t.FromTo("x", "value");
auto code = R"(x = 5; print("The value of x is...");)";
cout << t.Transform(code) << endl;
}
value = 5; print("The value of x is..."); #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; // This rule only targets the alphanumeric token 'x'. t.FromTo("x", "value"); auto code = R"(x = 5; print("The value of x is...");)"; cout << t.Transform(code) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
'// This rule only targets the alphanumeric token 'x'.
t.FromTo("x", "value")
Dim code = "x = 5; print(""The value of x is..."");"
Console.WriteLine(t.Transform(code))
End Sub
End Module
value = 5; print("The value of x is..."); Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() '// This rule only targets the alphanumeric token 'x'. t.FromTo("x", "value") Dim code = "x = 5; print(""The value of x is..."");" Console.WriteLine(t.Transform(code)) End Sub End Module
ID: 1227
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
// This rule replaces the ALPHANUMERIC token 'get_data', not just the text.
t.FromTo("get_data", "fetch_records");
var code = """
// Note: 'get_data' is the old function name.
results = get_data(source);
print("The 'get_data' function was called.");
""";
// The default tokenizer recognizes the comment and string literal as separate tokens,
// so the rule to replace the function name doesn't affect them.
Console.WriteLine(t.Transform(code));
// Note: 'get_data' is the old function name.
results = fetch_records(source);
print("The 'get_data' function was called."); using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); // This rule replaces the ALPHANUMERIC token 'get_data', not just the text. t.FromTo("get_data", "fetch_records"); var code = """ // Note: 'get_data' is the old function name. results = get_data(source); print("The 'get_data' function was called."); """; // The default tokenizer recognizes the comment and string literal as separate tokens, // so the rule to replace the function name doesn't affect them. Console.WriteLine(t.Transform(code));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
// This rule replaces the ALPHANUMERIC token 'get_data', not just the text.
t.FromTo("get_data", "fetch_records");
auto code = R"(
// Note: 'get_data' is the old function name.
results = get_data(source);
print("The 'get_data' function was called.");
)";
// The default tokenizer recognizes the comment and string literal as separate tokens,
// so the rule to replace the function name doesn't affect them.
cout << t.Transform(code) << endl;
}
// Note: 'get_data' is the old function name.
results = fetch_records(source);
print("The 'get_data' function was called."); #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; // This rule replaces the ALPHANUMERIC token 'get_data', not just the text. t.FromTo("get_data", "fetch_records"); auto code = R"( // Note: 'get_data' is the old function name. results = get_data(source); print("The 'get_data' function was called."); )"; // The default tokenizer recognizes the comment and string literal as separate tokens, // so the rule to replace the function name doesn't affect them. cout << t.Transform(code) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
'// This rule replaces the ALPHANUMERIC token 'get_data', not just the text.
t.FromTo("get_data", "fetch_records")
Dim code = "
// Note: 'get_data' is the old function name.
results = get_data(source);
print(""The 'get_data' function was called."");
"
'// The default tokenizer recognizes the comment and string literal as separate tokens,
'// so the rule to replace the function name doesn't affect them.
Console.WriteLine(t.Transform(code))
End Sub
End Module
// Note: 'get_data' is the old function name.
results = fetch_records(source);
print("The 'get_data' function was called."); Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() '// This rule replaces the ALPHANUMERIC token 'get_data', not just the text. t.FromTo("get_data", "fetch_records") Dim code = " // Note: 'get_data' is the old function name. results = get_data(source); print(""The 'get_data' function was called.""); " '// The default tokenizer recognizes the comment and string literal as separate tokens, '// so the rule to replace the function name doesn't affect them. Console.WriteLine(t.Transform(code)) End Sub End Module
ID: 1228
using uCalcSoftware;
var uc = new uCalc();
var code = """
rate = 0.05; print("rate"); // a rate
""";
Console.WriteLine("--- uCalc Transformer (Token-Aware & Correct) ---");
var t = new uCalc.Transformer();
t.Tokens.Add("//.*", TokenType.Whitespace);
// Rule targets only the alphanumeric token 'rate'
t.FromTo("rate", "annual_rate");
Console.WriteLine(t.Transform(code));
Console.WriteLine("");
Console.WriteLine("--- Simulated Regex (Character-Aware & Incorrect) ---");
// This simulates a simple find-and-replace for the word 'rate'
// which incorrectly changes the string literal and comment.
var incorrect_result = """
annual_rate = 0.05; print("annual_rate"); // a annual_rate
""";
Console.WriteLine(incorrect_result);
--- uCalc Transformer (Token-Aware & Correct) ---
annual_rate = 0.05; print("rate"); // a rate
--- Simulated Regex (Character-Aware & Incorrect) ---
annual_rate = 0.05; print("annual_rate"); // a annual_rate using uCalcSoftware; var uc = new uCalc(); var code = """ rate = 0.05; print("rate"); // a rate """; Console.WriteLine("--- uCalc Transformer (Token-Aware & Correct) ---"); var t = new uCalc.Transformer(); t.Tokens.Add("//.*", TokenType.Whitespace); // Rule targets only the alphanumeric token 'rate' t.FromTo("rate", "annual_rate"); Console.WriteLine(t.Transform(code)); Console.WriteLine(""); Console.WriteLine("--- Simulated Regex (Character-Aware & Incorrect) ---"); // This simulates a simple find-and-replace for the word 'rate' // which incorrectly changes the string literal and comment. var incorrect_result = """ annual_rate = 0.05; print("annual_rate"); // a annual_rate """; Console.WriteLine(incorrect_result);
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto code = R"(rate = 0.05; print("rate"); // a rate)";
cout << "--- uCalc Transformer (Token-Aware & Correct) ---" << endl;
uCalc::Transformer t;
t.Tokens().Add("//.*", TokenType::Whitespace);
// Rule targets only the alphanumeric token 'rate'
t.FromTo("rate", "annual_rate");
cout << t.Transform(code) << endl;
cout << "" << endl;
cout << "--- Simulated Regex (Character-Aware & Incorrect) ---" << endl;
// This simulates a simple find-and-replace for the word 'rate'
// which incorrectly changes the string literal and comment.
auto incorrect_result = R"(annual_rate = 0.05; print("annual_rate"); // a annual_rate)";
cout << incorrect_result << endl;
}
--- uCalc Transformer (Token-Aware & Correct) ---
annual_rate = 0.05; print("rate"); // a rate
--- Simulated Regex (Character-Aware & Incorrect) ---
annual_rate = 0.05; print("annual_rate"); // a annual_rate #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto code = R"(rate = 0.05; print("rate"); // a rate)"; cout << "--- uCalc Transformer (Token-Aware & Correct) ---" << endl; uCalc::Transformer t; t.Tokens().Add("//.*", TokenType::Whitespace); // Rule targets only the alphanumeric token 'rate' t.FromTo("rate", "annual_rate"); cout << t.Transform(code) << endl; cout << "" << endl; cout << "--- Simulated Regex (Character-Aware & Incorrect) ---" << endl; // This simulates a simple find-and-replace for the word 'rate' // which incorrectly changes the string literal and comment. auto incorrect_result = R"(annual_rate = 0.05; print("annual_rate"); // a annual_rate)"; cout << incorrect_result << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim code = "rate = 0.05; print(""rate""); // a rate"
Console.WriteLine("--- uCalc Transformer (Token-Aware & Correct) ---")
Dim t As New uCalc.Transformer()
t.Tokens.Add("//.*", TokenType.Whitespace)
'// Rule targets only the alphanumeric token 'rate'
t.FromTo("rate", "annual_rate")
Console.WriteLine(t.Transform(code))
Console.WriteLine("")
Console.WriteLine("--- Simulated Regex (Character-Aware & Incorrect) ---")
'// This simulates a simple find-and-replace for the word 'rate'
'// which incorrectly changes the string literal and comment.
Dim incorrect_result = "annual_rate = 0.05; print(""annual_rate""); // a annual_rate"
Console.WriteLine(incorrect_result)
End Sub
End Module
--- uCalc Transformer (Token-Aware & Correct) ---
annual_rate = 0.05; print("rate"); // a rate
--- Simulated Regex (Character-Aware & Incorrect) ---
annual_rate = 0.05; print("annual_rate"); // a annual_rate Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim code = "rate = 0.05; print(""rate""); // a rate" Console.WriteLine("--- uCalc Transformer (Token-Aware & Correct) ---") Dim t As New uCalc.Transformer() t.Tokens.Add("//.*", TokenType.Whitespace) '// Rule targets only the alphanumeric token 'rate' t.FromTo("rate", "annual_rate") Console.WriteLine(t.Transform(code)) Console.WriteLine("") Console.WriteLine("--- Simulated Regex (Character-Aware & Incorrect) ---") '// This simulates a simple find-and-replace for the word 'rate' '// which incorrectly changes the string literal and comment. Dim incorrect_result = "annual_rate = 0.05; print(""annual_rate""); // a annual_rate" Console.WriteLine(incorrect_result) End Sub End Module