Fixup types to make TS happy

This commit is contained in:
Tangent Wantwight 2023-08-06 02:09:30 -04:00
parent 8fc2892630
commit e9fbf137c7
3 changed files with 13 additions and 14 deletions

View file

@ -32,11 +32,11 @@ type Vm = {
};
/**
* @param {Vm} state VM state
* @param {string} code Script to run
* @returns {string} Markup to render / output
* @param state VM state
* @param code Script to run
* @returns Markup to render / output
*/
function renderCard(state, code) {
function renderCard(state: Vm, code: string) {
const script = parse(code);
if (script[0]) {
state.output = JSON.stringify(script[1], null, 2);
@ -48,9 +48,8 @@ function renderCard(state, code) {
/**
* Global state: a single card
* @type {Card}
*/
let theCard = {
let theCard: Card = {
id: 100,
fields: {},
code: String.raw`
@ -100,8 +99,8 @@ const display = document.createElement("blockquote");
const debugDisplay = document.createElement("pre");
function render() {
const vm = {
mode: /** @type {ScriptType} */ "render",
const vm: Vm = {
mode: "render",
output: "",
};
const html = renderCard(vm, theCard.code);

View file

@ -1,5 +1,5 @@
import { escapeHtml } from "./helpers";
import { AtLeast, Choose, End, Regex, Sequence, Use } from "./peg";
import { AtLeast, Choose, End, Pattern, Regex, Sequence, Use } from "./peg";
export type Word = {
text: string;
@ -23,7 +23,7 @@ const BasicWord = Regex(/(?!\{)[^\s;]+/y)
// WIP, need to be able to escape braces correctly
const Brace = Sequence(
const Brace: Pattern<string> = Sequence(
Regex(/\{/y).expects("{"),
AtLeast(
0,

View file

@ -74,7 +74,7 @@ export function Use<T>(getPattern: () => Pattern<T>): Pattern<T> {
* Creates a pattern matching a regex & returning any captures. The regex needs to be sticky (using the //y modifier)
*/
export function Regex(regex: RegExp): Pattern<RegExpExecArray> {
const pattern = WrapPattern((source, index) => {
const pattern: Pattern<RegExpExecArray> = WrapPattern((source, index) => {
regex.lastIndex = index;
const matches = regex.exec(source);
return matches
@ -151,10 +151,10 @@ export function Sequence<T extends unknown[]>(
* If the given pattern does not consume input, the matching will be terminated to prevent an eternal loop.
*
* Note that if the minimum run is zero, this pattern will always succeed, but might not consume any input.
* @param {number} min
* @param min
*/
export function AtLeast<T>(min, pattern: Pattern<T>): Pattern<T[]> {
export function AtLeast<T>(min: number, pattern: Pattern<T>): Pattern<T[]> {
return WrapPattern(function (source, index) {
const values: T[] = [];
let furthestFound = index;
@ -192,6 +192,6 @@ export function End(): Pattern<true> {
index,
end.expectLabel,
];
}).expects("<eof>");
}).expects("<eof>") as Pattern<true>;
return end;
}