Added string and character lexing
This commit is contained in:
parent
be648a6485
commit
ab690f4bb5
|
@ -16,6 +16,8 @@ typedef enum {
|
||||||
TOKEN_RBRACE,
|
TOKEN_RBRACE,
|
||||||
TOKEN_LBRACKET,
|
TOKEN_LBRACKET,
|
||||||
TOKEN_RBRACKET,
|
TOKEN_RBRACKET,
|
||||||
|
TOKEN_CHARACTER,
|
||||||
|
TOKEN_STRING,
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
26
src/lexer.c
26
src/lexer.c
|
@ -129,6 +129,32 @@ TokenResult lexer_next(Lexer *lexer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Characters
|
||||||
|
if (c == '\'') {
|
||||||
|
advance(lexer); // Opening `'`
|
||||||
|
if (peek(lexer) == '\\') advance(lexer); // Slash
|
||||||
|
advance(lexer); // Char
|
||||||
|
if (peek(lexer) == '\'') {
|
||||||
|
advance(lexer); // Closing `'`
|
||||||
|
return lexer_result(lexer, TOKEN_CHARACTER, start, start_line);
|
||||||
|
} else return lexer_error(lexer, "Invalid character format", start, start_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strings
|
||||||
|
if (c == '"') {
|
||||||
|
advance(lexer);
|
||||||
|
while (peek(lexer) != '"') {
|
||||||
|
if (peek(lexer) == '\\') {
|
||||||
|
advance(lexer);
|
||||||
|
} else if (peek(lexer) == '\n') {
|
||||||
|
return lexer_error(lexer, "Invalid string format", start, start_line);
|
||||||
|
}
|
||||||
|
advance(lexer);
|
||||||
|
}
|
||||||
|
advance(lexer);
|
||||||
|
return lexer_result(lexer, TOKEN_STRING, start, start_line);
|
||||||
|
}
|
||||||
|
|
||||||
// Identifiers
|
// Identifiers
|
||||||
if (is_identifier_start(c)) {
|
if (is_identifier_start(c)) {
|
||||||
while (is_identifier_char(peek(lexer))) advance(lexer);
|
while (is_identifier_char(peek(lexer))) advance(lexer);
|
||||||
|
|
|
@ -8,7 +8,8 @@ static void print_token(Token token) {
|
||||||
(const char *[]){
|
(const char *[]){
|
||||||
"EOF", "IDENTIFIER", "NUMBER", "OPERATOR",
|
"EOF", "IDENTIFIER", "NUMBER", "OPERATOR",
|
||||||
"LPAREN", "RPAREN", "SEMICOLON", "LBRACE",
|
"LPAREN", "RPAREN", "SEMICOLON", "LBRACE",
|
||||||
"RBRACE", "LBRACKET", "RBRACKET"
|
"RBRACE", "LBRACKET", "RBRACKET", "CHARACTER",
|
||||||
|
"STRING"
|
||||||
}[token.type],
|
}[token.type],
|
||||||
(int)token.length, token.start
|
(int)token.length, token.start
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue