Fix: error if preview type starts with a digit

This commit is contained in:
Nikita Ivanov 2022-07-11 18:39:36 +05:00
parent e463c0c79b
commit a45979244f
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
3 changed files with 49 additions and 5 deletions

View File

@ -180,10 +180,30 @@ static int preview_type_mime(char **type, char **subtype)
return STAT_OK; return STAT_OK;
} }
static inline void num_is_text(void)
{
lexer_set_opts(lexer, LEX_OPT_NUMISTEXT);
}
static inline void reset_lexer_opts(void)
{
lexer_set_opts(lexer, LEX_OPT_NONE);
}
static int preview_type(char **type, char **subtype, char **ext) static int preview_type(char **type, char **subtype, char **ext)
{ {
CHECK_NULL(preview_type_ext(ext)); int ret;
return preview_type_mime(type, subtype);
num_is_text();
if ((ret = preview_type_ext(ext)) != STAT_NULL)
goto exit;
ret = preview_type_mime(type, subtype);
exit:
reset_lexer_opts();
return ret;
} }
static struct Option *get_option(char *name) static struct Option *get_option(char *name)

View File

@ -23,7 +23,8 @@ typedef struct {
} TokenQueue; } TokenQueue;
struct Lexer { struct Lexer {
unsigned int line, col; enum LexerOpts opts;
unsigned line, col;
struct { struct {
unsigned int line, col; unsigned int line, col;
} tok_pos; } tok_pos;
@ -153,6 +154,7 @@ Lexer *lexer_init(FILE *f)
} }
init_input_buf(&ctx->input_buf, f); init_input_buf(&ctx->input_buf, f);
lexer_set_opts(ctx, LEX_OPT_NONE);
ctx->text_buf = ulist_new(sizeof(char), 1024); ctx->text_buf = ulist_new(sizeof(char), 1024);
ctx->line = ctx->col = 1; ctx->line = ctx->col = 1;
ctx->tok_queue.back = ctx->tok_queue.front = 0; ctx->tok_queue.back = ctx->tok_queue.front = 0;
@ -160,6 +162,11 @@ Lexer *lexer_init(FILE *f)
return ctx; return ctx;
} }
void lexer_set_opts(Lexer *ctx, enum LexerOpts flags)
{
ctx->opts = flags;
}
void lexer_free(Lexer *ctx) void lexer_free(Lexer *ctx)
{ {
ulist_free(ctx->text_buf); ulist_free(ctx->text_buf);
@ -263,12 +270,23 @@ static inline Token read_int(Lexer *ctx)
record_text(ctx); record_text(ctx);
read_while(ctx, isdigit, 1); read_while(ctx, isdigit, 1);
int i = atoi(get_text(ctx));
Token tok;
char *text = get_text(ctx);
/* If NUMISTEXT option is set, do not convert string to integer */
if (ctx->opts & LEX_OPT_NUMISTEXT) {
tok = get_tok(ctx, TOK_STR);
tok.val.s = text;
return tok;
}
int i = atoi(text);
if (!positive) if (!positive)
i *= -1; i *= -1;
Token tok = get_tok(ctx, TOK_INT); tok = get_tok(ctx, TOK_INT);
tok.val.i = i; tok.val.i = i;
return tok; return tok;

View File

@ -10,6 +10,11 @@
typedef struct Lexer Lexer; typedef struct Lexer Lexer;
enum LexerOpts {
LEX_OPT_NONE = 0,
LEX_OPT_NUMISTEXT = 1 << 0,
};
typedef struct { typedef struct {
unsigned int line, col; unsigned int line, col;
enum TokenType { enum TokenType {
@ -32,6 +37,7 @@ typedef struct {
} Token; } Token;
Lexer *lexer_init(FILE *f); Lexer *lexer_init(FILE *f);
void lexer_set_opts(Lexer *ctx, enum LexerOpts flags);
void lexer_free(Lexer *ctx); void lexer_free(Lexer *ctx);
Token lexer_get_token(Lexer *ctx); Token lexer_get_token(Lexer *ctx);
char *lexer_token_type_str(enum TokenType type); char *lexer_token_type_str(enum TokenType type);