TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: JARI LAAKSONEN
date: 1997-03-28 21:57:00
subject: 10/16 Comment utilities

========== CSplit: Version 2.0 ==========
========== CSplit: Begin part 10/16 ==========
}
/* CommentParser::init
 *   initialization function which opens the input file.
 *   Action processors that don't need output file can use this function.
 */
int CommentParser::init (char *file, State initState)
{
    itsState = initState;
    OutFile = stdout;
    if ((InFile = fopen (file, "r")) == NULL)
    {
        fprintf (stderr, CannotOpen, file, "in");
        return 0;
    }
    lines = 1;
    return 1;
}
/* CommentParser::unInit
 *  Closes input and output files.
 */
void CommentParser::unInit()
{
    if (bOutFile)
        fclose (OutFile);
    fclose (InFile);
    fflush (stdout);
}
#define LOG_STATE(s)                         \
  case s:                                    \
    fprintf (OutFile, "\t\t\t\t| " #s "\n"); \
    break;
void CommentParser::logState()
{
    if (itsPrevState == itsState)
        return;
    switch (itsState)
    {
    LOG_STATE(NormalInput)
    LOG_STATE(InsideString)
    LOG_STATE(InsideChar)
    LOG_STATE(BeginComment)
    LOG_STATE(InsideEscape)
    LOG_STATE(InCppComment)
    LOG_STATE(InCComment)
    LOG_STATE(StarInCppComment)
    LOG_STATE(StarInCComment)
    }
}
/* CommentParser::processState
 *  The heart of the comment parser. A finite state machine which does
 *  nothing else than determines the current state based on the events.
 */
void CommentParser::processState (Event theEvent)
{
    if (itsState != InsideEscape)
        itsPrevState = itsState;
    if (theEvent == FOUND_BACKSLASH)           // Escaped character.
    {
        itsState = InsideEscape;
        return;
    }
    switch (itsState)
    {
    case NormalInput:
        switch (theEvent)
        {
        case FOUND_QUOTE:
            itsState = InsideString;
            break;
        case FOUND_SINGLEQUOTE:
            itsState = InsideChar;
            break;
        case FOUND_SLASH:
            itsState = BeginComment;  // to be investigated: begin of \
\
comment.
            break;
        }
        break;
    case InsideString:
        switch (theEvent)
        {
        case FOUND_QUOTE:
            itsState = NormalInput;     // End of the string.
            break;
        }
        break;
    case InsideChar:
        switch (theEvent)
        {
        case FOUND_SINGLEQUOTE:
            itsState = NormalInput;     // End of the character constant.
            break;
        }
        break;
    // We have found a '/', maybe this is a comment...
    case BeginComment:
        switch (theEvent)
        {
        case FOUND_SLASH:
            itsState = InCppComment;    // Yes, it's a C++ comment.
            break;
        case FOUND_STAR:
            itsState = InCComment;      // Yes, it's a C comment.
            break;
        default:
            itsState = NormalInput;     // No, just a slash.
            break;
        }
        break;
    case InsideEscape:
        itsState = itsPrevState;  // Found backslash -- restore \
\
previous state.
        break;
    case InCppComment:
        switch (theEvent)
        {
        case FOUND_NL:
            itsState = NormalInput;     // Newline is end of C++ comment.
            break;
        case FOUND_STAR:
            itsState = StarInCppComment;
            break;
========== CSplit: End part 10/16 crc: 7efe ==========
        // Albert                      email: jla@to.icl.fi
--- GoldED/2 2.50+
---------------
* Origin: Albert's Point/2 in Finland, Europe (2:221/360.20)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.