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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
/*
* Copyright (C) 2012-2024 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdbool.h>
#include <glib.h>
#include <Scintilla.h>
#include "sciteco.h"
#include "string-utils.h"
#include "goto.h"
#include "undo.h"
#include "qreg.h"
/*
* Forward Declarations
*/
typedef const struct teco_state_t teco_state_t;
typedef struct teco_machine_t teco_machine_t;
typedef struct teco_machine_main_t teco_machine_main_t;
typedef struct {
/** how many iterations are left */
teco_int_t counter;
/** Program counter of loop start command */
gsize pc;
/** Brace level at loop start */
guint brace_level : sizeof(guint)*8 - 1;
/**
* Whether the loop represents an argument
* barrier or not (it "passes through"
* stack arguments).
*
* Since the program counter is usually
* a signed integer, it's ok steal one
* bit for the pass_through flag.
*/
bool pass_through : 1;
} teco_loop_context_t;
extern GArray *teco_loop_stack;
void undo__insert_val__teco_loop_stack(guint, teco_loop_context_t);
void undo__remove_index__teco_loop_stack(guint);
/**
* @defgroup states Parser states
*
* Parser states are defined as global constants using the TECO_DEFINE_STATE()
* macro, allowing individual fields and callbacks to be overwritten.
* Derived macros are defined to factor out common fields and settings.
* States therefore form a hierarchy, which is documented using
* \@interface and \@implements tags.
*
* @{
*/
/*
* FIXME: Remove _cb from all callback names. See qreg.h.
* FIXME: Maybe use TECO_DECLARE_VTABLE_METHOD()?
*/
typedef const struct {
bool string_building : 1;
bool last : 1;
/**
* Called repeatedly to process chunks of input and give interactive feedback.
*
* Can be NULL if no interactive feedback is required.
*/
gboolean (*process_cb)(teco_machine_main_t *ctx, const teco_string_t *str,
gsize new_chars, GError **error);
/**
* Called at the end of the string argument to determine the next state.
* Commands that don't give interactive feedback can use this callback
* to perform their main processing.
*/
teco_state_t *(*done_cb)(teco_machine_main_t *ctx, const teco_string_t *str, GError **error);
} teco_state_expectstring_t;
typedef const struct {
teco_qreg_type_t type;
/** Called when a register specification has been successfully parsed. */
teco_state_t *(*got_register_cb)(teco_machine_main_t *ctx, teco_qreg_t *qreg,
teco_qreg_table_t *table, GError **error);
} teco_state_expectqreg_t;
typedef gboolean (*teco_state_initial_cb_t)(teco_machine_t *ctx, GError **error);
typedef teco_state_t *(*teco_state_input_cb_t)(teco_machine_t *ctx, gunichar chr, GError **error);
typedef gboolean (*teco_state_refresh_cb_t)(teco_machine_t *ctx, GError **error);
typedef gboolean (*teco_state_end_of_macro_cb_t)(teco_machine_t *ctx, GError **error);
typedef gboolean (*teco_state_process_edit_cmd_cb_t)(teco_machine_t *ctx, teco_machine_t *parent_ctx,
gunichar key, GError **error);
typedef enum {
TECO_KEYMACRO_MASK_START = (1 << 0),
TECO_KEYMACRO_MASK_STRING = (1 << 1),
TECO_KEYMACRO_MASK_CASEINSENSITIVE = (1 << 2),
TECO_KEYMACRO_MASK_DEFAULT = ~((1 << 3)-1)
} teco_keymacro_mask_t;
/**
* A teco_machine_t state.
* These are declared as constants using TECO_DEFINE_STATE() and friends.
*
* @note Unless you don't want to manually "upcast" the teco_machine_t* in
* callback implementations, you will have to cast your callback types when initializing
* the teco_state_t vtables.
* Casting to functions of different signature is theoretically undefined behavior,
* but works on all major platforms including Emscripten, as long as they differ only
* in pointer types.
*/
struct teco_state_t {
/**
* Called the first time this state is entered.
* Theoretically, you can use teco_machine_main_transition_t instead,
* but this callback improves reusability.
*
* It can be NULL if not required.
*/
teco_state_initial_cb_t initial_cb;
/**
* Get next state given an input character.
*
* This is a mandatory field.
*/
teco_state_input_cb_t input_cb;
/**
* Provide interactive feedback.
*
* This gets called whenever a state with
* immediate interactive feedback should provide that
* feedback; allowing them to optimize batch mode,
* macro and many other cases.
*
* It can be NULL if not required.
*/
teco_state_refresh_cb_t refresh_cb;
/**
* Called at the end of a macro.
* Most states/commands are not allowed to end unterminated
* at the end of a macro.
*
* It can be NULL if not required.
*/
teco_state_end_of_macro_cb_t end_of_macro_cb;
/**
* Process editing command (or key press).
*
* This is part of command line handling in interactive
* mode and allows the definition of state-specific
* editing commands (behaviour on key press).
*
* By implementing this method, sub-states can either
* handle a key and return, chain to the
* parent's process_edit_cmd() implementation or even
* to the parent state machine's handler.
*
* All implementations of this method are defined in
* cmdline.c.
*
* This is a mandatory field.
*/
teco_state_process_edit_cmd_cb_t process_edit_cmd_cb;
/**
* Whether this state is a start state (ie. not within any
* escape sequence etc.).
* This is separate of TECO_KEYMACRO_MASK_START which is set
* only in the main machine's start states.
*/
bool is_start : 1;
/**
* Key macro mask.
* This is not a bitmask since it is compared with values set
* from TECO, so the bitorder needs to be defined.
*
* @fixme If we intend to "forward" masks from other state machines like
* teco_machine_stringbuilding_t, this should probably be a callback.
*/
teco_keymacro_mask_t keymacro_mask : 8;
/**
* Additional state-dependent callbacks and settings.
* This wastes some bytes compared to other techniques for extending teco_state_t
* but this is acceptable since there is only a limited number of constant instances.
* The main advantage of this approach is that we can use a single
* TECO_DEFINE_STATE() for defining and deriving all defaults.
*/
union {
teco_state_expectstring_t expectstring;
teco_state_expectqreg_t expectqreg;
};
};
/** @} */
gboolean teco_state_end_of_macro(teco_machine_t *ctx, GError **error);
/* in cmdline.c */
gboolean teco_state_process_edit_cmd(teco_machine_t *ctx, teco_machine_t *parent_ctx, gunichar chr, GError **error);
/**
* @interface TECO_DEFINE_STATE
* @implements teco_state_t
* @ingroup states
*
* @todo Should we eliminate required callbacks, this could be turned into a
* struct initializer TECO_INIT_STATE() and TECO_DECLARE_STATE() would become pointless.
* This would also ease declaring static states.
*/
#define TECO_DEFINE_STATE(NAME, ...) \
/** @ingroup states */ \
teco_state_t NAME = { \
.initial_cb = NULL, /* do nothing */ \
.input_cb = (teco_state_input_cb_t)NAME##_input, /* always required */ \
.refresh_cb = NULL, /* do nothing */ \
.end_of_macro_cb = teco_state_end_of_macro, \
.process_edit_cmd_cb = teco_state_process_edit_cmd, \
.is_start = FALSE, \
.keymacro_mask = TECO_KEYMACRO_MASK_DEFAULT, \
##__VA_ARGS__ \
}
/** @ingroup states */
#define TECO_DECLARE_STATE(NAME) \
extern teco_state_t NAME
/* in cmdline.c */
gboolean teco_state_caseinsensitive_process_edit_cmd(teco_machine_t *ctx, teco_machine_t *parent_ctx, gunichar chr, GError **error);
/**
* @interface TECO_DEFINE_STATE_CASEINSENSITIVE
* @implements TECO_DEFINE_STATE
* @ingroup states
*
* Base class of states with case-insensitive input.
*
* This is meant for states accepting command characters
* that can possibly be case-folded.
*/
#define TECO_DEFINE_STATE_CASEINSENSITIVE(NAME, ...) \
TECO_DEFINE_STATE(NAME, \
.keymacro_mask = TECO_KEYMACRO_MASK_CASEINSENSITIVE, \
.process_edit_cmd_cb = teco_state_caseinsensitive_process_edit_cmd, \
##__VA_ARGS__ \
)
/**
* Base class of state machine.
*
* @note On extending teco_machine_t:
* There is `-fplan9-extensions`, but Clang doesn't support it.
* There is `-fms-extensions`, but that would require type-unsafe
* casting to teco_machine_t*.
* It's possible to portably implement typesafe inheritance by using
* an anonymous union of an anonymous struct and a named struct, but it's
* not really worth the trouble in our flat "class" hierachy.
*/
struct teco_machine_t {
teco_state_t *current;
/**
* Whether side effects must be reverted on rubout.
* State machines created within macro calls don't have to
* even in interactive mode.
* In fact you MUST not revert side effects if this is FALSE
* as the data no longer exists on the call stack at undo-time.
*/
gboolean must_undo;
};
static inline void
teco_machine_init(teco_machine_t *ctx, teco_state_t *initial, gboolean must_undo)
{
ctx->current = initial;
ctx->must_undo = must_undo;
}
static inline void
teco_machine_reset(teco_machine_t *ctx, teco_state_t *initial)
{
if (ctx->must_undo && ctx->current != initial)
teco_undo_ptr(ctx->current);
ctx->current = initial;
}
gboolean teco_machine_input(teco_machine_t *ctx, gunichar chr, GError **error);
typedef enum {
TECO_STRINGBUILDING_MODE_NORMAL = 0,
TECO_STRINGBUILDING_MODE_UPPER,
TECO_STRINGBUILDING_MODE_LOWER
} teco_stringbuilding_mode_t;
/**
* A stringbuilding state machine.
*
* @extends teco_machine_t
*/
typedef struct teco_machine_stringbuilding_t {
teco_machine_t parent;
/**
* A teco_stringbuilding_mode_t.
* This is still a guint, so you can call teco_undo_guint().
*/
guint mode;
/**
* The escape/termination character.
*
* If this is `[` or `{`, it is assumed that `]` and `}` must
* be escaped as well by teco_machine_stringbuilding_escape().
*/
gunichar escape_char;
/**
* Q-Register table for local registers.
* This is stored here only to be passed to the Q-Reg spec machine.
*/
teco_qreg_table_t *qreg_table_locals;
/**
* A QRegister specification parser.
* It is allocated since it in turn contains a string building machine.
*/
teco_machine_qregspec_t *machine_qregspec;
/**
* A string to append characters to or NULL in parse-only mode.
*
* @bug As a side-effect, rubbing out in parse-only mode is severely limited
* (see teco_state_stringbuilding_start_process_edit_cmd()).
*/
teco_string_t *result;
/**
* Encoding of string in `result`.
* This is inherited from the embedding command and may depend on
* the buffer's or Q-Register's encoding.
*/
guint codepage;
} teco_machine_stringbuilding_t;
void teco_machine_stringbuilding_init(teco_machine_stringbuilding_t *ctx, gunichar escape_char,
teco_qreg_table_t *locals, gboolean must_undo);
static inline void
teco_machine_stringbuilding_set_codepage(teco_machine_stringbuilding_t *ctx,
guint codepage)
{
/* NOTE: This is not safe to undo in macro calls. */
if (ctx->parent.must_undo)
teco_undo_guint(ctx->codepage);
ctx->codepage = codepage;
}
void teco_machine_stringbuilding_reset(teco_machine_stringbuilding_t *ctx);
/**
* Parse a string building character.
*
* @param ctx The string building machine.
* @param chr The character to parse.
* @param result String to append characters to or NULL in parse-only mode.
* @param error GError.
* @return FALSE in case of error.
*/
static inline gboolean
teco_machine_stringbuilding_input(teco_machine_stringbuilding_t *ctx, gunichar chr,
teco_string_t *result, GError **error)
{
ctx->result = result;
return teco_machine_input(&ctx->parent, chr, error);
}
void teco_machine_stringbuilding_escape(teco_machine_stringbuilding_t *ctx, const gchar *str, gsize len,
teco_string_t *target);
void teco_machine_stringbuilding_clear(teco_machine_stringbuilding_t *ctx);
/**
* Peristent state for teco_state_expectstring_input().
*
* This is part of the main machine instead of being a global variable,
* so that parsers can be run in parallel.
*
* Since it will also be part of a macro invocation frame, it will allow
* for tricks like macro-hooks while in "expectstring" states or calling
* macros as part of string building characters or macro string arguments.
*/
typedef struct {
teco_string_t string;
gsize insert_len;
gint nesting;
teco_machine_stringbuilding_t machine;
} teco_machine_expectstring_t;
/**
* Scintilla message for collection by ES commands.
*
* @fixme This is a "forward" declaration, so that we don't introduce cyclic
* header dependencies.
* Could presumably be avoided by splitting parser.h in two.
*/
typedef struct {
unsigned int iMessage;
uptr_t wParam;
} teco_machine_scintilla_t;
typedef enum {
/** Normal parsing - ie. execute while parsing */
TECO_MODE_NORMAL = 0,
/** Parse, but don't execute until reaching not-yet-defined Goto-label */
TECO_MODE_PARSE_ONLY_GOTO,
/** Parse, but don't execute until reaching end of loop */
TECO_MODE_PARSE_ONLY_LOOP,
/** Parse, but don't execute until reaching end of conditional or its else-clause */
TECO_MODE_PARSE_ONLY_COND,
/** Parse, but don't execute until reaching the very end of conditional */
TECO_MODE_PARSE_ONLY_COND_FORCE
} teco_mode_t;
/** @extends teco_machine_t */
struct teco_machine_main_t {
teco_machine_t parent;
/** Program counter, ie. pointer to the next character in the current macro frame */
gsize macro_pc;
/**
* Aliases bitfield with an integer.
* This allows teco_undo_guint(__flags),
* while still supporting easy-to-access flags.
*/
union {
struct {
teco_mode_t mode : 8;
bool modifier_colon : 1;
bool modifier_at : 1;
};
guint __flags;
};
/** The nesting level of braces */
guint brace_level;
/** The nesting level of loops and control structures */
gint nest_level;
/**
* Loop frame pointer: The number of elements on
* the loop stack when a macro invocation frame is
* created.
* This is used to perform checks for flow control
* commands to avoid jumping with invalid PCs while
* not creating a new stack per macro frame.
*/
guint loop_stack_fp;
teco_goto_table_t goto_table;
teco_qreg_table_t *qreg_table_locals;
/*
* teco_state_t-dependent state.
*
* Some of these cannot be used concurrently and are therefore
* grouped into unions.
* We could further optimize memory usage by dynamically allocating
* some of these structures on demand.
*/
teco_machine_expectstring_t expectstring;
union {
teco_string_t goto_label;
teco_machine_qregspec_t *expectqreg;
teco_machine_scintilla_t scintilla;
};
};
void teco_machine_main_init(teco_machine_main_t *ctx,
teco_qreg_table_t *qreg_table_locals,
gboolean must_undo);
gboolean teco_machine_main_eval_colon(teco_machine_main_t *ctx);
gboolean teco_machine_main_step(teco_machine_main_t *ctx,
const gchar *macro, gsize stop_pos, GError **error);
gboolean teco_execute_macro(const gchar *macro, gsize macro_len,
teco_qreg_table_t *qreg_table_locals, GError **error);
gboolean teco_execute_file(const gchar *filename, teco_qreg_table_t *qreg_table_locals, GError **error);
typedef const struct {
teco_state_t *next;
void (*transition_cb)(teco_machine_main_t *ctx, GError **error);
} teco_machine_main_transition_t;
/*
* FIXME: There should probably be a teco_state_plain with
* the transitions and their length being stored in
* teco_state_t::transitions.
* This does not exclude the possibility of overwriting input_cb.
*/
teco_state_t *teco_machine_main_transition_input(teco_machine_main_t *ctx,
teco_machine_main_transition_t *transitions,
guint len, gunichar chr, GError **error);
void teco_machine_main_clear(teco_machine_main_t *ctx);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(teco_machine_main_t, teco_machine_main_clear);
gboolean teco_state_expectstring_initial(teco_machine_main_t *ctx, GError **error);
teco_state_t *teco_state_expectstring_input(teco_machine_main_t *ctx, gunichar chr, GError **error);
gboolean teco_state_expectstring_refresh(teco_machine_main_t *ctx, GError **error);
/* in cmdline.c */
gboolean teco_state_expectstring_process_edit_cmd(teco_machine_main_t *ctx, teco_machine_t *parent_ctx, gunichar key, GError **error);
/**
* @interface TECO_DEFINE_STATE_EXPECTSTRING
* @implements TECO_DEFINE_STATE
* @ingroup states
*
* Super-class for states accepting string arguments
* Opaquely cares about alternative-escape characters,
* string building commands and accumulation into a string
*
* @note Generating the input_cb could be avoided if there were a default
* implementation.
*/
#define TECO_DEFINE_STATE_EXPECTSTRING(NAME, ...) \
static teco_state_t * \
NAME##_input(teco_machine_main_t *ctx, gunichar chr, GError **error) \
{ \
return teco_state_expectstring_input(ctx, chr, error); \
} \
TECO_DEFINE_STATE(NAME, \
.initial_cb = (teco_state_initial_cb_t)teco_state_expectstring_initial, \
.refresh_cb = (teco_state_refresh_cb_t)teco_state_expectstring_refresh, \
.process_edit_cmd_cb = (teco_state_process_edit_cmd_cb_t) \
teco_state_expectstring_process_edit_cmd, \
.keymacro_mask = TECO_KEYMACRO_MASK_STRING, \
.expectstring.string_building = TRUE, \
.expectstring.last = TRUE, \
.expectstring.process_cb = NULL, /* do nothing */ \
.expectstring.done_cb = NAME##_done, /* always required */ \
##__VA_ARGS__ \
)
gboolean teco_state_expectfile_process(teco_machine_main_t *ctx, const teco_string_t *str,
gsize new_chars, GError **error);
/* in cmdline.c */
gboolean teco_state_expectfile_process_edit_cmd(teco_machine_main_t *ctx, teco_machine_t *parent_ctx, gunichar key, GError **error);
/**
* @interface TECO_DEFINE_STATE_EXPECTFILE
* @implements TECO_DEFINE_STATE_EXPECTSTRING
* @ingroup states
*/
#define TECO_DEFINE_STATE_EXPECTFILE(NAME, ...) \
TECO_DEFINE_STATE_EXPECTSTRING(NAME, \
.process_edit_cmd_cb = (teco_state_process_edit_cmd_cb_t) \
teco_state_expectfile_process_edit_cmd, \
.expectstring.process_cb = teco_state_expectfile_process, \
##__VA_ARGS__ \
)
/* in cmdline.c */
gboolean teco_state_expectdir_process_edit_cmd(teco_machine_main_t *ctx, teco_machine_t *parent_ctx, gunichar key, GError **error);
/**
* @interface TECO_DEFINE_STATE_EXPECTDIR
* @implements TECO_DEFINE_STATE_EXPECTFILE
* @ingroup states
*/
#define TECO_DEFINE_STATE_EXPECTDIR(NAME, ...) \
TECO_DEFINE_STATE_EXPECTFILE(NAME, \
.process_edit_cmd_cb = (teco_state_process_edit_cmd_cb_t) \
teco_state_expectdir_process_edit_cmd, \
##__VA_ARGS__ \
)
|