From 5cfb0e525b0ee8f04585693a5120b94cccb82b09 Mon Sep 17 00:00:00 2001 From: Araozu Date: Thu, 1 Aug 2024 20:33:44 -0500 Subject: [PATCH] Add semantic errors display from the compiler --- public/css/pages.css | 9 +++++++-- src/components/TwoColumn.astro | 2 +- src/lexer/highlighter.ts | 19 +++++++++++++++++++ src/pages/api/std/index.mdx | 17 +++++++++++++---- src/pages/api/std/print.mdx | 29 +++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 src/pages/api/std/print.mdx diff --git a/public/css/pages.css b/public/css/pages.css index 02f14a5..33aa2cb 100644 --- a/public/css/pages.css +++ b/public/css/pages.css @@ -48,7 +48,7 @@ height: 0; position: relative; right: 2.25rem; - opacity: 0.1; + opacity: 0.2; } .markdown ul { @@ -73,5 +73,10 @@ } .markdown blockquote { - color: red + color: red; +} + +.two-column a { + color: #2563eb; + text-decoration: underline; } diff --git a/src/components/TwoColumn.astro b/src/components/TwoColumn.astro index 7fe456b..7d9dfb4 100644 --- a/src/components/TwoColumn.astro +++ b/src/components/TwoColumn.astro @@ -1,3 +1,3 @@ -
+
diff --git a/src/lexer/highlighter.ts b/src/lexer/highlighter.ts index 14f1b7d..0eef368 100644 --- a/src/lexer/highlighter.ts +++ b/src/lexer/highlighter.ts @@ -34,6 +34,7 @@ type TokenType = export interface Err { Lex?: LexError Syntax?: SyntaxError + Semantic?: SemanticError } export interface LexError { @@ -47,8 +48,15 @@ export interface SyntaxError { reason: string } +export interface SemanticError { + error_start: number + error_end: number + reason: string +} + export interface TokenizeResult { Ok?: Token[], + SyntaxOnly?: [Token[], Err], TokensOnly?: [Token[], Err], Err?: Err, } @@ -72,6 +80,10 @@ export async function native_highlighter(code: string): Promise<[string, string, const [tokens, error] = result.TokensOnly!; return syntax_error_highlighter(formatted_code, tokens, error.Syntax!); } + else if (result.SyntaxOnly) { + const [tokens, error] = result.SyntaxOnly!; + return semantic_error_highlighter(formatted_code, tokens, error.Semantic!); + } const tokens = result.Ok!; @@ -107,6 +119,13 @@ function syntax_error_highlighter(code: string, tokens: Array, error: Syn return [highlighted, "Syntax", error_message]; } +function semantic_error_highlighter(code: string, tokens: Array, error: SyntaxError): [string, string, string] { + const highlighted = highlight_tokens(code, tokens, error.error_start, error.error_end); + + const error_message = `${error.reason} from position ${error.error_start} to ${error.error_end}`; + return [highlighted, "Semantic", error_message]; +} + function compiler_error(code: string, error: Error): [string, string, string] { return [code, "Fatal Compiler", error.message]; } diff --git a/src/pages/api/std/index.mdx b/src/pages/api/std/index.mdx index cc94bf6..a3a5238 100644 --- a/src/pages/api/std/index.mdx +++ b/src/pages/api/std/index.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/ApiLayout.astro --- import TwoColumn from "../../../components/TwoColumn.astro" +import Code from "../../../components/Code.astro" # module `std` @@ -27,12 +28,12 @@ if (str_contains("abc", "a")) { In THP there is no `str_contains` function. Instead, you'd call the `contains` method on the string: -```thp + ## On naming @@ -55,9 +56,9 @@ If you need to use a PHP class with a lowercase name you can use the following s class animal {} ``` -```thp + + + + +