Skip to content

Commit

Permalink
[lexer] Fix problem with lexing multi-line strings
Browse files Browse the repository at this point in the history
  • Loading branch information
thoni56 committed Sep 5, 2023
1 parent 4269749 commit b23d562
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/lexembuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,17 @@ void putStringConstantLexem(LexemBuffer *lb, CharacterBuffer *cb, int lexemStart
size++;
if (size < MAX_LEXEM_SIZE - 10)
putLexemChar(lb, ch);
/* TODO escape sequences */
if (ch == '\n') {
/* Escaped newline inside string */
cb->lineNumber++;
cb->lineBegin = cb->nextUnread;
cb->columnOffset = 0;
}
continue;
/* TODO other escape sequences */
ch = 0; /* TODO Not sure why 0, but 'continue' does not work... */
}
if (ch == '\n') {
/* Unescaped newline inside string */
cb->lineNumber++;
cb->lineBegin = cb->nextUnread;
cb->columnOffset = 0;
Expand Down
16 changes: 16 additions & 0 deletions tests/test_multiline_string_lexing/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include ../Makefile.boilerplate

COMMAND = ../../src/c-xref -xrefrc .c-xrefrc source.c

# This test will always succeed unless c-xref fails, it used to hang on unterminated macro
$(TEST): clean
@$(COMMAND) -o output.tmp
@$(NORMALIZE) output.tmp >> output
@$(OK)
@exit 0

clean:
@-rm -rf CXrefs output.log output.tmp output

debug:
gdb --args $(COMMAND) -log=log -trace
9 changes: 9 additions & 0 deletions tests/test_multiline_string_lexing/source.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define svg(...) printf(__VA_ARGS__)

svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");

char *multi_line = "
some more characters\
";

0 comments on commit b23d562

Please sign in to comment.