lexer: Add TT_COLONDUB and putback buffer

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-03-21 08:32:14 -04:00
parent cb3b3a6817
commit 7b45b6f260
3 changed files with 53 additions and 1 deletions

View File

@@ -36,6 +36,40 @@ lexer_is_ws(char c)
return false;
}
/*
* Place a character in the lexer putback buffer
*
* @state: Quip state
* @c: Character to insert
*/
static inline void
lexer_putback(struct quip_state *state, char c)
{
if (state == NULL) {
return;
}
state->lex_putback = c;
}
/*
* Pop the last character from the lexer putback
* buffer
*/
static inline char
lexer_putback_pop(struct quip_state *state)
{
char c;
if (state == NULL) {
return '\0';
}
c = state->lex_putback;
state->lex_putback = '\0';
return c;
}
/*
* Consume a single character in a buffered manner from the
* build source file.
@@ -88,6 +122,15 @@ lexer_consume(struct quip_state *state, bool skip_ws)
{
char c;
/*
* If there is anything in the putback buffer, take
* it.
*/
if ((c = lexer_putback_pop(state)) != '\0') {
if (!skip_ws || !lexer_is_ws(c))
return c;
}
while ((c = lexer_buffer_consume(state)) != '\0') {
if (skip_ws && lexer_is_ws(c)) {
continue;
@@ -118,8 +161,14 @@ lexer_scan(struct quip_state *state, struct token *tokres)
tokres->c = c;
return 0;
case ':':
tokres->type = TT_COLON;
tokres->c = c;
if ((c = lexer_consume(state, false)) == ':') {
tokres->type = TT_COLONDUB;
return 0;
}
lexer_putback(state, c);
tokres->type = TT_COLON;
return 0;
}

View File

@@ -23,6 +23,7 @@
* @lex_buf: Used to reduce system call frequency
* @lex_buf_cap: Lexer buffer capacity
* @lex_buf_i: Lexer buffer index
* @lex_putback: Lexer putback buffer
*/
struct quip_state {
int in_fd;
@@ -30,6 +31,7 @@ struct quip_state {
char lex_buf[LEX_FILEBUF_LEN];
size_t lex_buf_cap;
size_t lex_buf_i;
char lex_putback;
};
/*

View File

@@ -19,6 +19,7 @@ typedef enum {
TT_NAME, /* [name] */
TT_NEWLINE, /* [newline] */
TT_COLON, /* ':' */
TT_COLONDUB, /* '::' */
} tt_t;
/*