Files
lmxopcua/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Abstractions/PassthroughScript.cs
T

37 lines
1.9 KiB
C#

namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
using System.Text.RegularExpressions;
/// <summary>
/// Classifies the trivial "mirror" VirtualTag script shape <c>return ctx.GetTag("X").Value;</c>,
/// which can be evaluated by returning the dependency value directly — no Roslyn compilation.
/// Narrow, exact pattern: any near-miss returns false and falls through to the Roslyn path.
/// </summary>
/// <remarks>
/// Physically defined in the Core.Scripting.Abstractions assembly (Roslyn-free, so ControlPlane
/// can reference it); the namespace is Core.Scripting to keep consumer using-directives unchanged.
/// </remarks>
public static partial class PassthroughScript
{
// ^ \s* return \s+ ctx . GetTag ( "X" ) . Value ; \s* $ (whitespace-tolerant around tokens)
// Tag-name class [^"\\] excludes both the closing quote and backslash: a literal containing a
// backslash escape (e.g. "a\\b" → runtime name a\b) won't match, so it correctly falls through
// to Roslyn, which interprets the escape and resolves the actual dependency key.
[GeneratedRegex(@"^\s*return\s+ctx\s*\.\s*GetTag\s*\(\s*""([^""\\]+)""\s*\)\s*\.\s*Value\s*;\s*$")]
private static partial Regex MirrorRegex();
/// <summary>True if <paramref name="source"/> is the mirror passthrough shape; outputs the referenced tag.</summary>
/// <param name="source">The VirtualTag script source to classify.</param>
/// <param name="tagRef">On success, the tag reference captured from the mirror shape; otherwise empty.</param>
/// <returns><see langword="true"/> if the source is the mirror passthrough shape; otherwise <see langword="false"/>.</returns>
public static bool TryMatch(string? source, out string tagRef)
{
tagRef = string.Empty;
if (string.IsNullOrEmpty(source)) return false;
var m = MirrorRegex().Match(source);
if (!m.Success) return false;
tagRef = m.Groups[1].Value;
return true;
}
}