aboutsummaryrefslogtreecommitdiff
path: root/unit-tests/lexer/driver.cxx
blob: 4e6a47d731c6f19e49aa9ed26f2a41e4b857b61c (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
// file      : unit-tests/lexer/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>
#include <iostream>

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

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

using namespace std;

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

    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 == "attribute") m = lexer_mode::attribute;
        else if (a == "eval")      m = lexer_mode::eval;
        else if (a == "buildspec") m = lexer_mode::buildspec;
        else                       assert (false);
        break;
      }
    }

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

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

      if (m != lexer_mode::normal)
        l.mode (m);

      // 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);
}