public static TableSpec Parse(StringScanner p)
{
// Leading line space allowed
p.SkipLinespace();
// Quick check for typical case
if (p.Current != '|' && p.Current != ':' && p.Current != '-')
return null;
// Don't create the spec until it at least looks like one
TableSpec spec = null;
// Leading bar, looks like a table spec
if (p.SkipChar('|'))
{
spec=new TableSpec();
spec.LeadingBar=true;
}
// Process all columns
while (true)
{
// Parse column spec
p.SkipLinespace();
// Must have something in the spec
if (p.Current == '|')
return null;
bool AlignLeft = p.SkipChar(':');
while (p.Current == '-')
p.SkipForward(1);
bool AlignRight = p.SkipChar(':');
p.SkipLinespace();
// Work out column alignment
ColumnAlignment col = ColumnAlignment.NA;
if (AlignLeft && AlignRight)
col = ColumnAlignment.Center;
else if (AlignLeft)
col = ColumnAlignment.Left;
else if (AlignRight)
col = ColumnAlignment.Right;
if (p.Eol)
{
// Not a spec?
if (spec == null)
return null;
// Add the final spec?
spec.Columns.Add(col);
return spec;
}
// We expect a vertical bar
if (!p.SkipChar('|'))
return null;
// Create the table spec
if (spec==null)
spec=new TableSpec();
// Add the column
spec.Columns.Add(col);
// Check for trailing vertical bar
p.SkipLinespace();
if (p.Eol)
{
spec.TrailingBar = true;
return spec;
}
// Next column
}
}