Skip to content

Commit

Permalink
refactor(examples): fix up TS4.4 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 3, 2021
1 parent a6ab9a3 commit 1937f6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
1 change: 0 additions & 1 deletion examples/adaptive-threshold/src/threshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const adaptiveThreshold = (
offset = 0
) =>
convolveChannel(src, {
pad: false,
kernel: {
// pool kernel template for code generator:
// take a `body` array of pixel lookups (W x H items)
Expand Down
77 changes: 39 additions & 38 deletions examples/parse-playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { timedResult } from "@thi.ng/bench";
import { downloadWithMime } from "@thi.ng/dl-asset";
import { DOWNLOAD, withSize } from "@thi.ng/hiccup-carbon-icons";
import { anchor, div, h1, main, textArea } from "@thi.ng/hiccup-html";
import { defContext, defGrammar, print } from "@thi.ng/parse";
import { defContext, defGrammar, Language, print } from "@thi.ng/parse";
import { $compile } from "@thi.ng/rdom";
import {
dynamicDropdown,
Expand Down Expand Up @@ -39,7 +39,9 @@ import {
// this uses a base64 & msgpack encoded version of the two editors
const parseState = ((): Nullable<string[]> => {
try {
return deserialize([...base64Decode(location.hash.substr(1))]);
return deserialize(
new Uint8Array(base64Decode(location.hash.substr(1)))
);
} catch (e) {}
})() || [DEFAULT_GRAMMAR, DEFAULT_RULE, ...DEFAULT_INPUTS];

Expand All @@ -63,13 +65,15 @@ const inputID = reactive(0);
const activeInput = inputID.subscribe(metaStream((id) => srcInputs[id]));

// stream transform attempting to compile grammar
const lang = srcGrammar.map((src) => {
try {
return { lang: defGrammar(src) };
} catch (e) {
return { error: e };
const lang = srcGrammar.map(
(src): Partial<{ lang: Language; error: Error }> => {
try {
return { lang: defGrammar(src) };
} catch (e) {
return { error: <Error>e };
}
}
});
);

// stream transform to extract parser rule IDs
const ruleIDs = lang.transform(
Expand All @@ -94,39 +98,36 @@ const result = sync({
src: activeInput,
rule: activeRule,
},
}).map(
({ lang, src, rule }): ParseResult => {
// error if no valid grammar
if (!lang.lang) return $result("err", lang.error);
const parser = lang.lang.rules[rule];
if (!parser)
return $result("err", `invalid or missing parser: ${rule}`);
try {
const ast: string[] = [];
const ctx = defContext(src, { retain: true });
// measure execution time of the parsing process
const [res, time] = timedResult(() =>
print(parser, (x) => ast.push(x))(ctx)
);
const body = ast.join("\n");
return res
? ctx.done
? $result("ok", body, time)
: $result(
"partial",
`partial match only (stopped @ ${ctx.state.l}:${ctx.state.c})...\n\n${body}`,
time
)
}).map(({ lang, src, rule }): ParseResult => {
// error if no valid grammar
if (!lang.lang) return $result("err", lang.error!.message);
const parser = lang.lang.rules[rule];
if (!parser) return $result("err", `invalid or missing parser: ${rule}`);
try {
const ast: string[] = [];
const ctx = defContext(src, { retain: true });
// measure execution time of the parsing process
const [res, time] = timedResult(() =>
print(parser, (x) => ast.push(x))(ctx)
);
const body = ast.join("\n");
return res
? ctx.done
? $result("ok", body, time)
: $result(
"fail",
`input parse failure (no match)...\n\n${body}`,
"partial",
`partial match only (stopped @ ${ctx.state.l}:${ctx.state.c})...\n\n${body}`,
time
);
} catch (e) {
return $result("err", `Parse error: ${e}`);
}
)
: $result(
"fail",
`input parse failure (no match)...\n\n${body}`,
time
);
} catch (e) {
return $result("err", `Parse error: ${e}`);
}
);
});

// update URL hash fragment with the msgpack & base64 encoded version of
// the selected parser rule and contents of all editors
Expand Down
2 changes: 1 addition & 1 deletion examples/rstream-spreadsheet/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const updateCell = (id: string, val: string) => {
$eval(val, id);
DB.resetIn(<const>[id, "error"], null);
} catch (e) {
DB.resetIn(<const>[id, "error"], e.message);
DB.resetIn(<const>[id, "error"], (<Error>e).message);
}
} else {
removeCell(id);
Expand Down

0 comments on commit 1937f6b

Please sign in to comment.