aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/make-parser.cxx
blob: cab81d049bed7f593cb953befc1b12c2e73a835f (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// file      : libbuild2/make-parser.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/make-parser.hxx>

#include <libbuild2/diagnostics.hxx>

namespace build2
{
  auto make_parser::
  next (const string& l,
        size_t& p,
        const location& ll,
        bool strict) -> pair<type, path>
  {
    assert (state != end);

    pair<string, bool> r (
      next (l, p, !strict ? state == prereqs : optional<bool> ()));

    type t (state == prereqs ? type::prereq : type::target);

    // Deal with the end.
    //
    if (r.second)
    {
      if (state == begin && r.first.empty ())
        ; // Skip leading blank line.
      else
      {
        if (state != prereqs)
          fail (ll) << "end of make dependency declaration before ':'";

        state = end;
      }
    }
    // Deal with the first target.
    //
    else if (state == begin && !r.first.empty ())
      state = targets;

    // Deal with `:`.
    //
    if (p != l.size () && l[p] == ':')
    {
      switch (state)
      {
      case begin:   fail (ll) << "':' before make target";      break;
      case targets: state = prereqs;                            break;
      case prereqs: fail (ll) << "':' after make prerequisite"; break;
      case end:                                                 break;
      }

      if (++p == l.size ())
        state = end; // Not a mere optimization: the caller will get next line.
    }

    try
    {
      return pair<type, path> (t, path (move (r.first)));
    }
    catch (const invalid_path& e)
    {
      fail (ll) << "invalid make "
                << (t == type::prereq ? "prerequisite" : "target")
                << " path '" << e.path << "'" << endf;
    }
  }

  pair<string, bool> make_parser::
  next (const string& l, size_t& p, optional<bool> prereq)
  {
    size_t n (l.size ());

    // Skip leading spaces.
    //
    for (; p != n && l[p] == ' '; p++) ;

    // Lines containing multiple targets/prerequisites are customarily 80
    // characters max.
    //
    string r;
    r.reserve (n - p);

    // Scan the next target/prerequisite while watching out for escape
    // sequences.
    //
    // @@ Can't we do better for the (common) case where nothing is escaped?
    //
    for (char c, q (prereq && *prereq ? '\0' : ':');
         p != n && (c = l[p]) != ' ' && c != q; )
    {
      // If we have another character, then handle the escapes.
      //
      if (++p != n)
      {
        if (c == '\\')
        {
          // This may or may not be an escape sequence depending on whether
          // what follows is "escapable".
          //
          switch (c = l[p])
          {
          case '\\':
          case ' ':
          case ':': ++p; break;
          default: c = '\\'; // Restore.
          }
        }
        else if (c == '$')
        {
          // Got to be another (escaped) '$'.
          //
          if (l[p] == '$')
            ++p;
        }
      }
      // Note that the newline escape is not necessarily separated with space.
      //
      else if (c == '\\')
      {
        --p;
        break;
      }

      r += c;
    }

    // Skip trailing spaces.
    //
    for (; p != n && l[p] == ' '; p++) ;

    // Skip final '\' and determine if this is the end.
    //
    bool e (false);
    if (p == n - 1)
    {
      if (l[p] == '\\')
        p++;
    }
    else if (p == n)
      e = true;

    return pair<string, bool> (move (r), e);
  }
}