commit 436c4b0fc35ff425c4fe6d6130033995df75c67a
parent 64c8f388a55382122fd60a67fd5dce2c30640133
Author: Eyal Sawady <ecs@d2evs.net>
Date: Sat, 12 Dec 2020 10:40:27 -0500
lex: implement labels
Diffstat:
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/include/lex.h b/include/lex.h
@@ -112,6 +112,7 @@ enum lexical_token {
T_LAST_OPERATOR = T_XOREQ,
// Tokens with additional information
+ T_LABEL,
T_LITERAL,
T_NAME,
diff --git a/src/lex.c b/src/lex.c
@@ -598,6 +598,22 @@ lex3(struct lexer *lexer, struct token *out, uint32_t c)
}
static enum lexical_token
+lex_label(struct lexer *lexer, struct token *out)
+{
+ uint32_t c;
+ while ((c = next(lexer, NULL, true)) != UTF8_INVALID) {
+ if (c > 0x7F || (!isalnum(c) && c != '_')) {
+ push(lexer, c, true);
+ break;
+ }
+ }
+ out->token = T_LABEL;
+ out->name = strdup(lexer->buf);
+ consume(lexer, -1);
+ return out->token;
+}
+
+static enum lexical_token
lex2(struct lexer *lexer, struct token *out, uint32_t c)
{
assert(c != UTF8_INVALID);
@@ -688,6 +704,9 @@ lex2(struct lexer *lexer, struct token *out, uint32_t c)
break;
default:
push(lexer, c, false);
+ if (c <= 0x7F && (isalpha(c) || c == '_')) {
+ return lex_label(lexer, out);
+ }
out->token = T_COLON;
break;
}
@@ -844,6 +863,7 @@ token_finish(struct token *tok)
{
switch (tok->token) {
case T_NAME:
+ case T_LABEL:
free(tok->name);
break;
case T_LITERAL:
@@ -871,6 +891,8 @@ lexical_token_str(enum lexical_token tok)
switch (tok) {
case T_NAME:
return "name";
+ case T_LABEL:
+ return "label";
case T_LITERAL:
return "literal";
case T_EOF:
@@ -962,6 +984,9 @@ token_str(const struct token *tok)
switch (tok->token) {
case T_NAME:
return tok->name;
+ case T_LABEL:
+ snprintf(buf, sizeof(buf), ":%s", tok->name);
+ return buf;
case T_LITERAL:
switch (tok->storage) {
case TYPE_STORAGE_U8: