using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { // 1. Define integer constants for our syntax categories var TAG_KEYWORD = 1; var TAG_STRING = 2; var 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 = """ for (i=0; i<10; i++) { s = "hello"; // comment } """; t.Text = sourceCode; t.Find(); // 4. Build the highlighted output string string highlightedOutput = ""; var lastPos = 0; foreach(var match in t.Matches) { // Append the plain text between the last match and this one highlightedOutput = highlightedOutput + sourceCode.Substring(lastPos, match.StartPosition - lastPos); // Get the tag and wrap the matched text accordingly var 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.Substring(lastPos); Console.WriteLine(highlightedOutput); }