I don't know whether this can be done with LuaTeX (maybe yes). But under (pdf)TeX this can't be done in full generality.
First of all let's recall how comments work. TeX reads a line of input and, before starting to transform the characters into tokens, it does some jobs.
It throws away the operating system provided end-of-record byte (if any; some old operating systems don't have it) and everything that remains in the line after it.
It throws away all spaces that might remain at the end of the record (and tabs, under implementations based on Web2C, which are the most commonly used). This independently of category code, as they have not yet been attached to characters.
It adds at the end of the record the current
\endlinechar (none if the parameter is negative or beyond the engine dependent maximum value, which is 255 for TeX and
0x1FFFFF for XeTeX and LuaTeX).
It does what is prescribed by its current state with respect to category code 5 and 10 characters; under normal circumstanes it throws away initial spaces and it possibly inserts a
\par token (see the TeXbook or TeX by Topic for details).
Now it starts tokenizing and it's here where comments are discarded: a category code 14 character causes TeX to ignore it and everything up to the end of the line, the added
One might think to define
% as an active character, which looks for a following
% and, in this case, inserts a category code 14 character in the input. Maybe doing this only in the
document environment, so that comments work as usual in the preamble. This fails in two ways.
If TeX is not expanding macros, the
%% pair would be recognized too late. For example
wouldn't work as expected, because the active
% wouldn't be expanded when the argument to
\parbox is absorbed.
Even if one ensures that no
%% pair is in the argument to a macro, it's impossible to put a character code 14 character in the replacement text of a macro: no category code 0, 5, 9, 14 and 15 character can reach TeX's "stomach", where macro replacement texts are examined and stored in memory (TeXbook, exercise 7.3).
One might think to overcome this limitation by defining
% to look for a following
% and, in this case, to issue a macro
\gobbletoend defined by something like
but, alas, this can't be done for two reasons:
^^M (category code 5) can't reach TeX's stomach and, moreover, TeX wouldn't even see the pair of braces, because, when absorbing that line it would see
^^M which is the ASCII end-of-line and it would throw it away with the rest of the line. So the macro can't have its argument delimited by a category code 5
^^M, which is something like
\obeylines does, but you don't want that every end-of-line that's not preceded by
%% has a final
\par, so the definition ought to be much more complicated.
A possible way is to do like in the following example:
abc %% def
abc % def
The Kant paragraph is just to show that paragraphs are correctly terminated;
amsmath is used to avoid gobbling spaces. Recall, however, that
%% can't appear in the argument to a command.