-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.ts
138 lines (128 loc) · 3.47 KB
/
index.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
import parser from "./src/ShortcutsParser";
import { ParseResult } from "./src/Production";
import * as bplistc from "bplist-creator";
import { PositionedError, AsAble, ActionsParse } from "./src/ParserData";
import { ConvertingContext } from "./src/Converter";
import { Shortcut, WFShortcut } from "./src/OutputData";
import { InverseConvertingContext } from "./src/InverseConvertingContext";
import * as bplistp from "bplist-parser";
// export {default as parser} from "./src/ShortcutsParser";
export { PositionedError, ConvertingContext, AsAble, WFShortcut };
export {
allActions,
getActionFromID,
getActionFromName
} from "./src/ActionData";
export function parse(
string: string,
options: {
make?: ("shortcutjson" | "shortcutplist" | "outputdata")[];
useWarnings?: boolean;
// --- don't use unless necessary: ---
extraParseActions?: {
[key: string]: (cc: ConvertingContext, ...args: AsAble[]) => void;
};
ccOverride?: ConvertingContext;
// --- unused: ---
generateScPLData?: boolean;
// --- deprecated: ---
makePlist?: boolean;
makeShortcut?: boolean;
fileLoader?: (filename: string) => string;
}
) {
let parsed: ParseResult;
try {
parsed = parser.parse(string, [1, 1]);
} catch (er) {
if (er instanceof PositionedError) {
throw er;
}
throw new PositionedError(
`Error somewhere: ${er.toString()}`,
[1, 1],
[100, 1]
);
}
if (!parsed.success) {
throw new PositionedError("Failed to parse anything", [1, 1], [100, 1]);
}
if (parsed.remainingStr) {
if (!parsed.pos) {
throw new Error("!parsed.pos");
}
throw new PositionedError("Parsing error around here", parsed.pos, [
parsed.pos[0] + 100,
1
]);
}
const generateScPLData =
options.generateScPLData === undefined ? true : false;
let cc: ConvertingContext;
if (options.ccOverride) {
cc = options.ccOverride;
} else {
cc = new ConvertingContext();
}
const converterActions = options.extraParseActions;
if (converterActions) {
Object.keys(converterActions).forEach(key => {
cc.setParserAction(key, converterActions[key]);
});
}
cc.useWarnings = !!options.useWarnings;
let shortcut;
try {
shortcut = (<ActionsParse>parsed.data).asShortcut(cc, {
useWarnings: !!options.useWarnings
});
} catch (er) {
if (er instanceof PositionedError) {
throw er;
}
throw new PositionedError(
`Unknown location in error: ${er.message}`,
[1, 1],
[100, 1]
);
}
if (options.make) {
const data = shortcut.build();
const output: {
shortcutjson?: WFShortcut;
shortcutplist?: Buffer;
outputdata?: Shortcut;
warnings?: PositionedError[];
} = {};
if (options.make.indexOf("outputdata") > -1) {
output.outputdata = shortcut;
}
if (options.make.indexOf("shortcutjson") > -1) {
output.shortcutjson = data;
}
if (options.make.indexOf("shortcutplist") > -1) {
//eslint-disable-next-line @typescript-eslint/no-explicit-any
output.shortcutplist = (<any>bplistc)(data);
}
if (options.useWarnings) {
output.warnings = cc.warnings;
}
return output;
}
if (options.makePlist) {
//eslint-disable-next-line @typescript-eslint/no-explicit-any
return (<any>bplistc)(shortcut.build());
}
return shortcut;
}
export function inverse(
data: WFShortcut | Buffer,
options?: { quotes?: '"' | "'"; indent?: string | number }
): string {
const icc = new InverseConvertingContext(options);
if (data instanceof Buffer) {
data = <WFShortcut>bplistp.parseBuffer(data);
}
const result = icc.createActionsAble(Shortcut.inverse(data));
return result;
}