about summary refs log tree commit diff
path: root/third_party/nix/src/libexpr/parser.cc
blob: a96d345cc12659fb5850e250ced1b845a1f590c5 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "libexpr/parser.hh"

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "libexpr/eval.hh"
#include "libstore/download.hh"
#include "libstore/store-api.hh"

namespace nix {

void addAttr(ExprAttrs* attrs, AttrPath& attrPath, Expr* e, const Pos& pos) {
  AttrPath::iterator i;
  // All attrpaths have at least one attr
  assert(!attrPath.empty());
  // Checking attrPath validity.
  // ===========================
  for (i = attrPath.begin(); i + 1 < attrPath.end(); i++) {
    if (const auto* sym = std::get_if<Symbol>(&(*i)); sym && sym->set()) {
      ExprAttrs::AttrDefs::iterator j = attrs->attrs.find(*sym);
      if (j != attrs->attrs.end()) {
        if (!j->second.inherited) {
          ExprAttrs* attrs2 = dynamic_cast<ExprAttrs*>(j->second.e);
          if (!attrs2) {
            dupAttr(attrPath, pos, j->second.pos);
          }
          attrs = attrs2;
        } else {
          dupAttr(attrPath, pos, j->second.pos);
        }
      } else {
        ExprAttrs* nested = new ExprAttrs;
        attrs->attrs[*sym] = ExprAttrs::AttrDef(nested, pos);
        attrs = nested;
      }
    } else {
      // Yes, this code does not handle all conditions
      // exhaustively. We use std::get to throw if the condition
      // that isn't covered happens, which is potentially a
      // behaviour change from the previous default constructed
      // Symbol. It should alert us about anything untoward going
      // on here.
      auto* expr = std::get<Expr*>(*i);

      ExprAttrs* nested = new ExprAttrs;
      attrs->dynamicAttrs.push_back(
          ExprAttrs::DynamicAttrDef(expr, nested, pos));
      attrs = nested;
    }
  }
  // Expr insertion.
  // ==========================
  if (auto* sym = std::get_if<Symbol>(&(*i)); sym && sym->set()) {
    ExprAttrs::AttrDefs::iterator j = attrs->attrs.find(*sym);
    if (j != attrs->attrs.end()) {
      // This attr path is already defined. However, if both
      // e and the expr pointed by the attr path are two attribute sets,
      // we want to merge them.
      // Otherwise, throw an error.
      auto ae = dynamic_cast<ExprAttrs*>(e);
      auto jAttrs = dynamic_cast<ExprAttrs*>(j->second.e);
      if (jAttrs && ae) {
        for (auto& ad : ae->attrs) {
          auto j2 = jAttrs->attrs.find(ad.first);
          if (j2 !=
              jAttrs->attrs.end()) {  // Attr already defined in iAttrs, error.
            dupAttr(ad.first, j2->second.pos, ad.second.pos);
          }
          jAttrs->attrs[ad.first] = ad.second;
        }
      } else {
        dupAttr(attrPath, pos, j->second.pos);
      }
    } else {
      // This attr path is not defined. Let's create it.
      attrs->attrs[*sym] = ExprAttrs::AttrDef(e, pos);
    }
  } else {
    // Same caveat as the identical line above.
    auto* expr = std::get<Expr*>(*i);
    attrs->dynamicAttrs.push_back(ExprAttrs::DynamicAttrDef(expr, e, pos));
  }
}

void addFormal(const Pos& pos, Formals* formals, const Formal& formal) {
  if (formals->argNames.find(formal.name) != formals->argNames.end())
    throw ParseError(format("duplicate formal function argument '%1%' at %2%") %
                     formal.name % pos);
  formals->formals.push_front(formal);
  formals->argNames.insert(formal.name);
}

Expr* stripIndentation(const Pos& pos, SymbolTable& symbols,
                       std::vector<Expr*>& es) {
  if (es.empty()) {
    return new ExprString(symbols.Create(""));
  }

  /* Figure out the minimum indentation.  Note that by design
     whitespace-only final lines are not taken into account.  (So
     the " " in "\n ''" is ignored, but the " " in "\n foo''" is.) */
  bool atStartOfLine = true; /* = seen only whitespace in the current line */
  size_t minIndent = 1000000;
  size_t curIndent = 0;
  for (auto& i : es) {
    ExprIndStr* e = dynamic_cast<ExprIndStr*>(i);
    if (!e) {
      /* Anti-quotations end the current start-of-line whitespace. */
      if (atStartOfLine) {
        atStartOfLine = false;
        if (curIndent < minIndent) {
          minIndent = curIndent;
        }
      }
      continue;
    }
    for (size_t j = 0; j < e->s.size(); ++j) {
      if (atStartOfLine) {
        if (e->s[j] == ' ')
          curIndent++;
        else if (e->s[j] == '\n') {
          /* Empty line, doesn't influence minimum
             indentation. */
          curIndent = 0;
        } else {
          atStartOfLine = false;
          if (curIndent < minIndent) {
            minIndent = curIndent;
          }
        }
      } else if (e->s[j] == '\n') {
        atStartOfLine = true;
        curIndent = 0;
      }
    }
  }

  /* Strip spaces from each line. */
  std::vector<Expr*>* es2 = new std::vector<Expr*>;
  atStartOfLine = true;
  size_t curDropped = 0;
  size_t n = es.size();
  for (std::vector<Expr*>::iterator i = es.begin(); i != es.end(); ++i, --n) {
    ExprIndStr* e = dynamic_cast<ExprIndStr*>(*i);
    if (!e) {
      atStartOfLine = false;
      curDropped = 0;
      es2->push_back(*i);
      continue;
    }

    std::string s2;
    for (size_t j = 0; j < e->s.size(); ++j) {
      if (atStartOfLine) {
        if (e->s[j] == ' ') {
          if (curDropped++ >= minIndent) s2 += e->s[j];
        } else if (e->s[j] == '\n') {
          curDropped = 0;
          s2 += e->s[j];
        } else {
          atStartOfLine = false;
          curDropped = 0;
          s2 += e->s[j];
        }
      } else {
        s2 += e->s[j];
        if (e->s[j] == '\n') {
          atStartOfLine = true;
        }
      }
    }

    /* Remove the last line if it is empty and consists only of
       spaces. */
    if (n == 1) {
      std::string::size_type p = s2.find_last_of('\n');
      if (p != std::string::npos &&
          s2.find_first_not_of(' ', p + 1) == std::string::npos) {
        s2 = std::string(s2, 0, p + 1);
      }
    }

    es2->push_back(new ExprString(symbols.Create(s2)));
  }

  /* If this is a single string, then don't do a concatenation. */
  return es2->size() == 1 && dynamic_cast<ExprString*>((*es2)[0])
             ? (*es2)[0]
             : new ExprConcatStrings(pos, true, es2);
}

Path resolveExprPath(Path path) {
  assert(path[0] == '/');

  /* If `path' is a symlink, follow it.  This is so that relative
     path references work. */
  struct stat st;
  while (true) {
    if (lstat(path.c_str(), &st))
      throw SysError(format("getting status of '%1%'") % path);
    if (!S_ISLNK(st.st_mode)) {
      break;
    }
    path = absPath(readLink(path), dirOf(path));
  }

  /* If `path' refers to a directory, append `/default.nix'. */
  if (S_ISDIR(st.st_mode)) path = canonPath(path + "/default.nix");

  return path;
}

// These methods are actually declared in eval.hh, and were - for some
// reason - previously implemented in parser.y.

Expr* EvalState::parseExprFromFile(const Path& path) {
  return parseExprFromFile(path, staticBaseEnv);
}

Expr* EvalState::parseExprFromFile(const Path& path, StaticEnv& staticEnv) {
  return parse(readFile(path).c_str(), path, dirOf(path), staticEnv);
}

Expr* EvalState::parseExprFromString(const std::string& s, const Path& basePath,
                                     StaticEnv& staticEnv) {
  return parse(s.c_str(), "(std::string)", basePath, staticEnv);
}

Expr* EvalState::parseExprFromString(const std::string& s,
                                     const Path& basePath) {
  return parseExprFromString(s, basePath, staticBaseEnv);
}

Expr* EvalState::parseStdin() {
  // Activity act(*logger, lvlTalkative, format("parsing standard input"));
  return parseExprFromString(drainFD(0), absPath("."));
}

void EvalState::addToSearchPath(const std::string& s) {
  size_t pos = s.find('=');
  std::string prefix;
  Path path;
  if (pos == std::string::npos) {
    path = s;
  } else {
    prefix = std::string(s, 0, pos);
    path = std::string(s, pos + 1);
  }

  searchPath.emplace_back(prefix, path);
}

Path EvalState::findFile(const std::string& path) {
  return findFile(searchPath, path);
}

Path EvalState::findFile(SearchPath& searchPath, const std::string& path,
                         const Pos& pos) {
  for (auto& i : searchPath) {
    std::string suffix;
    if (i.first.empty())
      suffix = "/" + path;
    else {
      auto s = i.first.size();
      if (path.compare(0, s, i.first) != 0 ||
          (path.size() > s && path[s] != '/'))
        continue;
      suffix = path.size() == s ? "" : "/" + std::string(path, s);
    }
    auto r = resolveSearchPathElem(i);
    if (!r.first) {
      continue;
    }
    Path res = r.second + suffix;
    if (pathExists(res)) {
      return canonPath(res);
    }
  }
  format f = format(
      "file '%1%' was not found in the Nix search path (add it using $NIX_PATH "
      "or -I)" +
      std::string(pos ? ", at %2%" : ""));
  f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
  throw ThrownError(f % path % pos);
}

std::pair<bool, std::string> EvalState::resolveSearchPathElem(
    const SearchPathElem& elem) {
  auto i = searchPathResolved.find(elem.second);
  if (i != searchPathResolved.end()) {
    return i->second;
  }

  std::pair<bool, std::string> res;

  if (isUri(elem.second)) {
    try {
      CachedDownloadRequest request(elem.second);
      request.unpack = true;
      res = {true, getDownloader()->downloadCached(store, request).path};
    } catch (DownloadError& e) {
      LOG(WARNING) << "Nix search path entry '" << elem.second
                   << "' cannot be downloaded, ignoring";
      res = {false, ""};
    }
  } else {
    auto path = absPath(elem.second);
    if (pathExists(path)) {
      res = {true, path};
    } else {
      LOG(WARNING) << "Nix search path entry '" << elem.second
                   << "' does not exist, ignoring";
      res = {false, ""};
    }
  }

  DLOG(INFO) << "resolved search path element '" << elem.second << "' to '"
             << res.second << "'";

  searchPathResolved[elem.second] = res;
  return res;
}

}  // namespace nix