Fragments

References
public sealed class Fragments : IEnumerable<Fragment>
A string split into categorised fragments, plus the views derived from it.

Scanning happens once, on creation, and never fails. Problems such as an unterminated literal or an unmatched bracket are recorded on the fragment they belong to, so that half typed input still yields a complete and navigable result. Call Validate() when well formed input is required.

Every view is pinned to the same Source string and every fragment reports absolute indices into it, so error positions remain valid no matter how deeply a view is derived.

Properties

Body

#
public Fragment[] Body { get; }
Gets all fragments in this view except the two terminals. This is what interpreters pattern match against. Do not modify the returned array.

Count

#
public int Count { get; }
Gets the number of fragments in this view, including the two terminals.

First

#
public Fragment First { get; }
Gets the first fragment which is neither a terminal nor trivia, or the tail marker when there is no such fragment.

InnerContent

#
public Fragments InnerContent { get; }
Gets this view without any enclosing bracket pairs. The cleanup is repeated, so that "([5])" yields "5". Returns this view when the body is not wrapped in a bracket pair.

Options

#
public Fragmentation Options { get; }
Gets the choices this view was scanned with. Views inherit the options of the collection they were derived from, since they describe the same characters.

OuterBrackets

#
public BracketFragment OuterBrackets { get; }
Gets the opening bracket of the innermost pair removed by InnerContent, or null when this view is not wrapped. Access the corresponding closing bracket via BracketFragment.Partner.

Reduced

#
public Fragments Reduced { get; }
Gets a view without whitespace or comments. Line breaks are retained.

Source

#
public string Source { get; }
Gets the string these fragments were scanned from. This is the complete string in every view, never a substring of it.

Tail

#
public Fragment Tail { get; }
Gets the marker at the end of this view.

Indexers

this[]

#
public Fragment this[int index] { get; }
Gets the fragment at the given index.
index (int)

Methods

After

#
public Fragments After(Fragment fragment)
Create a view of everything following the given fragment.
fragment (Fragment)

Before

#
public Fragments Before(Fragment fragment)
Create a view of everything preceding the given fragment.
fragment (Fragment)

BreakLines

#
public Fragments[] BreakLines()
Create one view per line. The line breaks themselves are omitted; they are the delimiters. A bracket pair split across two lines loses track of itself, and both sides will report a null partner.

Content

#
public string Content()
Gets the source text from the first meaningful fragment of this view to the last, which is Coverage() without the whitespace and comments around the outside of it.

A view holding nothing but trivia yields an empty string, which is how a piece of a split can say it carries no content at all.

Coverage

#
public string Coverage()
Gets the characters spanned by this view. Unlike Source, which is always the complete string, this covers only the extent between the two terminals.

Create

#
public static Fragments Create(string source, Fragmentation options = Fragmentation.Default)
Split a string into fragments.
source (string)String to split. Null is treated as empty.
options (Fragmentation = Fragmentation.Default)Choices which change what the characters mean.

Find

#
public Fragment[] Find(Func<Fragment, bool> predicate)
Find every fragment matching the predicate which sits at the top level of this view, that is, inside none of its bracket pairs.
predicate (Func<Fragment, bool>)Test to apply to each fragment.

GetEnumerator

#
public IEnumerator<Fragment> GetEnumerator()
Enumerate every fragment in this view, terminals included.

Segment

#
public Fragments Segment(Fragment first, Fragment last)
Create a view of a run of this view's fragments, both ends included.
first (Fragment)First fragment to include.
last (Fragment)Last fragment to include. Must not precede the first.

Split

#
public Fragments[] Split(char symbol)
Cut this view into pieces at every top level occurrence of a symbol. The separators themselves are omitted, and a view without any separators yields a single piece, so the result always holds one more piece than there were separators.
symbol (char)Separator character.
Explicit

IEnumerable.GetEnumerator

#
IEnumerator IEnumerable.GetEnumerator()

ToString

#
public override string ToString()
Gets a description of this view, for debugging purposes.
Object.ToString

Validate

#
public IResult Validate()
Report the first defect in this view, if there is one.

Without

#
public Fragments Without(FragmentKind mask)
Create a view without the fragments matching the given mask.
mask (FragmentKind)One or more kinds to omit.