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