From d53c8a6ce3d868da66d97a9243365e88d0879343 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 31 Mar 2017 23:29:49 +0300 Subject: Add tab_parser --- butl/tab-parser | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 butl/tab-parser (limited to 'butl/tab-parser') diff --git a/butl/tab-parser b/butl/tab-parser new file mode 100644 index 0000000..6aa0705 --- /dev/null +++ b/butl/tab-parser @@ -0,0 +1,78 @@ +// file : butl/tab-parser -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BUTL_TAB_PARSER +#define BUTL_TAB_PARSER + +#include +#include +#include +#include // uint64_t +#include // runtime_error + +#include + +#include + +namespace butl +{ + class LIBBUTL_EXPORT tab_parsing: public std::runtime_error + { + public: + tab_parsing (const std::string& name, + std::uint64_t line, + std::uint64_t column, + const std::string& description); + + std::string name; + std::uint64_t line; + std::uint64_t column; + std::string description; + }; + + // Line and columns are useful for issuing diagnostics about invalid or + // missing fields. + // + struct tab_field + { + std::string value; // Field string (quoting preserved). + std::uint64_t column; // Field start column number (one-based). + }; + + struct tab_fields: std::vector + { + std::uint64_t line; // Line number (one-based). + std::uint64_t end_column; // End-of-line column (line length). + }; + + // Read and parse lines consisting of space-separated fields. Field can + // contain single or double quoted substrings (with spaces) which are + // interpreted but preserved. No escaping of the quote characters is + // supported. Blank lines and lines that start with # (collectively called + // empty lines) are ignored. + // + class LIBBUTL_EXPORT tab_parser: protected butl::char_scanner + { + public: + tab_parser (std::istream& is, const std::string& name) + : char_scanner (is), name_ (name) {} + + // Return next line of fields. Skip empty lines. Empty result denotes the + // end of stream. + // + tab_fields + next (); + + private: + // Skip spaces and return the first peeked non-space character. + // + xchar + skip_spaces (); + + private: + const std::string name_; + }; +} + +#endif // BUTL_TAB_PARSER -- cgit v1.1