Skip to content

Commit 3213473

Browse files
committed
fixed #462 - added CLI option -l to print line numbers
1 parent 5783afa commit 3213473

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

main.cpp

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

2223
// Settings..
2324
simplecpp::DUI dui;
@@ -74,6 +75,10 @@ int main(int argc, char **argv)
7475
fail_on_error = true;
7576
found = true;
7677
break;
78+
case 'l':
79+
linenrs = true;
80+
found = true;
81+
break;
7782
}
7883
if (!found) {
7984
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -106,6 +111,7 @@ int main(int argc, char **argv)
106111
std::cout << " -q Quiet mode (no output)." << std::endl;
107112
std::cout << " -is Use std::istream interface." << std::endl;
108113
std::cout << " -e Output errors only." << std::endl;
114+
std::cout << " -l Print lines numbers." << std::endl;
109115
std::exit(0);
110116
}
111117

@@ -136,7 +142,7 @@ int main(int argc, char **argv)
136142
// Output
137143
if (!quiet) {
138144
if (!error_only)
139-
std::cout << outputTokens.stringify() << std::endl;
145+
std::cout << outputTokens.stringify(linenrs) << std::endl;
140146

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

simplecpp.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ void simplecpp::TokenList::push_back(Token *tok)
553553
backToken = tok;
554554
}
555555

556-
void simplecpp::TokenList::dump() const
556+
void simplecpp::TokenList::dump(bool linenrs) const
557557
{
558-
std::cout << stringify() << std::endl;
558+
std::cout << stringify(linenrs) << std::endl;
559559
}
560560

561-
std::string simplecpp::TokenList::stringify() const
561+
std::string simplecpp::TokenList::stringify(bool linenrs) const
562562
{
563563
std::ostringstream ret;
564564
Location loc(files);
@@ -571,6 +571,8 @@ std::string simplecpp::TokenList::stringify() const
571571
while (tok->location.line > loc.line) {
572572
ret << '\n';
573573
loc.line++;
574+
if (linenrs)
575+
ret << loc.line << ": ";
574576
}
575577

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

simplecpp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ namespace simplecpp {
235235
}
236236
void push_back(Token *tok);
237237

238-
void dump() const;
239-
std::string stringify() const;
238+
void dump(bool linenrs = false) const;
239+
std::string stringify(bool linenrs = false) const;
240240

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

0 commit comments

Comments
 (0)