@the_dissidents/libemmm 0.0.5 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.md +28 -15
  2. package/dist/index.d.mts +124 -44
  3. package/dist/index.d.ts +124 -44
  4. package/dist/index.js +547 -227
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +544 -227
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +4 -1
  9. package/dist/src/builtin/builtin.d.ts +0 -2
  10. package/dist/src/builtin/builtin.js +0 -18
  11. package/dist/src/builtin/builtin.js.map +0 -1
  12. package/dist/src/builtin/define-modifier.d.ts +0 -12
  13. package/dist/src/builtin/define-modifier.js +0 -129
  14. package/dist/src/builtin/define-modifier.js.map +0 -1
  15. package/dist/src/builtin/define-shorthand.d.ts +0 -14
  16. package/dist/src/builtin/define-shorthand.js +0 -178
  17. package/dist/src/builtin/define-shorthand.js.map +0 -1
  18. package/dist/src/builtin/internal.d.ts +0 -41
  19. package/dist/src/builtin/internal.js +0 -106
  20. package/dist/src/builtin/internal.js.map +0 -1
  21. package/dist/src/builtin/module.d.ts +0 -16
  22. package/dist/src/builtin/module.js +0 -156
  23. package/dist/src/builtin/module.js.map +0 -1
  24. package/dist/src/builtin/slot.d.ts +0 -7
  25. package/dist/src/builtin/slot.js +0 -136
  26. package/dist/src/builtin/slot.js.map +0 -1
  27. package/dist/src/builtin/var.d.ts +0 -12
  28. package/dist/src/builtin/var.js +0 -103
  29. package/dist/src/builtin/var.js.map +0 -1
  30. package/dist/src/debug-print.d.ts +0 -14
  31. package/dist/src/debug-print.js +0 -113
  32. package/dist/src/debug-print.js.map +0 -1
  33. package/dist/src/debug.d.ts +0 -15
  34. package/dist/src/debug.js +0 -47
  35. package/dist/src/debug.js.map +0 -1
  36. package/dist/src/default/bullets.d.ts +0 -4
  37. package/dist/src/default/bullets.js +0 -39
  38. package/dist/src/default/bullets.js.map +0 -1
  39. package/dist/src/default/code.d.ts +0 -6
  40. package/dist/src/default/code.js +0 -13
  41. package/dist/src/default/code.js.map +0 -1
  42. package/dist/src/default/default.d.ts +0 -2
  43. package/dist/src/default/default.js +0 -20
  44. package/dist/src/default/default.js.map +0 -1
  45. package/dist/src/default/headings.d.ts +0 -20
  46. package/dist/src/default/headings.js +0 -125
  47. package/dist/src/default/headings.js.map +0 -1
  48. package/dist/src/default/html-renderer.d.ts +0 -26
  49. package/dist/src/default/html-renderer.js +0 -108
  50. package/dist/src/default/html-renderer.js.map +0 -1
  51. package/dist/src/default/inline-styles.d.ts +0 -4
  52. package/dist/src/default/inline-styles.js +0 -24
  53. package/dist/src/default/inline-styles.js.map +0 -1
  54. package/dist/src/default/misc.d.ts +0 -6
  55. package/dist/src/default/misc.js +0 -112
  56. package/dist/src/default/misc.js.map +0 -1
  57. package/dist/src/default/notes.d.ts +0 -28
  58. package/dist/src/default/notes.js +0 -109
  59. package/dist/src/default/notes.js.map +0 -1
  60. package/dist/src/default/quotes.d.ts +0 -4
  61. package/dist/src/default/quotes.js +0 -36
  62. package/dist/src/default/quotes.js.map +0 -1
  63. package/dist/src/default/vars.d.ts +0 -2
  64. package/dist/src/default/vars.js +0 -39
  65. package/dist/src/default/vars.js.map +0 -1
  66. package/dist/src/index.d.ts +0 -13
  67. package/dist/src/index.js +0 -58
  68. package/dist/src/index.js.map +0 -1
  69. package/dist/src/interface.d.ts +0 -174
  70. package/dist/src/interface.js +0 -93
  71. package/dist/src/interface.js.map +0 -1
  72. package/dist/src/messages.d.ts +0 -165
  73. package/dist/src/messages.js +0 -279
  74. package/dist/src/messages.js.map +0 -1
  75. package/dist/src/modifier-helper.d.ts +0 -6
  76. package/dist/src/modifier-helper.js +0 -113
  77. package/dist/src/modifier-helper.js.map +0 -1
  78. package/dist/src/parser-config.d.ts +0 -44
  79. package/dist/src/parser-config.js +0 -67
  80. package/dist/src/parser-config.js.map +0 -1
  81. package/dist/src/parser.d.ts +0 -3
  82. package/dist/src/parser.js +0 -652
  83. package/dist/src/parser.js.map +0 -1
  84. package/dist/src/renderer.d.ts +0 -49
  85. package/dist/src/renderer.js +0 -81
  86. package/dist/src/renderer.js.map +0 -1
  87. package/dist/src/scanner.d.ts +0 -22
  88. package/dist/src/scanner.js +0 -50
  89. package/dist/src/scanner.js.map +0 -1
  90. package/dist/src/temp.d.ts +0 -1
  91. package/dist/src/temp.js +0 -108
  92. package/dist/src/temp.js.map +0 -1
  93. package/dist/src/typing-helper.d.ts +0 -7
  94. package/dist/src/typing-helper.js +0 -3
  95. package/dist/src/typing-helper.js.map +0 -1
  96. package/dist/src/util.d.ts +0 -29
  97. package/dist/src/util.js +0 -177
  98. package/dist/src/util.js.map +0 -1
  99. package/dist/tests/advanced_syntax.test.d.ts +0 -1
  100. package/dist/tests/advanced_syntax.test.js +0 -180
  101. package/dist/tests/advanced_syntax.test.js.map +0 -1
  102. package/dist/tests/basic_syntax.test.d.ts +0 -1
  103. package/dist/tests/basic_syntax.test.js +0 -431
  104. package/dist/tests/basic_syntax.test.js.map +0 -1
  105. package/dist/tests/default/headings.test.d.ts +0 -1
  106. package/dist/tests/default/headings.test.js +0 -90
  107. package/dist/tests/default/headings.test.js.map +0 -1
  108. package/dist/tests/define_block.test.d.ts +0 -1
  109. package/dist/tests/define_block.test.js +0 -209
  110. package/dist/tests/define_block.test.js.map +0 -1
  111. package/dist/tests/define_inline.test.d.ts +0 -1
  112. package/dist/tests/define_inline.test.js +0 -289
  113. package/dist/tests/define_inline.test.js.map +0 -1
  114. package/dist/tests/modules.test.d.ts +0 -1
  115. package/dist/tests/modules.test.js +0 -137
  116. package/dist/tests/modules.test.js.map +0 -1
  117. package/dist/tests/shorthands.test.d.ts +0 -1
  118. package/dist/tests/shorthands.test.js +0 -79
  119. package/dist/tests/shorthands.test.js.map +0 -1
  120. package/dist/tsup.config.d.ts +0 -2
  121. package/dist/tsup.config.js +0 -12
  122. package/dist/tsup.config.js.map +0 -1
  123. package/dist/vitest.config.d.ts +0 -2
  124. package/dist/vitest.config.js +0 -17
  125. package/dist/vitest.config.js.map +0 -1
@@ -1,652 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parse = parse;
4
- const debug_1 = require("./debug");
5
- const debug_print_1 = require("./debug-print");
6
- const interface_1 = require("./interface");
7
- const messages_1 = require("./messages");
8
- const parser_config_1 = require("./parser-config");
9
- const util_1 = require("./util");
10
- const GROUP_BEGIN = ':--';
11
- const GROUP_END = '--:';
12
- const MODIFIER_BLOCK_OPEN = '[.';
13
- const MODIFIER_CLOSE_SIGN = ']';
14
- const MODIFIER_END_SIGN = ';';
15
- const MODIFIER_INLINE_OPEN = '[/';
16
- const MODIFIER_INLINE_END_TAG = '[;]';
17
- const MODIFIER_SYSTEM_OPEN = '[-';
18
- const UnknownModifier = {
19
- [interface_1.NodeType.BlockModifier]: new interface_1.BlockModifierDefinition('UNKNOWN', interface_1.ModifierSlotType.Normal),
20
- [interface_1.NodeType.InlineModifier]: new interface_1.InlineModifierDefinition('UNKNOWN', interface_1.ModifierSlotType.Normal),
21
- [interface_1.NodeType.SystemModifier]: new interface_1.SystemModifierDefinition('UNKNOWN', interface_1.ModifierSlotType.Normal)
22
- };
23
- class EmitEnvironment {
24
- scanner;
25
- root;
26
- messages = [];
27
- blockStack = [];
28
- inlineStack = [];
29
- constructor(scanner) {
30
- this.scanner = scanner;
31
- this.root = { type: interface_1.NodeType.Root, source: scanner.source, content: [] };
32
- }
33
- message(...m) {
34
- for (let msg of m) {
35
- this.messages.push(msg);
36
- debug_1.debug.trace('issued msg', msg.code, msg.info);
37
- }
38
- }
39
- addBlockNode(n) {
40
- (this.blockStack.at(-1) ?? this.root).content.push(n);
41
- return n;
42
- }
43
- addInlineNode(n) {
44
- (0, util_1.assert)(this.inlineStack.length > 0);
45
- this.inlineStack.at(-1).content.push(n);
46
- return n;
47
- }
48
- addString(str) {
49
- (0, util_1.assert)(this.inlineStack.length > 0);
50
- const content = this.inlineStack.at(-1).content;
51
- const last = content.at(-1);
52
- if (last?.type == interface_1.NodeType.Text) {
53
- last.content += str;
54
- last.location.end = this.scanner.position();
55
- }
56
- else
57
- content.push({
58
- type: interface_1.NodeType.Text,
59
- location: {
60
- source: this.scanner.source,
61
- start: this.scanner.position() - str.length,
62
- end: this.scanner.position(),
63
- },
64
- content: str
65
- });
66
- }
67
- startBlock(block) {
68
- this.addBlockNode(block);
69
- this.blockStack.push(block);
70
- }
71
- endBlock() {
72
- (0, util_1.assert)(this.blockStack.length > 0);
73
- const node = this.blockStack.pop();
74
- node.location.end = this.scanner.position();
75
- }
76
- startInline(n) {
77
- if (n.type == interface_1.NodeType.Paragraph)
78
- this.addBlockNode(n);
79
- else
80
- this.addInlineNode(n);
81
- this.inlineStack.push(n);
82
- }
83
- endInline() {
84
- (0, util_1.assert)(this.inlineStack.length > 0);
85
- const node = this.inlineStack.pop();
86
- node.location.end = this.scanner.position();
87
- }
88
- }
89
- class Parser {
90
- scanner;
91
- cxt;
92
- emit;
93
- delayDepth = 0;
94
- groupDepth = 0;
95
- constructor(scanner, cxt) {
96
- this.scanner = scanner;
97
- this.cxt = cxt;
98
- this.emit = new EmitEnvironment(scanner);
99
- }
100
- #loc(to) {
101
- return {
102
- source: this.scanner.source,
103
- start: this.scanner.position(),
104
- end: to ?? this.scanner.position()
105
- };
106
- }
107
- #locFrom(from, to) {
108
- return {
109
- source: this.scanner.source,
110
- start: from,
111
- end: to ?? this.scanner.position()
112
- };
113
- }
114
- #defs(type) {
115
- switch (type) {
116
- case interface_1.NodeType.SystemModifier: return this.cxt.config.systemModifiers;
117
- case interface_1.NodeType.InlineModifier: return this.cxt.config.inlineModifiers;
118
- case interface_1.NodeType.BlockModifier: return this.cxt.config.blockModifiers;
119
- default: return debug_1.debug.never(type);
120
- }
121
- }
122
- #reparse(nodes, depth) {
123
- if (depth > this.cxt.config.reparseDepthLimit)
124
- return false;
125
- let ok = true;
126
- for (const node of nodes) {
127
- switch (node.type) {
128
- case interface_1.NodeType.Preformatted:
129
- case interface_1.NodeType.Text:
130
- case interface_1.NodeType.Escaped:
131
- continue;
132
- case interface_1.NodeType.Paragraph:
133
- ok = this.#reparse(node.content, depth + 1) && ok;
134
- continue;
135
- case interface_1.NodeType.BlockModifier:
136
- case interface_1.NodeType.InlineModifier:
137
- case interface_1.NodeType.SystemModifier:
138
- ok = this.#expand(node, depth + 1) && ok;
139
- continue;
140
- default:
141
- debug_1.debug.never(node);
142
- }
143
- }
144
- return ok;
145
- }
146
- #expandArgument(arg) {
147
- if (arg.expansion !== undefined)
148
- return arg.expansion;
149
- let result = '';
150
- const immediate = this.delayDepth == 0;
151
- for (const e of arg.content) {
152
- switch (e.type) {
153
- case interface_1.NodeType.Text:
154
- case interface_1.NodeType.Escaped:
155
- result += e.content;
156
- break;
157
- case interface_1.NodeType.Interpolation:
158
- if (e.expansion === undefined) {
159
- const inner = this.#expandArgument(e.argument);
160
- if (inner === undefined
161
- || e.definition.expand === undefined
162
- || (!immediate && !e.definition.alwaysTryExpand))
163
- return undefined;
164
- e.expansion = e.definition.expand(inner, this.cxt, immediate);
165
- if (e.expansion === undefined)
166
- return undefined;
167
- }
168
- result += e.expansion;
169
- break;
170
- default:
171
- debug_1.debug.never(e);
172
- }
173
- }
174
- // debug.trace('expanded arg:', result);
175
- arg.expansion = result;
176
- return result;
177
- }
178
- #expandArguments(node) {
179
- for (const arg of node.arguments) {
180
- this.#expandArgument(arg);
181
- // if (!arg.expansion) debug.trace('expand arg failed');
182
- }
183
- }
184
- #expand(node, depth = 0) {
185
- if (node.expansion !== undefined) {
186
- debug_1.debug.trace('already expanded, skipping:', node.mod.name);
187
- return true;
188
- }
189
- if (depth > 0) {
190
- this.#expandArguments(node);
191
- }
192
- if (this.delayDepth > 0 && !node.mod.alwaysTryExpand) {
193
- debug_1.debug.trace('delaying expansion of', node.mod.name);
194
- return true;
195
- }
196
- const immediate = this.delayDepth == 0;
197
- if (depth > 0) {
198
- // simulate initial parse for generated content
199
- if (node.mod.beforeParseContent)
200
- this.emit.message(...node.mod.beforeParseContent(node, this.cxt, immediate));
201
- if (node.content.length > 0) {
202
- if (node.mod.delayContentExpansion)
203
- this.delayDepth++;
204
- this.#reparse(node.content, depth);
205
- if (node.mod.delayContentExpansion)
206
- this.delayDepth--;
207
- }
208
- if (node.mod.afterParseContent)
209
- this.emit.message(...node.mod.afterParseContent(node, this.cxt, immediate));
210
- }
211
- if (node.mod.prepareExpand)
212
- this.emit.message(...node.mod.prepareExpand(node, this.cxt, immediate));
213
- if (node.mod.expand) {
214
- node.expansion = node.mod.expand(node, this.cxt, immediate);
215
- if (!node.expansion) {
216
- // debug.trace('manually delaying expansion of', node.mod.name);
217
- return true;
218
- }
219
- debug_1.debug.trace(`${this.delayDepth > 0 ? 'early ' : ''}expanding:`, node.mod.name);
220
- if (node.expansion.length > 0)
221
- debug_1.debug.trace(() => '-->\n' + debug_print_1.debugPrint.node(...node.expansion));
222
- }
223
- const expansion = node.expansion ?? node.content;
224
- if (expansion.length == 0)
225
- return true;
226
- if (node.mod.beforeProcessExpansion)
227
- this.emit.message(...node.mod.beforeProcessExpansion(node, this.cxt, immediate));
228
- let ok = this.#reparse(expansion, depth);
229
- if (node.mod.afterProcessExpansion)
230
- this.emit.message(...node.mod.afterProcessExpansion(node, this.cxt, immediate));
231
- if (!ok && depth == 0) {
232
- const limit = this.cxt.config.reparseDepthLimit;
233
- this.emit.message(new messages_1.ReachedRecursionLimitMessage(node.location, limit, node.mod.name));
234
- }
235
- return ok;
236
- }
237
- parse() {
238
- this.DOCUMENT();
239
- return new parser_config_1.Document(this.emit.root, this.cxt, this.emit.messages);
240
- }
241
- WHITESPACES() {
242
- while (this.scanner.acceptWhitespaceChar() !== null) { }
243
- }
244
- WHITESPACES_OR_NEWLINES() {
245
- while (this.scanner.acceptWhitespaceChar() !== null
246
- || this.scanner.accept('\n')) { }
247
- }
248
- SHOULD_BE_A_NEWLINE() {
249
- this.WHITESPACES();
250
- if (!this.scanner.accept('\n'))
251
- this.emit.message(new messages_1.ContentShouldBeOnNewlineMessage(this.#loc()));
252
- }
253
- // TODO: this is awkward and doesn't emit messages in the most appropriate way
254
- WARN_IF_MORE_NEWLINES_THAN(n) {
255
- let nlines = 0;
256
- const start = this.scanner.position();
257
- while (true) {
258
- if (this.scanner.accept('\n')) {
259
- nlines++;
260
- continue;
261
- }
262
- if (this.scanner.acceptWhitespaceChar() == null)
263
- break;
264
- }
265
- const end = this.scanner.position();
266
- if (nlines > n)
267
- this.emit.message(new messages_1.UnnecessaryNewlineMessage(this.#locFrom(start, end)));
268
- }
269
- DOCUMENT() {
270
- this.WHITESPACES_OR_NEWLINES();
271
- while (!this.scanner.isEOF()) {
272
- this.BLOCK_ENTITY();
273
- this.WHITESPACES_OR_NEWLINES();
274
- }
275
- }
276
- BLOCK_ENTITY() {
277
- (0, util_1.assert)(!this.scanner.isEOF());
278
- if (this.scanner.peek(MODIFIER_BLOCK_OPEN)) {
279
- this.MODIFIER(interface_1.NodeType.BlockModifier);
280
- return;
281
- }
282
- if (this.scanner.peek(MODIFIER_SYSTEM_OPEN)) {
283
- this.MODIFIER(interface_1.NodeType.SystemModifier);
284
- return;
285
- }
286
- const short = this.cxt.config.blockShorthands.find((x) => this.scanner.accept(x.name));
287
- if (short)
288
- return this.SHORTHAND(interface_1.NodeType.BlockModifier, short);
289
- // simple paragraph(s)
290
- this.MAYBE_GROUPED_PARAGRAPH();
291
- }
292
- MODIFIER(type) {
293
- const posStart = this.scanner.position();
294
- (0, util_1.assert)(this.scanner.accept({
295
- [interface_1.NodeType.BlockModifier]: MODIFIER_BLOCK_OPEN,
296
- [interface_1.NodeType.SystemModifier]: MODIFIER_SYSTEM_OPEN,
297
- [interface_1.NodeType.InlineModifier]: MODIFIER_INLINE_OPEN
298
- }[type]));
299
- const result = this.#defs(type).find((x) => this.scanner.accept(x.name));
300
- const mod = result ?? UnknownModifier[type];
301
- if (result === undefined) {
302
- let name = '';
303
- while (!this.scanner.isEOF()
304
- && !this.scanner.acceptWhitespaceChar()
305
- && !this.scanner.peek(MODIFIER_CLOSE_SIGN)
306
- && !this.scanner.peek(MODIFIER_END_SIGN)) {
307
- if (this.scanner.accept('\\'))
308
- if (this.scanner.isEOF())
309
- break;
310
- name += this.scanner.acceptChar();
311
- }
312
- this.emit.message(new messages_1.UnknownModifierMessage(this.#locFrom(posStart), name));
313
- }
314
- const args = this.ARGUMENTS();
315
- debug_1.debug.trace(`PARSE ${interface_1.NodeType[type]}:`, mod.name);
316
- const endsign = this.scanner.accept(MODIFIER_END_SIGN);
317
- const flagMarker = mod.slotType == interface_1.ModifierSlotType.None;
318
- if (!this.scanner.accept(MODIFIER_CLOSE_SIGN))
319
- this.emit.message(new messages_1.ExpectedMessage(this.#loc(), MODIFIER_CLOSE_SIGN));
320
- const headEnd = this.scanner.position();
321
- const node = {
322
- type, mod: mod,
323
- head: this.#locFrom(posStart, headEnd),
324
- location: this.#locFrom(posStart, headEnd),
325
- arguments: args,
326
- content: [],
327
- expansion: undefined
328
- };
329
- const isMarker = flagMarker || endsign;
330
- return this.MODIFIER_BODY(type, node, MODIFIER_INLINE_END_TAG, isMarker);
331
- }
332
- // also handles "grouped" (delimited) pre-paragraphs
333
- PRE_PARAGRAPH() {
334
- (0, util_1.assert)(!this.scanner.isEOF());
335
- const posStart = this.scanner.position();
336
- const grouped = this.scanner.accept(GROUP_BEGIN);
337
- if (grouped)
338
- this.SHOULD_BE_A_NEWLINE();
339
- const posContentStart = this.scanner.position();
340
- let posContentEnd = this.scanner.position();
341
- let string = '';
342
- while (!this.scanner.isEOF()) {
343
- if (this.scanner.accept('\n')) {
344
- let white = "\n";
345
- let char;
346
- while ((char = this.scanner.acceptWhitespaceChar()) !== null)
347
- white += char;
348
- if (grouped && this.scanner.accept(GROUP_END)) {
349
- this.SHOULD_BE_A_NEWLINE();
350
- this.WARN_IF_MORE_NEWLINES_THAN(1);
351
- break;
352
- }
353
- if (!grouped && this.scanner.accept('\n')) {
354
- this.WARN_IF_MORE_NEWLINES_THAN(0);
355
- break;
356
- }
357
- if (this.scanner.isEOF()) {
358
- if (grouped)
359
- this.emit.message(new messages_1.ExpectedMessage(this.#loc(), GROUP_END));
360
- break;
361
- }
362
- string += white;
363
- }
364
- else {
365
- string += this.scanner.acceptChar();
366
- }
367
- posContentEnd = this.scanner.position();
368
- }
369
- const node = {
370
- type: interface_1.NodeType.Preformatted,
371
- location: this.#locFrom(posStart),
372
- content: {
373
- start: posContentStart,
374
- end: posContentEnd,
375
- text: string
376
- }
377
- };
378
- this.emit.addBlockNode(node);
379
- }
380
- MAYBE_GROUPED_PARAGRAPH() {
381
- (0, util_1.assert)(!this.scanner.isEOF());
382
- if (this.scanner.accept(GROUP_BEGIN)) {
383
- this.groupDepth++;
384
- this.SHOULD_BE_A_NEWLINE();
385
- this.WARN_IF_MORE_NEWLINES_THAN(1);
386
- while (!this.scanner.isEOF()) {
387
- if (this.scanner.accept(GROUP_END)) {
388
- this.groupDepth--;
389
- return;
390
- }
391
- this.BLOCK_ENTITY();
392
- this.WARN_IF_MORE_NEWLINES_THAN(1);
393
- }
394
- // EOF
395
- this.emit.message(new messages_1.ExpectedMessage(this.#loc(), GROUP_END));
396
- }
397
- else {
398
- this.PARAGRAPH();
399
- }
400
- }
401
- #trimNode(node) {
402
- if (node.content.length == 0)
403
- return;
404
- let first = node.content[0];
405
- let last = node.content.at(-1);
406
- if (first.type == interface_1.NodeType.Text)
407
- first.content = first.content.trimStart();
408
- if (last.type == interface_1.NodeType.Text)
409
- last.content = last.content.trimEnd();
410
- }
411
- PARAGRAPH() {
412
- (0, util_1.assert)(!this.scanner.isEOF());
413
- const node = {
414
- type: interface_1.NodeType.Paragraph,
415
- location: this.#loc(),
416
- content: []
417
- };
418
- // debug.trace('PARSE para');
419
- this.emit.startInline(node);
420
- while (!this.scanner.isEOF() && this.INLINE_ENTITY()) { }
421
- this.emit.endInline();
422
- const last = node.content.at(-1);
423
- node.location.actualEnd = last?.location.actualEnd ?? last?.location.end;
424
- this.#trimNode(node);
425
- // debug.trace('PARSE para end');
426
- }
427
- // returns false if breaking out of paragraph
428
- SHORTHAND(type, d) {
429
- const posStart = this.scanner.position() - d.name.length;
430
- let args = [];
431
- for (const part of d.parts) {
432
- let [arg, ok] = this.ARGUMENT_CONTENT(part, ['\n\n']);
433
- if (!ok) {
434
- this.emit.message(new messages_1.ExpectedMessage(this.#loc(), part));
435
- return false;
436
- }
437
- args.push(arg);
438
- }
439
- const headEnd = this.scanner.position();
440
- const node = {
441
- type, mod: d.mod,
442
- head: this.#locFrom(posStart, headEnd),
443
- location: this.#locFrom(posStart, headEnd),
444
- arguments: args,
445
- content: [],
446
- expansion: undefined
447
- };
448
- const isMarker = node.mod.slotType == interface_1.ModifierSlotType.None;
449
- return this.MODIFIER_BODY(type, node, d.postfix, isMarker);
450
- }
451
- MODIFIER_BODY(type, node, postfix, isMarker) {
452
- this.#expandArguments(node);
453
- const immediate = this.delayDepth == 0;
454
- if (node.mod.beforeParseContent)
455
- this.emit.message(...node.mod.beforeParseContent(node, this.cxt, immediate));
456
- if (node.mod.delayContentExpansion)
457
- this.delayDepth++;
458
- let ok = true;
459
- if (isMarker) {
460
- if (type === interface_1.NodeType.InlineModifier)
461
- this.emit.addInlineNode(node);
462
- else
463
- this.emit.addBlockNode(node);
464
- }
465
- else if (type == interface_1.NodeType.InlineModifier) {
466
- node = node;
467
- this.emit.startInline(node);
468
- const pre = node.mod.slotType == interface_1.ModifierSlotType.Preformatted;
469
- const entity = pre
470
- ? this.PREFORMATTED_INLINE_ENTITY.bind(this)
471
- : this.INLINE_ENTITY.bind(this);
472
- while (true) {
473
- if (postfix && this.scanner.accept(postfix))
474
- break;
475
- if (this.scanner.isEOF() || !(ok = entity())) {
476
- // TODO: use error 3
477
- if (postfix)
478
- this.emit.message(new messages_1.ExpectedMessage(this.#loc(), postfix));
479
- break;
480
- }
481
- }
482
- this.emit.endInline();
483
- if (!pre && node.content.length > 0) {
484
- this.#trimNode(node);
485
- }
486
- }
487
- else {
488
- this.emit.startBlock(node);
489
- this.WARN_IF_MORE_NEWLINES_THAN(1);
490
- if (!this.scanner.isEOF()) {
491
- if (node.mod.slotType == interface_1.ModifierSlotType.Preformatted)
492
- this.PRE_PARAGRAPH();
493
- else
494
- this.BLOCK_ENTITY();
495
- }
496
- this.emit.endBlock();
497
- }
498
- const last = node.content.at(-1);
499
- node.location.actualEnd = last?.location.actualEnd ?? last?.location.end;
500
- if (node.mod.delayContentExpansion)
501
- this.delayDepth--;
502
- if (node.mod.afterParseContent)
503
- this.emit.message(...node.mod.afterParseContent(node, this.cxt, immediate));
504
- this.#expand(node);
505
- return ok;
506
- }
507
- // returns false if breaking out of paragraph
508
- INLINE_ENTITY() {
509
- (0, util_1.assert)(!this.scanner.isEOF());
510
- if (this.scanner.peek(MODIFIER_INLINE_OPEN))
511
- return this.MODIFIER(interface_1.NodeType.InlineModifier);
512
- if (this.scanner.peek(MODIFIER_SYSTEM_OPEN))
513
- return false;
514
- const short = this.cxt.config.inlineShorthands.find((x) => this.scanner.accept(x.name));
515
- if (short)
516
- return this.SHORTHAND(interface_1.NodeType.InlineModifier, short);
517
- if (this.scanner.accept('\\')) {
518
- if (this.scanner.isEOF()) {
519
- this.emit.addString('\\');
520
- return true;
521
- }
522
- const start = this.scanner.position();
523
- const node = {
524
- type: interface_1.NodeType.Escaped,
525
- content: this.scanner.acceptChar(),
526
- location: this.#locFrom(start - 1)
527
- };
528
- this.emit.addInlineNode(node);
529
- return true;
530
- }
531
- return this.PREFORMATTED_INLINE_ENTITY();
532
- }
533
- // returns false if breaking out of paragraph
534
- PREFORMATTED_INLINE_ENTITY() {
535
- (0, util_1.assert)(!this.scanner.isEOF());
536
- if (this.scanner.accept('\n')) {
537
- // these whitespaces in a blank line have no effect
538
- this.WHITESPACES();
539
- if (this.scanner.peek(MODIFIER_BLOCK_OPEN)
540
- || this.scanner.peek(MODIFIER_SYSTEM_OPEN)
541
- || this.cxt.config.blockShorthands.find((x) => this.scanner.peek(x.name))
542
- || (this.scanner.peek(GROUP_END) && this.groupDepth > 0)
543
- || this.scanner.isEOF())
544
- return false;
545
- if (this.scanner.accept('\n')) {
546
- this.WARN_IF_MORE_NEWLINES_THAN(0);
547
- return false;
548
- }
549
- this.emit.addString('\n');
550
- return true;
551
- }
552
- // simple character
553
- this.emit.addString(this.scanner.acceptChar());
554
- return true;
555
- }
556
- // returns argument and isOk
557
- ARGUMENT_CONTENT(end = undefined, close = [MODIFIER_END_SIGN, MODIFIER_CLOSE_SIGN]) {
558
- let ok = true;
559
- const content = [];
560
- const posStart = this.scanner.position();
561
- let posEnd = this.scanner.position();
562
- const emitString = (s) => {
563
- const last = content.at(-1);
564
- if (last?.type == interface_1.NodeType.Text) {
565
- last.content += s;
566
- last.location.end += s.length;
567
- }
568
- else {
569
- const end = this.scanner.position();
570
- content.push({
571
- type: interface_1.NodeType.Text,
572
- location: this.#locFrom(end - s.length),
573
- content: s
574
- });
575
- }
576
- };
577
- while (true) {
578
- if (end && this.scanner.accept(end))
579
- break;
580
- if (end === undefined && this.scanner.accept(':'))
581
- break;
582
- if (close.find((x) => this.scanner.peek(x))
583
- || this.scanner.isEOF()) {
584
- ok = false;
585
- break;
586
- }
587
- if (this.scanner.accept('\\')) {
588
- // handle escaping
589
- posEnd = this.scanner.position();
590
- if (this.scanner.isEOF()) {
591
- emitString('\\');
592
- ok = false;
593
- break;
594
- }
595
- content.push({
596
- type: interface_1.NodeType.Escaped,
597
- content: this.scanner.acceptChar(),
598
- location: this.#locFrom(posEnd - 1)
599
- });
600
- continue;
601
- }
602
- const beforeInterp = this.scanner.position();
603
- const result = this.cxt.config.argumentInterpolators.find((x) => this.scanner.accept(x.name));
604
- if (result !== undefined) {
605
- const [inner, ok2] = this.ARGUMENT_CONTENT(result.postfix);
606
- posEnd = this.scanner.position();
607
- content.push({
608
- type: interface_1.NodeType.Interpolation,
609
- definition: result, argument: inner,
610
- location: this.#locFrom(beforeInterp)
611
- });
612
- if (!ok2) {
613
- this.emit.message(new messages_1.ExpectedMessage(this.#loc(), result.postfix));
614
- ok = false;
615
- break;
616
- }
617
- }
618
- else {
619
- emitString(this.scanner.acceptChar());
620
- posEnd = this.scanner.position();
621
- }
622
- }
623
- return [{
624
- location: this.#locFrom(posStart, posEnd),
625
- content
626
- }, ok];
627
- }
628
- ARGUMENTS() {
629
- // optionally accept semicolon before first argument
630
- const firstSemicolon = this.scanner.accept(':');
631
- // don't eat whites if there is a first semicolon
632
- if (!firstSemicolon)
633
- this.WHITESPACES_OR_NEWLINES();
634
- const list = [];
635
- let end = false;
636
- while (!end) {
637
- const [arg, ok] = this.ARGUMENT_CONTENT();
638
- if (!ok) {
639
- end = true;
640
- // if we haven't parsed anything so far: if there is no first semicolon, there's no arguments; otherwise, there is a single empty argument
641
- if (list.length == 0 && arg.content.length == 0 && !firstSemicolon)
642
- break;
643
- }
644
- list.push(arg);
645
- }
646
- return list;
647
- }
648
- }
649
- function parse(scanner, cxt) {
650
- return new Parser(scanner, cxt).parse();
651
- }
652
- //# sourceMappingURL=parser.js.map