Skip to content

Commit 4bbd4cd

Browse files
committed
fixed #462 - added CLI option -l to print line numbers
1 parent 4bbd1bf commit 4bbd4cd

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ int main(int argc, char **argv)
1919
const char *filename = nullptr;
2020
bool use_istream = false;
2121
bool fail_on_error = false;
22+
bool linenrs = false;
2223

2324
// Settings..
2425
simplecpp::DUI dui;
@@ -75,6 +76,10 @@ int main(int argc, char **argv)
7576
fail_on_error = true;
7677
found = true;
7778
break;
79+
case 'l':
80+
linenrs = true;
81+
found = true;
82+
break;
7883
}
7984
if (!found) {
8085
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -107,6 +112,7 @@ int main(int argc, char **argv)
107112
std::cout << " -q Quiet mode (no output)." << std::endl;
108113
std::cout << " -is Use std::istream interface." << std::endl;
109114
std::cout << " -e Output errors only." << std::endl;
115+
std::cout << " -l Print lines numbers." << std::endl;
110116
std::exit(0);
111117
}
112118

@@ -137,7 +143,7 @@ int main(int argc, char **argv)
137143
// Output
138144
if (!quiet) {
139145
if (!error_only)
140-
std::cout << outputTokens.stringify() << std::endl;
146+
std::cout << outputTokens.stringify(linenrs) << std::endl;
141147

142148
for (const simplecpp::Output &output : outputList) {
143149
std::cerr << output.location.file() << ':' << output.location.line << ": ";

simplecpp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void simplecpp::TokenList::dump() const
567567
std::cout << stringify() << std::endl;
568568
}
569569

570-
std::string simplecpp::TokenList::stringify() const
570+
std::string simplecpp::TokenList::stringify(bool linenrs) const
571571
{
572572
std::ostringstream ret;
573573
Location loc(files);
@@ -580,6 +580,8 @@ std::string simplecpp::TokenList::stringify() const
580580
while (tok->location.line > loc.line) {
581581
ret << '\n';
582582
loc.line++;
583+
if (linenrs)
584+
ret << loc.line << ": ";
583585
}
584586

585587
if (sameline(tok->previous, tok))

simplecpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ namespace simplecpp {
222222
void push_back(Token *tok);
223223

224224
void dump() const;
225-
std::string stringify() const;
225+
std::string stringify(bool linenrs = false) const;
226226

227227
void readfile(Stream &stream, const std::string &filename=std::string(), OutputList *outputList = nullptr);
228228
void constFold();

0 commit comments

Comments
 (0)