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

#include <build/parser>

#include <iostream>

#include <build/token>
#include <build/lexer>

using namespace std;

namespace build
{
  // Output the token type and value in a format suitable for diagnostics.
  //
  ostream&
  operator<< (ostream&, const token&);

  typedef token_type type;
  typedef token_punctuation punc;

  void parser::
  parse (istream& is, const path& p)
  {
    lexer l (is, p.string (), diag_);
    lexer_ = &l;
    path_ = &p;

    token t (0, 0); // eos
    type tt;

    for (next (t, tt); tt != type::eos; )
    {
      // We always start with one or more names.
      //
      names (t, tt);

      if (t.is (punc::colon))
      {
        next (t, tt);

        if (tt == type::name || t.is (punc::lcbrace))
          names (t, tt);

        if (t.is (punc::newline))
          next (t, tt);
        else if (tt != type::eos)
        {
          error (t) << "expected newline insetad of " << t << endl;
          throw parser_error ();
        }

        continue;
      }

      error (t) << "unexpected " << t << endl;
      throw parser_error ();
    }
  }

  void parser::
  names (token& t, type& tt)
  {
    for (bool first (true);; first = false)
    {
      // Untyped name group, e.g., '{foo bar}'.
      //
      if (t.is (punc::lcbrace))
      {
        next (t, tt);
        names (t, tt);

        if (!t.is (punc::rcbrace))
        {
          error (t) << "expected '}' instead of " << t << endl;
          throw parser_error ();
        }

        next (t, tt);
        continue;
      }

      // Name.
      //
      if (tt == type::name)
      {
        string name (t.name ());

        // See if this is a type name, that is, it is followed by '{'.
        //
        next (t, tt);

        if (t.is (punc::lcbrace))
        {
          //cout << "type: " << name << endl;

          //@@ TODO:
          //
          //   - detect nested typed name groups, e.g., 'cxx{hxx{foo}}'.
          //
          next (t, tt);
          names (t, tt);

          if (!t.is (punc::rcbrace))
          {
            error (t) << "expected '}' instead of " << t << endl;
            throw parser_error ();
          }

          next (t, tt);
          continue;
        }

        // This is a target, directory, or variable name.
        //cout << "name: " << name << endl;
        continue;
      }

      if (!first)
        break;

      error (t) << "expected name instead of " << t << endl;
      throw parser_error ();
    }
  }

  void parser::
  next (token& t, token_type& tt)
  {
    t = lexer_->next ();
    tt = t.type ();
  }

  ostream& parser::
  error (const token& t)
  {
    return diag_ << path_->string () << ':' << t.line () << ':' <<
      t.column () << ": error: ";
  }

  // Output the token type and value in a format suitable for diagnostics.
  //
  ostream&
  operator<< (ostream& os, const token& t)
  {
    switch (t.type ())
    {
    case token_type::eos: os << "<end-of-stream>"; break;
    case token_type::punctuation:
      {
        switch (t.punctuation ())
        {
        case token_punctuation::newline: os << "<newline>"; break;
        case token_punctuation::colon:   os << "':'"; break;
        case token_punctuation::lcbrace: os << "'{'"; break;
        case token_punctuation::rcbrace: os << "'}'"; break;
        }
        break;
      }
    case token_type::name: os << '\'' << t.name () << '\''; break;
    }

    return os;
  }
}