Skip to content

Commit 1420eb6

Browse files
authored
fixed #462 - added CLI option -l to print line numbers (#463)
1 parent 32cccf5 commit 1420eb6

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
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;
@@ -102,6 +103,10 @@ int main(int argc, char **argv)
102103
found = true;
103104
fail_on_error = true;
104105
break;
106+
case 'l':
107+
linenrs = true;
108+
found = true;
109+
break;
105110
}
106111
if (!found) {
107112
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -135,6 +140,7 @@ int main(int argc, char **argv)
135140
std::cout << " -is Use std::istream interface." << std::endl;
136141
std::cout << " -e Output errors only." << std::endl;
137142
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
143+
std::cout << " -l Print lines numbers." << std::endl;
138144
std::exit(0);
139145
}
140146

@@ -165,7 +171,7 @@ int main(int argc, char **argv)
165171
// Output
166172
if (!quiet) {
167173
if (!error_only)
168-
std::cout << outputTokens.stringify() << std::endl;
174+
std::cout << outputTokens.stringify(linenrs) << std::endl;
169175

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

simplecpp.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,24 +553,33 @@ 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);
565+
bool filechg = true;
565566
for (const Token *tok = cfront(); tok; tok = tok->next) {
566567
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
567568
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
568569
loc = tok->location;
570+
filechg = true;
571+
}
572+
573+
if (linenrs && filechg) {
574+
ret << loc.line << ": ";
575+
filechg = false;
569576
}
570577

571578
while (tok->location.line > loc.line) {
572579
ret << '\n';
573580
loc.line++;
581+
if (linenrs)
582+
ret << loc.line << ": ";
574583
}
575584

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

simplecpp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ namespace simplecpp {
284284
}
285285
void push_back(Token *tok);
286286

287-
void dump() const;
288-
std::string stringify() const;
287+
void dump(bool linenrs = false) const;
288+
std::string stringify(bool linenrs = false) const;
289289

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

0 commit comments

Comments
 (0)