lang: lex: add new lex token type ids

This commit is contained in:
2025-09-08 16:02:09 +01:00
parent 6e65349d70
commit 7d1b7c5d68
2 changed files with 40 additions and 19 deletions

View File

@@ -45,6 +45,8 @@ enum ivy_keyword {
IVY_KW_ELSE, IVY_KW_ELSE,
IVY_KW_WHILE, IVY_KW_WHILE,
IVY_KW_FOR, IVY_KW_FOR,
IVY_KW_CONTINUE,
IVY_KW_BREAK,
IVY_KW_MATCH, IVY_KW_MATCH,
IVY_KW_UNLESS, IVY_KW_UNLESS,
IVY_KW_IN, IVY_KW_IN,
@@ -52,7 +54,10 @@ enum ivy_keyword {
IVY_KW_GET, IVY_KW_GET,
IVY_KW_SET, IVY_KW_SET,
IVY_KW_END, IVY_KW_END,
IVY_KW_VAR, IVY_KW_GLOBAL,
IVY_KW_TRUE,
IVY_KW_FALSE,
IVY_KW_NULL,
__IVY_KW_INDEX_LIMIT, __IVY_KW_INDEX_LIMIT,
}; };

View File

@@ -42,6 +42,8 @@ static struct lex_token_def keywords[] = {
LEX_TOKEN_DEF(IVY_KW_NOT, "not"), LEX_TOKEN_DEF(IVY_KW_NOT, "not"),
LEX_TOKEN_DEF(IVY_KW_WHILE, "while"), LEX_TOKEN_DEF(IVY_KW_WHILE, "while"),
LEX_TOKEN_DEF(IVY_KW_FOR, "for"), LEX_TOKEN_DEF(IVY_KW_FOR, "for"),
LEX_TOKEN_DEF(IVY_KW_CONTINUE, "continue"),
LEX_TOKEN_DEF(IVY_KW_BREAK, "break"),
LEX_TOKEN_DEF(IVY_KW_MATCH, "match"), LEX_TOKEN_DEF(IVY_KW_MATCH, "match"),
LEX_TOKEN_DEF(IVY_KW_UNLESS, "unless"), LEX_TOKEN_DEF(IVY_KW_UNLESS, "unless"),
LEX_TOKEN_DEF(IVY_KW_IN, "in"), LEX_TOKEN_DEF(IVY_KW_IN, "in"),
@@ -49,7 +51,10 @@ static struct lex_token_def keywords[] = {
LEX_TOKEN_DEF(IVY_KW_GET, "get"), LEX_TOKEN_DEF(IVY_KW_GET, "get"),
LEX_TOKEN_DEF(IVY_KW_SET, "set"), LEX_TOKEN_DEF(IVY_KW_SET, "set"),
LEX_TOKEN_DEF(IVY_KW_END, "end"), LEX_TOKEN_DEF(IVY_KW_END, "end"),
LEX_TOKEN_DEF(IVY_KW_VAR, "var"), LEX_TOKEN_DEF(IVY_KW_GLOBAL, "global"),
LEX_TOKEN_DEF(IVY_KW_TRUE, "true"),
LEX_TOKEN_DEF(IVY_KW_FALSE, "false"),
LEX_TOKEN_DEF(IVY_KW_NULL, "null"),
}; };
static const size_t nr_keywords = sizeof keywords / sizeof keywords[0]; static const size_t nr_keywords = sizeof keywords / sizeof keywords[0];
@@ -376,11 +381,11 @@ static int peek(struct ivy_lexer *lex)
} }
if (status != IVY_OK) { if (status != IVY_OK) {
return status; return -status;
} }
if (lex->lex_linebuf_len == 0) { if (lex->lex_linebuf_len == 0) {
return IVY_ERR_EOF; return -IVY_ERR_EOF;
} }
int c = lex->lex_linebuf[lex->lex_linebuf_ptr]; int c = lex->lex_linebuf[lex->lex_linebuf_ptr];
@@ -396,15 +401,15 @@ static int peek_next(struct ivy_lexer *lex)
} }
if (status != IVY_OK) { if (status != IVY_OK) {
return status; return -status;
} }
if (lex->lex_linebuf_len == 0) { if (lex->lex_linebuf_len == 0) {
return IVY_ERR_EOF; return -IVY_ERR_EOF;
} }
if (lex->lex_linebuf_ptr + 1 >= lex->lex_linebuf_len) { if (lex->lex_linebuf_ptr + 1 >= lex->lex_linebuf_len) {
return IVY_ERR_EOF; return -IVY_ERR_EOF;
} }
int c = lex->lex_linebuf[lex->lex_linebuf_ptr + 1]; int c = lex->lex_linebuf[lex->lex_linebuf_ptr + 1];
@@ -420,11 +425,11 @@ static int advance(struct ivy_lexer *lex)
} }
if (status != IVY_OK) { if (status != IVY_OK) {
return status; return -status;
} }
if (lex->lex_linebuf_len == 0) { if (lex->lex_linebuf_len == 0) {
return IVY_ERR_EOF; return -IVY_ERR_EOF;
} }
int c = lex->lex_linebuf[lex->lex_linebuf_ptr++]; int c = lex->lex_linebuf[lex->lex_linebuf_ptr++];
@@ -623,12 +628,12 @@ static enum ivy_status read_line_comment(struct ivy_lexer *lex)
while (true) { while (true) {
int c = advance(lex); int c = advance(lex);
if (c == IVY_ERR_EOF || c == '\n') { if (c == -IVY_ERR_EOF || c == '\n') {
break; break;
} }
if (c < 0) { if (c < 0) {
return c; return -c;
} }
} }
@@ -643,7 +648,7 @@ static enum ivy_status read_block_comment(struct ivy_lexer *lex)
while (depth > 0) { while (depth > 0) {
int c = peek(lex); int c = peek(lex);
if (c < 0) { if (c < 0) {
return c; return -c;
} }
if (!buf[0]) { if (!buf[0]) {
@@ -717,12 +722,12 @@ static enum ivy_status read_atom(struct ivy_lexer *lex)
while (true) { while (true) {
int c = peek(lex); int c = peek(lex);
if (c == IVY_ERR_EOF) { if (c == -IVY_ERR_EOF) {
break; break;
} }
if (c < 0) { if (c < 0) {
return c; return -c;
} }
if (!isalnum(c) && c != ':' && c != '_') { if (!isalnum(c) && c != ':' && c != '_') {
@@ -756,6 +761,9 @@ static enum ivy_status read_string_content(struct ivy_lexer *lex)
while (true) { while (true) {
c = peek(lex); c = peek(lex);
if (c < 0) {
break;
}
if (state->s_type == STATE_FSTRING && (c == '\'' || c == '{')) { if (state->s_type == STATE_FSTRING && (c == '\'' || c == '{')) {
break; break;
@@ -794,6 +802,9 @@ static enum ivy_status read_symbol(struct ivy_lexer *lex)
while (true) { while (true) {
int c = peek(lex); int c = peek(lex);
if (c < 0) {
break;
}
struct ivy_lexer_symbol_node *next = get_symbol_node(node, c); struct ivy_lexer_symbol_node *next = get_symbol_node(node, c);
if (!next) { if (!next) {
@@ -853,12 +864,12 @@ static enum ivy_status read_number(struct ivy_lexer *lex)
while (true) { while (true) {
int c = peek(lex); int c = peek(lex);
if (c == IVY_ERR_EOF) { if (c == -IVY_ERR_EOF) {
break; break;
} }
if (c < 0) { if (c < 0) {
return c; return -c;
} }
if (c == '_') { if (c == '_') {
@@ -1036,7 +1047,7 @@ static enum ivy_status pump_tokens(struct ivy_lexer *lex)
int c = peek(lex); int c = peek(lex);
if (c < 0) { if (c < 0) {
return c; return -c;
} }
if (state->s_type == STATE_STRING && c != '"') { if (state->s_type == STATE_STRING && c != '"') {
@@ -1067,7 +1078,7 @@ static enum ivy_status pump_tokens(struct ivy_lexer *lex)
} }
if (c < 0) { if (c < 0) {
return c; return -c;
} }
return push_linefeed(lex); return push_linefeed(lex);
@@ -1227,6 +1238,8 @@ const char *ivy_keyword_to_string(enum ivy_keyword keyword)
ENUM_STR(IVY_KW_ELSE); ENUM_STR(IVY_KW_ELSE);
ENUM_STR(IVY_KW_WHILE); ENUM_STR(IVY_KW_WHILE);
ENUM_STR(IVY_KW_FOR); ENUM_STR(IVY_KW_FOR);
ENUM_STR(IVY_KW_CONTINUE);
ENUM_STR(IVY_KW_BREAK);
ENUM_STR(IVY_KW_MATCH); ENUM_STR(IVY_KW_MATCH);
ENUM_STR(IVY_KW_UNLESS); ENUM_STR(IVY_KW_UNLESS);
ENUM_STR(IVY_KW_IN); ENUM_STR(IVY_KW_IN);
@@ -1234,7 +1247,10 @@ const char *ivy_keyword_to_string(enum ivy_keyword keyword)
ENUM_STR(IVY_KW_GET); ENUM_STR(IVY_KW_GET);
ENUM_STR(IVY_KW_SET); ENUM_STR(IVY_KW_SET);
ENUM_STR(IVY_KW_END); ENUM_STR(IVY_KW_END);
ENUM_STR(IVY_KW_VAR); ENUM_STR(IVY_KW_GLOBAL);
ENUM_STR(IVY_KW_TRUE);
ENUM_STR(IVY_KW_FALSE);
ENUM_STR(IVY_KW_NULL);
default: default:
return ""; return "";
} }