public void Tests() { var p = new StringScanner(); p.Reset("This is a string with something [bracketed]"); Assert.IsTrue(p.Bof); Assert.IsFalse(p.Eof); Assert.IsTrue(p.SkipString("This")); Assert.IsFalse(p.Bof); Assert.IsFalse(p.Eof); Assert.IsFalse(p.SkipString("huh?")); Assert.IsTrue(p.SkipLinespace()); Assert.IsTrue(p.SkipChar('i')); Assert.IsTrue(p.SkipChar('s')); Assert.IsTrue(p.SkipWhitespace()); Assert.IsTrue(p.DoesMatchAny(new char[] { 'r', 'a', 't'} )); Assert.IsFalse(p.Find("Not here")); Assert.IsFalse(p.Find("WITH")); Assert.IsFalse(p.FindI("Not here")); Assert.IsTrue(p.FindI("WITH")); Assert.IsTrue(p.Find('[')); p.SkipForward(1); p.Mark(); Assert.IsTrue(p.Find(']')); Assert.AreEqual("bracketed", p.Extract()); Assert.IsTrue(p.SkipChar(']')); Assert.IsTrue(p.Eof); }
/// <summary>Parse row.</summary> /// /// <param name="p">The StringScanner to process.</param> /// /// <returns>A List<string></returns> public List <string> ParseRow(StringScanner p) { p.SkipLinespace(); if (p.eol) { return(null); // Blank line ends the table } bool bAnyBars = LeadingBar; if (LeadingBar && !p.SkipChar('|')) { return(null); } // Create the row List <string> row = new List <string>(); // Parse all columns except the last while (!p.eol) { // Find the next vertical bar p.Mark(); while (!p.eol && p.current != '|') { p.SkipForward(1); } row.Add(p.Extract().Trim()); bAnyBars |= p.SkipChar('|'); } // Require at least one bar to continue the table if (!bAnyBars) { return(null); } // Add missing columns while (row.Count < Columns.Count) { row.Add(" "); } p.SkipEol(); return(row); }
public List<string> ParseRow(StringScanner p) { p.SkipLinespace(); if (p.eol) return null; // Blank line ends the table bool bAnyBars=LeadingBar; if (LeadingBar && !p.SkipChar('|')) { return null; } // Create the row List<string> row = new List<string>(); // Parse all columns except the last while (!p.eol) { // Find the next vertical bar p.Mark(); while (!p.eol && p.current != '|') p.SkipForward(1); row.Add(p.Extract().Trim()); bAnyBars|=p.SkipChar('|'); } // Require at least one bar to continue the table if (!bAnyBars) return null; // Add missing columns while (row.Count < Columns.Count) { row.Add(" "); } p.SkipEol(); return row; }
internal static LinkDefinition ParseLinkDefinitionInternal(StringScanner p, bool ExtraMode) { // Skip leading white space p.SkipWhitespace(); // Must start with an opening square bracket if (!p.SkipChar('[')) { return(null); } // Extract the id p.Mark(); if (!p.Find(']')) { return(null); } string id = p.Extract(); if (id.Length == 0) { return(null); } if (!p.SkipString("]:")) { return(null); } // Parse the url and title var link = ParseLinkTarget(p, id, ExtraMode); // and trailing whitespace p.SkipLinespace(); // Trailing crap, not a valid link reference... if (!p.eol) { return(null); } return(link); }
// Parse just the link target // For reference link definition, this is the bit after "[id]: thisbit" // For inline link, this is the bit in the parens: [link text](thisbit) internal static LinkDefinition ParseLinkTarget(StringScanner p, string id, bool ExtraMode) { // Skip whitespace p.SkipWhitespace(); // End of string? if (p.eol) return null; // Create the link definition var r = new LinkDefinition(id); // Is the url enclosed in angle brackets if (p.SkipChar('<')) { // Extract the url p.Mark(); // Find end of the url while (p.current != '>') { if (p.eof) return null; p.SkipEscapableChar(ExtraMode); } string url = p.Extract(); if (!p.SkipChar('>')) return null; // Unescape it r.url = Utils.UnescapeString(url.Trim(), ExtraMode); // Skip whitespace p.SkipWhitespace(); } else { // Find end of the url p.Mark(); int paren_depth = 1; while (!p.eol) { char ch=p.current; if (char.IsWhiteSpace(ch)) break; if (id == null) { if (ch == '(') paren_depth++; else if (ch == ')') { paren_depth--; if (paren_depth==0) break; } } p.SkipEscapableChar(ExtraMode); } r.url = Utils.UnescapeString(p.Extract().Trim(), ExtraMode); } p.SkipLinespace(); // End of inline target if (p.DoesMatch(')')) return r; bool bOnNewLine = p.eol; int posLineEnd = p.position; if (p.eol) { p.SkipEol(); p.SkipLinespace(); } // Work out what the title is delimited with char delim; switch (p.current) { case '\'': case '\"': delim = p.current; break; case '(': delim = ')'; break; default: if (bOnNewLine) { p.position = posLineEnd; return r; } else return null; } // Skip the opening title delimiter p.SkipForward(1); // Find the end of the title p.Mark(); while (true) { if (p.eol) return null; if (p.current == delim) { if (delim != ')') { int savepos = p.position; // Check for embedded quotes in title // Skip the quote and any trailing whitespace p.SkipForward(1); p.SkipLinespace(); // Next we expect either the end of the line for a link definition // or the close bracket for an inline link if ((id == null && p.current != ')') || (id != null && !p.eol)) { continue; } p.position = savepos; } // End of title break; } p.SkipEscapableChar(ExtraMode); } // Store the title r.title = Utils.UnescapeString(p.Extract(), ExtraMode); // Skip closing quote p.SkipForward(1); // Done! return r; }
internal static LinkDefinition ParseLinkDefinitionInternal(StringScanner p, bool ExtraMode) { // Skip leading white space p.SkipWhitespace(); // Must start with an opening square bracket if (!p.SkipChar('[')) return null; // Extract the id p.Mark(); if (!p.Find(']')) return null; string id = p.Extract(); if (id.Length == 0) return null; if (!p.SkipString("]:")) return null; // Parse the url and title var link=ParseLinkTarget(p, id, ExtraMode); // and trailing whitespace p.SkipLinespace(); // Trailing crap, not a valid link reference... if (!p.eol) return null; return link; }
/// <summary>Parses the given p.</summary> /// /// <param name="p">The StringScanner to process.</param> /// /// <returns>A TableSpec.</returns> 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 } }
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 } }
// Parse just the link target // For reference link definition, this is the bit after "[id]: thisbit" // For inline link, this is the bit in the parens: [link text](thisbit) internal static LinkDefinition ParseLinkTarget(StringScanner p, string id, bool ExtraMode) { // Skip whitespace p.SkipWhitespace(); // End of string? if (p.eol) { return(null); } // Create the link definition var r = new LinkDefinition(id); // Is the url enclosed in angle brackets if (p.SkipChar('<')) { // Extract the url p.Mark(); // Find end of the url while (p.current != '>') { if (p.eof) { return(null); } p.SkipEscapableChar(ExtraMode); } string url = p.Extract(); if (!p.SkipChar('>')) { return(null); } // Unescape it r.url = Utils.UnescapeString(url.Trim(), ExtraMode); // Skip whitespace p.SkipWhitespace(); } else { // Find end of the url p.Mark(); int paren_depth = 1; while (!p.eol) { char ch = p.current; if (char.IsWhiteSpace(ch)) { break; } if (id == null) { if (ch == '(') { paren_depth++; } else if (ch == ')') { paren_depth--; if (paren_depth == 0) { break; } } } p.SkipEscapableChar(ExtraMode); } r.url = Utils.UnescapeString(p.Extract().Trim(), ExtraMode); } p.SkipLinespace(); // End of inline target if (p.DoesMatch(')')) { return(r); } bool bOnNewLine = p.eol; int posLineEnd = p.position; if (p.eol) { p.SkipEol(); p.SkipLinespace(); } // Work out what the title is delimited with char delim; switch (p.current) { case '\'': case '\"': delim = p.current; break; case '(': delim = ')'; break; default: if (bOnNewLine) { p.position = posLineEnd; return(r); } else { return(null); } } // Skip the opening title delimiter p.SkipForward(1); // Find the end of the title p.Mark(); while (true) { if (p.eol) { return(null); } if (p.current == delim) { if (delim != ')') { int savepos = p.position; // Check for embedded quotes in title // Skip the quote and any trailing whitespace p.SkipForward(1); p.SkipLinespace(); // Next we expect either the end of the line for a link definition // or the close bracket for an inline link if ((id == null && p.current != ')') || (id != null && !p.eol)) { continue; } p.position = savepos; } // End of title break; } p.SkipEscapableChar(ExtraMode); } // Store the title r.title = Utils.UnescapeString(p.Extract(), ExtraMode); // Skip closing quote p.SkipForward(1); // Done! return(r); }