frontend: lexer: Create identifier token for names

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-03-21 10:16:43 -04:00
parent 0dc25fe685
commit ccf717c6a3
2 changed files with 64 additions and 0 deletions

View File

@@ -10,12 +10,14 @@
#include <sys/types.h> #include <sys/types.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include "frontend/token.h" #include "frontend/token.h"
#include "frontend/lexer.h" #include "frontend/lexer.h"
#include "common/ptrbox.h"
/* /*
* Test if a given character counts as a whitespace character * Test if a given character counts as a whitespace character
@@ -142,6 +144,61 @@ lexer_consume(struct quip_state *state, bool skip_ws)
return '\0'; return '\0';
} }
/*
* Scan a name and create a token if valid
*
* @state: Quip state
* @lc: Last character
* @tokres: Token result written here
*
* Returns zero on success
*/
static int
lexer_scan_name(struct quip_state *state, int lc, struct token *tokres)
{
char c, *buf;
size_t bufsz, bufcap;
if (state == NULL || tokres == NULL) {
return -1;
}
if (!isalpha(lc) && lc != '_') {
return -1;
}
bufsz = 0;
bufcap = 8;
if ((buf = malloc(bufcap)) == NULL) {
return -1;
}
buf[bufsz++] = lc;
for (;;) {
c = lexer_consume(state, false);
if (!isalnum(c) && c != '_') {
buf[bufsz] = '\0';
lexer_putback(state, c);
break;
}
buf[bufsz++] = c;
if (bufsz >= bufcap - 1) {
bufcap += 8;
buf = realloc(buf, bufcap);
}
if (buf == NULL) {
return -1;
}
}
tokres->type = TT_NAME;
tokres->s = ptrbox_strdup(&state->ptrbox, buf);
free(buf);
return 0;
}
int int
lexer_scan(struct quip_state *state, struct token *tokres) lexer_scan(struct quip_state *state, struct token *tokres)
{ {
@@ -170,6 +227,12 @@ lexer_scan(struct quip_state *state, struct token *tokres)
lexer_putback(state, c); lexer_putback(state, c);
tokres->type = TT_COLON; tokres->type = TT_COLON;
return 0; return 0;
default:
if (lexer_scan_name(state, c, tokres) == 0) {
return 0;
}
break;
} }
return -1; return -1;

View File

@@ -31,6 +31,7 @@ struct token {
tt_t type; tt_t type;
union { union {
char c; char c;
char *s;
}; };
}; };