Skip to content

Commit

Permalink
[modules] Introduce putStringConstantLexem()
Browse files Browse the repository at this point in the history
  • Loading branch information
thoni56 committed Aug 22, 2023
1 parent ff3b7eb commit b6daa62
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 47 deletions.
46 changes: 46 additions & 0 deletions src/lexembuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "commons.h"
#include "globals.h"
#include "lexem.h"
#include "options.h"

//
// Basic manipulation, these should preferably be private in favour
Expand Down Expand Up @@ -200,6 +201,51 @@ int putIdentifierLexem(LexemBuffer *lexemBuffer, CharacterBuffer *characterBuffe
return ch;
}

void putStringConstantLexem(LexemBuffer *lb, CharacterBuffer *cb, int lexemStartingColumn) {
int ch;
int line = lineNumberFrom(cb);
int size = 0;

putLexemCode(lb, STRING_LITERAL);
do {
ch = getChar(cb);
size++;
if (ch != '\"'
&& size < MAX_LEXEM_SIZE - 10) /* WTF is 10? Perhaps required space for position info? */
putLexemChar(lb, ch);
if (ch == '\\') {
ch = getChar(cb);
size++;
if (size < MAX_LEXEM_SIZE - 10)
putLexemChar(lb, ch);
/* TODO escape sequences */
if (ch == '\n') {
cb->lineNumber++;
cb->lineBegin = cb->nextUnread;
cb->columnOffset = 0;
}
continue;
}
if (ch == '\n') {
cb->lineNumber++;
cb->lineBegin = cb->nextUnread;
cb->columnOffset = 0;
if (options.strictAnsi && (options.debug || options.errors)) {
warningMessage(ERR_ST, "string constant through end of line");
}
}
// in Java CR LF can't be a part of string, even there
// are benchmarks making Xrefactory coredump if CR or LF
// is a part of strings
} while (ch != '\"' && (ch != '\n' || !options.strictAnsi) && ch != -1);
if (ch == -1 && options.mode != ServerMode) {
warningMessage(ERR_ST, "string constant through EOF");
}
terminateLexemString(lb);
putLexemPositionFields(lb, fileNumberFrom(cb), lineNumberFrom(cb), lexemStartingColumn);
putLexemLines(lb, lineNumberFrom(cb) - line);
}

void putCharLiteralLexem(LexemBuffer *lb, CharacterBuffer *cb, int lexemStartingColumn,
int length, unsigned chval) {
putLexemCode(lb, CHAR_LITERAL);
Expand Down
2 changes: 1 addition & 1 deletion src/lexembuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ extern void moveLexemStreamWriteToBackpatchPositonWithOffset(LexemBuffer *lb, in
extern int strlenOfBackpatchedIdentifier(LexemBuffer *lb);

/* Put elementary values */
extern void putLexemChar(LexemBuffer *lb, char ch);
extern void putLexemCode(LexemBuffer *lb, LexemCode lexem);

/* Put semantically meaningful complete lexems including position, string, value, ... */
Expand All @@ -53,6 +52,7 @@ extern void putIntegerLexem(LexemBuffer *lb, LexemCode lexem, long unsigned valu
extern void putCompletionLexem(LexemBuffer *lb, CharacterBuffer *cb, int len);
extern void putFloatingPointLexem(LexemBuffer *lb, LexemCode lexem, CharacterBuffer *cb,
int lexemStartingColumn, int lexStartFilePos);
extern void putStringConstantLexem(LexemBuffer *lb, CharacterBuffer *cb, int lexemStartingColumn);
extern void putCharLiteralLexem(LexemBuffer *lb, CharacterBuffer *cb, int lexemStartingColumn,
int length, unsigned chval);
extern void terminateLexemString(LexemBuffer *lb);
Expand Down
2 changes: 2 additions & 0 deletions src/lexembuffer_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "commons.mock"
#include "fileio.mock"
#include "zlib.mock"
#include "options.mock"


static LexemBuffer lb;
Expand Down Expand Up @@ -175,6 +176,7 @@ Ensure(LexemBuffer, can_put_and_get_position) {
assert_that(lb.read, is_equal_to(pointer_after_put));
}

extern void putLexemChar(LexemBuffer *lb, char ch);
Ensure(LexemBuffer, can_backpatch_lexem) {
LexemCode backpatched_lexem = STRING_LITERAL;

Expand Down
47 changes: 1 addition & 46 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,51 +290,6 @@ static int scanIntegerValue(CharacterBuffer *cb, int ch, unsigned long *valueP)
return ch;
}

static void handleStringConstant(CharacterBuffer *cb, LexemBuffer *lb, int lexemStartingColumn) {
int ch;
int line = lineNumberFrom(cb);
int size = 0;

putLexemCode(lb, STRING_LITERAL);
do {
ch = getChar(cb);
size++;
if (ch != '\"'
&& size < MAX_LEXEM_SIZE - 10) /* WTF is 10? Perhaps required space for position info? */
putLexemChar(lb, ch);
if (ch == '\\') {
ch = getChar(cb);
size++;
if (size < MAX_LEXEM_SIZE - 10)
putLexemChar(lb, ch);
/* TODO escape sequences */
if (ch == '\n') {
cb->lineNumber++;
cb->lineBegin = cb->nextUnread;
cb->columnOffset = 0;
}
continue;
}
if (ch == '\n') {
cb->lineNumber++;
cb->lineBegin = cb->nextUnread;
cb->columnOffset = 0;
if (options.strictAnsi && (options.debug || options.errors)) {
warningMessage(ERR_ST, "string constant through end of line");
}
}
// in Java CR LF can't be a part of string, even there
// are benchmarks making Xrefactory coredump if CR or LF
// is a part of strings
} while (ch != '\"' && (ch != '\n' || !options.strictAnsi) && ch != -1);
if (ch == -1 && options.mode != ServerMode) {
warningMessage(ERR_ST, "string constant through EOF");
}
putLexemChar(lb, 0); /* Terminate string */
putLexemPositionFields(lb, fileNumberFrom(cb), lineNumberFrom(cb), lexemStartingColumn);
putLexemLines(lb, lineNumberFrom(cb) - line);
}

static int handleCharConstant(CharacterBuffer *cb, LexemBuffer *lb, int lexemStartingColumn) {
int fileOffsetForLexemStart, ch;
unsigned chval = 0;
Expand Down Expand Up @@ -732,7 +687,7 @@ bool buildLexemFromCharacters(CharacterBuffer *cb, LexemBuffer *lb) {
}

case '\"':
handleStringConstant(cb, lb, lexemStartingColumn);
putStringConstantLexem(lb, cb, lexemStartingColumn);
ch = getChar(cb);
goto nextLexem;

Expand Down

0 comments on commit b6daa62

Please sign in to comment.