aboutsummaryrefslogtreecommitdiff
path: root/build2/token
blob: 18d377dc7b00bbe64af17bb022702ff084f1f3f5 (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
107
108
109
110
111
112
113
114
// file      : build2/token -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD2_TOKEN
#define BUILD2_TOKEN

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

#include <build2/diagnostics>

namespace build2
{
  // Extendable/inheritable enum-like class.
  //
  struct token_type
  {
    enum
    {
      // NOTE: remember to update token_printer()!

      eos,
      name,
      newline,
      pair_separator,
      colon,
      lcbrace,       // {
      rcbrace,       // }
      lsbrace,       // [
      rsbrace,       // ]
      assign,        // =
      prepend,       // =+
      append,        // +=
      equal,         // ==
      not_equal,     // !=
      less,          // <
      greater,       // >
      less_equal,    // <=
      greater_equal, // >=
      dollar,        // $
      lparen,        // (
      rparen,        // )

      value_next
    };

    using value_type = uint16_t;

    token_type (value_type v = eos): v_ (v) {}
    operator value_type () const {return v_;}
    value_type v_;
  };

  class token;

  void
  token_printer (ostream&, const token&, bool);

  class token
  {
  public:
    using printer_type = void (ostream&, const token&, bool diag);

    token_type type;
    bool separated; // Whitespace-separated from the previous token.
    bool quoted;    // Name (or some part of it) was quoted.

    string value;   // Only valid for name.

    uint64_t line;
    uint64_t column;

    printer_type* printer;

  public:
    token ()
        : token (token_type::eos, false, 0, 0, token_printer) {}

    token (token_type t, bool s, uint64_t l, uint64_t c, printer_type* p)
        : type (t), separated (s), quoted (false),
          line (l), column (c),
          printer (p) {}

    token (string n, bool s, bool q, uint64_t l, uint64_t c)
        : type (token_type::name), separated (s), quoted (q), value (move (n)),
          line (l), column (c),
          printer (&token_printer) {}
  };

  // Output the token value in a format suitable for diagnostics.
  //
  inline ostream&
  operator<< (ostream& o, const token& t) {t.printer (o, t, true); return o;}

  // Diagnostics plumbing. We assume that any diag stream for which we can use
  // token as location has its aux data pointing to pointer to path.
  //
  inline location
  get_location (const token& t, const path& p)
  {
    return location (&p, t.line, t.column);
  }

  inline location
  get_location (const token& t, const void* data)
  {
    assert (data != nullptr); // E.g., must be &parser::path_.
    const path* p (*static_cast<const path* const*> (data));
    return get_location (t, *p);
  }
}

#endif // BUILD2_TOKEN