refactor: update highlighter to use compilers new if/else tokens

This commit is contained in:
Araozu 2024-08-27 16:15:16 -05:00
parent e510ab0d03
commit 3b5f26eab1
2 changed files with 8 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import type { LexError, SyntaxError, Token, TokenizeResult, TokenType } from "./
const error_classes = "underline underline-offset-4 decoration-wavy decoration-red-500"; const error_classes = "underline underline-offset-4 decoration-wavy decoration-red-500";
export async function native_highlighter(code: string, level = HighlightLevel.Lexic): Promise<[string, string, string | null]> { export async function native_highlighter(code: string, level = HighlightLevel.Syntactic): Promise<[string, string, string | null]> {
let formatted_code = leftTrimDedent(code).join("\n"); let formatted_code = leftTrimDedent(code).join("\n");
try { try {
@ -193,7 +193,7 @@ function process_token_value_and_end(value: string, token_type: TokenType, first
function translate_token_type(tt: TokenType, value: string): string { function translate_token_type(tt: TokenType, value: string): string {
const keywords = ["throws", "extends", "constructor", "case", "static", "const", const keywords = ["throws", "extends", "constructor", "case", "static", "const",
"enum", "union", "loop", "use", "break", "catch", "continue", "as", "do", "enum", "union", "loop", "use", "break", "catch", "continue", "as", "do",
"else", "finally", "for", "fun", "if", "in", "fn", "nil", "return", "throw", "finally", "for", "fun", "in", "fn", "nil", "return", "throw",
"try", "while", "type", "match", "with", "of", "abstract", "class", "interface", "try", "while", "type", "match", "with", "of", "abstract", "class", "interface",
"private", "protected", "pub", "override", "open", "init", "val", "var", "mut", "clone"]; "private", "protected", "pub", "override", "open", "init", "val", "var", "mut", "clone"];
@ -220,6 +220,8 @@ function translate_token_type(tt: TokenType, value: string): string {
case "VAL": case "VAL":
case "VAR": case "VAR":
case "FUN": case "FUN":
case "IF":
case "ELSE":
return "keyword"; return "keyword";
default: default:
return tt; return tt;

View File

@ -32,7 +32,9 @@ export type TokenType =
"VAL" | "VAL" |
"VAR" | "VAR" |
"EOF" | "EOF" |
"FUN"; "FUN" |
"IF" |
"ELSE";
export interface Err { export interface Err {
Lex?: LexError Lex?: LexError
@ -72,4 +74,4 @@ export enum HighlightLevel {
Lexic = 0, Lexic = 0,
Syntactic = 1, Syntactic = 1,
Semantic = 2, Semantic = 2,
} }