Demonstrates the power of token-awareness by safely renaming a variable while ignoring its name inside a string literal—a common failure point for character-based Regex.
ID: 1172
See: What is uCalc?
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
// A snippet of code where 'rate' is both a variable and part of a string
var source_code = """
rate = 0.05; // Set default rate
print("Current rate is: " + rate);
""";
Console.WriteLine("Original Code:");
Console.WriteLine(source_code);
Console.WriteLine("");
// Define a rule to rename the VARIABLE 'rate' to 'annual_rate'
t.FromTo("rate", "annual_rate");
t.SkipOver("// {text}");
// Run the transformation. The 'rate' inside the string is untouched.
Console.WriteLine("Transformed Code:");
Console.WriteLine(t.Transform(source_code));
Original Code:
rate = 0.05; // Set default rate
print("Current rate is: " + rate);
Transformed Code:
annual_rate = 0.05; // Set default rate
print("Current rate is: " + annual_rate); using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); // A snippet of code where 'rate' is both a variable and part of a string var source_code = """ rate = 0.05; // Set default rate print("Current rate is: " + rate); """; Console.WriteLine("Original Code:"); Console.WriteLine(source_code); Console.WriteLine(""); // Define a rule to rename the VARIABLE 'rate' to 'annual_rate' t.FromTo("rate", "annual_rate"); t.SkipOver("// {text}"); // Run the transformation. The 'rate' inside the string is untouched. Console.WriteLine("Transformed Code:"); Console.WriteLine(t.Transform(source_code));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
// A snippet of code where 'rate' is both a variable and part of a string
auto source_code = R"(rate = 0.05; // Set default rate
print("Current rate is: " + rate);)";
cout << "Original Code:" << endl;
cout << source_code << endl;
cout << "" << endl;
// Define a rule to rename the VARIABLE 'rate' to 'annual_rate'
t.FromTo("rate", "annual_rate");
t.SkipOver("// {text}");
// Run the transformation. The 'rate' inside the string is untouched.
cout << "Transformed Code:" << endl;
cout << t.Transform(source_code) << endl;
}
Original Code:
rate = 0.05; // Set default rate
print("Current rate is: " + rate);
Transformed Code:
annual_rate = 0.05; // Set default rate
print("Current rate is: " + annual_rate); #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; // A snippet of code where 'rate' is both a variable and part of a string auto source_code = R"(rate = 0.05; // Set default rate print("Current rate is: " + rate);)"; cout << "Original Code:" << endl; cout << source_code << endl; cout << "" << endl; // Define a rule to rename the VARIABLE 'rate' to 'annual_rate' t.FromTo("rate", "annual_rate"); t.SkipOver("// {text}"); // Run the transformation. The 'rate' inside the string is untouched. cout << "Transformed Code:" << endl; cout << t.Transform(source_code) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
'// A snippet of code where 'rate' is both a variable and part of a string
Dim source_code = "rate = 0.05; // Set default rate
print(""Current rate is: "" + rate);"
Console.WriteLine("Original Code:")
Console.WriteLine(source_code)
Console.WriteLine("")
'// Define a rule to rename the VARIABLE 'rate' to 'annual_rate'
t.FromTo("rate", "annual_rate")
t.SkipOver("// {text}")
'// Run the transformation. The 'rate' inside the string is untouched.
Console.WriteLine("Transformed Code:")
Console.WriteLine(t.Transform(source_code))
End Sub
End Module
Original Code:
rate = 0.05; // Set default rate
print("Current rate is: " + rate);
Transformed Code:
annual_rate = 0.05; // Set default rate
print("Current rate is: " + annual_rate); Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() '// A snippet of code where 'rate' is both a variable and part of a string Dim source_code = "rate = 0.05; // Set default rate print(""Current rate is: "" + rate);" Console.WriteLine("Original Code:") Console.WriteLine(source_code) Console.WriteLine("") '// Define a rule to rename the VARIABLE 'rate' to 'annual_rate' t.FromTo("rate", "annual_rate") t.SkipOver("// {text}") '// Run the transformation. The 'rate' inside the string is untouched. Console.WriteLine("Transformed Code:") Console.WriteLine(t.Transform(source_code)) End Sub End Module