Skip to content

Commit

Permalink
🥅 Handle errors for tokenizing process
Browse files Browse the repository at this point in the history
  • Loading branch information
fTrestour committed Jan 29, 2024
1 parent ec15c57 commit 3935efb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
29 changes: 17 additions & 12 deletions src/web/components/CodeViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import type { PropsWithChildren } from "@kitajs/html";
import { codeCss, type TokenWithId } from "../utils";
import type { Result } from "neverthrow";

export function CodeViewer(
props: PropsWithChildren<{
source: string;
tokens: TokenWithId[];
tokens: Result<TokenWithId[], Error>;
class?: string;
}>
) {
let previousBreakpoint = 0;
let spans = new Array<{ value: string; id: number | null }>();
for (const token of props.tokens) {
spans.push({
value: props.source.slice(previousBreakpoint, token.startIndex),
id: null,
});
spans.push({
value: props.source.slice(token.startIndex, token.endIndex),
id: token.startIndex,
});
previousBreakpoint = token.endIndex;
if (props.tokens.isErr()) {
spans = [{ value: props.source, id: null }];
} else {
let previousBreakpoint = 0;
for (const token of props.tokens.value) {
spans.push({
value: props.source.slice(previousBreakpoint, token.startIndex),
id: null,
});
spans.push({
value: props.source.slice(token.startIndex, token.endIndex),
id: token.startIndex,
});
previousBreakpoint = token.endIndex;
}
}

return (
Expand Down
12 changes: 9 additions & 3 deletions src/web/components/TokensViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import type { PropsWithChildren } from "@kitajs/html";

import { TokenViewer } from "./TokenViewer";
import type { TokenWithId } from "../utils";
import { codeCss, type TokenWithId } from "../utils";
import type { Result } from "neverthrow";

export function TokensViewer(
props: PropsWithChildren<{
source: string;
tokens: TokenWithId[];
tokens: Result<TokenWithId[], Error>;
class?: string;
}>
) {
if (props.tokens.isErr())
return (
<p class={codeCss + " text-red-500"}>{props.tokens.error.message}</p>
);

return (
<div class="flex-grow flex flex-col space-y-4 overflow-auto">
{props.tokens.map((token) => (
{props.tokens.value.map((token) => (
<TokenViewer
source={props.source.slice(token.startIndex, token.endIndex)}
token={token}
Expand Down
11 changes: 9 additions & 2 deletions src/web/pages/ASTViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { type Ast } from "../../domain/parse";
import { codeCss, type TokenWithId } from "../utils";
import { type PropsWithChildren } from "@kitajs/html";
import { NodeViewer } from "../components/NodeViewer";
import type { Result } from "neverthrow";

export function ASTViewer(
props: PropsWithChildren<{ parsed: Ast<TokenWithId>; class?: string }>
props: PropsWithChildren<{
parsed: Result<Ast<TokenWithId>, Error>;
class?: string;
}>
) {
if (props.parsed.isErr())
return <div class={codeCss + " overflow-auto grow " + props.class}></div>;

return (
<div class={codeCss + " overflow-auto grow " + props.class}>
<NodeViewer node={props.parsed} />
<NodeViewer node={props.parsed.value} />
</div>
);
}
18 changes: 11 additions & 7 deletions src/web/pages/parsed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { randomUUID } from "crypto";
import { Link } from "../components/Button";
import { Nav } from "../components/Nav";
import { ASTViewer } from "./ASTViewer";
import type { TokenWithId } from "../utils";

export default new Elysia().get(
"/parsed",
Expand All @@ -17,14 +18,17 @@ export default new Elysia().get(
// https://stackoverflow.com/a/16587536
const source = decodeURIComponent(req.query.source.replace(/\+/g, "%20"));

const tokens = tokenize(source)
._unsafeUnwrap()
.map((token) => ({
...token,
id: randomUUID(),
}));
const tokens = tokenize(source).map((tokens) =>
tokens.map(
(token) =>
({
...token,
id: randomUUID(),
} as TokenWithId)
)
);

const parsed = parse(tokens);
const parsed = tokens.map(parse);

return (
<App class="grid gap-6 h-auto grid-rows-layout grid-cols-3 auto-rows-auto">
Expand Down

0 comments on commit 3935efb

Please sign in to comment.