-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslangroom.ts
More file actions
190 lines (176 loc) · 6.98 KB
/
slangroom.ts
File metadata and controls
190 lines (176 loc) · 6.98 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
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
// SPDX-FileCopyrightText: 2024 Dyne.org foundation
//
// SPDX-License-Identifier: AGPL-3.0-or-later
import { getIgnoredStatements } from '@slangroom/ignored';
import {
type ZenOutput,
type ZenParams,
type JsonableObject,
zencodeExec
} from '@slangroom/shared';
import { lex, parse, visit, Plugin, PluginMap, PluginContextImpl } from '@slangroom/core';
import {
sentenceHighlight,
textHighlight,
errorColor,
suggestedColor,
missingColor,
extraColor,
lineNoColor
} from '@slangroom/shared';
/**
* Just a utility type to ease typing.
*/
export type Plugins = Plugin | Plugin[];
type GenericError = {
message: Error,
lineNo: number,
start?: number,
end?: number
}
const RUI = 'Rule unknown ignore\n';
/**
* The entrypoint to the Slangroom software.
*
* @example
* ```ts
* import {http} from "@slangroom/http";
* import {git} from "@slangroom/git";
*
* const sl = new Slangroom(http, git);
* const {result, logs} = sl.execute(contractWithCustomStatements, zenroomParameters)
* ```
*/
export class Slangroom {
/**
* The datastore that stores our plugins.
*/
#plugins = new PluginMap();
/**
* Creates an instance of {@link Slangroom}.
*
* @param first A plugin or list of it.
* @param rest A plugin or list of it, spreaded.
*
* @throws {@link @slangroom/core/plugin#DuplicatePluginError}
* If any of the plugin definitions have duplicates.
*/
constructor(first: Plugins, ...rest: Plugins[]) {
const p = this.#plugins;
[first, ...rest].forEach(function recurse(x) {
if (Array.isArray(x)) x.forEach(recurse);
else x.store.forEach(([k, v]) => p.set(k, v));
});
}
/**
* Executes a given contract with parameters using the list of plugins
* provided at the class ininitation.
*
* @param contract The Zenroom contract with optional custom statements.
* @param optParams The Zenroom parameters to be supplied.
*
* @returns The output of Zenroom execution along with custom executors
* applied to it (before or after).
*
* @throws {@link Error}
* If there exists any general errors of the parsed lines.
*
* @throws {@link Error}
* If there exists any errors on any matches.
*
* @throws {@link Error}
* If no plugin definitions can be matched against a custom statement.
*/
async execute(contract: string, optParams?: Partial<ZenParams>): Promise<ZenOutput> {
// substitute all tabs with 4 spaces in contract for better error reporting
contract = contract.replaceAll('\t', ' ');
const paramsGiven = requirifyZenParams(optParams);
const { ignoredLines, invalidLines } = await getIgnoredStatements(`${RUI}${contract}`);
if (typeof invalidLines[0] !== "undefined") {thorwErrors(invalidLines.map((x: {message: Error, lineNo: number}) => {
x.lineNo = x.lineNo - 1;
return x;
}), contract)}
// lex
const lexedResult = ignoredLines.map((ignored) => lex(ignored[0], ignored[1] - 1));
const lexedErrors = lexedResult.flatMap((x) => {if (!x.ok) return x.error; return [];});
if (typeof lexedErrors[0] !== "undefined") thorwErrors(lexedErrors, contract);
const lexedLines = lexedResult.flatMap((x) => {if(x.ok) return [x.value]; return [];});
// parse
const parsedLines = lexedLines.map((lexed) => parse(this.#plugins, ...lexed));
const parsedErrors = parsedLines.flatMap((x) => [...x.errors, ...(x.matches[0]?.err ?? [])])
if (typeof parsedErrors[0] !== "undefined") thorwErrors(parsedErrors, contract);
const cstGivens = parsedLines.filter((x) => x.givenThen === 'given');
for (const cst of cstGivens) {
const { ast, lineNo } = visit(cst, paramsGiven);
const exec = this.#plugins.get(ast.key);
if (!exec) return thorwErrors( [{message: new Error('no statements matched'), lineNo}], contract);
const res = await exec(new PluginContextImpl(ast));
if (!res.ok) return thorwErrors( [{message: res.error, lineNo}], contract, paramsGiven.data);
if (ast.into) paramsGiven.data[ast.into] = res.value;
else if (ast.intoSecret) paramsGiven.keys[ast.intoSecret] = res.value;
}
const zout = await zencodeExec(`${RUI}${contract}`, paramsGiven);
const paramsThen: ZenParams = { data: zout.result, keys: paramsGiven.keys };
const cstThens = parsedLines.filter((x) => x.givenThen === 'then');
for (const cst of cstThens) {
const { ast, lineNo } = visit(cst, paramsThen);
const exec = this.#plugins.get(ast.key);
if (!exec) return thorwErrors( [{message: new Error('no statements matched'), lineNo}], contract);
const res = await exec(new PluginContextImpl(ast));
if (!res.ok) return thorwErrors( [{message: res.error, lineNo}], contract, paramsThen.data);
if (ast.into) paramsThen.data[ast.into] = res.value;
else if (ast.intoSecret) paramsThen.keys[ast.intoSecret] = res.value;
}
// remove null values from output
Object.keys(paramsThen.data).forEach(k => (paramsThen.data[k] == null) && delete paramsThen.data[k]);
return { result: paramsThen.data, logs: zout.logs };
}
getPlugin() {
return this.#plugins
}
}
/**
* Converts a partial {@link ZenParams} into a required {@link ZenParams}.
*/
const requirifyZenParams = (params?: Partial<ZenParams>): Required<ZenParams> => {
if (!params) return { data: {}, keys: {}, conf: '', extra: {} };
if (!params.data) params.data = {};
if (!params.keys) params.keys = {};
return { extra: {}, conf: '', ...params } as Required<ZenParams>;
};
/**
* Print Error in a pretty way
* @param error {message, lineNo, ?start, ?end}
* @param contract {string}
*/
const thorwErrors = (errorArray: GenericError[], contract: string, params?: JsonableObject) => {
const contractLines = contract.split('\n');
const lineNumber = errorArray[0]!.lineNo;
const initialWS = contractLines[lineNumber-1]!.match(/^[\s]+/) || [''];
const colStart = errorArray[0]!.start ? errorArray[0]!.start+initialWS[0].length : initialWS[0].length;
const colEnd = errorArray[0]!.end ? errorArray[0]!.end+1+initialWS[0].length : contractLines[lineNumber-1]!.length;
const lineStart = lineNumber > 2 ? lineNumber - 2 : 0;
const lineEnd = lineNumber + 2 > contractLines.length ? contractLines.length : lineNumber + 2;
let e = "";
for (let i = lineStart; i < lineEnd; i++) {
const linePrefix = `${i} | `;
if (i === lineNumber -1) {
const boldError = textHighlight(contractLines[i]!.slice(colStart, colEnd));
const redBackground = sentenceHighlight(contractLines[i]!.slice(0, colStart) + boldError + contractLines[i]!.slice(colEnd));
e = e.concat(`${lineNoColor(linePrefix)}${redBackground}\n`);
e = e.concat(' '.repeat(colStart + linePrefix.length) + errorColor('^'.repeat(colEnd - colStart)) + '\n');
} else { e = e.concat(`${lineNoColor(linePrefix)}${contractLines[i]}\n`); }
}
e = e.concat('\n' + 'Error colors:\n');
e = e.concat(` - ${errorColor('error')}\n`);
e = e.concat(` - ${suggestedColor('suggested words')}\n`);
e = e.concat(` - ${missingColor('missing words')}\n`);
e = e.concat(` - ${extraColor('extra words')}\n`);
for (const err of errorArray) {
e = e.concat('\n' + err.message + '\n');
}
if(params) {
e = e.concat('\n' + 'Heap:\n' + JSON.stringify(params, null, 4) + '\n');
}
throw new Error(e);
}