Skip to content

Commit 1889ed5

Browse files
authored
add location tracking for merged strings
1 parent ea1af98 commit 1889ed5

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

parser/metac_expr_parser.c

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -739,33 +739,56 @@ metac_expr_t* MetaCParser_ParsePrimaryExpr(metac_parser_t* self, parse_expr_flag
739739
}
740740
else if (tokenType == tok_string)
741741
{
742-
// result = GetOrAddStringLiteral(_string_table, currentToken);
743-
MetaCParser_Match(self, tok_string);
742+
// Match the initial string token
743+
currentToken = MetaCParser_Match(self, tok_string);
744+
745+
// Extract the length and pointer to the string
744746
uint32_t stringLength = LENGTH_FROM_STRING_KEY(currentToken->StringKey);
745-
uint32_t currentLen = LENGTH_FROM_STRING_KEY(currentToken->StringKey);
746-
uint32_t adjacentStrings = 1;
747747
const char* currentStringP = IdentifierPtrToCharPtr(&self->Lexer->StringTable, currentToken->StringPtr);
748-
uint32_t stringHash = crc32c_nozero(~0, currentStringP, currentLen);
749-
char stringBuffer[8192]; // let's just hope we don't need to concat huge strings;
748+
749+
// Initialize hash and other variables
750+
uint32_t stringHash = crc32c_nozero(~0, currentStringP, stringLength);
751+
uint32_t currentLen = stringLength;
752+
uint32_t adjacentStrings = 1;
753+
754+
// A buffer to hold concatenated strings
755+
char stringBuffer[8192];
756+
757+
// A pointer that tracks the current position in the buffer
750758
char* stringBufferP = stringBuffer;
751759

752-
result = AllocNewExpr(expr_string);
760+
// Allocate result expression
761+
metac_expr_t* result = AllocNewExpr(expr_string);
753762

754-
for(metac_token_t* peekToken = 0; (peekToken = MetaCParser_PeekToken(self, 1))->TokenType == tok_string;)
763+
// Process adjacent string tokens
764+
while (MetaCParser_PeekMatch(self, tok_string, 1))
755765
{
756-
MetaCParser_Match(self, tok_string);
766+
// Match the next token and expand the location
767+
currentToken = MetaCParser_Match(self, tok_string);
768+
MetaCLocation_Expand(&loc, LocationFromToken(self, currentToken));
769+
770+
// Concatenate the string into the buffer
757771
memcpy(stringBufferP, currentStringP, currentLen);
758-
stringBufferP += currentLen;
772+
stringBufferP += currentLen; // Move the buffer pointer to the next free space
759773

760-
currentLen = LENGTH_FROM_STRING_KEY(peekToken->StringKey);
761-
currentStringP = IdentifierPtrToCharPtr(&self->Lexer->StringTable, peekToken->StringPtr);
774+
// Update current string and length for the next token
775+
currentLen = LENGTH_FROM_STRING_KEY(currentToken->StringKey);
776+
currentStringP = IdentifierPtrToCharPtr(&self->Lexer->StringTable, currentToken->StringPtr);
777+
778+
// Update the hash and total length
762779
stringHash = crc32c_nozero(stringHash, currentStringP, currentLen);
763780
stringLength += currentLen;
764781

765-
adjacentStrings += 1;
766-
if (stringLength >= sizeof(stringBuffer)) { assert(!"concat string too long"); }
782+
// Increment the count of adjacent strings
783+
adjacentStrings++;
784+
785+
// Check for buffer overflow
786+
if (stringLength >= sizeof(stringBuffer)) {
787+
assert(!"Concatenated string too long");
788+
}
767789
}
768790

791+
// If there was only one string, no need to concatenate
769792
if (adjacentStrings == 1)
770793
{
771794
result->StringPtr = RegisterString(self, currentToken);
@@ -774,20 +797,17 @@ metac_expr_t* MetaCParser_ParsePrimaryExpr(metac_parser_t* self, parse_expr_flag
774797
}
775798
else
776799
{
777-
// for the last one
800+
// For multiple concatenated strings, copy the last part
778801
memcpy(stringBufferP, currentStringP, currentLen);
779-
// we can now compute the string key
802+
803+
// Verify the hash matches the concatenated string
780804
assert(crc32c_nozero(~0, stringBuffer, stringLength) == stringHash);
781805

806+
// Register the concatenated string
782807
result->StringKey = STRING_KEY(stringHash, stringLength);
783-
// GetOrAddIdentifier copies the string into string table memory
784-
// therefore we can just concat it into a temporary buffer
785-
result->StringPtr = GetOrAddIdentifier(&self->StringTable,
786-
result->StringKey, stringBuffer);
808+
result->StringPtr = GetOrAddIdentifier(&self->StringTable, result->StringKey, stringBuffer);
787809
result->Hash = result->StringKey;
788-
789810
}
790-
//PushOperand(result);
791811
}
792812
else if (tokenType == tok_char)
793813
{
@@ -1765,7 +1785,7 @@ metac_expr_t* MetaCParser_ApplyOp(metac_parser_t* self, metac_expr_kind_t op)
17651785
}
17661786
else if (op == expr_assert)
17671787
{
1768-
1788+
17691789
}
17701790
else if (IsUnaryExp(op))
17711791
{

0 commit comments

Comments
 (0)