A complete syntax highlighter that finds keywords, strings, and comments, and wraps them in pseudo-HTML tags for styling.
ID: 1384
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);
}
<keyword>for</keyword> (i=0; i<10; i++) {
s = <string>"hello"</string>;
<comment>// comment</comment>
} 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 + "<keyword>" + match.Text + "</keyword>"; } else if (tag == TAG_STRING) { highlightedOutput = highlightedOutput + "<string>" + match.Text + "</string>"; } else if (tag == TAG_COMMENT) { highlightedOutput = highlightedOutput + "<comment>" + match.Text + "</comment>"; } 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); }
#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;
}
}
<keyword>for</keyword> (i=0; i<10; i++) {
s = <string>"hello"</string>;
<comment>// comment</comment>
} #include <iostream> #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 + "<keyword>" + match.Text() + "</keyword>"; } else if (tag == TAG_STRING) { highlightedOutput = highlightedOutput + "<string>" + match.Text() + "</string>"; } else if (tag == TAG_COMMENT) { highlightedOutput = highlightedOutput + "<comment>" + match.Text() + "</comment>"; } 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; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
'// 1. Define integer constants for our syntax categories
Dim TAG_KEYWORD = 1
Dim TAG_STRING = 2
Dim 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
Dim sourceCode As String = "for (i=0; i<10; i++) {
s = ""hello"";
// comment
}"
t.Text = sourceCode
t.Find()
'// 4. Build the highlighted output string
Dim highlightedOutput As String = ""
Dim lastPos = 0
For Each 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
Dim tag = match.Rule.Tag
If tag = TAG_KEYWORD Then
highlightedOutput = highlightedOutput + "" + match.Text + " "
ElseIf tag = TAG_STRING Then
highlightedOutput = highlightedOutput + "" + match.Text + " "
ElseIf tag = TAG_COMMENT Then
highlightedOutput = highlightedOutput + "" + match.Text + " "
Else
highlightedOutput = highlightedOutput + match.Text '// No tag, append as-is
End If
'// Update the position for the next iteration
lastPos = match.EndPosition
Next
'// Append any remaining text after the last match
highlightedOutput = highlightedOutput + sourceCode.Substring(lastPos)
Console.WriteLine(highlightedOutput)
End Using
End Sub
End Module
<keyword>for</keyword> (i=0; i<10; i++) {
s = <string>"hello"</string>;
<comment>// comment</comment>
} Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() '// 1. Define integer constants for our syntax categories Dim TAG_KEYWORD = 1 Dim TAG_STRING = 2 Dim 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 Dim sourceCode As String = "for (i=0; i<10; i++) { s = ""hello""; // comment }" t.Text = sourceCode t.Find() '// 4. Build the highlighted output string Dim highlightedOutput As String = "" Dim lastPos = 0 For Each 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 Dim tag = match.Rule.Tag If tag = TAG_KEYWORD Then highlightedOutput = highlightedOutput + "<keyword>" + match.Text + "</keyword>" ElseIf tag = TAG_STRING Then highlightedOutput = highlightedOutput + "<string>" + match.Text + "</string>" ElseIf tag = TAG_COMMENT Then highlightedOutput = highlightedOutput + "<comment>" + match.Text + "</comment>" Else highlightedOutput = highlightedOutput + match.Text '// No tag, append as-is End If '// Update the position for the next iteration lastPos = match.EndPosition Next '// Append any remaining text after the last match highlightedOutput = highlightedOutput + sourceCode.Substring(lastPos) Console.WriteLine(highlightedOutput) End Using End Sub End Module