aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/lexer.test.cxx
blob: 3458f56ad8ebf8581ccf42bc960dd8dd52339319 (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      : libbuild2/lexer.test.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cstdlib> // strtoul()
#include <cassert>
#include <iostream>

#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/token.hxx>
#include <libbuild2/lexer.hxx>

using namespace std;

namespace build2
{
  // Usage: argv[0] [-q] [<lexer-mode>[=<data>]]
  //
  int
  main (int argc, char* argv[])
  {
    bool quote (false);

    lexer_mode m (lexer_mode::normal);
    uintptr_t d (0);

    for (int i (1); i != argc; ++i)
    {
      string a (argv[i]);

      if (a == "-q")
        quote = true;
      else
      {
        if      (a == "normal")     m = lexer_mode::normal;
        else if (a == "variable")   m = lexer_mode::variable;
        else if (a == "value")      m = lexer_mode::value;
        else if (a == "attributes") m = lexer_mode::attributes;
        else if (a == "eval")       m = lexer_mode::eval;
        else if (a == "buildspec")  m = lexer_mode::buildspec;
        else if (a.compare (0, 8, "foreign=") == 0)
        {
          m = lexer_mode::foreign;
          d = strtoul (a.c_str () + 8, nullptr, 10);
        }
        else                        assert (false);
        break;
      }
    }

    try
    {
      cin.exceptions (istream::failbit | istream::badbit);

      // Most alternative modes auto-expire so we need something underneath.
      //
      path_name in ("<stdin>");
      lexer l (cin, in);

      if (m != lexer_mode::normal)
        l.mode (m, '\0', nullopt, d);

      // No use printing eos since we will either get it or loop forever.
      //
      for (token t (l.next ()); t.type != token_type::eos; t = l.next ())
      {
        if (t.separated && t.type != token_type::newline)
          cout << ' ';

        // Print each token on a separate line without quoting operators.
        //
        t.printer (cout, t, false);

        if (quote)
        {
          char q ('\0');
          switch (t.qtype)
          {
          case quote_type::single:   q = 'S'; break;
          case quote_type::double_:  q = 'D'; break;
          case quote_type::mixed:    q = 'M'; break;
          case quote_type::unquoted:          break;
          }

          if (q != '\0')
            cout << " [" << q << (t.qcomp ? "/C" : "/P") << ']';
        }

        cout << endl;
      }
    }
    catch (const failed&)
    {
      return 1;
    }

    return 0;
  }
}

int
main (int argc, char* argv[])
{
  return build2::main (argc, argv);
}