Back to API Reference
Class

ParseRule

abstract
Inherits:RangeSource

Base class for text parsing rules that identify modifier application ranges.

Remarks

Parse rules scan text for markup (XML tags, Markdown, custom markers) and produce ParsedRange entries that specify where modifiers should be applied. Rules are matched in priority order (highest first). Use higher priority for explicit markup rules (tags, Markdown) and lower priority for auto-detection rules (raw URLs).

Derived Types(15)

Types that inherit from ParseRule.

C
BackslashEscapeRule
Escapes a single ASCII punctuation character preceded by \. Example: \* becomes a literal * and is protected from any other parse rule.
C
CodeSpanRule
Protects content inside backtick-delimited code spans (e.g. `x`, ``x``, ```x```) from being processed by other parse rules. Follows CommonMark §6.1 balanced-run semantics: an N-backtick run is closed by the next N-backtick run of the same length.
C
CompositeParseRule
Combines multiple parse rules into a single rule.
C
LineBreakParseRule
Replaces the void <br> tag with a soft line break (U+2028 LINE SEPARATOR): the line wraps but stays in the same paragraph, keeping the surrounding direction and taking no inter-paragraph spacing — like HTML <br> or a word-processor Shift+Enter. A paragraph break is a literal newline (Enter) instead. Matches <br>, <br/> and <br /> case-insensitively with HTML void-element semantics: no closing tag, no nested content.
C
MarkdownLinkParseRule
Parses Markdown-style links: [link text](https://example.com).
C
MarkdownListParseRule
Parses Markdown-style lists (bulleted and numbered).
C
MarkdownWrapRule
Parses symmetric open/close markers in text (e.g., **text**, ~~text~~).
C
MathParseRule
Replaces <math>...</math> with one layout placeholder while preserving the enclosed formula as the modifier parameter.
C
NoparseTagRule
Protects content inside <noparse>...</noparse> from being processed by any other parse rule. The markers themselves are stripped; the content between them appears verbatim in the output.
C
RawUrlParseRule
Auto-detects URLs in plain text and converts them to clickable links.
C
RubyParseRule
Parses ruby (furigana) markup into base + reading ranges for [[RubyModifier]].
C
SeparatorParseRule
Replaces the void separator tag (default <sep>) with the configured separator string and marks the inserted range for a paired [[SeparatorModifier]].
C
StringParseRule
Matches literal string patterns and optionally replaces them.
C
TagParseRule
Base class for parsing XML-style markup tags (e.g., <b>, <color=#FF0000>).
C
TriggerWordParseRule
Auto-detects <trigger>word tokens in plain text (mentions, hashtags) and produces a style-only range per token whose parameter is the word without the trigger.
public int Priority{ get }

Gets the matching priority. Higher values are matched first. Default is 0. Use positive values for explicit markup, negative for auto-detection.

public char EscapePrefix{ get }

The literal-escape capability: a rule that consumes an escape prefix to protect the following character from markup interpretation returns that prefix; '\0' (default) = the rule provides no escaping. Consumers (literal-paste serialization, escaping injection) check this contract instead of naming a concrete rule type, so a custom escaping rule participates by overriding it.

public bool ProvidesLiteralEscape{ get }

Whether this rule provides literal escaping — see EscapePrefix.

public bool CanWrap{ get }

Whether Apply emits a content-wrapping form — i.e. the rule can express an inline style application. Rules that override Apply to return syntax must also override this; consumers gate on it instead of probing Apply with empty input.

public bool IsStandalone{ get }

Indicates whether this rule operates without a modifier (e.g., protection rules like noparse). When, the rule can be registered via UniText.RegisterRule without pairing it with a BaseModifier.

public string SourceToken{ get }

The source-markup token this rule's syntax is externally known by — whatever form the syntax takes (color for a tag rule, a marker for a marker rule) — or for a markerless rule. Interop consumers (clipboard bindings) pair rules with format schemas by this token instead of testing syntax families.

public string Identity{ get }

Stable identity shared by rule instances that match the same syntax, compared case-insensitively — pairs separately configured instances (style merging, ByRule chrome selectors). The default (full type name) is correct for configuration-free rules. A rule whose instances can be configured to match different syntaxes MUST qualify it (TagRule → "tag:" + name, marker rule → "marker:" + marker) or return (identity by reference only) — otherwise differently configured instances are falsely treated as the same rule. Called on hot paths — cache the string, don't rebuild it per access.

public string MarkupTriggers{ get }

Characters of which at least one must start any match of this rule that alters the visible text — strips markup (tags, markers), replaces it, or inserts content. Empty = the rule never consumes text (style-only matches like raw-URL detection); (default) = unknown, callers must assume any text can be consumed. Literal-paste escaping protects exactly these characters, so under-reporting silently mutates pasted text.

public string TypingTriggers{ get }

Characters whose insertion or removal can make this rule begin or complete a consuming markup match. The editable document parses typed syntax only when an edit contains one of these characters or touches its immediate boundary; empty means this rule has no source syntax, while conservatively parses every direct typing edit. Include closing delimiters and required separators, not only match starts.

public string ScanTriggers{ get }

Characters of which at least one must be at the match position for TryMatch to succeed at all (consuming or style-only). Defaults to MarkupTriggers — override when the rule matches at characters beyond its consuming set (raw-URL scheme letters). The parser bakes the union into a jump table and skips runs of non-trigger text, so an accurate set here is a large parse speedup; disables the fast scan for the whole component.

public virtual bool IsEscapable()

Whether EscapePrefix can protect c — the escapable set of this rule's grammar. Meaningful only when ProvidesLiteralEscape.

public static string MarkupTriggerUnion()

Deduplicated union of the rules' MarkupTriggers, or when any rule reports unknown — shared by the parser and CompositeParseRule to advertise the combined trigger set. Null rules are skipped.

public static string ScanTriggerUnion()

Deduplicated union of the rules' ScanTriggers; null when any rule reports unknown.

public static string TypingTriggerUnion()

Deduplicated union of the rules' TypingTriggers; null when every direct typing edit must be considered.

protected static string CachedIdentity()

Identity builder for configurable rules: returns prefix + name cached against the name's string instance, so hot-path Identity reads allocate only when the configured name actually changes. Null/empty name → identity.

public abstract int TryMatch(ReadOnlySpan<char> text, int index, PooledList<ParsedRange> results)

Attempts to match a pattern starting at the specified index.

public virtual void Finalize()

Called after parsing completes to finalize any pending ranges (e.g., unclosed tags).

public virtual void PostParse()

Called after tag stripping to add ranges in clean-text space.

public virtual bool IsCompleteMatch()

Whether a matched occurrence is complete enough to become persistent document markup.

public virtual void Reset()

Resets the rule state for a new parse operation.

public virtual string Apply()

Emits this rule's source syntax wrapping content — the inverse of parsing, used by the editing layer to apply a style and to reconstruct copied markup. Returns when the rule has no content-wrapping form (void, block, or composite rules), meaning it cannot be applied as an inline style.

See Also

TagParseRuleAttributeParser