A practical example using Clone() to create a specialized parser from a base template, demonstrating rule inheritance.

ID: 1055

See: Clone
				
					using uCalcSoftware;

var uc = new uCalc();
// 1. Create a "base" HTML transformer template
var baseHtmlParser = new uCalc.Transformer();
baseHtmlParser.Description = "Base HTML Parser";
// Rule to skip over comments
baseHtmlParser.SkipOver("<!--{body}->");
// Rule to find any tag
baseHtmlParser.Pattern("<{tag}>");
baseHtmlParser.DefaultRuleSet.SetStatementSensitive(false);

// 2. Create a specialized clone to find only image tags
var imageParser = baseHtmlParser.Clone();
imageParser.Description = "Image Tag Finder";
imageParser.FromTo("<img {attribs} />", "FOUND_IMG_TAG");

string html = " <body> <img src='a.jpg' /> <!-- <img src='b.jpg' /> --> </body> ";

// The clone inherits the SkipOver rule from the base, so the commented img tag is ignored.
Console.WriteLine(imageParser.Transform(html));

imageParser.Release();
baseHtmlParser.Release();
				
			
 <body> FOUND_IMG_TAG <!-- <img src='b.jpg' /> --> </body> 
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // 1. Create a "base" HTML transformer template
   uCalc::Transformer baseHtmlParser;
   baseHtmlParser.Description("Base HTML Parser");
   // Rule to skip over comments
   baseHtmlParser.SkipOver("<!--{body}->");
   // Rule to find any tag
   baseHtmlParser.Pattern("<{tag}>");
   baseHtmlParser.DefaultRuleSet().SetStatementSensitive(false);

   // 2. Create a specialized clone to find only image tags
   auto imageParser = baseHtmlParser.Clone();
   imageParser.Description("Image Tag Finder");
   imageParser.FromTo("<img {attribs} />", "FOUND_IMG_TAG");

   string html = " <body> <img src='a.jpg' /> <!-- <img src='b.jpg' /> --> </body> ";

   // The clone inherits the SkipOver rule from the base, so the commented img tag is ignored.
   cout << imageParser.Transform(html) << endl;

   imageParser.Release();
   baseHtmlParser.Release();
}
				
			
 <body> FOUND_IMG_TAG <!-- <img src='b.jpg' /> --> </body> 
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// 1. Create a "base" HTML transformer template
      Dim baseHtmlParser As New uCalc.Transformer()
      baseHtmlParser.Description = "Base HTML Parser"
      '// Rule to skip over comments
      baseHtmlParser.SkipOver("<!--{body}->")
      '// Rule to find any tag
      baseHtmlParser.Pattern("<{tag}>")
      baseHtmlParser.DefaultRuleSet.SetStatementSensitive(false)
      
      '// 2. Create a specialized clone to find only image tags
      Dim imageParser = baseHtmlParser.Clone()
      imageParser.Description = "Image Tag Finder"
      imageParser.FromTo("<img {attribs} />", "FOUND_IMG_TAG")
      
      Dim html As String = " <body> <img src='a.jpg' /> <!-- <img src='b.jpg' /> --> </body> "
      
      '// The clone inherits the SkipOver rule from the base, so the commented img tag is ignored.
      Console.WriteLine(imageParser.Transform(html))
      
      imageParser.Release()
      baseHtmlParser.Release()
   End Sub
End Module
				
			
 <body> FOUND_IMG_TAG <!-- <img src='b.jpg' /> --> </body>