\RequirePackage[logonly]{trace}
\documentclass{article}
\makeatletter
\newcommand*\newcomment{%
\begingroup
\def\do##1{\catcode`##1=12}%
\dospecials
\endlinechar`\^^M\catcode`\^^M=12\relax
\@comment
}
\begingroup
\catcode`\^^M=12 %
\endlinechar\m@ne%
\gdef\@comment#1^^M{
\def\@stuff{#1}
\ifx\@stuff\endnewcomment\expandafter
\endgroup\else\expandafter\@comment\fi
}
\endgroup
\begingroup
\catcode`\{=12
\catcode`\}=12
\catcode`\!=0
\catcode`\[=1
\catcode`\]=2
\catcode`\\=12
!gdef!endnewcomment[\end{newcomment}]
!endgroup
\traceon
\begin{newcomment}
xxx
xxx
xxx
\end{newcomment}
\traceoff
\stop
\begin{document}
\end{document}
% Log file:
(\end occurred inside a group at level 1)
### semi simple group (level 1) entered at line 32 (\begingroup)
### bottom level
\begin{foo} already opens a group, so you don't need to open another
one in the definition of \newcomment. Alternatively, you can close two
groups (yours and LaTeX's in the \end-definition). If you want the
command to work both as an environment and a command, you will need to
test \@currenvir to see \newcomment was invoked from
\begin{newcomment} or on its own.
Regards,
Bruno
because \begin opens a group and \end is gobbled and does not closes it.
besides \expandafter\endgroup \else is \endgroup \else !