Skip to content

Commit

Permalink
Almoçaremos.
Browse files Browse the repository at this point in the history
  • Loading branch information
0Walle authored Apr 23, 2024
1 parent b993525 commit 14b1cfd
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
66 changes: 54 additions & 12 deletions harpsichord.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const KEY = new RegExp("\\[([^\\]]+)\\]\\s*","y");
const KEY = new RegExp("\\[([^\\]]+)\\]","y");
const COMMENT = new RegExp("#[^\n]*","y");
const WS = new RegExp("[ \t\f\r\n]+","my");
const NL = new RegExp("[ \\t]*\\n","my");
const UNICODE = new RegExp("U\\+([0-9a-fA-F]{4,6})","my");
const INCLUDE = new RegExp("include", "y");
const DELETE = new RegExp("delete", "y");
const REPLACE = new RegExp("replace", "y");

RegExp.prototype.match = function(s, p) {
this.lastIndex = p;
Expand All @@ -23,11 +25,11 @@ function ignore_ws(s, p) {
return p;
}

function match_endline(s, p) {
function match_endline(msg, s, p) {
let m;
if (p == s.length) { return p; }
if (m = NL.match(s, p)) { return m.end; }
throw make_error(`Expected new line`, s, p);
throw make_error(`Expected new line ${msg}`, s, p);
}

function match_char(s, p, ch) {
Expand All @@ -46,13 +48,45 @@ function parse_harpsichord(s, p=0) {
let include;
[include, p] = parse_string(s, p);
if (include == null) {
throw make_error(`Expected string for include`, s, p);
throw make_error(`Expected string at include`, s, p);
}
p = match_endline(s, p);
p = match_endline(`at include '${include}'`, s, p);
lines.push({include:include})
continue;
}

if (m = DELETE.match(s, p)) {
p = ignore_ws(s, p+6);
let key;
if (m = KEY.match(s, p)) {
p = m.end;
key = m[1];
} else {
throw make_error(`Expected [KEYCODE] at delete`, s, p);
}
p = match_endline(`at delete [${key}]`, s, p);
lines.push({delete:key})
continue;
}

if (m = REPLACE.match(s, p)) {
p = ignore_ws(s, p+7);
let from, to;
[from, p] = parse_string(s, p);
if (from == null) {
throw make_error(`Expected string at replace`, s, p);
}
p = ignore_ws(s, p);
if (!match_char(s, p, '>')) {
throw make_error(`Expected > at replace '${from}'`, s, p);
}
p = ignore_ws(s, p+1);
[to, p] = parse_string(s, p);
p = match_endline(`at replace '${from}' > '${to}'`, s, p);
lines.push({replace:from, to:to})
continue;
}

if (p >= s.length) break;
if (s[p] == '\n') {
p += 1;
Expand All @@ -61,7 +95,7 @@ function parse_harpsichord(s, p=0) {

let def;
[def, p] = parse_definition(s, p);
p = match_endline(s, p);
p = match_endline(`at ${def.ctx ? '['+def.ctx+']: ' : ''}[${def.key}]`, s, p);
lines.push(def)
}
return lines;
Expand Down Expand Up @@ -93,17 +127,24 @@ function parse_string(s, p) {
str += s[p];
p += 1;
}
return [str, p]
if (str == '') str = "'";
return [str, p];
}

function parse_definition(s, p) {
let m;
let key, ctx, string;
let key, ctx, string, display;
if (m = KEY.match(s, p)) {
p = m.end;
key = m[1];
} else {
throw make_error(`Expected [KEY]`, s, p);
throw make_error(`Expected [KEYCODE]`, s, p);
}
p = ignore_ws(s, p);
if (match_char(s, p, '-')) {
p = ignore_ws(s, p+1);
[display, p] = parse_string(s, p);
p = ignore_ws(s, p);
}
if (match_char(s, p, ':')) {
p = ignore_ws(s, p+1);
Expand All @@ -112,14 +153,15 @@ function parse_definition(s, p) {
p = m.end;
key = m[1];
}
p = ignore_ws(s, p);
}
if (!match_char(s, p, '>')) {
throw make_error(`Expected > for [${key}]`, s, p);
throw make_error(`Expected > at ${ctx ? '['+ctx+']: ' : ''}[${key}]`, s, p);
}
p = ignore_ws(s, p+1);
[string, p] = parse_string(s, p);
if (string == null) {
throw make_error(`Expected string value for [${key}]`, s, p);
throw make_error(`Expected string value at [${key}]`, s, p);
}
return [{key, ctx, string}, p];
return [{key, ctx, string, display}, p];
}
46 changes: 40 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,47 @@
if (dead_key_context) {
insertTextAtCursor(dead_key_context.bind)
dead_key_context = null
if (desc == '[Space]') return
if (desc == '[Space]' || desc == '[Space SHIFT]') return
}

insertTextAtCursor(char_value)
}

const sel = window.getSelection();
const trange = sel.getRangeAt(0);
const range = document.createRange();
range.setStart(document.getElementById('play-area'), 0);
range.setEnd(trange.endContainer, trange.endOffset);
let range_text = range.toString();
for (const seq in replacers) {
if (range_text.endsWith(seq)) {
range_text = range_text.replace(seq, replacers[seq]);
trange.setStart(document.getElementById('play-area'), 0);
trange.deleteContents();
let node = document.createTextNode(range_text);
trange.insertNode(node);
trange.setStart(node, node.length);
trange.setEnd(node, node.length);
}
}
}

function play_keyup(e) {
if (ignore_input(e)) return
document.dispatchEvent(new CustomEvent('pressup', { detail: { scancode: e.code }, bubbles: true }))
}

let replacers = {_len: 0}

const INCLUDES = {
'en-us': DEFAULT_EN_US,
'qwerty-lower': DEFAULT_QWERTY_LOWER,
'qwerty-upper': DEFAULT_QWERTY_UPPER,
}
function parse_keyboard(e, key_binds, error) {
let code = e.srcElement.value
let code = e.srcElement.value;

replacers = {};

let matches;
let included = {};
Expand Down Expand Up @@ -283,17 +305,30 @@
for (const match of matches) {
if (match == null) continue

if (match.delete) {
delete key_binds[`[${match.delete}]`];
continue;
}

if (match.replace) {
replacers[match.replace] = match.to;
if (match.replace.length > replacers._len) {
replacers._len = match.replace.length;
}
continue;
}

if (match.ctx == null) {
if (!(`[${match.key}]` in key_binds)) {
key_binds[`[${match.key}]`] = { bind: match.string, dead: {}, display: match.string }
key_binds[`[${match.key}]`] = { bind: match.string, dead: {}, display: match.display }
} else {
key_binds[`[${match.key}]`].bind = match.string
key_binds[`[${match.key}]`].display = match.string
key_binds[`[${match.key}]`].display = match.display
}
} else {
const ctx = `[${match.ctx}]`
if (!(ctx in key_binds)) {
key_binds[ctx] = { bind: null, dead: { [`[${match.key}]`]: match.string }, display: match.string }
key_binds[ctx] = { bind: null, dead: { [`[${match.key}]`]: match.string }, display: match.display }
} else {
key_binds[ctx].dead[`[${match.key}]`] = match.string
}
Expand Down Expand Up @@ -321,7 +356,6 @@
function on_load(e, key_binds, error) {
if (window.location.search.length > 0) {
const link = window.location.search.slice(1)
console.log(link)
fetch(link, {"mode" : "cors", "method": 'GET'})
.then(resp => {
if (!resp.ok) {
Expand Down

0 comments on commit 14b1cfd

Please sign in to comment.