Skip to content

Commit

Permalink
Make a src/syntax subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
lynn committed May 10, 2024
1 parent 2b05c0c commit 74bf71d
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Glosser } from './morphology/gloss';
import yargs from 'yargs';
import { pngGlossSentence } from './modes/png-gloss';
import { Tree } from './tree';
import { recover } from './core/recover';
import { recover } from './syntax/recover';
import { trimTree } from './tree/trim';
import { drawTreeToCanvas } from './tree/draw';
import { parse } from './modes/parse';
Expand Down
2 changes: 1 addition & 1 deletion src/modes/boxes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inTone } from '../morphology/tokenize';
import { Tree, assertBranch, skipFree, treeText } from '../tree';
import { Tone } from '../core/types';
import { Tone } from '../morphology/tone';
import { Impossible, Ungrammatical, Unimplemented } from '../core/error';

export interface PostField {
Expand Down
2 changes: 1 addition & 1 deletion src/morphology/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inTone } from './tokenize';
import { Tone } from '../core/types';
import { Tone } from './tone';

export const verbTypes = [
'name quote',
Expand Down
2 changes: 1 addition & 1 deletion src/morphology/gloss.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entry, dictionary } from './dictionary';
import { bare, clean, splitPrefixes, tone } from './tokenize';
import { Tone } from '../core/types';
import { Tone } from './tone';
import toaduaGlossesJson from '../../data/toadua/toadua.json';

interface Gloss {
Expand Down
2 changes: 1 addition & 1 deletion src/morphology/tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
splitPrefixes,
ToaqTokenizer,
} from './tokenize';
import { Tone } from '../core/types';
import { Tone } from './tone';

test('it cleans up Toaq words', () => {
expect(clean('gi')).toEqual('gı');
Expand Down
2 changes: 1 addition & 1 deletion src/morphology/tokenize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dictionary, underscoredWordTypes } from './dictionary';
import { Impossible, Ungrammatical } from '../core/error';
import { Tone } from '../core/types';
import { Tone } from './tone';

// Vyái → ꝡáı
export function clean(word: string): string {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/semantics/denote.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from 'vitest';
import { parse } from '../modes/parse';
import { recover } from '../core/recover';
import { recover } from '../syntax/recover';
import { denote } from './denote';
import { Expr } from './model';
import { toPlainText } from './render';
Expand Down
2 changes: 1 addition & 1 deletion src/semantics/denote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Word,
effectiveLabel,
} from '../tree';
import { Tone } from '../core/types';
import { Tone } from '../morphology/tone';
import { compose } from './compose';
import {
adjuncts,
Expand Down
38 changes: 21 additions & 17 deletions src/core/recover.ts → src/syntax/recover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
assertLeaf,
effectiveLabel,
} from '../tree';
import { Impossible } from './error';
import { reverse } from './misc';
import { Impossible } from '../core/error';
import { reverse } from '../core/misc';
import { inTone } from '../morphology/tokenize';
import { Tone } from './types';
import { Tone } from '../morphology/tone';

interface Quantification {
type: 'quantification';
Expand Down Expand Up @@ -166,20 +166,27 @@ class Scope {
}
}

/**
* Recurses down a parsed syntax tree to recover the deep structure.
*/
class Recoverer {
private nextBinding = 0;
private nextCoindex = 0;

constructor() {}

private newBinding(): number {
return this.nextBinding++;
}

private newCoindex(): string {
return String.fromCodePoint('𝑖'.codePointAt(0)! + this.nextCoindex++);
}

private newScope(): Scope {
return new Scope(() => this.nextBinding++);
}

private fixSerial(serial: Tree, terms: Tree[]): Tree {
return fixSerial(serial, terms, () => this.newCoindex());
}

recover(tree: Tree, scope: Scope | undefined): StrictTree {
if ('children' in tree) {
if (tree.label === '*𝘷P') {
Expand All @@ -190,27 +197,23 @@ class Recoverer {
}
if (!('children' in serial)) throw new Impossible('strange *Serial');

const vP = fixSerial(serial, tree.children.slice(1), () =>
this.newCoindex(),
);
const vP = this.fixSerial(serial, tree.children.slice(1));
return this.recover(vP, scope);
} else {
throw new Impossible('unexpected non-binary tree: ' + tree.label);
}
} else if ('left' in tree) {
if (tree.label === 'VP' && tree.left.label === '*Serial') {
// Tiny hack to extract a VP from fixSerial:
const vP = fixSerial(tree.left, [pro(), tree.right], () =>
this.newCoindex(),
);
const vP = this.fixSerial(tree.left, [pro(), tree.right]);
assertBranch(vP);
assertBranch(vP.right);
return this.recover(vP.right.right, undefined);
}

// Subclauses open a new scope
if (tree.label === 'CP' || tree.label === 'CPrel') {
const newScope = new Scope(() => this.newBinding());
const newScope = this.newScope();
const right = this.recover(tree.right, newScope);
return {
label: tree.label,
Expand All @@ -221,9 +224,9 @@ class Recoverer {

// Conjoined clauses each get a new scope of their own
if (tree.label === '&P' && effectiveLabel(tree) !== 'DP') {
const leftScope = new Scope(() => this.newBinding());
const leftScope = this.newScope();
const left = this.recover(tree.left, leftScope);
const rightScope = new Scope(() => this.newBinding());
const rightScope = this.newScope();
assertBranch(tree.right);
const conjunction = this.recover(tree.right.left, scope);
const right = this.recover(tree.right.right, rightScope);
Expand Down Expand Up @@ -260,7 +263,8 @@ class Recoverer {
}

/**
* Recover a deep-structure Toaq syntax tree by undoing movement.
* Recover a deep-structure Toaq syntax tree by undoing movement and creating
* QP/FocAdvP around scope boundaries.
*
* @param tree A surface-structure tree parsed by the Nearley grammar.
* @returns A strictly binary deep-structure syntax tree.
Expand Down
4 changes: 2 additions & 2 deletions src/core/serial.ts → src/syntax/serial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Impossible, Ungrammatical, Unimplemented } from './error';
import { splitNonEmpty } from './misc';
import { Impossible, Ungrammatical, Unimplemented } from '../core/error';
import { splitNonEmpty } from '../core/misc';
import { Branch, Label, Leaf, Tree, effectiveLabel, makeNull } from '../tree';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/tree/productions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dictionary } from '../morphology/dictionary';
import { getFrame } from '../core/serial';
import { getFrame } from '../syntax/serial';
import { toadua } from '../morphology/toadua';
import { bare, ToaqToken, tone } from '../morphology/tokenize';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/tree/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entry } from '../morphology/dictionary';
import { Impossible } from '../core/error';
import { Tone } from '../core/types';
import { Tone } from '../morphology/tone';

export interface Word {
covert: false;
Expand Down
2 changes: 1 addition & 1 deletion src/web/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useDarkMode, useLocalStorage } from 'usehooks-ts';
import { boxify } from '../modes/boxes';
import { trimTree } from '../tree/trim';
import { treeToEnglish } from '../english/tree';
import { recover } from '../core/recover';
import { recover } from '../syntax/recover';
import { Glosser } from '../morphology/gloss';
import { parse } from '../modes/parse';
import { denote } from '../semantics/denote';
Expand Down
2 changes: 1 addition & 1 deletion src/web/Sentences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useInView } from 'react-intersection-observer';
import refgramSentencesTxt from '../../sentences/refgram.txt?raw';
// @ts-ignore
import aSentencesTxt from '../../sentences/a.txt?raw';
import { recover } from '../core/recover';
import { recover } from '../syntax/recover';
import { denote } from '../semantics/denote';

const rSentences: string[] = refgramSentencesTxt.split('\n');
Expand Down

0 comments on commit 74bf71d

Please sign in to comment.