Demonstrates the Transformer's token-aware safety by correctly renaming a variable without corrupting a string literal or comment.
ID: 1264
See: uCalc Transformer
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
// Turn single-line comments into whitespace tokens (to be ignored)
t.Tokens.Add("//.*", TokenType.Whitespace);
// Define a rule to replace the alphanumeric token 'x' with 'value'
t.FromTo("x", "value");
var code = "x = 10; print('The max value is x.'); // x is 10 here";
// The Transformer correctly identifies that only the first 'x' is
// a token on its own. Imbedded occurrences of 'x' are left alone.
Console.WriteLine(t.Transform(code));
value = 10; print('The max value is x.'); // x is 10 here using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); // Turn single-line comments into whitespace tokens (to be ignored) t.Tokens.Add("//.*", TokenType.Whitespace); // Define a rule to replace the alphanumeric token 'x' with 'value' t.FromTo("x", "value"); var code = "x = 10; print('The max value is x.'); // x is 10 here"; // The Transformer correctly identifies that only the first 'x' is // a token on its own. Imbedded occurrences of 'x' are left alone. Console.WriteLine(t.Transform(code));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
// Turn single-line comments into whitespace tokens (to be ignored)
t.Tokens().Add("//.*", TokenType::Whitespace);
// Define a rule to replace the alphanumeric token 'x' with 'value'
t.FromTo("x", "value");
auto code = "x = 10; print('The max value is x.'); // x is 10 here";
// The Transformer correctly identifies that only the first 'x' is
// a token on its own. Imbedded occurrences of 'x' are left alone.
cout << t.Transform(code) << endl;
}
value = 10; print('The max value is x.'); // x is 10 here #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; // Turn single-line comments into whitespace tokens (to be ignored) t.Tokens().Add("//.*", TokenType::Whitespace); // Define a rule to replace the alphanumeric token 'x' with 'value' t.FromTo("x", "value"); auto code = "x = 10; print('The max value is x.'); // x is 10 here"; // The Transformer correctly identifies that only the first 'x' is // a token on its own. Imbedded occurrences of 'x' are left alone. 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()
'// Turn single-line comments into whitespace tokens (to be ignored)
t.Tokens.Add("//.*", TokenType.Whitespace)
'// Define a rule to replace the alphanumeric token 'x' with 'value'
t.FromTo("x", "value")
Dim code = "x = 10; print('The max value is x.'); // x is 10 here"
'// The Transformer correctly identifies that only the first 'x' is
'// a token on its own. Imbedded occurrences of 'x' are left alone.
Console.WriteLine(t.Transform(code))
End Sub
End Module
value = 10; print('The max value is x.'); // x is 10 here Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() '// Turn single-line comments into whitespace tokens (to be ignored) t.Tokens.Add("//.*", TokenType.Whitespace) '// Define a rule to replace the alphanumeric token 'x' with 'value' t.FromTo("x", "value") Dim code = "x = 10; print('The max value is x.'); // x is 10 here" '// The Transformer correctly identifies that only the first 'x' is '// a token on its own. Imbedded occurrences of 'x' are left alone. Console.WriteLine(t.Transform(code)) End Sub End Module