A practical example using tags to build a simple syntax highlighter that categorizes matches.
ID: 995
See: Tag = [int]
using uCalcSoftware;
var uc = new uCalc();
// Practical: Basic Syntax Highlighter
var t = new uCalc.Transformer();
// Define categories with integer tags
var TAG_KEYWORD = 1;
var TAG_STRING = 2;
var TAG_COMMENT = 3;
// Define 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);
t.Text = """
for (i=0; i<10; i++) { s = "hello"; // comment }
""";
t.Find();
foreach(var match in t.Matches) {
var tag = match.Rule.Tag;
if (tag == TAG_KEYWORD) {
Console.WriteLine($"TAG_KEYWORD: {match.Text}");
} else if (tag == TAG_STRING) {
Console.WriteLine($"TAG_STRING: {match.Text}");
} else if (tag == TAG_COMMENT) {
Console.WriteLine($"TAG_COMMENT: {match.Text}");
}
}
TAG_KEYWORD: for
TAG_STRING: "hello"
TAG_COMMENT: // comment using uCalcSoftware; var uc = new uCalc(); // Practical: Basic Syntax Highlighter var t = new uCalc.Transformer(); // Define categories with integer tags var TAG_KEYWORD = 1; var TAG_STRING = 2; var TAG_COMMENT = 3; // Define 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); t.Text = """ for (i=0; i<10; i++) { s = "hello"; // comment } """; t.Find(); foreach(var match in t.Matches) { var tag = match.Rule.Tag; if (tag == TAG_KEYWORD) { Console.WriteLine($"TAG_KEYWORD: {match.Text}"); } else if (tag == TAG_STRING) { Console.WriteLine($"TAG_STRING: {match.Text}"); } else if (tag == TAG_COMMENT) { Console.WriteLine($"TAG_COMMENT: {match.Text}"); } }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Practical: Basic Syntax Highlighter
uCalc::Transformer t;
// Define categories with integer tags
auto TAG_KEYWORD = 1;
auto TAG_STRING = 2;
auto TAG_COMMENT = 3;
// Define 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);
t.Text(R"(for (i=0; i<10; i++) { s = "hello"; // comment })");
t.Find();
for(auto match : t.Matches()) {
auto tag = match.Rule().Tag();
if (tag == TAG_KEYWORD) {
cout << "TAG_KEYWORD: " << match.Text() << endl;
} else if (tag == TAG_STRING) {
cout << "TAG_STRING: " << match.Text() << endl;
} else if (tag == TAG_COMMENT) {
cout << "TAG_COMMENT: " << match.Text() << endl;
}
}
}
TAG_KEYWORD: for
TAG_STRING: "hello"
TAG_COMMENT: // comment #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Practical: Basic Syntax Highlighter uCalc::Transformer t; // Define categories with integer tags auto TAG_KEYWORD = 1; auto TAG_STRING = 2; auto TAG_COMMENT = 3; // Define 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); t.Text(R"(for (i=0; i<10; i++) { s = "hello"; // comment })"); t.Find(); for(auto match : t.Matches()) { auto tag = match.Rule().Tag(); if (tag == TAG_KEYWORD) { cout << "TAG_KEYWORD: " << match.Text() << endl; } else if (tag == TAG_STRING) { cout << "TAG_STRING: " << match.Text() << endl; } else if (tag == TAG_COMMENT) { cout << "TAG_COMMENT: " << match.Text() << endl; } } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Practical: Basic Syntax Highlighter
Dim t As New uCalc.Transformer()
'// Define categories with integer tags
Dim TAG_KEYWORD = 1
Dim TAG_STRING = 2
Dim TAG_COMMENT = 3
'// Define 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)
t.Text = "for (i=0; i<10; i++) { s = ""hello""; // comment }"
t.Find()
For Each match In t.Matches
Dim tag = match.Rule.Tag
If tag = TAG_KEYWORD Then
Console.WriteLine($"TAG_KEYWORD: {match.Text}")
ElseIf tag = TAG_STRING Then
Console.WriteLine($"TAG_STRING: {match.Text}")
ElseIf tag = TAG_COMMENT Then
Console.WriteLine($"TAG_COMMENT: {match.Text}")
End If
Next
End Sub
End Module
TAG_KEYWORD: for
TAG_STRING: "hello"
TAG_COMMENT: // comment Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Practical: Basic Syntax Highlighter Dim t As New uCalc.Transformer() '// Define categories with integer tags Dim TAG_KEYWORD = 1 Dim TAG_STRING = 2 Dim TAG_COMMENT = 3 '// Define 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) t.Text = "for (i=0; i<10; i++) { s = ""hello""; // comment }" t.Find() For Each match In t.Matches Dim tag = match.Rule.Tag If tag = TAG_KEYWORD Then Console.WriteLine($"TAG_KEYWORD: {match.Text}") ElseIf tag = TAG_STRING Then Console.WriteLine($"TAG_STRING: {match.Text}") ElseIf tag = TAG_COMMENT Then Console.WriteLine($"TAG_COMMENT: {match.Text}") End If Next End Sub End Module