#include #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope // 1. Define integer constants for our syntax categories auto TAG_KEYWORD = 1; auto TAG_STRING = 2; auto TAG_COMMENT = 3; // 2. Define the transformation rules and tag them t.Pattern("{ if | else | for | while }").SetTag(TAG_KEYWORD); t.Pattern("{@String}").SetTag(TAG_STRING); t.Pattern("// {text}").SetTag(TAG_COMMENT); // 3. Set the source code and run the find operation string sourceCode = R"(for (i=0; i<10; i++) { s = "hello"; // comment })"; t.Text(sourceCode); t.Find(); // 4. Build the highlighted output string string highlightedOutput = ""; auto lastPos = 0; for(auto match : t.Matches()) { // Append the plain text between the last match and this one highlightedOutput = highlightedOutput + sourceCode.substr(lastPos, match.StartPosition() - lastPos); // Get the tag and wrap the matched text accordingly auto tag = match.Rule().Tag(); if (tag == TAG_KEYWORD) { highlightedOutput = highlightedOutput + "" + match.Text() + ""; } else if (tag == TAG_STRING) { highlightedOutput = highlightedOutput + "" + match.Text() + ""; } else if (tag == TAG_COMMENT) { highlightedOutput = highlightedOutput + "" + match.Text() + ""; } else { highlightedOutput = highlightedOutput + match.Text(); // No tag, append as-is } // Update the position for the next iteration lastPos = match.EndPosition(); } // Append any remaining text after the last match highlightedOutput = highlightedOutput + sourceCode.substr(lastPos); cout << highlightedOutput << endl; } }