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

#include <libbuild2/token.hxx>

using namespace std;

namespace build2
{
  void
  token_printer (ostream& os, const token& t, print_mode m)
  {
    // Only quote non-name tokens for diagnostics.
    //
    const char* q (m == print_mode::diagnostics ? "'" : "");
    bool r (m == print_mode::raw);

    switch (t.type)
    {
    case token_type::eos:
      {
        if (!r)
          os <<"<end of file>";

        break;
      }
    case token_type::newline:
      {
        os << (r ? "\n" : "<newline>");
        break;
      }
    case token_type::pair_separator:
      {
        if (r)
          os << t.value[0];
        else
          os << "<pair separator " << t.value[0] << ">";

        break;
      }
    case token_type::word:
      {
        if (r)
          os << t.value;
        else
          os << '\'' << t.value << '\'';

        break;
      }

    case token_type::colon:          os << q << ':'     << q; break;
    case token_type::dollar:         os << q << '$'     << q; break;
    case token_type::question:       os << q << '?'     << q; break;
    case token_type::percent:        os << q << '%'     << q; break;
    case token_type::comma:          os << q << ','     << q; break;
    case token_type::backtick:       os << q << '`'     << q; break;

    case token_type::lparen:         os << q << '('     << q; break;
    case token_type::rparen:         os << q << ')'     << q; break;

    case token_type::lcbrace:        os << q << '{'     << q; break;
    case token_type::rcbrace:        os << q << '}'     << q; break;

    case token_type::multi_lcbrace:  os << q << t.value << q; break;
    case token_type::multi_rcbrace:  os << q << t.value << q; break;

    case token_type::lsbrace:        os << q << '['     << q; break;
    case token_type::rsbrace:        os << q << ']'     << q; break;

    case token_type::labrace:        os << q << '<'     << q; break;
    case token_type::rabrace:        os << q << '>'     << q; break;

    case token_type::assign:         os << q << '='     << q; break;
    case token_type::prepend:        os << q << "=+"    << q; break;
    case token_type::append:         os << q << "+="    << q; break;
    case token_type::default_assign: os << q << "?="    << q; break;

    case token_type::equal:          os << q << "=="    << q; break;
    case token_type::not_equal:      os << q << "!="    << q; break;
    case token_type::less:           os << q << '<'     << q; break;
    case token_type::greater:        os << q << '>'     << q; break;
    case token_type::less_equal:     os << q << "<="    << q; break;
    case token_type::greater_equal:  os << q << ">="    << q; break;

    case token_type::bit_or:         os << q << '|'     << q; break;

    case token_type::log_or:         os << q << "||"    << q; break;
    case token_type::log_and:        os << q << "&&"    << q; break;
    case token_type::log_not:        os << q << '!'     << q; break;

    default: assert (false); // Unhandled extended token.
    }
  }
}