nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.55k stars 7.69k forks source link

stb_c_lexer: invalid char literal #1652

Open Wonshtrum opened 3 months ago

Wonshtrum commented 3 months ago

The stb C lexer consumes one extra character when parsing a char literal.

This can be demonstrated with this code:

stb_lexer l = {0};
char source[] = "char lit = 'A';";
char store[1024];
stb_c_lexer_init(&l, source, source+sizeof(source)-1, store, sizeof(store));

while (stb_c_lexer_get_token(&l)) {
    printf("%d: %.*s\n", l.token, l.where_lastchar-l.where_firstchar+1, l.where_firstchar);
}

which outputs:

260: char
260: lit
61: =
263: 'A';

when we expected:

260: char
260: lit
61: =
263: 'A'
59: ;