-
Notifications
You must be signed in to change notification settings - Fork 2
/
language.ts
204 lines (178 loc) · 4.5 KB
/
language.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// A parser for a custom language.
import * as Z from "./parsers/parser-5.js"
function asWord<T extends string>(word: T): Word<T> {
return { type: "word", word }
}
export type Word<T extends string = string> = {
readonly type: "word"
readonly word: T
}
export type Preposition = {
readonly type: "preposition"
readonly head: Word
readonly tail: readonly Word[]
}
export type Modifier =
| {
readonly type: "modifier"
readonly word: Word
}
| {
readonly type: "mod-group"
readonly head: Word
readonly tail: readonly Word[]
}
export type Noun = {
readonly type: "noun"
readonly article: Word<"le"> | undefined
readonly head: Word
readonly tail: readonly Modifier[]
}
export type Verb = {
readonly type: "verb"
readonly head: Word
readonly tail: readonly Word[]
readonly preverbs: readonly Word[]
}
const Whitespace = Z.regex(/^[ \t]+/).void()
export namespace Word {
export const Content: readonly string[] = [
"ane", // food
"ave", // to have
"ilo", // tool
"kama", // to acquire, future
"kela", // that
"kili", // fruit
"lako", // they all
"lekume", // vegetable
"lili", // small
"lupo", // dog
"mana", // energy
"mi", // me
"muko", // very
"musi", // playful
"noso", // us
"olelo", // speech, language
"ona", // he, she, they (singular)
"oto", // sound
"pini", // to finish, past
"poko", // a little
"soje", // land animal
"sona", // knowledge
"suli", // large
"tepo", // time
"tomo", // house
"tosa", // you all
"tu", // you
"uta", // fight, to battle
"waso", // bird
]
export const Preverb: readonly string[] = [
"kama", // ___ will ...
"pini", // ___ have ...
]
export const Preposition: readonly string[] = [
"li", // marks the action
"e", // marks the direct object
"tane", // “from ...”
"tawa", // “to ...”
"lo", // “at ...”
"pove", // “in the opinion of ...”
"ko", // “using ...”
"pa", // “for ...”
]
}
export const ContentWord: Z.Parser<Word> = Z.oneOf(Word.Content).map(asWord)
export const PiGroup: Z.Parser<Modifier> = Z.seq(
Z.text("pi"),
Z.many(Z.seq(Whitespace, ContentWord).map((value) => value[1])),
).map(([, matches]) => ({
type: "mod-group",
head: asWord("pi"),
tail: matches,
}))
export const Modifier: Z.Parser<Modifier> = Z.any(
PiGroup,
ContentWord.map<Modifier>((word) => ({
type: "modifier",
word,
})),
)
export const Noun: Z.Parser<Noun> = Z.seq(
Z.optional(Z.seq(Z.text("le"), Whitespace)),
ContentWord,
Z.many(Z.seq(Whitespace, Modifier).map((value) => value[1])),
).map(([le, head, tail]) => ({
type: "noun",
article: le ? asWord("le") : undefined,
head,
tail,
}))
export const Verb: Z.Parser<Verb> = Z.sepBy1(ContentWord, Whitespace).map(
(_words) => {
const words = [..._words]
const preverbs: Word[] = []
while (words.length > 1) {
if (Word.Preverb.includes(words[0]!.word)) {
preverbs.push(words[0]!)
words.shift()
}
}
return {
type: "verb",
head: words[0]!,
tail: words.slice(1),
preverbs,
}
},
)
export const Subject: Z.Parser<readonly Noun[]> = Z.seq(
Z.optional(Z.seq(Z.text("en"), Whitespace)),
Z.sepBy(Noun, Z.seq(Whitespace, Z.text("en"), Whitespace)),
).map((value) => value[1])
export type Action = {
readonly type: "action"
readonly action: Verb
readonly head: Word<"li" | "o">
}
export const Action: Z.Parser<Action> = Z.seq(
Z.oneOf(["li", "o"] as const).map(asWord),
Whitespace,
Verb,
).map(([head, , action]) => ({
type: "action",
head,
action,
}))
export type Object = {
readonly type: "object"
readonly object: Noun
}
export const Object: Z.Parser<Object> = Z.seq(
Z.text("e"),
Whitespace,
Noun,
).map(([, , object]) => ({ type: "object", object }))
export const Preposition = Z.any(Action, Object)
export type Sentence = {
readonly type: "sentence"
readonly head: readonly Noun[]
readonly tail: readonly (Action | Object)[]
}
export const Sentence: Z.Parser<Sentence> = Z.any(
Z.seq(
Z.lookahead(Z.seq(Z.text("o"), Whitespace)),
Action,
Z.many(Z.seq(Whitespace, Preposition).map((value) => value[1])),
).map(
([, first, tail]): Sentence => ({
type: "sentence",
head: [],
tail: [first, ...tail],
}),
),
Z.seq(
Subject,
Z.many(Z.seq(Whitespace, Preposition).map((value) => value[1])),
).map(([head, tail]): Sentence => ({ type: "sentence", head, tail })),
)