2024-03-27 13:12:32 +00:00
|
|
|
import type { Token } from "./lexer.ts";
|
|
|
|
import { is_identifier_char } from "./utils.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans an identifier, at the given position in the input string.
|
|
|
|
* This function assumes that the character at the given position is a letter.
|
2024-03-27 13:18:31 +00:00
|
|
|
*
|
|
|
|
* @param input the input string
|
|
|
|
* @param starting_position the position to start scanning from
|
|
|
|
* @param is_datatype whether the identifier is a datatype
|
2024-03-27 13:12:32 +00:00
|
|
|
*/
|
2024-03-27 13:18:31 +00:00
|
|
|
export function scan_identifier(input: string, starting_position: number, is_datatype = false): [Token, number] {
|
2024-03-27 13:12:32 +00:00
|
|
|
let value = input[starting_position];
|
|
|
|
let pos = starting_position + 1;
|
|
|
|
|
|
|
|
while (pos < input.length) {
|
|
|
|
const c = input[pos];
|
|
|
|
|
|
|
|
if (is_identifier_char(c)) {
|
|
|
|
pos += 1;
|
|
|
|
value += c;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 13:18:31 +00:00
|
|
|
if (is_datatype) {
|
|
|
|
return [{ v: value, token_type: "class-name" }, pos];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return [{ v: value, token_type: check_keyword(value) }, pos];
|
|
|
|
}
|
2024-03-27 13:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function check_keyword(value: string): string {
|
2024-03-28 01:55:35 +00:00
|
|
|
const keywords = ["case", "static", "const", "enum", "loop", "use", "break", "catch", "continue", "do", "else", "finally", "for", "fun", "if", "in", "fn", "nil", "return", "throw", "try", "while", "type", "match", "with", "of", "abstract", "class", "interface", "private", "pub", "override", "open", "init", "val", "var", "mut", "clone"];
|
2024-03-27 13:12:32 +00:00
|
|
|
|
|
|
|
if (keywords.includes(value)) {
|
|
|
|
return "keyword";
|
|
|
|
}
|
|
|
|
return "identifier";
|
|
|
|
}
|
|
|
|
|