engine/source/preprocessorlib/pp_parser.c
/**
 * Implements the C "stringify" operator.
 */
Implements the C "stringify" operator.
Implements the C " stringify " operator .
HRESULT pp_parser_stringify(pp_parser *self)
{
    TEXTPOS lexerPosition = {1, 0};
    char *contents = (char *)List_Retrieve(&self->ctx->macros);
    pp_parser parser;
    pp_token *token;
    pp_token_Init(&self->token, PP_TOKEN_STRING_LITERAL, "\"",
                  self->token.theTextPosition, 0);
    pp_parser_init(&parser, self->ctx, self->filename, contents, lexerPosition);
    while((token = pp_parser_emit_token(&parser)) && token->theType != PP_TOKEN_EOF)
    {
        char *source = token->theSource;
        bool in_string = false;
        while(*source)
        {
            if(*source == '"')
            {
                strcat(self->token.theSource, "\\\"");
                in_string = !in_string;
            }
            else if(*source == '\\' && in_string)
            {
                strcat(self->token.theSource, "\\\\");
            }
            else
            {
                strncat(self->token.theSource, source, 1);
            }
            if(strlen(self->token.theSource) + 2 > MAX_TOKEN_LENGTH)
            {
                return pp_error(self, "sequence is too long to stringify");
            }
            source++;
        }
    }
    strcat(self->token.theSource, "\"");
    return S_OK;
}
HRESULT pp_parser_stringify ( pp_parser * self ) { TEXTPOS lexerPosition = { 1 , 0 } ; char * contents = ( char * ) List_Retrieve ( & self -> ctx -> macros ) ; pp_parser parser ; pp_token * token ; pp_token_Init ( & self -> token , PP_TOKEN_STRING_LITERAL , " \" " , self -> token . theTextPosition , 0 ) ; pp_parser_init ( & parser , self -> ctx , self -> filename , contents , lexerPosition ) ; while ( ( token = pp_parser_emit_token ( & parser ) ) && token -> theType != PP_TOKEN_EOF ) { char * source = token -> theSource ; bool in_string = false ; while ( * source ) { if ( * source == ' ' ) { strcat ( self -> token . theSource , " \\ \" " ) ; in_string = ! in_string ; } else if ( * source == ' \\ ' && in_string ) { strcat ( self -> token . theSource , " \\ \\ " ) ; } else { strncat ( self -> token . theSource , source , 1 ) ; } if ( strlen ( self -> token . theSource ) + 2 > MAX_TOKEN_LENGTH ) { return pp_error ( self , " " ) ; } source ++ ; } } strcat ( self -> token . theSource , " \" " ) ; return S_OK ; }
Implements the C "stringify" operator.
Implements the C " stringify " operator .