aboutsummaryrefslogtreecommitdiff
path: root/butl/tab-parser
blob: 6aa0705209080f5203489bbee1258f421a322903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 <string>
#include <vector>
#include <iosfwd>
#include <cstdint>   // uint64_t
#include <stdexcept> // runtime_error

#include <butl/export>

#include <butl/char-scanner>

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<tab_field>
  {
    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