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

#include <cassert>
#include <sstream>
#include <iostream>

#include <build/path>
#include <build/scope>
#include <build/target>
#include <build/native>

#include <build/lexer>
#include <build/parser>

using namespace std;
using namespace build;

static bool
parse (const char*);

int
main ()
{
  target_types.insert (file::static_type);
  target_types.insert (exe::static_type);
  target_types.insert (obj::static_type);

  assert (parse (""));
  assert (parse ("foo:"));
  assert (parse ("foo bar:"));
  assert (parse ("foo:\nbar:"));
  assert (parse ("foo: bar"));
  assert (parse ("foo: bar baz"));
  assert (parse ("foo bar: baz biz"));

  assert (parse ("{foo}:"));
  assert (parse ("{foo bar}:"));
  assert (parse ("{{foo bar}}:"));
  assert (parse ("{{foo bar} {baz} {biz fox} fix}:"));

  assert (parse ("exe{foo}:"));
  assert (parse ("exe{foo bar}:"));
  assert (parse ("{exe{foo bar}}:"));
  assert (parse ("exe{{foo bar} fox}:"));
  assert (parse ("exe{foo}: obj{bar baz} biz.o file{fox}"));

  assert (!parse (":"));
  assert (!parse ("foo"));
  assert (!parse ("{"));
  assert (!parse ("{foo:"));
  assert (!parse ("{foo{:"));
  assert (!parse ("foo: bar:"));
  assert (!parse ("exe{foo:"));

  // Directory prefix.
  //
  assert (parse ("../{foo}: ../{bar}"));
  assert (parse ("../exe{foo}: ../obj{bar}"));
  assert (!parse ("../exe{exe{foo}}:"));

  // Directory scope.
  //
  assert (parse ("test/:\n{\n}"));
  assert (parse ("test/:\n{\n}\n"));
  assert (parse ("test/:\n{\nfoo:bar\n}"));
  assert (parse ("test/:\n{\nfoo:bar\n}"));
  assert (parse ("test/:\n{\nmore/:\n{\n}\n}"));
  assert (parse ("test/:\n{\nmore/:\n{\nfoo:{bar baz}\n}\n}"));

  assert (!parse ("test/:\n{"));
  assert (!parse ("test/:\n{\n"));
  assert (!parse ("test/:\n{\n:"));
  assert (!parse ("test/:\n{\n} foo: bar\n"));
  assert (!parse ("test/ foo:\n{\n}"));
  assert (!parse ("test foo/:\n{\n}"));
  assert (!parse ("test/ foo/:\n{\n}"));
}

ostream cnull (nullptr);

static bool
parse (const char* s)
{
  istringstream is (s);

  is.exceptions (istream::failbit | istream::badbit);
  parser p (cnull);
  //parser p (cerr);

  try
  {
    p.parse (is, path (), scopes[path::current ()]);
  }
  catch (const parser_error&)
  {
    return false;
  }

  return true;
}