VirtualBox

Ignore:
Timestamp:
Feb 1, 2025 7:20:09 PM (4 weeks ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
167286
Message:

Runtime/common/script/scriptlex.cpp: Fix C string literal scanning and add some helper APIs to create tokens for errors and identifiers, bugref:10733

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/script/scriptlex.cpp

    r106061 r108014  
    422422
    423423
    424 /**
    425  * Produce an error token with the given error message.
    426  *
    427  * @returns IPRT status code.
    428  * @param   pThis                  The lexer state.
    429  * @param   pTok                   The token to fill.
    430  * @param   rc                     The status code to use in the message.
    431  * @param   pszMsg                 The format string for the error message.
    432  * @param   ...                    Arguments to the format string.
    433  */
    434 static int rtScriptLexProduceTokError(PRTSCRIPTLEXINT pThis, PRTSCRIPTLEXTOKEN pTok,
    435                                       int rc, const char *pszMsg, ...)
    436 {
     424RTDECL(int) RTScriptLexProduceTokError(RTSCRIPTLEX hScriptLex, PRTSCRIPTLEXTOKEN pTok,
     425                                       int rc, const char *pszMsg, ...)
     426{
     427    PRTSCRIPTLEXINT pThis = hScriptLex;
     428
    437429    va_list va;
    438430    va_start(va, pszMsg);
     
    447439
    448440    return rc;
     441}
     442
     443
     444RTDECL(int) RTScriptLexProduceTokIde(RTSCRIPTLEX hScriptLex, PRTSCRIPTLEXTOKEN pTok, const char *pszIde, size_t cchIde)
     445{
     446    PRTSCRIPTLEXINT pThis = hScriptLex;
     447
     448    /* Insert into string cache. */
     449    pTok->enmType = RTSCRIPTLEXTOKTYPE_IDENTIFIER;
     450    pTok->Type.Id.pszIde = RTStrCacheEnterN(pThis->hStrCacheId, pszIde, cchIde);
     451    if (RT_UNLIKELY(!pTok->Type.Id.pszIde))
     452        return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Out of memory inserting identifier into string cache");
     453
     454    pTok->PosEnd = pThis->Pos;
     455    return VINF_SUCCESS;
    449456}
    450457
     
    476483            break;
    477484        default:
    478             rtScriptLexProduceTokError(pThis, pTok, VERR_INVALID_PARAMETER,
     485            RTScriptLexProduceTokError(pThis, pTok, VERR_INVALID_PARAMETER,
    479486                                       "Lexer: The match contains an invalid token type: %d\n",
    480487                                       pTok->enmType);
     
    541548            pThis->rcRdr = pThis->pCfg->pfnProdDef(pThis, ch, pTok, pThis->pCfg->pvProdDefUser);
    542549        else
    543             rtScriptLexProduceTokError(pThis, pTok, VERR_INVALID_PARAMETER,
     550            RTScriptLexProduceTokError(pThis, pTok, VERR_INVALID_PARAMETER,
    544551                                       "Lexer: Invalid character found in input: %c\n",
    545552                                       ch);
     
    974981    if (   idx == sizeof(aszIde) - 1
    975982        && rtScriptLexLocateChInStrConsume(hScriptLex, ch, pszCharSet))
    976         return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_BUFFER_OVERFLOW, "Lexer: Identifier exceeds the allowed length");
     983        return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_BUFFER_OVERFLOW, "Lexer: Identifier exceeds the allowed length");
    977984
    978985    /* Insert into string cache. */
     
    980987    pTok->Type.Id.pszIde = RTStrCacheEnterN(pThis->hStrCacheId, &aszIde[0], idx);
    981988    if (RT_UNLIKELY(!pTok->Type.Id.pszIde))
    982         return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Out of memory inserting identifier into string cache");
     989        return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Out of memory inserting identifier into string cache");
    983990
    984991    pTok->PosEnd = pThis->Pos;
     
    10341041    int rc = rtScriptLexScanStringLiteralChAdd(pThis, '\0', idxChCur);
    10351042    if (RT_FAILURE(rc))
    1036         return rtScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
     1043        return RTScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
    10371044
    10381045    ch = RTScriptLexGetCh(hScriptLex);
     
    10401047    {
    10411048        if (ch == '\0')
    1042             return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_EOF, "Lexer: End of stream before closing string literal terminal");
     1049            return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_EOF, "Lexer: End of stream before closing string literal terminal");
    10431050        else if (ch == '\"')
    10441051        {
     1052            RTScriptLexConsumeCh(hScriptLex);
     1053
    10451054            /* End of string, add it to the string literal cache and build the token. */
    10461055            pTok->enmType = RTSCRIPTLEXTOKTYPE_STRINGLIT;
     
    10481057            pTok->Type.StringLit.pszString = RTStrCacheEnterN(pThis->hStrCacheStringLit, pThis->pszStrLit, idxChCur);
    10491058            if (RT_UNLIKELY(!pTok->Type.StringLit.pszString))
    1050                 return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Error adding string literal to the cache");
     1059                return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Error adding string literal to the cache");
    10511060            else
    10521061                break;
     
    11041113                default:
    11051114                    /* Not supported for now. */
    1106                     return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_NOT_SUPPORTED, "Lexer: Invalid/unsupported escape sequence");
     1115                    return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_NOT_SUPPORTED, "Lexer: Invalid/unsupported escape sequence");
    11071116            }
    11081117        }
     
    11121121            idxChCur++;
    11131122        else
    1114             return rtScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
     1123            return RTScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
    11151124
    11161125        ch = RTScriptLexConsumeChEx(hScriptLex, RTSCRIPT_LEX_CONV_F_NOTHING);
     
    11321141    int rc = rtScriptLexScanStringLiteralChAdd(pThis, '\0', idxChCur);
    11331142    if (RT_FAILURE(rc))
    1134         return rtScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
     1143        return RTScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
    11351144
    11361145    ch = RTScriptLexGetChEx(hScriptLex, RTSCRIPT_LEX_CONV_F_NOTHING);
     
    11381147    {
    11391148        if (ch == '\0')
    1140             return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_EOF, "Lexer: End of stream before closing string literal terminal");
     1149            return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_EOF, "Lexer: End of stream before closing string literal terminal");
    11411150        else if (ch == '\'')
    11421151        {
     
    11531162                pTok->Type.StringLit.pszString = RTStrCacheEnterN(pThis->hStrCacheStringLit, pThis->pszStrLit, idxChCur);
    11541163                if (RT_UNLIKELY(!pTok->Type.StringLit.pszString))
    1155                     return rtScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Error adding string literal to the cache");
     1164                    return RTScriptLexProduceTokError(hScriptLex, pTok, VERR_NO_STR_MEMORY, "Lexer: Error adding string literal to the cache");
    11561165                else
    11571166                    break;
     
    11641173            idxChCur++;
    11651174        else
    1166             return rtScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
     1175            return RTScriptLexProduceTokError(hScriptLex, pTok, rc, "Lexer: Error adding character to string literal");
    11671176        ch = RTScriptLexConsumeChEx(hScriptLex, RTSCRIPT_LEX_CONV_F_NOTHING);
    11681177    }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette