aboutsummaryrefslogtreecommitdiff
path: root/tests/buildfile-scanner/driver.cxx
blob: fec15bde1e52106382531d41dea472cf5bdd7f66 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// file      : tests/buildfile-scanner/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <ios>      // ios_base::failbit, ios_base::badbit
#include <string>
#include <iostream>

#include <libbutl/utf8.hxx>
#include <libbutl/utility.hxx>      // operator<<(ostream,exception)
#include <libbutl/char-scanner.hxx>

#include <libbpkg/buildfile-scanner.hxx>

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace butl;
using namespace bpkg;

// Usages:
//
// argv[0] (-e|-l [<char>]|-b)
//
// Read and scan the buildfile from stdin and print the scan result to stdout.
//
// -e          scan evaluation context
// -l [<char>] scan single line, optionally terminated with the stop character
// -b          scan buildfile block
//
int
main (int argc, char* argv[])
{
  assert (argc >= 2);

  string mode (argv[1]);

  cin.exceptions  (ios_base::failbit | ios_base::badbit);
  cout.exceptions (ios_base::failbit | ios_base::badbit);

  using scanner = char_scanner<utf8_validator>;

  scanner s (cin);

  string bsn ("stdin");
  buildfile_scanner<utf8_validator, 1> bs (s, bsn);

  try
  {
    string r;

    if (mode == "-e")
    {
      scanner::xchar c (s.get ());
      assert (c == '(');

      r += c;
      r += bs.scan_eval ();

      c = s.get ();
      assert (c == ')');

      r += c;
    }
    else if (mode == "-l")
    {
      char stop ('\0');

      if (argc == 3)
      {
        const char* chr (argv[2]);
        assert (chr[0] != '\0' && chr[1] == '\0');

        stop = chr[0];
      }

      r += bs.scan_line (stop);

      scanner::xchar c (s.get ());
      assert (scanner::eos (c) || c == '\n' || (stop != '\0' && c == stop));
    }
    else if (mode == "-b")
    {
      scanner::xchar c (s.get ());
      assert (c == '{');

      r += c;
      r += bs.scan_block ();

      assert (scanner::eos (s.peek ()));

      r += "}\n";
    }
    else
      assert (false);

    cout << r;
  }
  catch (const buildfile_scanning& e)
  {
    cerr << e << endl;
    return 1;
  }

  return 0;
}