web: use parser in web

This commit is contained in:
Anish Lakhwara 2024-10-21 21:14:51 -07:00
parent efecf5b4de
commit d484329c5b
3 changed files with 78 additions and 33 deletions

View file

@ -4,6 +4,7 @@
import wasmUrl from '@vlcn.io/crsqlite-wasm/crsqlite.wasm?url'; import wasmUrl from '@vlcn.io/crsqlite-wasm/crsqlite.wasm?url';
import { start } from "@vlcn.io/ws-client/worker.js"; import { start } from "@vlcn.io/ws-client/worker.js";
import { createDbProvider } from "@vlcn.io/ws-browserdb"; import { createDbProvider } from "@vlcn.io/ws-browserdb";
import * as addParser from '../../parser/add_parser.js';
let db; let db;
let ws; let ws;
@ -17,7 +18,13 @@
await db.exec(` await db.exec(`
CREATE TABLE IF NOT EXISTS todos ( CREATE TABLE IF NOT EXISTS todos (
id BLOB PRIMARY KEY NOT NULL, id BLOB PRIMARY KEY NOT NULL,
content TEXT, description TEXT,
project text,
tags text,
due text,
wait text,
priority text,
urgency real,
completed INTEGER NOT NULL DEFAULT 0 completed INTEGER NOT NULL DEFAULT 0
); );
SELECT crsql_as_crr('todos'); SELECT crsql_as_crr('todos');
@ -87,12 +94,37 @@
todos = result; todos = result;
} }
// TODO:
// This is fucking sucks
// make the stupid parser do this 😩
async function Marshall(parsed) {
let project = "";
const tags = [];
console.log(parsed);
for (const attr of parsed.attributes) {
console.log(attr);
switch (attr.type) {
case 'project':
console.log("project" + attr.value);
project = attr.value;
break;
case 'tag':
console.log("tag" + attr.value);
tags.push(attr.value);
break;
}
}
return {description: parsed.description, tags: tags, project: project};
}
async function addTodo() { async function addTodo() {
if (newTodo.trim()) { if (newTodo.trim()) {
const parsed = await Marshall(addParser.parse(newTodo));
console.log(parsed);
await db.exec(` await db.exec(`
INSERT INTO todos (id, content, completed) INSERT INTO todos (id, description, tags, project, completed)
VALUES (lower(hex(randomblob(16))), ?, 0) VALUES (lower(hex(randomblob(16))), ?, ?, ?, 0)
`, [newTodo.trim()]); `, [parsed.description, JSON.stringify(parsed.tags), parsed.project]);
newTodo = ''; newTodo = '';
loadTodos(); loadTodos();
} }
@ -109,7 +141,7 @@
</script> </script>
<main> <main>
<h1>Svelte Todo with cr-sqlite</h1> <h1>Mast</h1>
<input <input
bind:value={newTodo} bind:value={newTodo}
on:keypress={(e) => e.key === 'Enter' && addTodo()} on:keypress={(e) => e.key === 'Enter' && addTodo()}
@ -124,7 +156,7 @@
checked={todo.completed} checked={todo.completed}
on:change={() => toggleTodo(todo.id)} on:change={() => toggleTodo(todo.id)}
/> />
<span class:completed={todo.completed}>{todo.content}</span> <span class:completed={todo.completed}>{todo.description} pro:{todo.project} {todo.tags}</span>
</li> </li>
{/each} {/each}
</ul> </ul>

View file

@ -25,7 +25,7 @@ Attributes = attributes:(_ Attribute)* {
Attribute = Project / Priority / Tag Attribute = Project / Priority / Tag
Project = ("pro:" / "project:") value:Word { Project = ("pro:" / "project:" / "+") value:Word {
return { type: "project", value: value }; return { type: "project", value: value };
} }
@ -33,7 +33,7 @@ Priority = "priority:" value:[HML] {
return { type: "priority", value: value }; return { type: "priority", value: value };
} }
Tag = "+" value:Word { Tag = "#" value:Word {
return { type: "tag", value: value }; return { type: "tag", value: value };
} }

View file

@ -1,9 +1,7 @@
// @generated by Peggy 4.0.3. // @generated by Peggy 4.1.1.
// //
// https://peggyjs.org/ // https://peggyjs.org/
"use strict";
function peg$subclass(child, parent) { function peg$subclass(child, parent) {
function C() { this.constructor = child; } function C() { this.constructor = child; }
@ -179,8 +177,9 @@ function peg$parse(input, options) {
var peg$c0 = "pro:"; var peg$c0 = "pro:";
var peg$c1 = "project:"; var peg$c1 = "project:";
var peg$c2 = "priority:"; var peg$c2 = "+";
var peg$c3 = "+"; var peg$c3 = "priority:";
var peg$c4 = "#";
var peg$r0 = /^[HML]/; var peg$r0 = /^[HML]/;
var peg$r1 = /^[a-zA-Z0-9_\-]/; var peg$r1 = /^[a-zA-Z0-9_\-]/;
@ -189,11 +188,12 @@ function peg$parse(input, options) {
var peg$e0 = peg$anyExpectation(); var peg$e0 = peg$anyExpectation();
var peg$e1 = peg$literalExpectation("pro:", false); var peg$e1 = peg$literalExpectation("pro:", false);
var peg$e2 = peg$literalExpectation("project:", false); var peg$e2 = peg$literalExpectation("project:", false);
var peg$e3 = peg$literalExpectation("priority:", false); var peg$e3 = peg$literalExpectation("+", false);
var peg$e4 = peg$classExpectation(["H", "M", "L"], false, false); var peg$e4 = peg$literalExpectation("priority:", false);
var peg$e5 = peg$literalExpectation("+", false); var peg$e5 = peg$classExpectation(["H", "M", "L"], false, false);
var peg$e6 = peg$classExpectation([["a", "z"], ["A", "Z"], ["0", "9"], "_", "-"], false, false); var peg$e6 = peg$literalExpectation("#", false);
var peg$e7 = peg$classExpectation([" ", "\\", "t"], false, false); var peg$e7 = peg$classExpectation([["a", "z"], ["A", "Z"], ["0", "9"], "_", "-"], false, false);
var peg$e8 = peg$classExpectation([" ", "\\", "t"], false, false);
var peg$f0 = function(desc, attributes) { var peg$f0 = function(desc, attributes) {
return MakeAdd(desc, attributes); return MakeAdd(desc, attributes);
@ -544,6 +544,15 @@ function peg$parse(input, options) {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e2); } if (peg$silentFails === 0) { peg$fail(peg$e2); }
} }
if (s1 === peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 43) {
s1 = peg$c2;
peg$currPos++;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e3); }
}
}
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parseWord(); s2 = peg$parseWord();
@ -566,12 +575,12 @@ function peg$parse(input, options) {
var s0, s1, s2; var s0, s1, s2;
s0 = peg$currPos; s0 = peg$currPos;
if (input.substr(peg$currPos, 9) === peg$c2) { if (input.substr(peg$currPos, 9) === peg$c3) {
s1 = peg$c2; s1 = peg$c3;
peg$currPos += 9; peg$currPos += 9;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e3); } if (peg$silentFails === 0) { peg$fail(peg$e4); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = input.charAt(peg$currPos); s2 = input.charAt(peg$currPos);
@ -579,7 +588,7 @@ function peg$parse(input, options) {
peg$currPos++; peg$currPos++;
} else { } else {
s2 = peg$FAILED; s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e4); } if (peg$silentFails === 0) { peg$fail(peg$e5); }
} }
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
@ -600,12 +609,12 @@ function peg$parse(input, options) {
var s0, s1, s2; var s0, s1, s2;
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 43) { if (input.charCodeAt(peg$currPos) === 35) {
s1 = peg$c3; s1 = peg$c4;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e5); } if (peg$silentFails === 0) { peg$fail(peg$e6); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parseWord(); s2 = peg$parseWord();
@ -634,7 +643,7 @@ function peg$parse(input, options) {
peg$currPos++; peg$currPos++;
} else { } else {
s2 = peg$FAILED; s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e6); } if (peg$silentFails === 0) { peg$fail(peg$e7); }
} }
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
while (s2 !== peg$FAILED) { while (s2 !== peg$FAILED) {
@ -644,7 +653,7 @@ function peg$parse(input, options) {
peg$currPos++; peg$currPos++;
} else { } else {
s2 = peg$FAILED; s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e6); } if (peg$silentFails === 0) { peg$fail(peg$e7); }
} }
} }
} else { } else {
@ -668,7 +677,7 @@ function peg$parse(input, options) {
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e7); } if (peg$silentFails === 0) { peg$fail(peg$e8); }
} }
while (s1 !== peg$FAILED) { while (s1 !== peg$FAILED) {
s0.push(s1); s0.push(s1);
@ -677,7 +686,7 @@ function peg$parse(input, options) {
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e7); } if (peg$silentFails === 0) { peg$fail(peg$e8); }
} }
} }
@ -747,8 +756,12 @@ function peg$parse(input, options) {
} }
} }
module.exports = { const peg$allowedStartRules = [
StartRules: ["Add"], "Add"
SyntaxError: peg$SyntaxError, ];
parse: peg$parse
export {
peg$allowedStartRules as StartRules,
peg$SyntaxError as SyntaxError,
peg$parse as parse
}; };