-
Notifications
You must be signed in to change notification settings - Fork 3
/
jassc.cpp
189 lines (154 loc) · 5.13 KB
/
jassc.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
* Copyright (C) 2010 by Tamino Dauth *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <list>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include "../jass.hpp"
#include "../i18n.hpp"
namespace
{
const char *version = "0.1";
}
int main(int argc, char *argv[])
{
// Set the current locale.
setlocale(LC_ALL, "");
// Set the text message domain.
bindtextdomain("jassc", LOCALE_DIR);
textdomain("jassc");
typedef std::vector<std::string> Strings;
Strings headers;
Strings inputFiles;
boost::filesystem::path outputFile;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("version,V", _("Shows current version of jassc."))
("help,h", _("Shows this text."))
// options
("headers,j", boost::program_options::value<Strings>(&headers), _("Includes JASS headers"))
// warning options
("fsyntax-only", _("Check the code for syntax errors, but don't do anything beyond that."))
("i", boost::program_options::value<Strings>(&inputFiles), _("Input files."))
("o", boost::program_options::value<boost::filesystem::path>(&outputFile), _("Output file."))
;
boost::program_options::positional_options_description p;
p.add("i", -1);
boost::program_options::variables_map vm;
try
{
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
}
catch (std::exception &exception)
{
std::cerr << boost::format(_("Error while parsing program options: \"%1%\"")) % exception.what() << std::endl;
return EXIT_FAILURE;
}
boost::program_options::notify(vm);
if (vm.count("version"))
{
std::cout <<
boost::format(_("jassc %1%.")) % version
<< std::endl
<< wc3lib::wc3libCopyright()
<< std::endl;
return EXIT_SUCCESS;
}
if (vm.count("help"))
{
std::cout << _("Usage: jassc [options] [input files]") << std::endl << std::endl;
std::cout << desc << std::endl;
std::cout << wc3lib::wc3libReportBugs() << std::endl;
return EXIT_SUCCESS;
}
if (inputFiles.empty())
{
std::cerr << _("Missing input files.") << std::endl;
return EXIT_FAILURE;
}
wc3lib::jass::Grammar grammar;
wc3lib::jass::jass_ast ast;
bool error = false;
BOOST_FOREACH(Strings::const_reference ref, inputFiles)
{
boost::filesystem::path file(ref);
if (boost::filesystem::is_regular_file(file))
{
wc3lib::ifstream ifstream(file);
if (ifstream)
{
try
{
if (!grammar.parse(ifstream, ast, ref))
{
error = true;
std::cerr << boost::format(_("Error while parsing file \"%1%\".")) % ref << std::endl;
}
}
catch (wc3lib::Exception &e)
{
error = true;
std::cerr << boost::format(_("Error while parsing file \"%1%\": %2%")) % ref % e.what() << std::endl;
}
}
}
else
{
std::cerr << boost::format(_("File \"%1%\" is no regular file.")) % ref << std::endl;
}
}
/*
* If an error did occur when parsing the grammar we should not check the AST since
* it will be corrupted anyways.
* So do only check the AST for types etc. if it had been parsed properly.
*/
if (!error)
{
wc3lib::jass::Analyzer analyzer(grammar.typeSymbols(), grammar.globalSymbols(), grammar.functionSymbols());
wc3lib::jass::Analyzer::Reports reports;
analyzer.checkAst(ast, reports);
if (!reports.empty())
{
BOOST_FOREACH(wc3lib::jass::Analyzer::Reports::const_reference ref, reports)
{
std::cerr << ref.output() << std::endl;
}
if (vm.count("fsyntax-only"))
{
return EXIT_FAILURE;
}
}
else
{
std::cout << _("No errors occured.") << std::endl;
if (vm.count("fsyntax-only"))
{
return EXIT_SUCCESS;
}
}
}
return EXIT_SUCCESS;
}