about summary refs log tree commit diff stats
path: root/080display.cc
blob: 6bf8e51dc2e4746c6da08e112100113f0f9c53fd (plain) (blame)
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
//: Take raw control of the text-mode display and console, putting it in
//: 'console' mode rather than the usual automatically-scrolling 'typewriter'
//: mode.

//:: Display management

:(before "End Globals")
int Display_row = 0;
int Display_column = 0;

:(before "End Includes")
#define CHECK_SCREEN \
    if (!tb_is_active()) { \
      if (Run_tests) \
        raise << maybe(current_recipe_name()) << "tried to print to real screen in a test!\n" << end(); \
      else \
        raise << maybe(current_recipe_name()) << "tried to print to real screen before 'open-console' or after 'close-console'\n" << end(); \
      break; \
    }
#define CHECK_CONSOLE \
    if (!tb_is_active()) { \
      if (Run_tests) \
        raise << maybe(current_recipe_name()) << "tried to read event from real keyboard/mouse in a test!\n" << end(); \
      else \
        raise << maybe(current_recipe_name()) << "tried to read event from real keyboard/mouse before 'open-console' or after 'close-console'\n" << end(); \
      break; \
    }

:(before "End Primitive Recipe Declarations")
OPEN_CONSOLE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "open-console", OPEN_CONSOLE);
:(before "End Primitive Recipe Checks")
case OPEN_CONSOLE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case OPEN_CONSOLE: {
  tb_init();
  std::setvbuf(stdout, NULL, _IONBF, 0);  // disable buffering in cout
  Display_row = Display_column = 0;
  int width = tb_width();
  int height = tb_height();
  if (width > 222 || height > 222) {
    if (width > 222)
      raise << "sorry, Mu doesn't support windows wider than 222 characters in console mode. Please resize your window.\n" << end();
    if (height > 222)
      raise << "sorry, Mu doesn't support windows taller than 222 characters in console mode. Please resize your window.\n" << end();
    exit(1);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
CLOSE_CONSOLE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "close-console", CLOSE_CONSOLE);
:(before "End Primitive Recipe Checks")
case CLOSE_CONSOLE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case CLOSE_CONSOLE: {
  tb_shutdown();
  break;
}

:(before "End Primitive Recipe Declarations")
CLEAR_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "clear-display", CLEAR_DISPLAY);
:(before "End Primitive Recipe Checks")
case CLEAR_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case CLEAR_DISPLAY: {
  CHECK_SCREEN;
  tb_clear();
  Display_row = Display_column = 0;
  break;
}

:(before "End Primitive Recipe Declarations")
PRINT_CHARACTER_TO_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "print-character-to-display", PRINT_CHARACTER_TO_DISPLAY);
:(before "End Primitive Recipe Checks")
case PRINT_CHARACTER_TO_DISPLAY: {
  if (inst.ingredients.empty()) {
    raise << maybe(get(Recipe, r).name) << "'print-character-to-display' requires at least one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'print-character-to-display' should be a character, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  if (SIZE(inst.ingredients) > 1) {
    if (!is_mu_number(inst.ingredients.at(1))) {
      raise << maybe(get(Recipe, r).name) << "second ingredient of 'print-character-to-display' should be a foreground color number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
      break;
    }
  }
  if (SIZE(inst.ingredients) > 2) {
    if (!is_mu_number(inst.ingredients.at(2))) {
      raise << maybe(get(Recipe, r).name) << "third ingredient of 'print-character-to-display' should be a background color number, but got '" << inst.ingredients.at(2).original_string << "'\n" << end();
      break;
    }
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case PRINT_CHARACTER_TO_DISPLAY: {
  CHECK_SCREEN;
  int h=tb_height(), w=tb_width();
  int height = (h >= 0) ? h : 0;
  int width = (w >= 0) ? w : 0;
  int c = ingredients.at(0).at(0);
  int color = TB_WHITE;
  if (SIZE(ingredients) > 1) {
    color = ingredients.at(1).at(0);
  }
  int bg_color = TB_BLACK;
  if (SIZE(ingredients) > 2) {
    bg_color = ingredients.at(2).at(0);
    if (bg_color == 0) bg_color = TB_BLACK;
  }
  tb_print(c, color, bg_color);
  // track row and column, mimicking what happens on screen
  if (c == '\n') {
    if (Display_row < height-1) ++Display_row;  // otherwise we scroll and Display_row remains unchanged
  }
  else if (c == '\r') {
    Display_column = 0;
  }
  else if (c == '\b') {
    if (Display_column > 0) --Display_column;
  }
  else {
    ++Display_column;
    if (Display_column >= width) {
      Display_column = 0;
      if (Display_row < height-1) ++Display_row;
    }
  }
  break;
}

:(before "End Primitive Recipe Declarations")
CURSOR_POSITION_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "cursor-position-on-display", CURSOR_POSITION_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case CURSOR_POSITION_ON_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case CURSOR_POSITION_ON_DISPLAY: {
  CHECK_SCREEN;
  products.resize(2);
  products.at(0).push_back(Display_row);
  products.at(1).push_back(Display_column);
  break;
}

:(before "End Primitive Recipe Declarations")
MOVE_CURSOR_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "move-cursor-on-display", MOVE_CURSOR_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case MOVE_CURSOR_ON_DISPLAY: {
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(get(Recipe, r).name) << "'move-cursor-on-display' requires two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'move-cursor-on-display' should be a row number, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(1))) {
    raise << maybe(get(Recipe, r).name) << "second ingredient of 'move-cursor-on-display' should be a column number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case MOVE_CURSOR_ON_DISPLAY: {
  CHECK_SCREEN;
  Display_row = ingredients.at(0).at(0);
  Display_column = ingredients.at(1).at(0);
  tb_set_cursor(Display_column, Display_row);
  break;
}

:(before "End Primitive Recipe Declarations")
MOVE_CURSOR_DOWN_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "move-cursor-down-on-display", MOVE_CURSOR_DOWN_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case MOVE_CURSOR_DOWN_ON_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case MOVE_CURSOR_DOWN_ON_DISPLAY: {
  CHECK_SCREEN;
  int h=tb_height();
  int height = (h >= 0) ? h : 0;
  if (Display_row < height-1) {
    ++Display_row;
    tb_set_cursor(Display_column, Display_row);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
MOVE_CURSOR_UP_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "move-cursor-up-on-display", MOVE_CURSOR_UP_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case MOVE_CURSOR_UP_ON_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case MOVE_CURSOR_UP_ON_DISPLAY: {
  CHECK_SCREEN;
  if (Display_row > 0) {
    --Display_row;
    tb_set_cursor(Display_column, Display_row);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
MOVE_CURSOR_RIGHT_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "move-cursor-right-on-display", MOVE_CURSOR_RIGHT_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
  CHECK_SCREEN;
  int w=tb_width();
  int width = (w >= 0) ? w : 0;
  if (Display_column < width-1) {
    ++Display_column;
    tb_set_cursor(Display_column, Display_row);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
MOVE_CURSOR_LEFT_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "move-cursor-left-on-display", MOVE_CURSOR_LEFT_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case MOVE_CURSOR_LEFT_ON_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case MOVE_CURSOR_LEFT_ON_DISPLAY: {
  CHECK_SCREEN;
  if (Display_column > 0) {
    --Display_column;
    tb_set_cursor(Display_column, Display_row);
  }
  break;
}

//: as a convenience, make $print mostly work in console mode
:(before "End $print 10/newline Special-cases")
else if (tb_is_active()) {
  move_cursor_to_start_of_next_line_on_display();
}
:(code)
void move_cursor_to_start_of_next_line_on_display() {
  if (Display_row < tb_height()-1) ++Display_row;
  else Display_row = 0;
  Display_column = 0;
  tb_set_cursor(Display_column, Display_row);
}

:(before "End Primitive Recipe Declarations")
DISPLAY_WIDTH,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "display-width", DISPLAY_WIDTH);
:(before "End Primitive Recipe Checks")
case DISPLAY_WIDTH: {
  break;
}
:(before "End Primitive Recipe Implementations")
case DISPLAY_WIDTH: {
  CHECK_SCREEN;
  products.resize(1);
  products.at(0).push_back(tb_width());
  break;
}

:(before "End Primitive Recipe Declarations")
DISPLAY_HEIGHT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "display-height", DISPLAY_HEIGHT);
:(before "End Primitive Recipe Checks")
case DISPLAY_HEIGHT: {
  break;
}
:(before "End Primitive Recipe Implementations")
case DISPLAY_HEIGHT: {
  CHECK_SCREEN;
  products.resize(1);
  products.at(0).push_back(tb_height());
  break;
}

//:: Keyboard/mouse management

:(before "End Primitive Recipe Declarations")
WAIT_FOR_SOME_INTERACTION,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "wait-for-some-interaction", WAIT_FOR_SOME_INTERACTION);
:(before "End Primitive Recipe Checks")
case WAIT_FOR_SOME_INTERACTION: {
  break;
}
:(before "End Primitive Recipe Implementations")
case WAIT_FOR_SOME_INTERACTION: {
  CHECK_SCREEN;
  tb_event event;
  tb_poll_event(&event);
  break;
}

:(before "End Primitive Recipe Declarations")
CHECK_FOR_INTERACTION,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "check-for-interaction", CHECK_FOR_INTERACTION);
:(before "End Primitive Recipe Checks")
case CHECK_FOR_INTERACTION: {
  break;
}
:(before "End Primitive Recipe Implementations")
case CHECK_FOR_INTERACTION: {
  CHECK_CONSOLE;
  products.resize(2);  // result and status
  tb_event event;
  int event_type = tb_peek_event(&event, 5/*ms*/);
  if (event_type == TB_EVENT_KEY && event.ch) {
    products.at(0).push_back(/*text event*/0);
    products.at(0).push_back(event.ch);
    products.at(0).push_back(0);
    products.at(0).push_back(0);
    products.at(1).push_back(/*found*/true);
    break;
  }
  // treat keys within ascii as unicode characters
  if (event_type == TB_EVENT_KEY && event.key < 0xff) {
    products.at(0).push_back(/*text event*/0);
    if (event.key == TB_KEY_CTRL_C) exit(1);
    if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE;
    if (event.key == TB_KEY_CARRIAGE_RETURN) event.key = TB_KEY_NEWLINE;
    products.at(0).push_back(event.key);
    products.at(0).push_back(0);
    products.at(0).push_back(0);
    products.at(1).push_back(/*found*/true);
    break;
  }
  // keys outside ascii aren't unicode characters but arbitrary termbox inventions
  if (event_type == TB_EVENT_KEY) {
    products.at(0).push_back(/*keycode event*/1);
    products.at(0).push_back(event.key);
    products.at(0).push_back(0);
    products.at(0).push_back(0);
    products.at(1).push_back(/*found*/true);
    break;
  }
  if (event_type == TB_EVENT_MOUSE) {
    products.at(0).push_back(/*touch event*/2);
    products.at(0).push_back(event.key);  // which button, etc.
    products.at(0).push_back(event.y);  // row
    products.at(0).push_back(event.x);  // column
    products.at(1).push_back(/*found*/true);
    break;
  }
  if (event_type == TB_EVENT_RESIZE) {
    products.at(0).push_back(/*resize event*/3);
    products.at(0).push_back(event.w);  // width
    products.at(0).push_back(event.h);  // height
    products.at(0).push_back(0);
    products.at(1).push_back(/*found*/true);
    break;
  }
  assert(event_type == 0);
  products.at(0).push_back(0);
  products.at(0).push_back(0);
  products.at(0).push_back(0);
  products.at(0).push_back(0);
  products.at(1).push_back(/*found*/false);
  break;
}

:(before "End Primitive Recipe Declarations")
INTERACTIONS_LEFT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "interactions-left?", INTERACTIONS_LEFT);
:(before "End Primitive Recipe Checks")
case INTERACTIONS_LEFT: {
  break;
}
:(before "End Primitive Recipe Implementations")
case INTERACTIONS_LEFT: {
  CHECK_CONSOLE;
  products.resize(1);
  products.at(0).push_back(tb_event_ready());
  break;
}

//: hacks to make text-mode apps more responsive under Unix

:(before "End Primitive Recipe Declarations")
CLEAR_LINE_ON_DISPLAY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "clear-line-on-display", CLEAR_LINE_ON_DISPLAY);
:(before "End Primitive Recipe Checks")
case CLEAR_LINE_ON_DISPLAY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case CLEAR_LINE_ON_DISPLAY: {
  CHECK_SCREEN;
  int width = tb_width();
  for (int x = Display_column;  x < width;  ++x)
    tb_print(' ', TB_WHITE, TB_BLACK);
  tb_set_cursor(Display_column, Display_row);
  break;
}

:(before "End Primitive Recipe Declarations")
CLEAR_DISPLAY_FROM,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "clear-display-from", CLEAR_DISPLAY_FROM);
:(before "End Primitive Recipe Checks")
case CLEAR_DISPLAY_FROM: {
  break;
}
:(before "End Primitive Recipe Implementations")
case CLEAR_DISPLAY_FROM: {
  CHECK_SCREEN;
  // todo: error checking
  int row = ingredients.at(0).at(0);
  int column = ingredients.at(1).at(0);
  int left = ingredients.at(2).at(0);
  int right = ingredients.at(3).at(0);
  int height=tb_height();
  for (/*nada*/;  row < height;  ++row, column=left) {  // start column from left in every inner loop except first
    tb_set_cursor(column, row);
    for (/*nada*/;  column <= right;  ++column)
      tb_print(' ', TB_WHITE, TB_BLACK);
  }
  tb_set_cursor(Display_column, Display_row);
  break;
}
s="p">.eof()) { getline(in, curr_line); if (starts_with(curr_line, ":(")) { ++line_number; process_next_hunk(in, trim(curr_line), filename, line_number, out); continue; } if (starts_with(curr_line, "//:")) { ++line_number; continue; } out.push_back(Line(curr_line, filename, line_number)); ++line_number; } // Trace all line contents, inserting directives just at discontinuities. if (!Trace_stream) return; EMIT(out, Trace_stream->stream("tangle")); } // just for tests void tangle(istream& in, list<Line>& out) { tangle(in, "", out); } void process_next_hunk(istream& in, const string& directive, const string& filename, size_t& line_number, list<Line>& out) { istringstream directive_stream(directive.substr(2)); // length of ":(" string cmd = next_tangle_token(directive_stream); // first slurp all lines until next directive list<Line> hunk; { string curr_line; while (!in.eof()) { std::streampos old = in.tellg(); getline(in, curr_line); if (starts_with(curr_line, ":(")) { in.seekg(old); break; } if (starts_with(curr_line, "//:")) { // tangle comments ++line_number; continue; } hunk.push_back(Line(curr_line, filename, line_number)); ++line_number; } } if (cmd == "code") { out.insert(out.end(), hunk.begin(), hunk.end()); return; } if (cmd == "before" || cmd == "after" || cmd == "replace" || cmd == "replace{}" || cmd == "delete" || cmd == "delete{}") { list<Line>::iterator target = locate_target(out, directive_stream); if (target == out.end()) { raise << "couldn't find target " << directive << '\n' << die(); return; } indent_all(hunk, target); if (cmd == "before") { out.splice(target, hunk); } else if (cmd == "after") { ++target; out.splice(target, hunk); } else if (cmd == "replace" || cmd == "delete") { out.splice(target, hunk); out.erase(target); } else if (cmd == "replace{}" || cmd == "delete{}") { if (find_trim(hunk, ":OLD_CONTENTS") == hunk.end()) { out.splice(target, hunk); out.erase(target, balancing_curly(target)); } else { list<Line>::iterator next = balancing_curly(target); list<Line> old_version; old_version.splice(old_version.begin(), out, target, next); old_version.pop_back(); old_version.pop_front(); // contents only please, not surrounding curlies list<Line>::iterator new_pos = find_trim(hunk, ":OLD_CONTENTS"); indent_all(old_version, new_pos); hunk.splice(new_pos, old_version); hunk.erase(new_pos); out.splice(next, hunk); } } return; } raise << "unknown directive " << cmd << '\n' << die(); } list<Line>::iterator locate_target(list<Line>& out, istream& directive_stream) { string pat = next_tangle_token(directive_stream); if (pat == "") return out.end(); string next_token = next_tangle_token(directive_stream); if (next_token == "") { return find_substr(out, pat); } // first way to do nested pattern: pattern 'following' intermediate else if (next_token == "following") { string pat2 = next_tangle_token(directive_stream); if (pat2 == "") return out.end(); list<Line>::iterator intermediate = find_substr(out, pat2); if (intermediate == out.end()) return out.end(); return find_substr(out, intermediate, pat); } // second way to do nested pattern: intermediate 'then' pattern else if (next_token == "then") { list<Line>::iterator intermediate = find_substr(out, pat); if (intermediate == out.end()) return out.end(); string pat2 = next_tangle_token(directive_stream); if (pat2 == "") return out.end(); return find_substr(out, intermediate, pat2); } raise << "unknown keyword in directive: " << next_token << '\n'; return out.end(); } // indent all lines in l like indentation at exemplar void indent_all(list<Line>& l, list<Line>::iterator exemplar) { string curr_indent = indent(exemplar->contents); for (list<Line>::iterator p = l.begin(); p != l.end(); ++p) if (!p->contents.empty()) p->contents.insert(p->contents.begin(), curr_indent.begin(), curr_indent.end()); } string next_tangle_token(istream& in) { in >> std::noskipws; ostringstream out; skip_whitespace(in); if (in.peek() == '"') slurp_tangle_string(in, out); else slurp_word(in, out); return out.str(); } void slurp_tangle_string(istream& in, ostream& out) { in.get(); char c; while (in >> c) { if (c == '\\') { // skip backslash and save next character unconditionally in >> c; out << c; continue; } if (c == '"') break; out << c; } } void slurp_word(istream& in, ostream& out) { char c; while (in >> c) { if (isspace(c) || c == ')') { in.putback(c); break; } out << c; } } void skip_whitespace(istream& in) { while (isspace(in.peek())) in.get(); } list<Line>::iterator balancing_curly(list<Line>::iterator curr) { long open_curlies = 0; do { for (string::iterator p = curr->contents.begin(); p != curr->contents.end(); ++p) { if (*p == '{') ++open_curlies; if (*p == '}') --open_curlies; } ++curr; // no guard so far against unbalanced curly, including inside comments or strings } while (open_curlies != 0); return curr; } list<Line>::iterator find_substr(list<Line>& in, const string& pat) { for (list<Line>::iterator p = in.begin(); p != in.end(); ++p) if (p->contents.find(pat) != string::npos) return p; return in.end(); } list<Line>::iterator find_substr(list<Line>& in, list<Line>::iterator p, const string& pat) { for (; p != in.end(); ++p) if (p->contents.find(pat) != string::npos) return p; return in.end(); } list<Line>::iterator find_trim(list<Line>& in, const string& pat) { for (list<Line>::iterator p = in.begin(); p != in.end(); ++p) if (trim(p->contents) == pat) return p; return in.end(); } string escape(string s) { s = replace_all(s, "\\", "\\\\"); s = replace_all(s, "\"", "\\\""); s = replace_all(s, "", "\\n"); return s; } string replace_all(string s, const string& a, const string& b) { for (size_t pos = s.find(a); pos != string::npos; pos = s.find(a, pos+b.size())) s = s.replace(pos, a.size(), b); return s; } // does s start with pat, after skipping whitespace? // pat can't start with whitespace bool starts_with(const string& s, const string& pat) { for (size_t pos = 0; pos < s.size(); ++pos) if (!isspace(s.at(pos))) return s.compare(pos, pat.size(), pat) == 0; return false; } string indent(const string& s) { for (size_t pos = 0; pos < s.size(); ++pos) if (!isspace(s.at(pos))) return s.substr(0, pos); return ""; } string strip_indent(const string& s, size_t n) { if (s.empty()) return ""; string::const_iterator curr = s.begin(); while (curr != s.end() && n > 0 && isspace(*curr)) { ++curr; --n; } return string(curr, s.end()); } string trim(const string& s) { string::const_iterator first = s.begin(); while (first != s.end() && isspace(*first)) ++first; if (first == s.end()) return ""; string::const_iterator last = --s.end(); while (last != s.begin() && isspace(*last)) --last; ++last; return string(first, last); } const Line& front(const list<Line>& l) { assert(!l.empty()); return l.front(); } //// Tests for tangle void test_tangle() { istringstream in("a\n" "b\n" "c\n" ":(before b)\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "d\n" "b\n" "c\n"); } void test_tangle_with_linenumber() { istringstream in("a\n" "b\n" "c\n" ":(before b)\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "#line 1\n" "a\n" "#line 5\n" "d\n" "#line 2\n" "b\n" "c\n"); // no other #line directives CHECK_TRACE_DOESNT_CONTAIN("tangle", "#line 3"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "#line 4"); } void test_tangle_linenumbers_with_filename() { istringstream in("a\n" "b\n" "c\n" ":(before b)\n" "d\n"); list<Line> dummy; tangle(in, "foo", dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "#line 5 \"foo\"\n" "d\n" "b\n" "c\n"); } void test_tangle_line_numbers_with_multiple_filenames() { istringstream in1("a\n" "b\n" "c"); list<Line> dummy; tangle(in1, "foo", dummy); CLEAR_TRACE; istringstream in2(":(before b)\n" "d\n"); tangle(in2, "bar", dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "#line 2 \"bar\"\n" "d\n" "#line 2 \"foo\"\n" "b\n" "c\n"); } void test_tangle_linenumbers_with_multiple_directives() { istringstream in1("a\n" "b\n" "c"); list<Line> dummy; tangle(in1, "foo", dummy); CLEAR_TRACE; istringstream in2(":(before b)\n" "d\n" ":(before c)\n" "e"); tangle(in2, "bar", dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "#line 2 \"bar\"\n" "d\n" "#line 2 \"foo\"\n" "b\n" "#line 4 \"bar\"\n" "e\n" "#line 3 \"foo\"\n" "c\n"); } void test_tangle_with_multiple_filenames_after() { istringstream in1("a\n" "b\n" "c"); list<Line> dummy; tangle(in1, "foo", dummy); CLEAR_TRACE; istringstream in2(":(after b)\n" "d\n"); tangle(in2, "bar", dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "b\n" "#line 2 \"bar\"\n" "d\n" "#line 3 \"foo\"\n" "c\n"); } void test_tangle_skip_tanglecomments() { istringstream in("a\n" "b\n" "c\n" "//: 1\n" "//: 2\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "b\n" "c\n" "\n" "\n" "d\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "//: 1"); } void test_tangle_with_tanglecomments_and_directive() { istringstream in("a\n" "//: 1\n" "b\n" "c\n" ":(before b)\n" "d\n" ":(code)\n" "e\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "#line 6\n" "d\n" "#line 3\n" "b\n" "c\n" "#line 8\n" "e\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "//: 1"); } void test_tangle_with_tanglecomments_inside_directive() { istringstream in("a\n" "//: 1\n" "b\n" "c\n" ":(before b)\n" "//: abc\n" "d\n" ":(code)\n" "e\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "#line 7\n" "d\n" "#line 3\n" "b\n" "c\n" "#line 9\n" "e\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "//: 1"); } void test_tangle_with_multiword_directives() { istringstream in("a b\n" "c\n" ":(after \"a b\")\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a b\n" "d\n" "c\n"); } void test_tangle_with_quoted_multiword_directives() { istringstream in("a \"b\"\n" "c\n" ":(after \"a \\\"b\\\"\")\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a \"b\"\n" "d\n" "c\n"); } void test_tangle2() { istringstream in("a\n" "b\n" "c\n" ":(after b)\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "b\n" "d\n" "c\n"); } void test_tangle_at_end() { istringstream in("a\n" "b\n" "c\n" ":(after c)\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "b\n" "c\n" "d\n"); } void test_tangle_indents_hunks_correctly() { istringstream in("a\n" " b\n" "c\n" ":(after b)\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" " b\n" " d\n" "c\n"); } void test_tangle_warns_on_missing_target() { Hide_warnings = true; istringstream in(":(before)\n" "abc def\n"); list<Line> lines; tangle(in, lines); CHECK_TRACE_WARNS(); } void test_tangle_warns_on_unknown_target() { Hide_warnings = true; istringstream in(":(before \"foo\")\n" "abc def\n"); list<Line> lines; tangle(in, lines); CHECK_TRACE_WARNS(); } void test_tangle_delete_range_of_lines() { istringstream in("a\n" "b {\n" "c\n" "}\n" ":(delete{} \"b\")\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "b"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "c"); } void test_tangle_replace() { istringstream in("a\n" "b\n" "c\n" ":(replace b)\n" "d\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "d\n" "c\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "b"); } void test_tangle_replace_range_of_lines() { istringstream in("a\n" "b {\n" "c\n" "}\n" ":(replace{} \"b\")\n" "d\n" "e\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "d\n" "e\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "b {"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "c"); } void test_tangle_replace_tracks_old_lines() { istringstream in("a\n" "b {\n" "c\n" "}\n" ":(replace{} \"b\")\n" "d\n" ":OLD_CONTENTS\n" "e\n"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "d\n" "c\n" "e\n"); CHECK_TRACE_DOESNT_CONTAIN("tangle", "b {"); } void test_tangle_nested_patterns() { istringstream in("a\n" "c\n" "b\n" "c\n" "d\n" ":(after \"b\" then \"c\")\n" "e"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "c\n" "b\n" "c\n" "e\n" "d\n"); } void test_tangle_nested_patterns2() { istringstream in("a\n" "c\n" "b\n" "c\n" "d\n" ":(after \"c\" following \"b\")\n" "e"); list<Line> dummy; tangle(in, dummy); CHECK_TRACE_CONTENTS("tangle", "a\n" "c\n" "b\n" "c\n" "e\n" "d\n"); } // todo: include line numbers in tangle errors void test_trim() { CHECK_EQ(trim(""), ""); CHECK_EQ(trim(" "), ""); CHECK_EQ(trim(" "), ""); CHECK_EQ(trim("a"), "a"); CHECK_EQ(trim(" a"), "a"); CHECK_EQ(trim(" a"), "a"); CHECK_EQ(trim(" ab"), "ab"); CHECK_EQ(trim("a "), "a"); CHECK_EQ(trim("a "), "a"); CHECK_EQ(trim("ab "), "ab"); CHECK_EQ(trim(" a "), "a"); CHECK_EQ(trim(" a "), "a"); CHECK_EQ(trim(" ab "), "ab"); } void test_strip_indent() { CHECK_EQ(strip_indent("", 0), ""); CHECK_EQ(strip_indent("", 1), ""); CHECK_EQ(strip_indent("", 3), ""); CHECK_EQ(strip_indent(" ", 0), " "); CHECK_EQ(strip_indent(" a", 0), " a"); CHECK_EQ(strip_indent(" ", 1), ""); CHECK_EQ(strip_indent(" a", 1), "a"); CHECK_EQ(strip_indent(" ", 2), ""); CHECK_EQ(strip_indent(" a", 2), "a"); CHECK_EQ(strip_indent(" ", 0), " "); CHECK_EQ(strip_indent(" a", 0), " a"); CHECK_EQ(strip_indent(" ", 1), " "); CHECK_EQ(strip_indent(" a", 1), " a"); CHECK_EQ(strip_indent(" ", 2), ""); CHECK_EQ(strip_indent(" a", 2), "a"); CHECK_EQ(strip_indent(" ", 3), ""); CHECK_EQ(strip_indent(" a", 3), "a"); } //// Test harness typedef void (*test_fn)(void); const test_fn Tests[] = { #include "tangle.test_list" // auto-generated; see 'build*' scripts }; // Names for each element of the 'Tests' global, respectively. const string Test_names[] = { #include "tangle.test_name_list" // auto-generated; see 'build*' scripts }; int run_tests() { for (unsigned long i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) { //? cerr << "running " << Test_names[i] << '\n'; START_TRACING_UNTIL_END_OF_SCOPE; setup(); (*Tests[i])(); verify(); } cerr << '\n'; if (Num_failures > 0) cerr << Num_failures << " failure" << (Num_failures > 1 ? "s" : "") << '\n'; return Num_failures; }