@@ -726,6 +726,133 @@ void simplecpp::TokenList::lineDirective(unsigned int fileIndex_, unsigned int l
726726
727727static const std::string COMMENT_END (" */" );
728728
729+ /* *
730+ * Read a name or a number.
731+ * In a number, a ' digit separator is removed, so 1'000 gives "1000".
732+ * The stream is left after the last character of the token.
733+ * @param stream the stream to read from
734+ * @param ch the first character, already read from the stream
735+ * @return the name or number
736+ */
737+ static simplecpp::TokenString readNameOrNumber (simplecpp::TokenList::Stream &stream, unsigned char ch)
738+ {
739+ simplecpp::TokenString currentToken;
740+ const bool num = !!std::isdigit (ch);
741+ while (stream.good () && isNameChar (ch)) {
742+ currentToken += ch;
743+ ch = stream.readChar ();
744+ if (num && ch==' \' ' && isNameChar (stream.peekChar ()))
745+ ch = stream.readChar ();
746+ }
747+ stream.ungetChar ();
748+ return currentToken;
749+ }
750+
751+ /* *
752+ * Read a // comment, up to but not including the newline.
753+ * A backslash followed by a newline continues the comment on the next line.
754+ * The backslash, and any spaces or tabs after it, are then replaced by a
755+ * newline in the comment text.
756+ * @param stream the stream to read from
757+ * @param ch the first '/', already read from the stream
758+ * @param location the location of the comment, used for warnings
759+ * @param multiline incremented for each continued line
760+ * @param outputList if not null, a portability warning is added when there is
761+ * whitespace between the backslash and the newline
762+ * @return the comment text, including the leading //
763+ */
764+ static simplecpp::TokenString readLineComment (simplecpp::TokenList::Stream &stream, unsigned char ch, const simplecpp::Location &location, unsigned int &multiline, simplecpp::OutputList *outputList)
765+ {
766+ simplecpp::TokenString currentToken;
767+ while (stream.good () && ch != ' \n ' ) {
768+ currentToken += ch;
769+ ch = stream.readChar ();
770+ if (ch == ' \\ ' ) {
771+ simplecpp::TokenString tmp;
772+ char tmp_ch = ch;
773+ while ((stream.good ()) && (tmp_ch == ' \\ ' || tmp_ch == ' ' || tmp_ch == ' \t ' )) {
774+ tmp += tmp_ch;
775+ tmp_ch = stream.readChar ();
776+ }
777+ if (!stream.good ()) {
778+ break ;
779+ }
780+
781+ if (tmp_ch != ' \n ' ) {
782+ currentToken += tmp;
783+ } else {
784+ const simplecpp::TokenString check_portability = currentToken + tmp;
785+ const std::string::size_type pos = check_portability.find_last_not_of (" \t " );
786+ if (pos < check_portability.size () - 1U && check_portability[pos] == ' \\ ' )
787+ portabilityBackslash (outputList, location);
788+ ++multiline;
789+ tmp_ch = stream.readChar ();
790+ currentToken += ' \n ' ;
791+ }
792+ ch = tmp_ch;
793+ }
794+ }
795+ if (ch == ' \n ' ) {
796+ stream.ungetChar ();
797+ }
798+ return currentToken;
799+ }
800+
801+ simplecpp::TokenString simplecpp::TokenList::readBlockComment (Stream &stream, unsigned int &multiline)
802+ {
803+ TokenString currentToken = " /*" ;
804+ (void )stream.readChar ();
805+ unsigned char ch = stream.readChar ();
806+ while (stream.good ()) {
807+ currentToken += ch;
808+ if (currentToken.size () >= 4U && endsWith (currentToken, COMMENT_END ))
809+ break ;
810+ ch = stream.readChar ();
811+ }
812+ // multiline..
813+
814+ std::string::size_type pos = 0 ;
815+ while ((pos = currentToken.find (" \\\n " ,pos)) != std::string::npos) {
816+ currentToken.erase (pos,2 );
817+ ++multiline;
818+ }
819+ if (multiline || isLastLinePreprocessor ()) {
820+ pos = 0 ;
821+ while ((pos = currentToken.find (' \n ' ,pos)) != std::string::npos) {
822+ currentToken.erase (pos,1 );
823+ ++multiline;
824+ }
825+ }
826+ return currentToken;
827+ }
828+
829+ void simplecpp::TokenList::readErrorText (Stream &stream, unsigned char ch, Location &location)
830+ {
831+ TokenString currentToken;
832+ char prev = ' ' ;
833+ while (stream.good () && (prev == ' \\ ' || (ch != ' \r ' && ch != ' \n ' ))) {
834+ currentToken += ch;
835+ prev = ch;
836+ ch = stream.readChar ();
837+ }
838+ stream.ungetChar ();
839+ std::string::size_type pos = 0 ;
840+ unsigned int spliced = 0 ;
841+ while ((pos = currentToken.find (' \\ ' , pos)) != std::string::npos) {
842+ if (pos + 1 < currentToken.size () && currentToken[pos + 1 ] == ' \n ' ) {
843+ currentToken.erase (pos, 2 );
844+ ++spliced;
845+ } else {
846+ ++pos;
847+ }
848+ }
849+ if (!currentToken.empty ()) {
850+ push_back (new Token (currentToken, location));
851+ location.adjust (currentToken);
852+ }
853+ location.line += spliced;
854+ }
855+
729856void simplecpp::TokenList::readfile (Stream &stream, const std::string &filename, const DUI &dui, OutputList *outputList)
730857{
731858 unsigned int multiline = 0U ;
@@ -880,105 +1007,24 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
8801007 if (cback () && cback ()->location .line == location.line && cback ()->previous && cback ()->previous ->op == ' #' ) {
8811008 const Token* const ppTok = cback ()->previous ;
8821009 if (ppTok->next && (ppTok->next ->str () == " error" || ppTok->next ->str () == " warning" )) {
883- char prev = ' ' ;
884- while (stream.good () && (prev == ' \\ ' || (ch != ' \r ' && ch != ' \n ' ))) {
885- currentToken += ch;
886- prev = ch;
887- ch = stream.readChar ();
888- }
889- stream.ungetChar ();
890- std::string::size_type pos = 0 ;
891- unsigned int spliced = 0 ;
892- while ((pos = currentToken.find (' \\ ' , pos)) != std::string::npos) {
893- if (pos + 1 < currentToken.size () && currentToken[pos + 1 ] == ' \n ' ) {
894- currentToken.erase (pos, 2 );
895- ++spliced;
896- } else {
897- ++pos;
898- }
899- }
900- if (!currentToken.empty ()) {
901- push_back (new Token (currentToken, location));
902- location.adjust (currentToken);
903- }
904- location.line += spliced;
1010+ readErrorText (stream, ch, location);
9051011 continue ;
9061012 }
9071013 }
9081014
9091015 // number or name
9101016 if (isNameChar (ch)) {
911- const bool num = !!std::isdigit (ch);
912- while (stream.good () && isNameChar (ch)) {
913- currentToken += ch;
914- ch = stream.readChar ();
915- if (num && ch==' \' ' && isNameChar (stream.peekChar ()))
916- ch = stream.readChar ();
917- }
918-
919- stream.ungetChar ();
1017+ currentToken = readNameOrNumber (stream, ch);
9201018 }
9211019
9221020 // comment
9231021 else if (ch == ' /' && stream.peekChar () == ' /' ) {
924- while (stream.good () && ch != ' \n ' ) {
925- currentToken += ch;
926- ch = stream.readChar ();
927- if (ch == ' \\ ' ) {
928- TokenString tmp;
929- char tmp_ch = ch;
930- while ((stream.good ()) && (tmp_ch == ' \\ ' || tmp_ch == ' ' || tmp_ch == ' \t ' )) {
931- tmp += tmp_ch;
932- tmp_ch = stream.readChar ();
933- }
934- if (!stream.good ()) {
935- break ;
936- }
937-
938- if (tmp_ch != ' \n ' ) {
939- currentToken += tmp;
940- } else {
941- const TokenString check_portability = currentToken + tmp;
942- const std::string::size_type pos = check_portability.find_last_not_of (" \t " );
943- if (pos < check_portability.size () - 1U && check_portability[pos] == ' \\ ' )
944- portabilityBackslash (outputList, location);
945- ++multiline;
946- tmp_ch = stream.readChar ();
947- currentToken += ' \n ' ;
948- }
949- ch = tmp_ch;
950- }
951- }
952- if (ch == ' \n ' ) {
953- stream.ungetChar ();
954- }
1022+ currentToken = readLineComment (stream, ch, location, multiline, outputList);
9551023 }
9561024
9571025 // comment
9581026 else if (ch == ' /' && stream.peekChar () == ' *' ) {
959- currentToken = " /*" ;
960- (void )stream.readChar ();
961- ch = stream.readChar ();
962- while (stream.good ()) {
963- currentToken += ch;
964- if (currentToken.size () >= 4U && endsWith (currentToken, COMMENT_END ))
965- break ;
966- ch = stream.readChar ();
967- }
968- // multiline..
969-
970- std::string::size_type pos = 0 ;
971- while ((pos = currentToken.find (" \\\n " ,pos)) != std::string::npos) {
972- currentToken.erase (pos,2 );
973- ++multiline;
974- }
975- if (multiline || isLastLinePreprocessor ()) {
976- pos = 0 ;
977- while ((pos = currentToken.find (' \n ' ,pos)) != std::string::npos) {
978- currentToken.erase (pos,1 );
979- ++multiline;
980- }
981- }
1027+ currentToken = readBlockComment (stream, multiline);
9821028 }
9831029
9841030 // string / char literal
0 commit comments