uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

Nested Patterns

Product: 

Class: 

Remarks

uCalc allows you to group segments of a pattern using curly braces { ... }. This is primarily used to delimit the scope of Alternative Parts (|) or to create complex structures inside variables.

Recursive Nesting:Patterns can be nested arbitrarily deep. For example:

  • An Alternation { A | B } can contain an Optional part [ ... ].
  • That Optional part can contain another Alternation or group.
  • This flexibility allows you to model complex grammar structures like Menu { Open [ Recent { File | Project } ] | Save }.

Syntax:

  • Anonymous Grouping: Start { OptionA | OptionB } End
    • The braces define where the alternation begins and ends.
  • Named Grouping (Capture): {name: OptionA | OptionB }
    • Matches the pattern inside and captures the result into the variable {name}.

Ambiguity Resolution:Since {name} denotes a variable and { pattern } denotes a group, uCalc distinguishes them based on content:

  1. Variable: A simple name (e.g., {x}).
  2. Group: Contains structure like alternatives | or multiple tokens (e.g., { A | B }).

Comparative Analysis: Why uCalc?

  • vs. Regex Groups (...):
    • Consistency: Regex uses parentheses () for grouping and capturing. uCalc uses curly braces {} for everything—variables, groups, and blocks. This creates a unified syntax where "everything dynamic is in braces."
    • Non-Capturing by Default: In Regex, (A|B) captures automatically (Group 1). You have to use (?:A|B) to avoid capturing. In uCalc, { A | B } is purely structural (non-capturing) unless you explicitly name it {val: A | B}. This avoids "index shifting" bugs.
  • vs. EBNF:
    • Familiarity: uCalc's syntax is very close to EBNF (Extended Backus-Naur Form), making it intuitive for developers familiar with language grammar definitions.

Examples

Grouping alternatives to limit scope.
				
					using uCalcSoftware;

var uc = new uCalc();
var t = uc.NewTransformer();
// Matches "File Open" or "File Close".
// Capture needs explicit naming.
t.FromTo("File {cmd: Open | Close }", "Command: {cmd}");

Console.WriteLine(t.Transform("File Open"));
Console.WriteLine(t.Transform("File Close"));
				
			
Command: Open
Command: Close
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto t = uc.NewTransformer();
   // Matches "File Open" or "File Close".
   // Capture needs explicit naming.
   t.FromTo("File {cmd: Open | Close }", "Command: {cmd}");

   cout << t.Transform("File Open") << endl;
   cout << t.Transform("File Close") << endl;
}
				
			
Command: Open
Command: Close
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim t = uc.NewTransformer()
      '// Matches "File Open" or "File Close". 
      '// Capture needs explicit naming.
      t.FromTo("File {cmd: Open | Close }", "Command: {cmd}")
      
      Console.WriteLine(t.Transform("File Open"))
      Console.WriteLine(t.Transform("File Close"))
   End Sub
End Module
				
			
Command: Open
Command: Close
Nested grouping for complex commands.
				
					using uCalcSoftware;

var uc = new uCalc();
var t = uc.NewTransformer();
// Pattern: "Set" followed by (Color OR (Size followed by Big/Small))
t.FromTo("Set {prop: Color | Size { Big | Small } }", "Property: {prop}");

Console.WriteLine(t.Transform("Set Color"));
Console.WriteLine(t.Transform("Set Size Big"));
Console.WriteLine(t.Transform("Set Size Small"));
				
			
Property: Color
Property: Size Big
Property: Size Small
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto t = uc.NewTransformer();
   // Pattern: "Set" followed by (Color OR (Size followed by Big/Small))
   t.FromTo("Set {prop: Color | Size { Big | Small } }", "Property: {prop}");

   cout << t.Transform("Set Color") << endl;
   cout << t.Transform("Set Size Big") << endl;
   cout << t.Transform("Set Size Small") << endl;
}
				
			
Property: Color
Property: Size Big
Property: Size Small
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim t = uc.NewTransformer()
      '// Pattern: "Set" followed by (Color OR (Size followed by Big/Small))
      t.FromTo("Set {prop: Color | Size { Big | Small } }", "Property: {prop}")
      
      Console.WriteLine(t.Transform("Set Color"))
      Console.WriteLine(t.Transform("Set Size Big"))
      Console.WriteLine(t.Transform("Set Size Small"))
   End Sub
End Module
				
			
Property: Color
Property: Size Big
Property: Size Small
Recursive Nesting: Alternation -> Optional -> Alternation.
				
					using uCalcSoftware;

var uc = new uCalc();
var t = uc.NewTransformer();
// Structure:
// 1. Alternation: "Open" OR "Save"
// 2. Inside "Open": Optional "Recent"
// 3. Inside "Recent": Alternation "File" OR "Project"
var pattern = "Menu { Open [ Recent {type: File | Project } ] | Save }";

t.FromTo(pattern, "Action Detected");

Console.WriteLine(t.Transform("Menu Save"));                 // Match (Simple Alternation)
Console.WriteLine(t.Transform("Menu Open"));                 // Match (Optional omitted)
Console.WriteLine(t.Transform("Menu Open Recent File"));     // Match (Deep nesting)
Console.WriteLine(t.Transform("Menu Open Recent Project"));  // Match (Deep nesting alt)
				
			
Action Detected
Action Detected
Action Detected
Action Detected
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto t = uc.NewTransformer();
   // Structure:
   // 1. Alternation: "Open" OR "Save"
   // 2. Inside "Open": Optional "Recent"
   // 3. Inside "Recent": Alternation "File" OR "Project"
   auto pattern = "Menu { Open [ Recent {type: File | Project } ] | Save }";

   t.FromTo(pattern, "Action Detected");

   cout << t.Transform("Menu Save") << endl;                 // Match (Simple Alternation)
   cout << t.Transform("Menu Open") << endl;                 // Match (Optional omitted)
   cout << t.Transform("Menu Open Recent File") << endl;     // Match (Deep nesting)
   cout << t.Transform("Menu Open Recent Project") << endl;  // Match (Deep nesting alt)
}
				
			
Action Detected
Action Detected
Action Detected
Action Detected
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim t = uc.NewTransformer()
      '// Structure:
      '// 1. Alternation: "Open" OR "Save"
      '// 2. Inside "Open": Optional "Recent"
      '// 3. Inside "Recent": Alternation "File" OR "Project"
      Dim pattern = "Menu { Open [ Recent {type: File | Project } ] | Save }"
      
      t.FromTo(pattern, "Action Detected")
      
      Console.WriteLine(t.Transform("Menu Save"))                 '// Match (Simple Alternation)
      Console.WriteLine(t.Transform("Menu Open"))                 '// Match (Optional omitted)
      Console.WriteLine(t.Transform("Menu Open Recent File"))     '// Match (Deep nesting)
      Console.WriteLine(t.Transform("Menu Open Recent Project"))  '// Match (Deep nesting alt)
   End Sub
End Module
				
			
Action Detected
Action Detected
Action Detected
Action Detected