aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/parser/parser.cc
blob: 896a549819e2beec8cb99b09edacd9a8c22a5575 (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
#include "attr-set.hh"
#include "error.hh"
#include "eval-settings.hh"
#include "eval.hh"
#include "finally.hh"
#include "nixexpr.hh"
#include "symbol-table.hh"
#include "users.hh"

#include "change_head.hh"
#include "grammar.hh"
#include "state.hh"

#include <charconv>
#include <memory>

// Linter complains that this is a "suspicious include of file with '.cc' extension".
// While that is correct and generally not great, it is one of the less bad options to pick
// in terms of diff noise.
// NOLINTNEXTLINE(bugprone-suspicious-include)
#include "parser-impl1.inc.cc"

namespace nix {

Expr * EvalState::parse(
    char * text,
    size_t length,
    Pos::Origin origin,
    const SourcePath & basePath,
    std::shared_ptr<StaticEnv> & staticEnv,
    const FeatureSettings & featureSettings)
{
    parser::State s = {
        symbols,
        positions,
        basePath,
        positions.addOrigin(origin, length),
        exprSymbols,
        featureSettings,
    };

    assert(length >= 2);
    assert(text[length - 1] == 0);
    assert(text[length - 2] == 0);
    length -= 2;

    p::string_input<p::tracking_mode::lazy> inp{std::string_view{text, length}, "input"};
    try {
        parser::v1::ExprState x;
        p::parse<parser::grammar::v1::root, parser::v1::BuildAST, parser::v1::Control>(inp, x, s);

        auto [_pos, result] = x.finish(s);
        result->bindVars(*this, staticEnv);
        return result.release();
    } catch (p::parse_error & e) {
        auto pos = e.positions().back();
        throw ParseError({
            .msg = HintFmt("syntax error, %s", e.message()),
            .pos = positions[s.positions.add(s.origin, pos.byte)]
        });
    }
}

}