aboutsummaryrefslogtreecommitdiff
path: root/build/parser
blob: 164cf10641dd097e38c7d820da8cd0bd021eb2a8 (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
// file      : build/parser -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_PARSER
#define BUILD_PARSER

#include <string>
#include <iosfwd>

#include <build/path>
#include <build/token>
#include <build/name>
#include <build/spec>
#include <build/diagnostics>

namespace build
{
  class scope;
  class target;
  class lexer;

  class parser
  {
  public:
    parser (): fail (&path_) {}

    // Issue diagnostics and throw failed in case of an error.
    //
    void
    parse_buildfile (std::istream&, const path&, scope& root, scope& base);

    buildspec
    parse_buildspec (std::istream&, const std::string& name);

    // Recursive descent parser.
    //
  private:
    typedef build::names names_type;

    void
    clause (token&, token_type&);

    void
    print (token&, token_type&);

    void
    source (token&, token_type&);

    void
    include (token&, token_type&);

    void
    load (token&, token_type&);

    names_type
    names (token& t, token_type& tt)
    {
      names_type ns;
      names (t, tt, ns, 0, nullptr, nullptr);
      return ns;
    }

    void
    names (token&, token_type&,
           names_type&, std::size_t pair,
           const path* dir, const std::string* type);

    // Buildspec.
    //
    buildspec
    buildspec_clause (token&, token_type&, token_type end);

    // Utilities.
    //
  private:
    void
    process_default_target (token&);

    // Lexer.
    //
  private:
    token_type
    next (token&, token_type&);

    token_type
    peek ();

    const token&
    peeked () const
    {
      assert (peeked_);
      return peek_;
    }

    // Diagnostics.
    //
  private:
    const fail_mark<failed> fail;

  private:
    const std::string* path_; // Path processed by diagnostic_string().
    lexer* lexer_;
    scope* scope_; // Current base scope (out_base).
    scope* root_;  // Current root scope (out_root).
    target* default_target_;

    const path* out_root_;
    const path* src_root_;

    token peek_ {token_type::eos, false, 0, 0};
    bool peeked_ {false};
  };
}

#endif // BUILD_PARSER