summaryrefslogtreecommitdiffstats
path: root/cli/cli.cxx
blob: eaf15f7de648eec3cb08c5ce11da52cc6584bbc6 (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
// file      : cli/cli.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <memory>   // std::auto_ptr
#include <fstream>
#include <iostream>

#include "options.hxx"
#include "parser.hxx"
#include "generator.hxx"

using namespace std;

int main (int argc, char* argv[])
{
  if (argc < 2)
  {
    cerr << "usage: " << argv[0] << " file.cli" << endl;
    return 1;
  }

  try
  {
    int end;
    options ops (argc, argv, end);

    if (end == argc)
    {
      cerr << "error: no input file specified" << endl;
      return 1;
    }

    semantics::path path (argv[end]);

    ifstream ifs (path.string ().c_str ());
    if (!ifs.is_open ())
    {
      cerr << path << ": error: unable to open in read mode" << endl;
      return 1;
    }

    ifs.exceptions (ifstream::failbit | ifstream::badbit);

    parser p;
    auto_ptr<semantics::cli_unit> unit (p.parse (ifs, path));

    generator g;
    g.generate (ops, *unit, path);
  }
  catch (cli::exception const& e)
  {
    cerr << e << endl;
    return 1;
  }
  catch (semantics::invalid_path const& e)
  {
    cerr << "error: '" << e.path () << "' is not a valid filesystem path"
         << endl;
    return 1;
  }
  catch (std::ios_base::failure const&)
  {
    cerr << argv[1] << ": error: read failure" << endl;
    return 1;
  }
  catch (parser::invalid_input const&)
  {
    // Diagnostics has already been issued by the parser.
    //
    return 1;
  }
  catch (generator::failed const&)
  {
    // Diagnostics has already been issued by the generator.
    //
    return 1;
  }
}