-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAPI.h
More file actions
371 lines (281 loc) · 8.62 KB
/
Copy pathAPI.h
File metadata and controls
371 lines (281 loc) · 8.62 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#pragma once
#include "Source.h" // for SourceConvention
//
// Despite being mentioned here:
// language/LibraryLink/tutorial/LibraryStructure.html
//
// It is not actually possible to include "wstp.h" for use with WolframLibrary.h
//
// Using wstp.h results in errors like:
// error: unknown type name 'MLINK'
//
// Related bugs: 357133
//
// This bug was fixed in v12.0
//
// When targeting 12.0 as a minimum, then we can switch to using WSTP
//
// Also be a good citizen and cleanup the leftover defines from mathlink and WolframLibrary
//
#if USE_MATHLINK
#include "mathlink.h"
#undef P
#endif // USE_MATHLINK
#include "WolframLibrary.h"
#undef True
#undef False
#include <cstddef> // for size_t
#include <cstdint> // for uint_8
#include <ostream>
class Node;
class ParserSession;
using NodePtr = const Node *;
using ParserSessionPtr = ParserSession *;
using Buffer = const unsigned char *;
using MBuffer = unsigned char *;
#if USE_EXPR_LIB
using expr = void *;
#endif // USE_EXPR_LIB
//
// The modes that stringifying could happen in
//
// Normal:
// Tokens are treated normally
//
// Tag:
// Stringify the next token as a tag:
// a::bcd
// a::"bcd"
// #abc
// #"abc"
//
// File:
// Stringify the next token as a file:
// << foo
// foo >> bar
// foo >>> bar
//
enum StringifyMode : uint8_t {
STRINGIFYMODE_NORMAL = 0,
STRINGIFYMODE_TAG = 1,
STRINGIFYMODE_FILE = 2,
};
//
// Different encoding modes
//
// Normal
// generates issues that you would expect if coming from a file or a string
//
//
// Box
// Coming from a box, so some issues will be disabled
// These issues will be disabled:
// NonASCIICharacters
// Unexpected newline character: \[IndentingNewLine]
//
enum EncodingMode : uint8_t {
ENCODINGMODE_NORMAL = 0,
ENCODINGMODE_BOX = 1,
};
enum FirstLineBehavior : uint8_t {
//
// Source is a string or something, so if #! is on first line, then do not treat special
//
FIRSTLINEBEHAVIOR_NOTSCRIPT = 0,
//
// Source is something like .wl file that is being treated as a script
// Or source is .wl file that is NOT being treated as a script
// #! may be present, or it might not
//
FIRSTLINEBEHAVIOR_CHECK = 1,
//
// Source is a .wls file and there is definitely a #! on first line
//
FIRSTLINEBEHAVIOR_SCRIPT = 2,
};
enum UnsafeCharacterEncodingFlag : uint8_t {
UNSAFECHARACTERENCODING_OK = 0,
UNSAFECHARACTERENCODING_INCOMPLETEUTF8SEQUENCE = 1,
UNSAFECHARACTERENCODING_STRAYSURROGATE = 2,
UNSAFECHARACTERENCODING_BOM = 3,
};
struct ParserSessionOptions {
SourceConvention srcConvention;
uint32_t tabWidth;
FirstLineBehavior firstLineBehavior;
EncodingMode encodingMode;
int8_t alreadyHasEOFSentinel;
};
EXTERN_C DLLEXPORT int CreateParserSession(ParserSessionPtr *sessionOut);
EXTERN_C DLLEXPORT void DestroyParserSession(ParserSessionPtr session);
EXTERN_C DLLEXPORT int ParserSessionInit(ParserSessionPtr session, Buffer buf, uint64_t len, WolframLibraryData libData, ParserSessionOptions opts);
EXTERN_C DLLEXPORT int ParserSessionInitSimple(ParserSessionPtr session, Buffer Buf, uint64_t Len, int AlreadyHasEOFSentinel);
EXTERN_C DLLEXPORT void ParserSessionDeinit(ParserSessionPtr session);
EXTERN_C DLLEXPORT int ParserSessionConcreteParse(ParserSessionPtr session, NodePtr *nOut);
EXTERN_C DLLEXPORT int ParserSessionTokenize(ParserSessionPtr session, NodePtr *nOut);
EXTERN_C DLLEXPORT int ParserSessionConcreteParseLeaf(ParserSessionPtr session, StringifyMode mode, NodePtr *nOut);
EXTERN_C DLLEXPORT int ParserSessionSafeString(ParserSessionPtr session, NodePtr *nOut);
EXTERN_C DLLEXPORT void ParserSessionReleaseNode(ParserSessionPtr session, NodePtr N);
EXTERN_C DLLEXPORT int NodeSyntaxQ(NodePtr N);
DLLEXPORT void NodePrint(ParserSessionPtr session, NodePtr N, std::ostream& s);
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int NodeToExpr(ParserSessionPtr session, NodePtr N, expr *eOut);
#endif // USE_EXPR_LIB
#if USE_MATHLINK
EXTERN_C DLLEXPORT int NodePut(ParserSessionPtr session, NodePtr N, MLINK callLink);
#endif // USE_MATHLINK
EXTERN_C DLLEXPORT mint WolframLibrary_getVersion();
EXTERN_C DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData);
EXTERN_C DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData);
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int CreateParserSession_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int CreateParserSession_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int DestroyParserSession_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int DestroyParserSession_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int ConcreteParseBytes_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int ConcreteParseBytes_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int ConcreteParseFile_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int ConcreteParseFile_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int TokenizeBytes_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int TokenizeBytes_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int TokenizeFile_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int TokenizeFile_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int ConcreteParseLeaf_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int ConcreteParseLeaf_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
#if USE_EXPR_LIB
EXTERN_C DLLEXPORT int SafeString_LibraryLink(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res);
#elif USE_MATHLINK
EXTERN_C DLLEXPORT int SafeString_LibraryLink(WolframLibraryData libData, MLINK link);
#endif // USE_EXPR_LIB
//
//
//
class ScopedFileBuffer {
private:
MBuffer buf;
size_t len;
bool inited;
public:
DLLEXPORT ScopedFileBuffer(Buffer inStrIn, size_t inLen);
DLLEXPORT ~ScopedFileBuffer();
DLLEXPORT Buffer getBuf() const;
DLLEXPORT size_t getLen() const;
DLLEXPORT bool fail() const;
};
#if USE_MATHLINK
//
// A UTF8 String from MathLink that has lexical scope
//
class ScopedMLUTF8String {
private:
MLINK link;
Buffer buf;
//
// used by MLReleaseUTF8String
//
// not exposed publicly, to stay consistent with other strings
//
int b;
//
// unused
//
int c;
public:
ScopedMLUTF8String(MLINK link);
~ScopedMLUTF8String();
bool read();
Buffer get() const;
};
//
// A String from MathLink that has lexical scope
//
class ScopedMLString {
private:
MLINK link;
const char *buf;
public:
ScopedMLString(MLINK link);
~ScopedMLString();
int read();
const char *get() const;
};
//
// A Symbol from MathLink that has lexical scope
//
class ScopedMLSymbol {
private:
MLINK link;
const char *buf;
public:
ScopedMLSymbol(MLINK link);
~ScopedMLSymbol();
int read();
const char *get() const;
};
#endif // USE_MATHLINK
#if USE_MATHLINK
//
// A ByteArray from MathLink that has lexical scope
//
class ScopedMLByteArray {
private:
MLINK link;
MBuffer buf;
int *dims;
char **heads;
int depth;
public:
ScopedMLByteArray(MLINK link);
~ScopedMLByteArray();
int read();
Buffer get() const;
size_t getByteCount() const;
};
#endif // USE_MATHLINK
#if USE_EXPR_LIB
//
//
//
class ScopedUTF8String {
private:
WolframLibraryData libData;
Buffer str;
public:
ScopedUTF8String(WolframLibraryData libData, MArgument Arg);
~ScopedUTF8String();
Buffer data() const;
};
//
//
//
class ScopedNumericArray {
private:
WolframLibraryData libData;
MNumericArray arr;
public:
ScopedNumericArray(WolframLibraryData libData, MArgument Arg);
~ScopedNumericArray();
size_t size() const;
Buffer data() const;
};
#endif // USE_EXPR_LIB