/// <summary> /// Parse supplied text using default settings. /// </summary> /// <param name="line">Line to parse.</param> /// <returns>List with list of parse results.</returns> public static List <List <string> > ParseTable(string line) { LexListSettings settings = new LexListSettings(); settings.MultipleRecordsUsingNewLine = true; return(Parse(line, settings)); }
/// <summary> /// Parse a non-quoted item e.g. 123 as opposed to "123". /// </summary> /// <param name="settings">Parse settings to use.</param> private void ParseNonQuotedItem(LexListSettings settings) { // Read non-quoted text until the separator char is hit. string item = ReadNonQuotedToken(_separatorMap, true); if (settings.TrimWhiteSpace) { item = item.Trim(); } // Store the current list item. _tokenList.Add(item); }
/// <summary> /// Check for and handle new line. /// </summary> /// <param name="settings">Parse settings to use.</param> /// <returns>False if no eol was detected, true if it was handled.</returns> public bool CheckAndHandleNewLine(LexListSettings settings) { if (_reader.IsEol()) { _reader.ConsumeNewLine(); // Check if newline means start of new record. if (settings.MultipleRecordsUsingNewLine) { _lines.Add(_tokenList); _tokenList = new List <string>(); } return(true); } return(false); }
/// <summary> /// Parse a quoted item. e.g. "batman" /// </summary> /// <param name="settings">Parse settings to use.</param> private void ParseQuotedItem(LexListSettings settings) { char quote = _reader.CurrentChar; string item = ReadQuotedToken(); if (settings.TrimWhiteSpace) { item = item.Trim(); } // Store the current list item. _tokenList.Add(item); // Check that closing quote is present. if (Expect(quote)) { _reader.ReadChar(); } }
/// <summary> /// Parse supplied text using supplied settings. /// </summary> /// <param name="text">Text to parse.</param> /// <param name="settings">Parse settings to use.</param> /// <returns>List with list of parse results.</returns> public static List <List <string> > Parse(string text, LexListSettings settings) { LexList lex = new LexList(settings); return(lex.ParseLines(text)); }
/// <summary> /// Parse lines. /// </summary> /// <param name="text">Text to parse.</param> /// <returns>List with list of parse results.</returns> public List <List <string> > ParseLines(string text) { // First cut of lex parser. // This needs to be handled differently and should allow whitespace to // be a separator. LexListSettings settings = _settings as LexListSettings; Reset(text); // Move to first char. _reader.ReadChar(); // Get rid of initial whitespace. _reader.ConsumeWhiteSpace(); while (!_reader.IsEnded()) { if (_reader.IsWhiteSpace()) { _reader.ConsumeWhiteSpace(); // Continue check. // The consuming of white space always leaves // the reader at the beginning of a non-whitespace char. continue; } // Check for quotes. else if (_reader.CurrentChar == '\'' || _reader.CurrentChar == '"') { ParseQuotedItem(settings); } else { ParseNonQuotedItem(settings); } // Check for and consume whitespace. CheckAndConsumeWhiteSpace(); // Check for comma and consume it. CheckAndHandleComma(); // Consume whitespace CheckAndConsumeWhiteSpace(); // Handle new lines and whether to store // next line as another record. if (!CheckAndHandleNewLine(settings)) { // Now read next char _reader.ReadChar(); } } // Handle errors. CheckAndThrowErrors(); // Token list always gets reset in the beginning of this method. if (_tokenList.Count > 0) { _lines.Add(_tokenList); } return(_lines); }
/// <summary> /// Create with supplied settings. /// </summary> /// <param name="settings">Parse settings to use.</param> public LexList(LexListSettings settings) { Init(settings); }