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
|
/*
* Copyright (C) 2012-2016 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/>.
*/
#ifndef __PARSER_H
#define __PARSER_H
#include <string.h>
#include <glib.h>
#include "sciteco.h"
#include "memory.h"
#include "undo.h"
#include "error.h"
#include "expressions.h"
namespace SciTECO {
/* TECO uses only lower 7 bits for commands */
#define MAX_TRANSITIONS 127
class State : public Object {
protected:
/* static transitions */
State *transitions[MAX_TRANSITIONS];
inline void
init(const gchar *chars, State &state)
{
while (*chars)
transitions[(int)*chars++] = &state;
}
inline void
init(const gchar *chars)
{
init(chars, *this);
}
public:
State();
static void input(gchar chr);
State *get_next_state(gchar chr);
/**
* 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.
*/
virtual void refresh(void) {}
protected:
static bool eval_colon(void);
/** Get next state given an input character */
virtual State *
custom(gchar chr)
{
throw SyntaxError(chr);
return NULL;
}
};
template <typename Type>
class MicroStateMachine : public Object {
protected:
/* label pointers */
typedef const void *MicroState;
const MicroState StateStart;
#define MICROSTATE_START G_STMT_START { \
if (this->state != StateStart) \
goto *this->state; \
} G_STMT_END
MicroState state;
#ifdef EMSCRIPTEN
/* FIXME: Shouldn't be required! */
__attribute__((noinline))
#else
inline
#endif
void
set(MicroState next)
{
if (next != state)
undo.push_var(state) = next;
}
public:
MicroStateMachine() : StateStart(NULL), state(StateStart) {}
virtual ~MicroStateMachine() {}
virtual inline void
reset(void)
{
set(StateStart);
}
virtual bool input(gchar chr, Type &result) = 0;
};
/* avoid circular dependency on qregisters.h */
class QRegSpecMachine;
class StringBuildingMachine : public MicroStateMachine<gchar *> {
enum Mode {
MODE_NORMAL,
MODE_UPPER,
MODE_LOWER
} mode;
bool toctl;
public:
QRegSpecMachine *qregspec_machine;
StringBuildingMachine() : MicroStateMachine<gchar *>(),
mode(MODE_NORMAL), toctl(false),
qregspec_machine(NULL) {}
~StringBuildingMachine();
void reset(void);
bool input(gchar chr, gchar *&result);
};
/*
* Super-class for states accepting string arguments
* Opaquely cares about alternative-escape characters,
* string building commands and accumulation into a string
*/
class StateExpectString : public State {
gsize insert_len;
gint nesting;
bool string_building;
bool last;
public:
/*
* FIXME: Only public as long as cmdline.cpp
* needs to access it.
* Can be avoided one process_edit_cmd() is in State.
*/
StringBuildingMachine machine;
StateExpectString(bool _building = true, bool _last = true)
: insert_len(0), nesting(1),
string_building(_building), last(_last) {}
private:
State *custom(gchar chr);
void refresh(void);
protected:
virtual void initial(void) {}
virtual void process(const gchar *str, gint new_chars) {}
virtual State *done(const gchar *str) = 0;
};
class StateExpectFile : public StateExpectString {
public:
StateExpectFile(bool _building = true, bool _last = true)
: StateExpectString(_building, _last) {}
private:
State *done(const gchar *str);
protected:
virtual State *got_file(const gchar *filename) = 0;
};
class StateExpectDir : public StateExpectFile {
public:
StateExpectDir(bool _building = true, bool _last = true)
: StateExpectFile(_building, _last) {}
};
class StateStart : public State {
public:
StateStart();
private:
void insert_integer(tecoInt v);
tecoInt read_integer(void);
tecoBool move_chars(tecoInt n);
tecoBool move_lines(tecoInt n);
tecoBool delete_words(tecoInt n);
State *custom(gchar chr);
};
class StateControl : public State {
public:
StateControl();
private:
State *custom(gchar chr);
};
class StateASCII : public State {
public:
StateASCII();
private:
State *custom(gchar chr);
};
class StateEscape : public State {
public:
StateEscape();
private:
State *custom(gchar chr);
};
class StateFCommand : public State {
public:
StateFCommand();
private:
State *custom(gchar chr);
};
class UndoTokenChangeDir : public UndoToken {
gchar *dir;
public:
/**
* Construct undo token.
*
* This passes ownership of the directory string
* to the undo token object.
*/
UndoTokenChangeDir(gchar *_dir) : dir(_dir) {}
~UndoTokenChangeDir()
{
g_free(dir);
}
void run(void);
};
class StateChangeDir : public StateExpectDir {
private:
State *got_file(const gchar *filename);
};
class StateCondCommand : public State {
public:
StateCondCommand();
private:
State *custom(gchar chr);
};
class StateECommand : public State {
public:
StateECommand();
private:
State *custom(gchar chr);
};
class StateScintilla_symbols : public StateExpectString {
public:
StateScintilla_symbols() : StateExpectString(true, false) {}
private:
State *done(const gchar *str);
};
class StateScintilla_lParam : public StateExpectString {
private:
State *done(const gchar *str);
};
/*
* also serves as base class for replace-insertion states
*/
class StateInsert : public StateExpectString {
public:
StateInsert(bool building = true)
: StateExpectString(building) {}
protected:
void initial(void);
void process(const gchar *str, gint new_chars);
State *done(const gchar *str);
};
class StateInsertIndent : public StateInsert {
protected:
void initial(void);
};
namespace States {
extern StateStart start;
extern StateControl control;
extern StateASCII ascii;
extern StateEscape escape;
extern StateFCommand fcommand;
extern StateChangeDir changedir;
extern StateCondCommand condcommand;
extern StateECommand ecommand;
extern StateScintilla_symbols scintilla_symbols;
extern StateScintilla_lParam scintilla_lparam;
extern StateInsert insert_building;
extern StateInsert insert_nobuilding;
extern StateInsertIndent insert_indent;
extern State *current;
static inline bool
is_start()
{
/*
* The "escape" state exists only as a hack,
* to support $$. Otherwise it should behave
* like the start state.
*/
return current == &start || current == &escape;
}
static inline bool
is_string()
{
return dynamic_cast<StateExpectString *>(current);
}
static inline bool
is_insertion()
{
return dynamic_cast<StateInsert *>(current);
}
static inline bool
is_file()
{
return dynamic_cast<StateExpectFile *>(current);
}
static inline bool
is_dir()
{
return dynamic_cast<StateExpectDir *>(current);
}
}
extern enum Mode {
MODE_NORMAL = 0,
MODE_PARSE_ONLY_GOTO,
MODE_PARSE_ONLY_LOOP,
MODE_PARSE_ONLY_COND
} mode;
#define BEGIN_EXEC(STATE) G_STMT_START { \
if (mode > MODE_NORMAL) \
return STATE; \
} G_STMT_END
extern gint macro_pc;
extern gchar *strings[2];
extern gchar escape_char;
struct LoopContext {
/** how many iterations are left */
tecoInt counter;
/** Program counter of loop start command */
guint pc : 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;
};
typedef ValueStack<LoopContext> LoopStack;
extern LoopStack loop_stack;
namespace Execute {
void step(const gchar *macro, gint stop_pos);
void macro(const gchar *macro, bool locals = true);
void file(const gchar *filename, bool locals = true);
}
} /* namespace SciTECO */
#endif
|