Definitions:
- A line is a possibly empty sequence of characters terminated by a “newline character.” A “newline character” is a character that cannot be a component of the aforementioned character sequence.
- A line number comment is a character sequence that does not alter the meaning of source code in the programming language of your submission.
It contains a decimal representation of the (integral) unique line number associated with the line of input.
(A conventional notation is sufficient, e. g.
4.2E1
for42
.) - A content line is a line that conveys meaning in the programming language of your submission.
Task:
- Write a program (or function) that numbers all lines, but at least all content lines. The program shall print a line number comment followed by the respective processed (otherwise unaltered) input line. The function shall return a value accordingly.
Conditions:
- Numbered lines must be numbered in ascending order in the order of input (i. e. not in the order a processor for the respective programming language might possibly process the input).
- Line numbers must be consecutive. That means you may not omit numbers.
- The first line is identified either by the line number zero (
0
) or one (1
). The behavior must be consistent for all possible input. - Line numbers must reproduce: Feeding the output back as input again produces the same line numbers.
Accommodations:
- You may assume that input is valid source code in your submission’s programming language.
- You may assume that no token is spread across multiple lines (e. g. multiline string literals or backslash-continuation), thus insertion of a line number comment is guaranteed not to alter the source code’s meaning.
- You may assume input consists of lines. Numbering of non-lines (that is empty input or input not terminated by a newline-character) is implementation-dependent.
Test cases:
C (note that tabulator characters are preserved):
#include <stdio.h>
int main()
{
char c;
c = getchar();
do
{
putchar(c);
}
while (c);
}
/* 0*/#include <stdio.h>
/* 1*/int main()
/* 2*/{
/* 3*/ char c;
/* 4*/ c = getchar();
/* 5*/ do
/* 6*/ {
/* 7*/ putchar(c);
/* 8*/ }
/* 9*/ while (c);
/*10*/}
Pascal (note that it is not specified that input is indeed source code):
Esolanging fruit is hanging low.
{ 1 }Esolanging fruit is hanging low.
If you input that output back in again, it must reproduce the same line number comments:
{ 1 }{ 1 }Esolanging fruit is hanging low.
SQL (note that only content lines were numbered):
-- populate foo with some data
insert into foo values (1, 'bar');
insert into foo values (2, 'foobar');
select * from foo;
-- populate foo with some data
/*0*/insert into foo values (1, 'bar');
/*1*/insert into foo values (2, 'foobar');
/*2*/select * from foo;
AssertionError
unless the stack trace contained the phrasefoo.py
, which was expected to come from a file name but could also appear as a line comment in the context. \$\endgroup\$