-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathparser.ts
More file actions
122 lines (107 loc) · 3.09 KB
/
parser.ts
File metadata and controls
122 lines (107 loc) · 3.09 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
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
import {
version,
Parser as RustParser,
type ScaledRecipeWithReport,
} from "../pkg/cooklang_wasm";
// for temporary backwards compatibility, let's export it with the old name
const Parser = RustParser;
export { version, Parser, type ScaledRecipeWithReport };
class CooklangRecipe {
metadata = {};
ingredients = new Map();
// TODO should we use something other than array here?
sections = [];
cookware = new Map();
timers = [];
constructor(rawParsed?: ScaledRecipeWithReport) {
if (rawParsed) {
this.setRecipe(rawParsed);
}
}
setRecipe(rawParsed: ScaledRecipeWithReport) {
this.metadata = {};
// this.ingredients = [];
// this.steps = [];
// this.cookware = [];
// this.timers = [];
}
}
class CooklangParser extends CooklangRecipe {
public version: string;
public extensionList: string[];
constructor(public rawContent?: string) {
super();
this.version = version();
this.extensionList = [] as string[];
}
set raw(raw: string) {
this.rawContent = raw;
}
get raw() {
if (!this.rawContent)
throw new Error("recipe not set, call .raw(content) to set it first");
return this.rawContent;
}
#handleFunctionalOrInstance(instanceInput: string | undefined) {
if (this.rawContent) {
if (instanceInput)
throw new Error("recipe already set, create a new instance");
return this.rawContent;
}
if (!instanceInput) {
throw new Error("pass a recipe as a string or generate a new instance");
}
return instanceInput;
}
// TODO create issue to fill this in
set extensions(extensions: string[]) {
this.extensionList = extensions;
}
get extensions() {
if (!this.extensionList) throw new Error("TODO");
return this.extensionList;
}
// TODO create issue for this
renderPrettyString(recipeString?: string) {
const input = this.#handleFunctionalOrInstance(recipeString);
// TODO renderPrettyString this then return
return input;
}
renderHTML(recipeString?: string) {
const input = this.#handleFunctionalOrInstance(recipeString);
// TODO renderHTML this then return
return input;
}
parseRaw(recipeString?: string) {
const input = this.#handleFunctionalOrInstance(recipeString);
// TODO parseRaw this then return
return input;
}
// TODO return fully typed JS Object
parse(recipeString?: string) {
const input = this.#handleFunctionalOrInstance(recipeString);
// TODO actually parse
const parsed = {
recipe: { ingredients: [input] },
} as unknown as ScaledRecipeWithReport;
if (this.rawContent) {
this.setRecipe(parsed);
}
if (!this.rawContent && recipeString) {
const direct = new CooklangRecipe(parsed);
return direct;
} else {
throw new Error("should never reach this");
}
}
debug(recipeString?: string): {
version: string;
ast: string;
events: string;
} {
const input = this.#handleFunctionalOrInstance(recipeString);
// TODO debug parse this then return
return { version: this.version, ast: input, events: input };
}
}
export { CooklangParser };