aboutsummaryrefslogtreecommitdiff
path: root/build/parser
blob: b62e8e711ef0041ccdc3d38af97e4202ec50aa2a (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
// 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 <vector>
#include <iosfwd>
#include <utility>       // std::move
#include <unordered_set>

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

namespace build
{
  class scope;
  class lexer;

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

    // Issues diagnostics and throws failed in case of an error.
    //
    void
    parse (std::istream&, const path&, scope&);

    // Recursive descent parser.
    //
  private:
    struct name_type
    {
      name_type (std::string t, path d, std::string n)
          : type (std::move (t)), dir (std::move (d)), name (std::move (n)) {}

      std::string type; // Empty if untyped.
      path dir;
      std::string name;
    };

    typedef std::vector<name_type> names;

    void
    parse_clause (token&, token_type&);

    void
    parse_print (token&, token_type&);

    void
    parse_source (token&, token_type&);

    void
    parse_include (token&, token_type&);

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

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

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

    token_type
    peek ();

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

  private:
    const std::string* path_; // Path processed by diagnostic_string().
    lexer* lexer_;
    scope* scope_;

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

    std::unordered_set<path> include_;
  };
}

#endif // BUILD_PARSER