Changing accepted tokens with ItemOf().Regex or ExpressionTokens().Token
ID: 72
using uCalcSoftware;
var uc = new uCalc();
// (See alternate version of this example using ExpressionTokens instead of ItemOf)
// In this section underscore, _, and numeric digits
// are accepted as part of alphanumeric tokens
uc.DefineVariable("My_Variable = 111");
Console.WriteLine(uc.Error.Message);
uc.DefineVariable("Variable123 = 222");
Console.WriteLine(uc.Error.Message);
Console.WriteLine(uc.ItemOf("_Token_Alphanumeric").Regex);
Console.WriteLine(uc.EvalStr("My_Variable"));
Console.WriteLine(uc.EvalStr("Variable123"));
Console.WriteLine("---");
// Now we no longer want underscore, _, or numeric digits
// to be accepted in alphanumeric tokens; only A-Z
uc.ItemOf("_Token_Alphanumeric").Regex = "[a-zA-Z]+";
uc.DefineVariable("Other_Variable = 333");
Console.WriteLine(uc.Error.Message);
uc.DefineVariable("OtherVariable123 = 444");
Console.WriteLine(uc.Error.Message);
Console.WriteLine(uc.EvalStr("Other_Variable"));
Console.WriteLine(uc.EvalStr("OtherVariable123 "));
Console.WriteLine(uc.EvalStr("My_Variable"));
Console.WriteLine(uc.EvalStr("Variable123"));
Console.WriteLine("---");
//
// We restore the alphanumeric regex to support _ and numbers again
// Note: My_Variable and Variable123 remained; they were simply inaccessible
// Also: We can't use the commented line below because it has the underscore, _,
// character that we had removed.
// uc.ItemOf("_token_alphanumeric").Regex("[a-zA-Z_][a-zA-Z0-9_]*");
uc.ExpressionTokens[TokenType.AlphaNumeric].Regex = "[a-zA-Z_][a-zA-Z0-9_]*";
Console.WriteLine(uc.EvalStr("My_Variable"));
Console.WriteLine(uc.EvalStr("Variable123"));
No error
No error
[a-zA-Z_][a-zA-Z0-9_]*
111
222
---
Invalid definition
Invalid definition
Undefined identifier
Undefined identifier
Undefined identifier
Undefined identifier
---
111
222 using uCalcSoftware; var uc = new uCalc(); // (See alternate version of this example using ExpressionTokens instead of ItemOf) // In this section underscore, _, and numeric digits // are accepted as part of alphanumeric tokens uc.DefineVariable("My_Variable = 111"); Console.WriteLine(uc.Error.Message); uc.DefineVariable("Variable123 = 222"); Console.WriteLine(uc.Error.Message); Console.WriteLine(uc.ItemOf("_Token_Alphanumeric").Regex); Console.WriteLine(uc.EvalStr("My_Variable")); Console.WriteLine(uc.EvalStr("Variable123")); Console.WriteLine("---"); // Now we no longer want underscore, _, or numeric digits // to be accepted in alphanumeric tokens; only A-Z uc.ItemOf("_Token_Alphanumeric").Regex = "[a-zA-Z]+"; uc.DefineVariable("Other_Variable = 333"); Console.WriteLine(uc.Error.Message); uc.DefineVariable("OtherVariable123 = 444"); Console.WriteLine(uc.Error.Message); Console.WriteLine(uc.EvalStr("Other_Variable")); Console.WriteLine(uc.EvalStr("OtherVariable123 ")); Console.WriteLine(uc.EvalStr("My_Variable")); Console.WriteLine(uc.EvalStr("Variable123")); Console.WriteLine("---"); // // We restore the alphanumeric regex to support _ and numbers again // Note: My_Variable and Variable123 remained; they were simply inaccessible // Also: We can't use the commented line below because it has the underscore, _, // character that we had removed. // uc.ItemOf("_token_alphanumeric").Regex("[a-zA-Z_][a-zA-Z0-9_]*"); uc.ExpressionTokens[TokenType.AlphaNumeric].Regex = "[a-zA-Z_][a-zA-Z0-9_]*"; Console.WriteLine(uc.EvalStr("My_Variable")); Console.WriteLine(uc.EvalStr("Variable123"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// (See alternate version of this example using ExpressionTokens instead of ItemOf)
// In this section underscore, _, and numeric digits
// are accepted as part of alphanumeric tokens
uc.DefineVariable("My_Variable = 111");
cout << uc.Error().Message() << endl;
uc.DefineVariable("Variable123 = 222");
cout << uc.Error().Message() << endl;
cout << uc.ItemOf("_Token_Alphanumeric").Regex() << endl;
cout << uc.EvalStr("My_Variable") << endl;
cout << uc.EvalStr("Variable123") << endl;
cout << "---" << endl;
// Now we no longer want underscore, _, or numeric digits
// to be accepted in alphanumeric tokens; only A-Z
uc.ItemOf("_Token_Alphanumeric").Regex("[a-zA-Z]+");
uc.DefineVariable("Other_Variable = 333");
cout << uc.Error().Message() << endl;
uc.DefineVariable("OtherVariable123 = 444");
cout << uc.Error().Message() << endl;
cout << uc.EvalStr("Other_Variable") << endl;
cout << uc.EvalStr("OtherVariable123 ") << endl;
cout << uc.EvalStr("My_Variable") << endl;
cout << uc.EvalStr("Variable123") << endl;
cout << "---" << endl;
//
// We restore the alphanumeric regex to support _ and numbers again
// Note: My_Variable and Variable123 remained; they were simply inaccessible
// Also: We can't use the commented line below because it has the underscore, _,
// character that we had removed.
// uc.ItemOf("_token_alphanumeric").Regex("[a-zA-Z_][a-zA-Z0-9_]*");
uc.ExpressionTokens()[TokenType::AlphaNumeric].Regex("[a-zA-Z_][a-zA-Z0-9_]*");
cout << uc.EvalStr("My_Variable") << endl;
cout << uc.EvalStr("Variable123") << endl;
}
No error
No error
[a-zA-Z_][a-zA-Z0-9_]*
111
222
---
Invalid definition
Invalid definition
Undefined identifier
Undefined identifier
Undefined identifier
Undefined identifier
---
111
222 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // (See alternate version of this example using ExpressionTokens instead of ItemOf) // In this section underscore, _, and numeric digits // are accepted as part of alphanumeric tokens uc.DefineVariable("My_Variable = 111"); cout << uc.Error().Message() << endl; uc.DefineVariable("Variable123 = 222"); cout << uc.Error().Message() << endl; cout << uc.ItemOf("_Token_Alphanumeric").Regex() << endl; cout << uc.EvalStr("My_Variable") << endl; cout << uc.EvalStr("Variable123") << endl; cout << "---" << endl; // Now we no longer want underscore, _, or numeric digits // to be accepted in alphanumeric tokens; only A-Z uc.ItemOf("_Token_Alphanumeric").Regex("[a-zA-Z]+"); uc.DefineVariable("Other_Variable = 333"); cout << uc.Error().Message() << endl; uc.DefineVariable("OtherVariable123 = 444"); cout << uc.Error().Message() << endl; cout << uc.EvalStr("Other_Variable") << endl; cout << uc.EvalStr("OtherVariable123 ") << endl; cout << uc.EvalStr("My_Variable") << endl; cout << uc.EvalStr("Variable123") << endl; cout << "---" << endl; // // We restore the alphanumeric regex to support _ and numbers again // Note: My_Variable and Variable123 remained; they were simply inaccessible // Also: We can't use the commented line below because it has the underscore, _, // character that we had removed. // uc.ItemOf("_token_alphanumeric").Regex("[a-zA-Z_][a-zA-Z0-9_]*"); uc.ExpressionTokens()[TokenType::AlphaNumeric].Regex("[a-zA-Z_][a-zA-Z0-9_]*"); cout << uc.EvalStr("My_Variable") << endl; cout << uc.EvalStr("Variable123") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// (See alternate version of this example using ExpressionTokens instead of ItemOf)
'// In this section underscore, _, and numeric digits
'// are accepted as part of alphanumeric tokens
uc.DefineVariable("My_Variable = 111")
Console.WriteLine(uc.Error.Message)
uc.DefineVariable("Variable123 = 222")
Console.WriteLine(uc.Error.Message)
Console.WriteLine(uc.ItemOf("_Token_Alphanumeric").Regex)
Console.WriteLine(uc.EvalStr("My_Variable"))
Console.WriteLine(uc.EvalStr("Variable123"))
Console.WriteLine("---")
'// Now we no longer want underscore, _, or numeric digits
'// to be accepted in alphanumeric tokens; only A-Z
uc.ItemOf("_Token_Alphanumeric").Regex = "[a-zA-Z]+"
uc.DefineVariable("Other_Variable = 333")
Console.WriteLine(uc.Error.Message)
uc.DefineVariable("OtherVariable123 = 444")
Console.WriteLine(uc.Error.Message)
Console.WriteLine(uc.EvalStr("Other_Variable"))
Console.WriteLine(uc.EvalStr("OtherVariable123 "))
Console.WriteLine(uc.EvalStr("My_Variable"))
Console.WriteLine(uc.EvalStr("Variable123"))
Console.WriteLine("---")
'//
'// We restore the alphanumeric regex to support _ and numbers again
'// Note: My_Variable and Variable123 remained; they were simply inaccessible
'// Also: We can't use the commented line below because it has the underscore, _,
'// character that we had removed.
'// uc.ItemOf("_token_alphanumeric").Regex("[a-zA-Z_][a-zA-Z0-9_]*");
uc.ExpressionTokens(TokenType.AlphaNumeric).Regex = "[a-zA-Z_][a-zA-Z0-9_]*"
Console.WriteLine(uc.EvalStr("My_Variable"))
Console.WriteLine(uc.EvalStr("Variable123"))
End Sub
End Module
No error
No error
[a-zA-Z_][a-zA-Z0-9_]*
111
222
---
Invalid definition
Invalid definition
Undefined identifier
Undefined identifier
Undefined identifier
Undefined identifier
---
111
222 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// (See alternate version of this example using ExpressionTokens instead of ItemOf) '// In this section underscore, _, and numeric digits '// are accepted as part of alphanumeric tokens uc.DefineVariable("My_Variable = 111") Console.WriteLine(uc.Error.Message) uc.DefineVariable("Variable123 = 222") Console.WriteLine(uc.Error.Message) Console.WriteLine(uc.ItemOf("_Token_Alphanumeric").Regex) Console.WriteLine(uc.EvalStr("My_Variable")) Console.WriteLine(uc.EvalStr("Variable123")) Console.WriteLine("---") '// Now we no longer want underscore, _, or numeric digits '// to be accepted in alphanumeric tokens; only A-Z uc.ItemOf("_Token_Alphanumeric").Regex = "[a-zA-Z]+" uc.DefineVariable("Other_Variable = 333") Console.WriteLine(uc.Error.Message) uc.DefineVariable("OtherVariable123 = 444") Console.WriteLine(uc.Error.Message) Console.WriteLine(uc.EvalStr("Other_Variable")) Console.WriteLine(uc.EvalStr("OtherVariable123 ")) Console.WriteLine(uc.EvalStr("My_Variable")) Console.WriteLine(uc.EvalStr("Variable123")) Console.WriteLine("---") '// '// We restore the alphanumeric regex to support _ and numbers again '// Note: My_Variable and Variable123 remained; they were simply inaccessible '// Also: We can't use the commented line below because it has the underscore, _, '// character that we had removed. '// uc.ItemOf("_token_alphanumeric").Regex("[a-zA-Z_][a-zA-Z0-9_]*"); uc.ExpressionTokens(TokenType.AlphaNumeric).Regex = "[a-zA-Z_][a-zA-Z0-9_]*" Console.WriteLine(uc.EvalStr("My_Variable")) Console.WriteLine(uc.EvalStr("Variable123")) End Sub End Module