summaryrefslogtreecommitdiffstats
path: root/parsers.h
blob: d7d35a3a8561bd874077605e2467c64440fca5a4 (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
#ifndef NINJA_PARSERS_H_
#define NINJA_PARSERS_H_

#include <string>
#include <vector>

using namespace std;

struct Token {
  enum Type {
    NONE,
    UNKNOWN,
    IDENT,
    RULE,
    BUILD,
    NEWLINE,
    EQUALS,
    COLON,
    INDENT,
    OUTDENT,
    TEOF
  };
  explicit Token(Type type) : type_(type) {}

  void Clear() { type_ = NONE; extra_.clear(); }
  string AsString() const;

  Type type_;
  const char* pos_;
  string extra_;
};

struct Tokenizer {
  Tokenizer()
      : token_(Token::NONE), line_number_(1),
        last_indent_(0), cur_indent_(-1) {}

  void Start(const char* start, const char* end);
  bool Error(const string& message, string* err);

  const Token& token() const { return token_; }

  void SkipWhitespace(bool newline=false);
  bool Newline(string* err);
  bool ExpectToken(Token::Type expected, string* err);
  bool ReadIdent(string* out);
  bool ReadToNewline(string* text, string* err);

  Token::Type PeekToken();
  void ConsumeToken();

  const char* cur_;
  const char* end_;

  const char* cur_line_;
  Token token_;
  int line_number_;
  int last_indent_, cur_indent_;
};

struct MakefileParser {
  bool Parse(const string& input, string* err);

  Tokenizer tokenizer_;
  string out_;
  vector<string> ins_;
};

struct State;

struct ManifestParser {
  ManifestParser(State* state) : state_(state) {}
  bool Load(const string& filename, string* err);
  bool Parse(const string& input, string* err);

  bool ParseRule(string* err);
  bool ParseLet(string* key, string* val, string* err);
  bool ParseEdge(string* err);

  string ExpandFile(const string& file);

  State* state_;
  Tokenizer tokenizer_;
  string builddir_;
};

#endif  // NINJA_PARSERS_H_