Skip to content

Commit

Permalink
Define Error subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
lynn committed Sep 5, 2023
1 parent ee83bf5 commit 0b29484
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 75 deletions.
20 changes: 11 additions & 9 deletions src/boxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Glosser } from './gloss';
import { inTone } from './tokenize';
import { Branch, Tree, assertBranch, isQuestion } from './tree';
import { Tone } from './types';
import { Impossible, Ungrammatical, Unimplemented } from './error';

export interface PostField {
earlyAdjuncts: string[];
Expand Down Expand Up @@ -88,15 +89,15 @@ function boxifyClause(tree: Tree): BoxClause {
let postField: PostField | undefined = undefined;
let conjunction: AndClause | undefined = undefined;
const cp = skipFree(tree);
if (!('left' in cp)) throw new Error('bad CP?');
if (!('left' in cp)) throw new Impossible('bad CP?');
const c = cp.left;
if (!('word' in c)) throw new Error('C without word?');
if (!('word' in c)) throw new Impossible('C without word?');
if (!c.word.covert) {
complementizer = c.word.text;
}
for (let node = cp.right; ; ) {
if ('children' in node) {
if (node.label !== '*𝘷P') throw new Error('non-*𝘷P Rose');
if (node.label !== '*𝘷P') throw new Impossible('non-*𝘷P Rose');
const serial = node.children[0];
verbalComplexWords.push(words(serial));
postField = boxifyPostField(node.children.slice(1));
Expand Down Expand Up @@ -131,10 +132,10 @@ function boxifyClause(tree: Tree): BoxClause {
break;
default:
console.log(node);
throw new Error('unimplemented in boxifyClause: ' + node.label);
throw new Unimplemented('in boxifyClause: ' + node.label);
}
} else {
throw new Error('unexpected leaf in clause');
throw new Impossible('hit leaf in boxifyClause');
}
}
const verbalComplex = verbalComplexWords.join(' ').trim();
Expand All @@ -144,11 +145,12 @@ function boxifyClause(tree: Tree): BoxClause {
export function boxify(tree: Tree): BoxSentence {
let speechAct: string = '';
tree = skipFree(tree);
if (tree.label !== 'SAP') throw new Error('Cannot boxify non-sentence');
if (!('left' in tree)) throw new Error('bad SAP?');
if (tree.label !== 'SAP')
throw new Ungrammatical('Cannot boxify non-sentence');
if (!('left' in tree)) throw new Impossible('bad SAP?');
const sa = tree.right;
if (sa.label !== 'SA') throw new Error('SAP without SA?');
if (!('word' in sa)) throw new Error('SA without word?');
if (sa.label !== 'SA') throw new Impossible('SAP without SA?');
if (!('word' in sa)) throw new Impossible('SA without word?');
if (!sa.word.covert) {
speechAct = sa.word.text;
}
Expand Down
19 changes: 10 additions & 9 deletions src/english.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parse } from './parse';
import { bare, clean } from './tokenize';
import { Branch, Label, Leaf, Tree, assertBranch, isQuestion } from './tree';
import { VerbForm, conjugate } from './english-conjugation';
import { Impossible, Unimplemented } from './error';

interface Constituent {
text: string;
Expand All @@ -11,7 +12,7 @@ interface Constituent {

function leafText(tree: Tree): string {
if (!('word' in tree)) {
throw new Error('Unexpected non-leaf ' + tree.label);
throw new Impossible('Unexpected non-leaf ' + tree.label);
}
if (tree.word.covert) return '';
return tree.word.text;
Expand Down Expand Up @@ -42,14 +43,14 @@ function verbToEnglish(tree: Tree): string {
const sep = left.endsWith('-') ? '' : ' ';
return left + sep + right;
} else {
throw new Error('weird verb ' + tree.label);
throw new Impossible('weird verb ' + tree.label);
}
}

function serialToEnglish(serial: Tree): string {
if ('word' in serial && serial.word.covert) return '';
if (serial.label !== '*Serial') throw new Error('non-*Serial serial');
if (!('children' in serial)) throw new Error('non-Rose serial');
if (serial.label !== '*Serial') throw new Impossible('non-*Serial serial');
if (!('children' in serial)) throw new Impossible('non-Rose serial');
return serial.children.map(x => verbToEnglish(x)).join('-');
}

Expand Down Expand Up @@ -81,7 +82,7 @@ class ClauseTranslator {
public processClause(tree: Tree): void {
for (let node = tree; ; ) {
if ('children' in node) {
if (node.label !== '*𝘷P') throw new Error('non-*𝘷P Rose');
if (node.label !== '*𝘷P') throw new Impossible('non-*𝘷P Rose');
this.verb = serialToEnglish(node.children[0]);
let late = false;
for (let i = 1; i < node.children.length; i++) {
Expand Down Expand Up @@ -141,10 +142,10 @@ class ClauseTranslator {
break;
default:
console.log(node);
throw new Error('unimplemented in processClause: ' + node.label);
throw new Unimplemented('in processClause: ' + node.label);
}
} else {
throw new Error('unexpected leaf in clause');
throw new Impossible('hit leaf in clause');
}
}
}
Expand Down Expand Up @@ -395,7 +396,7 @@ function branchToEnglish(tree: Branch<Tree>): Constituent {
return { text: left + ' ' + right, person };
}
}
throw new Error('unimplemented in branchToEnglish: ' + tree.label);
throw new Unimplemented('in branchToEnglish: ' + tree.label);
}

function treeToEnglish(tree: Tree): Constituent {
Expand All @@ -419,7 +420,7 @@ function treeToEnglish(tree: Tree): Constituent {
} else if ('left' in tree) {
return branchToEnglish(tree);
} else {
throw new Error('unexpected Rose in treeToEnglish: ' + tree.label);
throw new Impossible('unexpected Rose in treeToEnglish: ' + tree.label);
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Thrown when the input is detected to be ungrammatical during the fix or
* denote step.
*/
export class Ungrammatical extends Error {
constructor(message?: string) {
super(message);
this.name = 'Ungrammatical';
Object.setPrototypeOf(this, new.target.prototype);
}
}

/**
* Thrown when Kuna doesn't know what a word means, and can't continue denoting
* the sentence.
*/
export class Unrecognized extends Error {
constructor(message?: string) {
super(message);
this.name = 'Unrecognized';
Object.setPrototypeOf(this, new.target.prototype);
}
}

/**
* Thrown in code paths that aren't expected to be reached by any input sentence
* (so if this gets thrown, there's a bug in Kuna).
*/
export class Impossible extends Error {
constructor(message?: string) {
super(message);
this.name = 'Impossible';
Object.setPrototypeOf(this, new.target.prototype);
}
}

/**
* Thrown in code paths that aren't handled yet.
*/
export class Unimplemented extends Error {
constructor(message?: string) {
super(message);
this.name = 'Unimplemented';
Object.setPrototypeOf(this, new.target.prototype);
}
}
12 changes: 7 additions & 5 deletions src/fix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { analyzeSerial } from './serial';
import { CovertValue, StrictTree, Tree, assertLeaf } from './tree';
import { Impossible } from './error';

interface Quantification {
quantifier: CovertValue;
Expand Down Expand Up @@ -55,13 +56,14 @@ export function fix(tree: Tree, scope?: Scope): StrictTree {
if ('children' in tree) {
if (tree.label === '*𝘷P') {
const serial = tree.children[0];
if (!serial) throw new Error('*𝘷P without children');
if (serial.label !== '*Serial') throw new Error('*𝘷P without *Serial');
if (!('children' in serial)) throw new Error('strange *Serial');
if (!serial) throw new Impossible('*𝘷P without children');
if (serial.label !== '*Serial')
throw new Impossible('*𝘷P without *Serial');
if (!('children' in serial)) throw new Impossible('strange *Serial');
const vP = analyzeSerial(serial, tree.children.slice(1));
return fix(vP, scope);
} else {
throw new Error('unexpected non-binary tree');
throw new Impossible('unexpected non-binary tree');
}
} else if ('left' in tree) {
if (tree.label === 'CP') {
Expand All @@ -78,7 +80,7 @@ export function fix(tree: Tree, scope?: Scope): StrictTree {
if (scope && tree.label === 'DP') {
const d = tree.left;
assertLeaf(d);
if (d.word.covert) throw new Error('covert D');
if (d.word.covert) throw new Impossible('covert D');
const q = quantifiers[d.word.entry?.toaq ?? ''];
if (q) {
scope.quantify(q, right);
Expand Down
Loading

0 comments on commit 0b29484

Please sign in to comment.