-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequenceGrammar.ts
More file actions
52 lines (44 loc) · 1.38 KB
/
sequenceGrammar.ts
File metadata and controls
52 lines (44 loc) · 1.38 KB
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
import { ContinuesWith, Hybrid, InitialInstructionBlock, InstructionBlock, InstructionChain, Semantic } from "../source/semantium.ts";
class Result<T = string> extends InstructionChain<Result<T>>
{
elements: string;
matchInSequence(sequence: T[])
{
//Logic to detect when this sentence occurs
}
}
class Start extends InitialInstructionBlock<Result>
{
beginsWith = Hybrid({
accessed: ContinuesWith(OptionalModifier, MultiMatch),
called: (word: string) => ContinuesWith(Continuation, End)
});
}
class Continuation extends InstructionBlock<Result>
{
followedBy = Hybrid({
accessed: ContinuesWith(OptionalModifier, MultiMatch),
called: (word: string) => ContinuesWith(Continuation, End)
});
}
class End extends InstructionBlock<Result>
{
end = ContinuesWith(Result);
endsWith = (word: string) => ContinuesWith(Result);
}
class OptionalModifier extends InstructionBlock<Result>
{
optional = Hybrid({
accessed: ContinuesWith(MultiMatch),
called: (word: string) => ContinuesWith(MultiMatch, Continuation, End)
});
}
class MultiMatch extends InstructionBlock<Result>
{
either = (...words: string[]) => ContinuesWith(Continuation, End);
}
export const sequenceAPI = Semantic.DefineAPI({
blocks: [Start, Continuation, End, OptionalModifier, MultiMatch],
resultBuilder: Result,
result: Result
});