summaryrefslogtreecommitdiff
path: root/cli/cli.cxx
blob: a8e7faec15497f062dda644729f32e6a72bf182f (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
// 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 <cutl/compiler/code-stream.hxx>

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

using namespace std;
using namespace cutl;

int main (int argc, char* argv[])
{
  ostream& e (cerr);

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

    // Handle --version
    //
    if (ops.version ())
    {
      e << "CodeSynthesis CLI command line interface compiler 1.0.0" << endl
        << "Copyright (C) 2009 Code Synthesis Tools CC" << endl;

      e << "This is free software; see the source for copying conditions. "
        << "There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS "
        << "FOR A PARTICULAR PURPOSE." << endl;

      return 0;
    }

    // Handle --help
    //
    if (ops.help ())
    {
      e << "Usage: " << argv[0] << " [options] file"
        << endl
        << "Options:" << endl;

      compiler::ostream_filter<usage_indenter, char> filt (e);

      e << "--help" << endl
        << " Print usage information and exit."
        << endl;

      e << "--version" << endl
        << " Print version and exit."
        << endl;

      generator::usage ();

      return 0;
    }

    if (end == argc)
    {
      e << "error: no input file specified" << endl
        << "info: try '" << argv[0] << " --help' for more information" << endl;

      return 1;
    }

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

    ifstream ifs (path.string ().c_str ());
    if (!ifs.is_open ())
    {
      e << 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& ex)
  {
    e << ex << endl;
    return 1;
  }
  catch (semantics::invalid_path const& ex)
  {
    e << "error: '" << ex.path () << "' is not a valid filesystem path"
      << endl;
    return 1;
  }
  catch (std::ios_base::failure const&)
  {
    e << 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;
  }
}